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

Go to the SVN repository for this file.

1 #ifndef BDB___BDB_DICT_STORE__HPP
2 #define BDB___BDB_DICT_STORE__HPP
3 
4 /* $Id: bdb_dict_store.hpp 42205 2009-06-15 15:40:12Z ivanovp $
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 
35 #include <db/bdb/bdb_env.hpp>
37 
38 
40 
41 
42 /////////////////////////////////////////////////////////////////////////////
43 ///
44 /// Templatized candidate for dictionaries
45 /// This template defines the basic interface that must be supported by all
46 /// dictionaries. Dictionaries do not need to inherit from this, so long as
47 /// this API is supported.
48 ///
49 
50 template <typename Key>
52 {
53 public:
54  typedef Key TKey;
55  typedef Uint4 TKeyId;
56 
58 
59  /// @name Required CBDB_BlobDictionary<> interface
60  /// @{
61 
62  TKeyId GetKey(const TKey& key);
63  TKeyId PutKey(const TKey& key);
64 
65  /// @}
66 
67  /// retrieve the current key
68  Key GetCurrentKey() const;
69  TKeyId GetCurrentUid() const;
70  TKeyId GetMaxUid() const;
71 
72  /// read a particular key's value
74 
75  /// read the current key's value
77 
78  /// write a key/value pair to the store
80 
81 protected:
83 
84 private:
85  /// key
87  /// data
89 };
90 
91 
92 ////////////////////////////////////////////////////////////////////////////
93 ///
94 /// Pass-through for Uint4 key types
95 ///
96 
97 template<>
99 {
100 public:
101  /// @name Required CBDB_BlobDictionary<> interface
102  /// @{
103 
104  Uint4 GetKey(const Uint4& key) { return key; }
105  Uint4 PutKey(const Uint4& key) { return key; }
106 
107  /// @}
108 
109  /// no-ops for compatibility
110  void SetEnv(CBDB_Env& env) {}
111  void Open(const string&, CBDB_RawFile::EOpenMode) {}
112  bool IsOpen() const { return true; }
113 };
114 
115 
116 template<>
118 {
119 public:
120  /// @name Required CBDB_BlobDictionary<> interface
121  /// @{
122 
123  Uint4 GetKey(const Int4& key) { return key; }
124  Uint4 PutKey(const Int4& key) { return key; }
125 
126  /// @}
127 
128  /// no-ops for compatibility
129  void SetEnv(CBDB_Env& env) {}
130  void Open(const string&, CBDB_RawFile::EOpenMode) {}
131  bool IsOpen() const { return true; }
132 };
133 
134 
135 /////////////////////////////////////////////////////////////////////////////
136 
137 
138 template <class BV, class TL=CFastMutex>
140 : public CBDB_BlobSplitStore<BV, CBDB_BlobDeMuxPersistent, TL>
141 {
142 public:
144  CBDB_PersistentSplitStore(const string& demux_path)
145  : TParent(new CBDB_BlobDeMuxPersistent(demux_path,
146  3.0 * 1024.0 * 1024.0 * 1024.0,
147  6 * 1000000))
148  {
149  }
150 };
151 
152 
153 /////////////////////////////////////////////////////////////////////////////
154 
155 
156 template <typename Key,
158  typename Store = CBDB_PersistentSplitStore< bm::bvector<> > >
160 {
161 public:
162  typedef Key TKey;
163  typedef Uint4 TKeyId;
165  typedef Store TStore;
166 
167  CBDB_BlobDictStore(const string& demux_path = kEmptyStr);
169  Store& store,
170  EOwnership own = eNoOwnership);
172 
175  void Open(const string& fname, CBDB_RawFile::EOpenMode mode);
176 
177  const string& GetFileName() const;
178 
182  EBDB_ErrCode Write (const Key& key, const void* data, size_t size);
184  EBDB_ErrCode WriteById(TKeyId key_id, const void* data, size_t size);
186  const void* data,
187  size_t size);
188 
189  /// Delete the underlying blob
190  EBDB_ErrCode Delete(unsigned id,
191  CBDB_RawFile::EIgnoreError on_error =
193  CBDB_Transaction* trans = 0);
194 
196  Store& GetStore() { return x_GetOpenStore(); }
197 
198 protected:
201 
202  string m_Filename;
204 
207 
208 private:
209  /// forbidden
212 };
213 
214 
215 //////////////////////////////////////////////////////////////////////////////
216 
217 
218 template <typename Key>
219 inline
221  : m_MaxUid(0)
222 {
223  BindKey ("key", &m_Key);
224  BindData("uid", &m_Uid);
225 }
226 
227 template <typename Key>
228 inline
230 {
231  m_Key = key;
232  return Read(val);
233 }
234 
235 
236 template <typename Key>
237 inline
239 {
240  EBDB_ErrCode err = Fetch();
241  if (err == eBDB_Ok && val) {
242  *val = m_Uid;
243  _ASSERT(*val <= m_MaxUid);
244  }
245  return err;
246 }
247 
248 
249 template <typename Key>
250 inline
252 {
253  m_Key = key;
254  m_Uid = val;
255  m_MaxUid = max(m_MaxUid, val);
256  return UpdateInsert();
257 }
258 
259 
260 template <typename Key>
261 inline
263 {
264  return (Key)m_Key;
265 }
266 
267 
268 template <typename Key>
269 inline
272 {
273  return (TKeyId)m_Uid;
274 }
275 
276 template <typename Key>
277 inline
280 {
281  return m_MaxUid;
282 }
283 
284 
285 template <typename Key>
286 inline
288 {
289  Uint4 uid = 0;
290  Read(key, &uid) ;
291  return uid;
292 }
293 
294 
295 template <typename Key>
296 inline
298 {
299  Uint4 uid = GetKey(key);
300  if (uid != 0) {
301  _ASSERT(uid <= m_MaxUid);
302  return uid;
303  }
304 
305  ++m_MaxUid;
306  uid = m_MaxUid;
307  if (uid) {
308  if (Write(key, uid) != eBDB_Ok) {
309  uid = 0;
310  }
311  }
312  return uid;
313 }
314 
315 
316 /////////////////////////////////////////////////////////////////////////////
317 
318 
319 template <typename Key, typename Dictionary, typename Store>
321 ::CBDB_BlobDictStore(const string& demux_path)
322  : m_Dict(new Dictionary)
323  , m_Store(new Store(demux_path))
324 {
325 }
326 
327 
328 template <typename Key, typename Dictionary, typename Store>
331  Store& store,
332  EOwnership own)
333  : m_Dict(&generator, own),
334  m_Store(&store, own)
335 {
336 }
337 
338 
339 template <typename Key, typename Dictionary, typename Store>
340 inline
342 {
343 }
344 
345 
346 template <typename Key, typename Dictionary, typename Store>
347 inline
349 {
350  return m_Store->GetEnv();
351 }
352 
353 
354 template <typename Key, typename Dictionary, typename Store>
355 inline
357 {
358  return m_Filename;
359 }
360 
361 
362 template <typename Key, typename Dictionary, typename Store>
363 inline
365 {
366  m_Dict->SetEnv(env);
367  m_Store->SetEnv(env);
368 }
369 
370 
371 template <typename Key, typename Dictionary, typename Store>
372 inline
375 {
376  m_Filename = fname;
377  m_OpenMode = mode;
378 
379  /// open our dictionary
380  /// this we always do
381  x_GetOpenDict();
382  /**
383  _ASSERT(m_Dict.get());
384  if ( !m_Dict->IsOpen() ) {
385  _ASSERT( !fname.empty() );
386  m_Dict->Open(fname + ".dict", mode);
387  }
388  **/
389 
390  /// delay opening our store until we need to
391  //x_GetOpenStore();
392  //m_Store->Open(fname, mode);
393 }
394 
395 
396 template <typename Key, typename Dictionary, typename Store>
397 inline
400 {
401  _ASSERT(m_Dict.get());
402  if ( !m_Dict->IsOpen() && !m_Filename.empty() ) {
403  m_Dict->Open(m_Filename + ".dict", m_OpenMode);
404  }
405  return *m_Dict;
406 }
407 
408 
409 template <typename Key, typename Dictionary, typename Store>
410 inline
413 {
414  _ASSERT(m_Store);
415  if ( !m_Store->IsOpen() && !m_Filename.empty() ) {
416  m_Store->Open(m_Filename, m_OpenMode);
417  }
418  return *m_Store;
419 }
420 
421 
422 template <typename Key, typename Dictionary, typename Store>
423 inline EBDB_ErrCode
426 {
427  TKeyId key_id = x_GetOpenDict().GetKey(key);
428  return ReadById(key_id, data);
429 }
430 
431 
432 template <typename Key, typename Dictionary, typename Store>
433 inline EBDB_ErrCode
436 {
437  return x_GetOpenStore().ReadRealloc(key_id, data);
438 }
439 
440 
441 template <typename Key, typename Dictionary, typename Store>
442 inline EBDB_ErrCode
445 {
446  return Write(key, &data[0], data.size());
447 }
448 
449 
450 template <typename Key, typename Dictionary, typename Store>
451 inline EBDB_ErrCode
453  const void* data,
454  size_t size)
455 {
456  TKeyId key_id = x_GetOpenDict().PutKey(key);
457  return UpdateInsert(key_id, data, size);
458 }
459 
460 
461 template <typename Key, typename Dictionary, typename Store>
462 inline EBDB_ErrCode
464  const void* data,
465  size_t size)
466 {
467  return UpdateInsert(key_id, data, size);
468 }
469 
470 
471 template <typename Key, typename Dictionary, typename Store>
472 inline EBDB_ErrCode
475 {
476  return UpdateInsert(key_id, &data[0], data.size());
477 }
478 
479 
480 template <typename Key, typename Dictionary, typename Store>
481 inline EBDB_ErrCode
483  const void* data,
484  size_t size)
485 {
486  return x_GetOpenStore().UpdateInsert(uid, data, size);
487 }
488 
489 
490 template <typename Key, typename Dictionary, typename Store>
491 inline EBDB_ErrCode
494  CBDB_Transaction* trans)
495 {
496  return x_GetOpenStore().Delete(id, on_error, trans);
497 }
498 
500 
501 #endif // BDB___BDB_DICT_STORE__HPP
Wrapper around Berkeley DB environment structure.
BDB library split BLOB store.
Split demux which can save and load state into a file Stateful (persistent) class.
CBDB_BlobDictStore(const CBDB_BlobDictStore< Key, Dictionary, Store > &)
forbidden
const string & GetFileName() const
EBDB_ErrCode Write(const Key &key, const CBDB_RawFile::TBuffer &data)
EBDB_ErrCode Delete(unsigned id, CBDB_RawFile::EIgnoreError on_error=CBDB_RawFile::eThrowOnError, CBDB_Transaction *trans=0)
Delete the underlying blob.
TDictionary & x_GetOpenDict()
EBDB_ErrCode WriteById(TKeyId key_id, const void *data, size_t size)
virtual ~CBDB_BlobDictStore()
EBDB_ErrCode Read(const Key &key, CBDB_RawFile::TBuffer &data)
CBDB_BlobDictStore(const string &demux_path=kEmptyStr)
EBDB_ErrCode Write(const Key &key, const void *data, size_t size)
CBDB_BlobDictStore(Dictionary &dict, Store &store, EOwnership own=eNoOwnership)
CBDB_RawFile::EOpenMode m_OpenMode
EBDB_ErrCode ReadById(TKeyId key, CBDB_RawFile::TBuffer &data)
EBDB_ErrCode WriteById(TKeyId key_id, const CBDB_RawFile::TBuffer &data)
AutoPtr< TStore > m_Store
Dictionary & GetDictionary()
void SetEnv(CBDB_Env &env)
CBDB_BlobDictStore< Key, Dictionary, Store > & operator=(const CBDB_BlobDictStore< Key, Dictionary, Store > &)
AutoPtr< TDictionary > m_Dict
EBDB_ErrCode UpdateInsert(Uint4 uid, const void *data, size_t size)
void Open(const string &fname, CBDB_RawFile::EOpenMode mode)
void Open(const string &, CBDB_RawFile::EOpenMode)
void SetEnv(CBDB_Env &env)
no-ops for compatibility
Uint4 GetKey(const Int4 &key)
Uint4 PutKey(const Int4 &key)
void SetEnv(CBDB_Env &env)
no-ops for compatibility
Uint4 GetKey(const Uint4 &key)
Uint4 PutKey(const Uint4 &key)
void Open(const string &, CBDB_RawFile::EOpenMode)
Templatized candidate for dictionaries This template defines the basic interface that must be support...
TKeyId GetKey(const TKey &key)
SBDB_TypeTraits< Uint4 >::TFieldType m_Uid
data
EBDB_ErrCode Read(TKeyId *val)
read the current key's value
SBDB_TypeTraits< Key >::TFieldType m_Key
key
EBDB_ErrCode Read(const TKey &key, TKeyId *val)
read a particular key's value
Key GetCurrentKey() const
retrieve the current key
EBDB_ErrCode Write(const TKey &key, TKeyId val)
write a key/value pair to the store
TKeyId GetCurrentUid() const
TKeyId GetMaxUid() const
TKeyId PutKey(const TKey &key)
BLOB storage based on single unsigned integer key Supports BLOB volumes and different base page size ...
BDB environment object a collection including support for some or all of caching, locking,...
Definition: bdb_env.hpp:61
Berkeley DB file class.
Definition: bdb_file.hpp:445
CBDB_BlobSplitStore< BV, CBDB_BlobDeMuxPersistent, TL > TParent
CBDB_PersistentSplitStore(const string &demux_path)
BDB transaction object.
Definition: bdb_trans.hpp:63
Reallocable memory buffer (no memory copy overhead) Mimics vector<>, without the overhead of explicit...
static HENV env
Definition: transaction2.c:38
char data[12]
Definition: iconv.c:80
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:135
EOpenMode
BDB file open mode.
Definition: bdb_file.hpp:78
void BindKey(const char *field_name, CBDB_Field *key_field, size_t buf_size=0)
Definition: bdb_file.cpp:1281
void BindData(const char *field_name, CBDB_Field *data_field, size_t buf_size=0, ENullable is_null=eNullable)
Definition: bdb_file.cpp:1296
EBDB_ErrCode
BDB Return codes.
Definition: bdb_file.hpp:57
@ eBDB_Ok
Definition: bdb_file.hpp:58
DB_ENV * GetEnv()
Return underlying DB_ENV structure pointer for low level access.
Definition: bdb_env.hpp:151
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
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
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.
mdb_mode_t mode
Definition: lmdb++.h:38
const struct ncbi::grid::netcache::search::fields::SIZE size
const struct ncbi::grid::netcache::search::fields::KEY key
T max(T x_, T y_)
static CSafeStatic< map< string, string > > Dictionary
Definition: spell_check.cpp:84
Type trait classes for finding the correct BDB field type automagically.
Definition: bdb_types.hpp:2009
#define _ASSERT
Modified on Tue May 28 05:52:59 2024 by modify_doxy.py rev. 669887