NCBI C++ ToolKit
bdb_bv_split_store.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef BDB___BV_DICT_IDX__HPP
2 #define BDB___BV_DICT_IDX__HPP
3 
4 /* $Id: bdb_bv_split_store.hpp 85434 2019-02-11 15:08:11Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio
30  *
31  * File Description:
32  *
33  */
34 
37 
39 
40 
41 
42 /////////////////////////////////////////////////////////////////////////////
43 
44 
45 template <typename Key,
47  typename BvStore = CBDB_PersistentSplitStore< bm::bvector<> >,
48  typename BV = bm::bvector<> >
49 class CBDB_BvSplitDictStore : public CBDB_BlobDictStore<Key, Dictionary, BvStore>
50 {
51 public:
53  typedef Key TKey;
54  typedef Uint4 TKeyId;
55  typedef BV TBitVector;
56 
57  enum EReadOp {
62  };
63 
64  CBDB_BvSplitDictStore(const string& demux_path = kEmptyStr);
65  CBDB_BvSplitDictStore(Dictionary& dict, BvStore& store,
68 
69  /// read a vector by the relevant key, performing a logical operation
71  EReadOp op = eOp_Replace);
74 
75  /// read a vector by the relevant key ID, performing a logical operation
76  /// these are useful in situations in which the key ID is known beforehand
78  EReadOp op = eOp_Replace);
81 
82  enum ECompact {
85  };
87  ECompact compact);
89  ECompact compact);
90 
91 protected:
92 
93  /// Fetch, deserialize bvector
96  EReadOp op);
97 
98 private:
99  /// private data buffer
101 
102  /// temporary bit-vector for write operations
104 
105  /// temp block for bitvector serialization
107 
110  ECompact compact);
111 };
112 
113 
114 /////////////////////////////////////////////////////////////////////////////
115 
116 
117 template <typename Key, typename Dictionary, typename BvStore, typename BV>
119 ::CBDB_BvSplitDictStore(const string& demux_path)
120 : TParent(demux_path)
121 , m_STmpBlock(NULL)
122 {
123 }
124 
125 
126 template <typename Key, typename Dictionary, typename BvStore, typename BV>
129  BvStore& store,
130  EOwnership own)
131 : TParent(dict, store, own)
132 , m_STmpBlock(NULL)
133 {
134 }
135 
136 
137 template <typename Key, typename Dictionary, typename BvStore, typename BV>
138 inline
140 {
141  if (m_STmpBlock) {
142  bm::aligned_free(m_STmpBlock);
143  }
144 }
145 
146 
147 ///
148 /// Internal read routines
149 ///
150 
151 template<typename Key, typename Dictionary, typename BvStore, typename BV>
152 inline void
155  EReadOp op)
156 {
157  _ASSERT(bv);
158  if ( !m_STmpBlock ) {
160  }
161  switch (op) {
162  case eOp_Replace:
163  /**
164  bv->clear(true);
165  bm::deserialize(*bv, (const unsigned char*)buf, m_STmpBlock);
166  **/
167 
169  buf,
170  m_STmpBlock,
172  break;
173 
174  case eOp_Or:
175  /**
176  bm::deserialize(*bv, (const unsigned char*)buf, m_STmpBlock);
177  **/
179  buf,
180  m_STmpBlock,
181  bm::set_OR);
182  break;
183 
184  case eOp_And:
185  {{
186  /**
187  m_TmpVec.clear(true);
188  bm::deserialize(m_TmpVec, (const unsigned char*)buf, m_STmpBlock);
189  *bv &= m_TmpVec;
190  **/
191 
193  buf,
194  m_STmpBlock,
195  bm::set_AND);
196  }}
197  break;
198 
199  default:
200  _ASSERT(false);
202  "Deserialize(): unknown op");
203  }
204 }
205 
206 
207 ///
208 /// Public read interface
209 /// These interfaces read values given a key
210 ///
211 template <typename Key, typename Dictionary, typename BvStore, typename BV>
212 inline EBDB_ErrCode
214  TBitVector* bv,
215  EReadOp op)
216 {
217  _ASSERT(bv);
219  if (bv) {
220  err = Read(key, m_Buffer);
221  if (err == eBDB_Ok) {
222  Deserialize(bv, &m_Buffer[0], op);
223  }
224  }
225  return err;
226 }
227 
228 
229 template <typename Key, typename Dictionary, typename BvStore, typename BV>
230 inline EBDB_ErrCode
232  TBitVector* bv)
233 {
234  return ReadVector(key, bv, eOp_Or);
235 }
236 
237 
238 template <typename Key, typename Dictionary, typename BvStore, typename BV>
239 inline EBDB_ErrCode
241  TBitVector* bv)
242 {
243  return ReadVector(key, bv, eOp_And);
244 }
245 
246 
247 ///
248 /// These interfaces read given a key ID
249 ///
250 
251 template <typename Key, typename Dictionary, typename BvStore, typename BV>
252 inline EBDB_ErrCode
254  TBitVector* bv,
255  EReadOp op)
256 {
257  _ASSERT(bv);
259  if (bv) {
260  err = TParent::ReadById(key, m_Buffer);
261  if (err == eBDB_Ok) {
262  Deserialize(bv, &m_Buffer[0], op);
263  }
264  }
265  return err;
266 }
267 
268 
269 template <typename Key, typename Dictionary, typename BvStore, typename BV>
270 inline EBDB_ErrCode
272  TBitVector* bv)
273 {
274  return ReadVectorById(key, bv, eOp_Or);
275 }
276 
277 
278 template <typename Key, typename Dictionary, typename BvStore, typename BV>
279 inline EBDB_ErrCode
281  TBitVector* bv)
282 {
283  return ReadVectorById(key, bv, eOp_And);
284 }
285 
286 
287 ///
288 /// Public write interface
289 ///
290 template <typename Key, typename Dictionary, typename BvStore, typename BV>
294  ECompact compact)
295 {
296  if ( !m_STmpBlock ) {
298  }
299 
300  const TBitVector* bv_to_store = &bv;
301  typename TBitVector::statistics st1;
302  if (compact == eCompact) {
303  m_TmpVec.clear(true); // clear vector by memory deallocation
304  m_TmpVec = bv;
305  m_TmpVec.optimize(0, BV::opt_compress, &st1);
306  bv_to_store = &m_TmpVec;
307  } else {
308  bv_to_store->calc_stat(&st1);
309  }
310 
311  if (st1.max_serialize_mem > m_Buffer.size()) {
312  m_Buffer.resize(st1.max_serialize_mem);
313  }
314  size_t size = bm::serialize(*bv_to_store, (unsigned char*)&m_Buffer[0],
315  m_STmpBlock, bm::BM_NO_BYTE_ORDER);
316  m_Buffer.resize(size);
317 }
318 
319 
320 template <typename Key, typename Dictionary, typename BvStore, typename BV>
321 inline EBDB_ErrCode
323  const TBitVector& bv,
324  ECompact compact)
325 {
326  x_SerializeBitVector(bv, m_Buffer, compact);
327  return Write(key, m_Buffer);
328 }
329 
330 
331 template <typename Key, typename Dictionary, typename BvStore, typename BV>
332 inline EBDB_ErrCode
334  const TBitVector& bv,
335  ECompact compact)
336 {
337  x_SerializeBitVector(bv, m_Buffer, compact);
338  return TParent::WriteById(key_id, m_Buffer);
339 }
340 
341 
342 
343 
344 
346 
347 #endif // BDB___BV_DICT_IDX__HPP
Templatized candidate for dictionaries This template defines the basic interface that must be support...
CBDB_RawFile::TBuffer m_Buffer
private data buffer
EBDB_ErrCode ReadVectorAndById(TKeyId key, TBitVector *bv)
EBDB_ErrCode WriteVectorById(TKeyId key_id, const TBitVector &bv, ECompact compact)
EBDB_ErrCode ReadVectorOrById(TKeyId key, TBitVector *bv)
void Deserialize(TBitVector *bv, CBDB_RawFile::TBuffer::value_type *buf, EReadOp op)
Fetch, deserialize bvector.
EBDB_ErrCode ReadVectorAnd(const TKey &key, TBitVector *bv)
TBitVector m_TmpVec
temporary bit-vector for write operations
CBDB_BlobDictStore< Key, Dictionary, BvStore > TParent
bm::word_t * m_STmpBlock
temp block for bitvector serialization
EBDB_ErrCode WriteVector(const TKey &key, const TBitVector &bv, ECompact compact)
EBDB_ErrCode ReadVectorById(TKeyId key, TBitVector *bv, EReadOp op=eOp_Replace)
read a vector by the relevant key ID, performing a logical operation these are useful in situations i...
EBDB_ErrCode ReadVectorOr(const TKey &key, TBitVector *bv)
CBDB_BvSplitDictStore(const string &demux_path=kEmptyStr)
CBDB_BvSplitDictStore(Dictionary &dict, BvStore &store, EOwnership own=eTakeOwnership)
void x_SerializeBitVector(const TBitVector &bv, CBDB_RawFile::TBuffer &buffer, ECompact compact)
Public write interface.
EBDB_ErrCode ReadVector(const TKey &key, TBitVector *bv, EReadOp op=eOp_Replace)
read a vector by the relevant key, performing a logical operation
Reallocable memory buffer (no memory copy overhead) Mimics vector<>, without the overhead of explicit...
size_type deserialize(bvector_type &bv, const unsigned char *buf, set_operation op, bool exit_on_one=false)
Deserialize bvector using buffer as set operation argument.
Definition: bmserial.h:6581
@ eTakeOwnership
An object can take ownership of another.
Definition: ncbi_types.h:136
EBDB_ErrCode
BDB Return codes.
Definition: bdb_file.hpp:57
@ eBDB_Ok
Definition: bdb_file.hpp:58
@ eBDB_NotFound
Definition: bdb_file.hpp:59
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
@ eUnknown
Definition: app_popup.hpp:72
void Read(CObjectIStream &in, TObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:60
void Write(CObjectOStream &out, TConstObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:55
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
enum ENcbiOwnership EOwnership
Ownership relations between objects.
@ set_OR
Definition: bmconst.h:170
@ set_ASSIGN
Definition: bmconst.h:173
@ set_AND
Definition: bmconst.h:169
size_t serialize(const BV &bv, unsigned char *buf, bm::word_t *temp_block=0, unsigned serialization_flags=0)
Saves bitvector into memory.
Definition: bmserial.h:3071
@ BM_NO_BYTE_ORDER
save no byte-order info (save some space)
Definition: bmserial.h:3024
char * buf
unsigned int word_t
Definition: bmconst.h:39
void aligned_free(void *ptr) BMNOEXCEPT
Aligned free.
Definition: bmalloc.h:464
void * aligned_new_malloc(size_t size)
Aligned malloc (unlike classic malloc it throws bad_alloc exception)
Definition: bmalloc.h:436
const unsigned set_block_alloc_size
Definition: bmconst.h:61
const struct ncbi::grid::netcache::search::fields::SIZE size
const struct ncbi::grid::netcache::search::fields::KEY key
Compressed bitset (entry point to bm.h)
static uint8_t * buffer
Definition: pcre2test.c:1016
static CSafeStatic< map< string, string > > Dictionary
Definition: spell_check.cpp:84
#define _ASSERT
void Deserialize(CNcbiIstream &istr, CRawScoreVector< Key, Score > &)
Modified on Wed Sep 04 14:59:37 2024 by modify_doxy.py rev. 669887