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

Go to the SVN repository for this file.

1 #ifndef BDB_VOLUMES_HPP__
2 #define BDB_VOLUMES_HPP__
3 
4 /* $Id: bdb_volumes.hpp 84663 2018-11-27 18:22:00Z 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  * Author: Anatoliy Kuznetsov
30  *
31  * File Description: Volumes management
32  *
33  *
34  */
35 
36 #include <corelib/ncbistd.hpp>
37 #include <corelib/ncbiexpt.hpp>
38 #include <corelib/ncbimisc.hpp>
39 #include <db/bdb/bdb_file.hpp>
40 #include <db/bdb/bdb_env.hpp>
41 
43 
44 /// BDB file for storing volumes registry
45 ///
47 {
48  CBDB_FieldUint4 volume_id; ///< Volume ID
49 
50  CBDB_FieldUint4 type; ///< Volume type
51  CBDB_FieldUint4 status; ///< Volume status
52  CBDB_FieldUint4 raw_status; ///< Raw data status
53  CBDB_FieldUint4 lock; ///< Volume lock counter
54  CBDB_FieldUint4 version; ///< Volume version
55  CBDB_FieldUint4 date_from; ///< Date interval
56  CBDB_FieldUint4 date_to; ///< Date interval
57  CBDB_FieldUint4 mtimestamp; ///< Modification date
58  CBDB_FieldUint4 relo_volume_id; ///< Modification date
59  CBDB_FieldString location; ///< Mount point for the volume
60  CBDB_FieldString backup_loc; ///< Location of volume backup
61 
62 
64  : CBDB_File(CBDB_File::eDuplicatesDisable,
66  {
67  DisableNull();
68 
69  BindKey ("volume_id", &volume_id);
70 
71  BindData("type", &type);
72  BindData("status", &status);
73  BindData("raw_status", &raw_status);
74  BindData("lock", &lock);
75  BindData("version", &version);
76  BindData("date_from", &date_from);
77  BindData("date_to", &date_to);
78  BindData("mtimestamp", &mtimestamp);
79  BindData("relo_volume_id", &relo_volume_id);
80  BindData("location", &location, 1024);
81  BindData("backup_loc", &backup_loc, 1024);
82  }
83 
84 private:
87 };
88 
89 /// Volumes management
90 ///
91 /// Stores all volume registration as a transactional BDB database.
92 /// All operations are implemented as atomic transactions.
93 /// The transactional database can be used across process boundaries
94 /// (but not across network).
95 ///
97 {
98 public:
99  /// Volume status codes
101  eOnlinePassive = 0, ///< Online, read-only closed for updates
102  eOnlineActive, ///< Online, new updates can come
103  eOnlineMaintenance, ///< Online, readable, under background processing
104  eOffline, ///< Offline, unmounted
105  eOfflineRelocated, ///< Offline, data moved to another volume
106  eOfflineArchived, ///< Offline, data moved to archive
107  eOfflineRestore ///< Offline, restore request received
108  };
109 public:
110  CBDB_Volumes();
111  ~CBDB_Volumes();
112 
113  /// Open (mount) volume management database.
114  /// Volume management uses a dedicated directory to store volume management
115  /// database, which is always opens as transactional BDB environment.
116  /// Do NOT keep any other databases or environments at the same location
117  ///
118  /// @param dir_path
119  /// Location of volume management database.
120  ///
121  void Open(const string& dir_path);
122 
123  /// Close volume management database
124  ///
125  void Close();
126 
127  /// Get volume record
128  /// (throws an exception if record not found)
129  ///
130  const SVolumesDB& FetchVolumeRec(unsigned volume_id);
131 
132  /// Get low level access to volumes database (be careful will you!)
133  SVolumesDB& GetVolumeDB();
134 
135 
136  /// Register a new volume
137  ///
138  /// @return volume id
139  unsigned AddVolume(const string& location,
140  unsigned type,
141  unsigned version,
142  EVolumeStatus status = eOffline);
143 
144  /// Set backup token (location) for volume
145  void SetBackupLocation(unsigned volume_id, const string& backup_loc);
146 
147  /// Change volume status
148  void ChangeStatus(unsigned volume_id, EVolumeStatus status);
149 
150  /// Increment volume lock counter
151  /// Volume locking sets restrictions on some operations
152  /// (like status change)
153  ///
154  void LockVolume(unsigned volume_id);
155 
156  /// Decrement volume lock counter
157  void UnLockVolume(unsigned volume_id);
158 
159  /// Transactional volume switch: old volume goes offline
160  /// new volume comes online
161  ///
162  void SwitchVolumes(unsigned volume_id_old,
163  unsigned volume_id_new,
164  EVolumeStatus new_status = eOnlineActive);
165 
166  /// Old volume goes offline, new volumes online
167  /// All volume records should be created upfront
168  void Split(unsigned volume_id_old,
169  unsigned volume_id_new1,
170  unsigned volume_id_new2,
171  EVolumeStatus new_status = eOnlineActive);
172 
173  /// Delete volume
174  void Delete(const vector<unsigned>& remove_list);
175 
176  /// Sort volumes
177  void SortVolumes();
178 
179  /// Merge volumes into one
180  void Merge(unsigned volume_id_new,
181  const vector<unsigned>& merge_list,
182  EVolumeStatus new_status = eOnlineActive);
183 
184  /// Update date range
185  void SetDateRange(unsigned volume_id,
186  unsigned from,
187  unsigned to);
188 
189  /// Get list of all available volumes
190  /// @param vlist
191  /// List of volumes
192  /// @param avail
193  /// When TRUE returns only online volumes
194  /// (FALSE - all volumes)
195  void EnumerateVolumes(vector<unsigned>& vlist, bool avail = false);
196 
197  /// Utility to convert status to string
198  static string StatusToString(EVolumeStatus status);
199 protected:
200  /// Check if status change is possible
201  bool x_CheckStatusChange(EVolumeStatus old_status,
202  EVolumeStatus new_status);
203  void x_ChangeCurrentStatus(unsigned volume_id,
204  EVolumeStatus status);
205 private:
209 private:
210  unique_ptr<CBDB_Env> m_Env;
211  unique_ptr<SVolumesDB> m_VolumesDB;
212  string m_Path;
213 };
214 
215 /// Exceptions specific to volumes management
216 ///
218 {
219 public:
220  enum EErrCode {
226  };
227 
228  virtual const char* GetErrCodeString(void) const override
229  {
230  switch (GetErrCode())
231  {
232  case eTransactionsNotAvailable: return "eTransactionsNotAvailable";
233  case eVolumeNotFound: return "eVolumeNotFound";
234  case eVolumeLocked: return "eVolumeLocked";
235  case eVolumeNotLocked: return "eVolumeNotLocked";
236  case eVolumeStatusIncorrect: return "eVolumeStatusIncorrect";
237 
238  default: return CException::GetErrCodeString();
239  }
240  }
241 
243 };
244 
245 
246 
248 
249 #endif // BDB_VOLUMES_HPP__
250 
Wrapper around Berkeley DB environment structure.
BDB File management.
Base BDB exception class.
Definition: bdb_expt.hpp:53
String field type designed to work with C-strings (ASCIIZ)
Definition: bdb_types.hpp:1590
Uint4 field type.
Definition: bdb_types.hpp:1267
Berkeley DB file class.
Definition: bdb_file.hpp:445
Exceptions specific to volumes management.
NCBI_EXCEPTION_DEFAULT(CBDB_VolumesException, CBDB_Exception)
virtual const char * GetErrCodeString(void) const override
Transaction class for volumes management.
Definition: bdb_volumes.cpp:50
Volumes management.
Definition: bdb_volumes.hpp:97
CBDB_Volumes(const CBDB_Volumes &)
CBDB_Volumes & operator=(const CBDB_Volumes &)
unique_ptr< CBDB_Env > m_Env
EVolumeStatus
Volume status codes.
@ eOnlineMaintenance
Online, readable, under background processing.
@ eOnlineActive
Online, new updates can come.
@ eOffline
Offline, unmounted.
@ eOfflineRelocated
Offline, data moved to another volume.
@ eOfflineArchived
Offline, data moved to archive.
unique_ptr< SVolumesDB > m_VolumesDB
Include a standard set of the NCBI C++ Toolkit most basic headers.
static const char location[]
Definition: config.c:97
@ eQueue
Definition: grid_cli.hpp:191
EBDB_ErrCode Delete(EIgnoreError on_error=eThrowOnError)
Delete record corresponding to the current key value.
Definition: bdb_file.cpp:1496
void Close()
Close file.
Definition: bdb_file.cpp:191
void Open(const string &filename, EOpenMode open_mode, bool support_dirty_read=false, unsigned rec_len=0)
Open file with specified access mode.
Definition: bdb_file.hpp:774
void DisableNull()
Disable NULL/not NULL in data fields (Performance tweak, call before BindData)
Definition: bdb_file.hpp:634
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
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
#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 NCBI_BDB_EXPORT
Definition: ncbi_export.h:272
static int version
Definition: mdb_load.c:29
Defines NCBI C++ exception handling.
Miscellaneous common-use basic types and functionality.
BDB file for storing volumes registry.
Definition: bdb_volumes.hpp:47
CBDB_FieldUint4 lock
Volume lock counter.
Definition: bdb_volumes.hpp:53
CBDB_FieldUint4 relo_volume_id
Modification date.
Definition: bdb_volumes.hpp:58
CBDB_FieldUint4 raw_status
Raw data status.
Definition: bdb_volumes.hpp:52
SVolumesDB(const SVolumesDB &)
SVolumesDB & operator=(const SVolumesDB &)
CBDB_FieldUint4 version
Volume version.
Definition: bdb_volumes.hpp:54
CBDB_FieldUint4 volume_id
Volume ID.
Definition: bdb_volumes.hpp:48
CBDB_FieldUint4 mtimestamp
Modification date.
Definition: bdb_volumes.hpp:57
CBDB_FieldUint4 type
Volume type.
Definition: bdb_volumes.hpp:50
CBDB_FieldUint4 date_from
Date interval.
Definition: bdb_volumes.hpp:55
CBDB_FieldString location
Mount point for the volume.
Definition: bdb_volumes.hpp:59
CBDB_FieldUint4 status
Volume status.
Definition: bdb_volumes.hpp:51
CBDB_FieldString backup_loc
Location of volume backup.
Definition: bdb_volumes.hpp:60
CBDB_FieldUint4 date_to
Date interval.
Definition: bdb_volumes.hpp:56
Definition: type.c:6
void Merge(wxMenu &menu_1, const wxMenu &menu_2)
merges all items form menu_2 into menu_1, preserving the structure if possible
Definition: wx_utils.cpp:579
Modified on Tue May 28 05:49:54 2024 by modify_doxy.py rev. 669887