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

Go to the SVN repository for this file.

1 #ifndef LOG___BDB_SPLIT_CURSOR__HPP
2 #define LOG___BDB_SPLIT_CURSOR__HPP
3 
4 /* $Id: bdb_split_cursor.hpp 91306 2020-10-08 11:57:15Z gouriano $
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 <corelib/ncbistd.hpp>
36 #include <corelib/ncbifile.hpp>
37 #include <db/bdb/bdb_file.hpp>
38 #include <db/bdb/bdb_cursor.hpp>
39 #include <db/bdb/bdb_env.hpp>
40 #include <db/error_codes.hpp>
41 
42 
44 
45 
46 
47 template <typename BDB_SplitStore, typename BDB_Vol = typename BDB_SplitStore::TBlobFile>
49 {
50 public:
51  struct SVolumeLess
52  {
53  bool operator() (const string& s1, const string& s2) const
54  {
55  string::size_type pos1 = s1.find_last_of("_");
56  if (pos1 == string::npos) {
57  pos1 = 0;
58  }
59  string::size_type pos2 = s2.find_last_of("_");
60  if (pos2 == string::npos) {
61  pos2 = 0;
62  }
63 
64  CTempString ts1(s1, pos1, s1.size());
65  CTempString ts2(s2, pos2, s2.size());
66  if (ts1 < ts2) {
67  return true;
68  }
69  if (ts2 < ts1) {
70  return false;
71  }
72 
73  return s1 < s2;
74  }
75  };
76  typedef BDB_SplitStore TSplitStore;
77  typedef BDB_Vol TVolume;
78 
80  void InitMultiFetch(size_t buffer_size);
82  const void* GetLastMultiFetchData() const;
83  size_t GetLastMultiFetchDataLen() const;
86  Uint4 GetLastBlobId() const;
87 
88 private:
90  string m_Path;
91  string m_StoreName;
92  size_t m_BufferSize;
93 
94  vector<string> m_Files;
95  unique_ptr<TVolume> m_Volume;
96  unique_ptr<CBDB_FileCursor> m_Cursor;
97 
99 
100  void x_NextVolume();
101 };
102 
103 
104 template <typename BDB_SplitStore, typename BDB_Vol>
105 inline
107  : m_Env(NULL)
108  , m_BufferSize(40 * 1024 * 1024)
109 {
110  m_SW.Start();
111  m_Env = store.GetEnv();
112 
113  ///
114  /// find our relevant files
115  ///
116  {{
117  vector<string> paths;
118  vector<string> masks;
119 
120  string path = store.GetFileName();
121  string path_dir;
122  string path_base;
123  string path_ext;
124  CDirEntry::SplitPath(path, &path_dir, &path_base, &path_ext);
125  path_base += path_ext;
126 
127  if (CDirEntry::IsAbsolutePath(path_dir)) {
128  path = path_dir;
129  } else {
130  path.erase();
131  if (m_Env) {
132  path = m_Env->GetPath();
133  path += "/";
134  }
135  path += path_dir;
136  path = CDirEntry::CreateAbsolutePath(path);
137  }
138 
139  paths.push_back(path);
140  masks.push_back(path_base + "_*");
142  paths.begin(), paths.end(), masks.begin(), masks.end(),
143  fFF_File);
144 
145  std::sort(m_Files.begin(), m_Files.end(), SVolumeLess());
146 
147  LOG_POST_XX(Db_Bdb_Cursor, 2, Info <<
148  "found " << m_Files.size() << " candidate files");
149  }}
150 }
151 
152 
153 template <typename BDB_SplitStore, typename BDB_Vol>
154 inline EBDB_ErrCode
156 {
157  for (;;) {
158  if ( !m_Cursor.get() || m_Cursor->Fetch() != eBDB_Ok) {
159  x_NextVolume();
160  if ( !m_Cursor.get() ) {
161  return eBDB_NotFound;
162  }
163  } else {
164  break;
165  }
166  }
167 
168  return eBDB_Ok;
169 }
170 
171 
172 template <typename BDB_SplitStore, typename BDB_Vol>
173 inline void
175 {
176  m_BufferSize = size;
177 }
178 
179 
180 template <typename BDB_SplitStore, typename BDB_Vol>
181 inline const void*
183 {
184  if (m_Cursor.get()) {
185  return m_Cursor->GetLastMultiFetchData();
186  }
187  return NULL;
188 }
189 
190 
191 template <typename BDB_SplitStore, typename BDB_Vol>
192 inline size_t
194 {
195  if (m_Cursor.get()) {
196  return m_Cursor->GetLastMultiFetchDataLen();
197  }
198  return 0;
199 }
200 
201 
202 template <typename BDB_SplitStore, typename BDB_Vol>
205 {
206  if (m_Volume.get()) {
207  return *m_Volume;
208  }
209  NCBI_THROW(CException, eUnknown, "no open volume");
210 }
211 
212 
213 template <typename BDB_SplitStore, typename BDB_Vol>
214 inline Uint4
216 {
217  return GetLastBlobId();
218 }
219 
220 
221 template <typename BDB_SplitStore, typename BDB_Vol>
222 inline Uint4
224 {
225  if (m_Volume.get()) {
226  return (Uint4)m_Volume->GetUid();
227  }
228  NCBI_THROW(CException, eUnknown, "no open volume");
229 }
230 
231 
232 template <typename BDB_SplitStore, typename BDB_Vol>
233 inline void
235 {
236  /// get rid of our existing cursor + volume
237  m_Cursor.reset();
238  m_Volume.reset();
239  if ( !m_Files.size() ) {
240  return;
241  }
242 
243  /// open the next file
244  string path = m_Files.back();
245  m_Files.pop_back();
246 
247  m_Volume.reset(new TVolume);
248  m_Volume->SetCacheSize(10 * 1024 * 1024);
249  if (m_Env) {
250  m_Volume->SetEnv(*m_Env);
251  }
252 
253  LOG_POST_XX(Db_Bdb_Cursor, 1, Info
254  << "CBDB_SplitCursor::x_NextVolume(): opening: " << path);
255  m_Volume->Open(path, CBDB_RawFile::eReadOnly);
256 
257  m_Cursor.reset(new CBDB_FileCursor(*m_Volume));
258  m_Cursor->InitMultiFetch(m_BufferSize);
259 }
260 
261 
263 
264 
265 #endif // LOG___BDB_SPLIT_CURSOR__HPP
Berkeley BDB file cursor.
Wrapper around Berkeley DB environment structure.
BDB File management.
BDB environment object a collection including support for some or all of caching, locking,...
Definition: bdb_env.hpp:61
Berkeley DB file cursor class.
Definition: bdb_cursor.hpp:95
unique_ptr< CBDB_FileCursor > m_Cursor
BDB_SplitStore TSplitStore
size_t GetLastMultiFetchDataLen() const
TVolume & GetSourceVolume()
vector< string > m_Files
Uint4 GetLastBlobId() const
unique_ptr< TVolume > m_Volume
Uint4 GetCurrentBlobId() const
const void * GetLastMultiFetchData() const
EBDB_ErrCode Fetch()
CBDB_SplitCursor(TSplitStore &store)
void InitMultiFetch(size_t buffer_size)
CStopWatch –.
Definition: ncbitime.hpp:1937
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
Include a standard set of the NCBI C++ Toolkit most basic headers.
EBDB_ErrCode
BDB Return codes.
Definition: bdb_file.hpp:57
@ eBDB_Ok
Definition: bdb_file.hpp:58
@ eBDB_NotFound
Definition: bdb_file.hpp:59
const string & GetPath() const
return the path to the environment
Definition: bdb_env.hpp:326
DB_ENV * GetEnv()
Return underlying DB_ENV structure pointer for low level access.
Definition: bdb_env.hpp:151
#define NULL
Definition: ncbistd.hpp:225
#define LOG_POST_XX(error_name, err_subcode, message)
Definition: ncbidiag.hpp:569
#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
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
static string CreateAbsolutePath(const string &path, ERelativeToWhat rtw=eRelativeToCwd)
Get an absolute path from some, possibly relative, path.
Definition: ncbifile.cpp:665
static bool IsAbsolutePath(const string &path)
Check if a "path" is absolute for the current OS.
Definition: ncbifile.cpp:508
void FindFiles(TPathIterator path_begin, TPathIterator path_end, const vector< string > &masks, TFindFunc &find_func, TFindFiles flags=fFF_Default)
Generic algorithm for file search.
Definition: ncbifile.hpp:3145
static void SplitPath(const string &path, string *dir=0, string *base=0, string *ext=0)
Split a path string into its basic components.
Definition: ncbifile.cpp:358
@ fFF_File
find files
Definition: ncbifile.hpp:3008
@ eUnknown
Definition: app_popup.hpp:72
#define NCBI_DEPRECATED
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
void Start(void)
Start the timer.
Definition: ncbitime.hpp:2764
Definition of all error codes used in bdb library (bdb.lib and ncbi_xcache_bdb.lib).
constexpr auto sort(_Init &&init)
const struct ncbi::grid::netcache::search::fields::SIZE size
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
static int buffer_size
Definition: pcretest.c:1050
bool operator()(const string &s1, const string &s2) const
Modified on Sat Jun 22 10:44:14 2024 by modify_doxy.py rev. 669887