NCBI C++ ToolKit
demo4.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: demo4.cpp 66574 2015-03-12 15:02:10Z ivanov $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Anatoliy Kuznetsov
27  *
28  * File Description: Demo4 application for NCBI Berkeley DB library (BDB).
29  * BLOB storage demonstration.
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbiapp.hpp>
35 #include <corelib/ncbiargs.hpp>
36 #include <assert.h>
37 
38 #include <db/bdb/bdb_blob.hpp>
39 
41 
42 
43 string s_LobDBFileName = "blobstore.db";
44 string s_LobDBFileName2 = "blobstore2.db";
45 
46 
47 const int array1[] = {1, 2, 3, 4, 5, 0};
48 const int array2[] = {10, 20, 30, 40, 50, 0};
49 
50 ////////////////////////////////////////////////////////////////
51 // Demo case 1:
52 // Demonstrates how to use CBDB_LobFile
53 // (BLOBs are accessed using integer access key)
54 
55 //
56 // Load LOB storage
57 
59 {
60  CBDB_LobFile lob;
62 
63 
64  EBDB_ErrCode ret = lob.Insert(1, array1, sizeof(array1));
65  if (ret != eBDB_Ok) {
66  cout << "Insert failed!" << endl;
67  exit(1);
68  }
69  ret = lob.Insert(2, array2, sizeof(array2));
70  if (ret != eBDB_Ok) {
71  cout << "Insert failed!" << endl;
72  exit(1);
73  }
74 }
75 
76 
77 
79 {
80  int buffer[256];
81 
82  CBDB_LobFile lob;
84 
85  // The safest way to read a LOB is first to retrieve the correspondent
86  // record and use LobSize() to get information about the BLOB size.
87  // When the size is known we can allocate memory and fetch the LOB
88  // data using GetData() function.
89 
90 
91  EBDB_ErrCode ret = lob.Fetch(1);
92 
93  if (ret == eBDB_Ok) {
94  unsigned size = lob.LobSize();
95  if (size > sizeof(buffer)) {
96  cout << "Insufficient buffer size!" << endl;
97  }
98  else {
99  ret = lob.GetData(buffer, sizeof(buffer));
100  assert(ret == eBDB_Ok);
101  for (unsigned int i = 0; i < size / sizeof(buffer[0]); ++i) {
102  cout << buffer[i] << " ";
103  }
104  cout << endl;
105  }
106  } else {
107  cout << "BLOB 1 not found" << endl;
108  }
109 
110 
111  // In case we know the maximum possible BLOB size we can implement
112  // one phase fetch. It should work faster in many cases.
113 
114  void* ptr = (void*)buffer;
115  ret = lob.Fetch(2,
116  &ptr,
117  sizeof(buffer),
119 
120  if (ret == eBDB_Ok) {
121  unsigned size = lob.LobSize();
122  for (unsigned int i = 0; i < size / sizeof(buffer[0]); ++i) {
123  cout << buffer[i] << " ";
124  }
125  cout << endl;
126  } else {
127  cout << "BLOB 2 not found" << endl;
128  }
129 
130 }
131 
132 ////////////////////////////////////////////////////////////////
133 // Demo case 2:
134 // Demonstrates how to use custom BLOB storage
135 // (derived from CBDB_BLobFile)
136 
137 
138 // @internal
139 struct SDemoDB : public CBDB_BLobFile
140 {
143 
145  {
146  BindKey("key", &key, 256);
147  BindKey("subkey", &subkey, 256);
148  }
149 };
150 
151 
153 {
154  SDemoDB lob;
156 
157 
158  lob.key = "Key1";
159  lob.subkey = "SubKey1";
160 
161  EBDB_ErrCode ret = lob.Insert(array1, sizeof(array1));
162  if (ret != eBDB_Ok) {
163  NcbiCout << "Insert failed!" << endl;
164  exit(1);
165  }
166 
167  lob.key = "Key2";
168  lob.subkey = "SubKey2";
169 
170  ret = lob.Insert(array2, sizeof(array2));
171  if (ret != eBDB_Ok) {
172  NcbiCerr << "Insert failed!" << endl;
173  exit(1);
174  }
175 }
176 
178 {
179  int buffer[256];
180 
181  SDemoDB lob;
183 
184  lob.key = "Key1";
185  lob.subkey = "SubKey1";
186 
187  EBDB_ErrCode ret = lob.Fetch();
188 
189  if (ret == eBDB_Ok) {
190  unsigned size = lob.LobSize();
191  if (size > sizeof(buffer)) {
192  NcbiCout << "Insufficient buffer size!" << NcbiEndl;
193  }
194  else {
195  ret = lob.GetData(buffer, sizeof(buffer));
196  assert(ret == eBDB_Ok);
197  for (unsigned int i = 0; i < size / sizeof(buffer[0]); ++i) {
198  NcbiCout << buffer[i] << " ";
199  }
200  NcbiCout << NcbiEndl;
201  }
202  } else {
203  NcbiCout << "BLOB 1 not found" << NcbiEndl;
204  }
205 
206 }
207 
208 
209 
210 /// BLob Demo application
211 ///
212 /// @internal
213 ///
215 {
216 public:
217  void Init(void);
218  int Run(void);
219 };
220 
221 
223 {
228  d->SetUsageContext("bdb demo1",
229  "Demo1 application for BDB library");
231 }
232 
234 {
235  try
236  {
237  LoadBLOB_Table();
238  PrintBLOB_Table();
239 
240  LoadDemoDB();
241  PrintDemoDB();
242  }
243  catch (CBDB_ErrnoException& ex)
244  {
245  cout << "Error! DBD errno exception:" << ex.what();
246  return 1;
247  }
248  catch (CBDB_LibException& ex)
249  {
250  cout << "Error! DBD library exception:" << ex.what();
251  return 1;
252  }
253 
254  return 0;
255 }
256 
257 
258 int main(int argc, const char* argv[])
259 {
260  return CBDB_BLobDemo1().AppMain(argc, argv);
261 }
#define NcbiCerr
Definition: ncbistre.hpp:407
size_t LobSize() const
Get LOB size. Becomes available right after successfull Fetch.
Definition: bdb_blob.cpp:164
#define assert(x)
Definition: srv_diag.hpp:58
Set by default if _DEBUG; else not set.
Definition: ncbidiag.hpp:682
Defines command line argument related classes.
SDemoDB()
Definition: demo4.cpp:144
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:789
#define NcbiCout
Definition: ncbistre.hpp:406
Berkeley DB BLOB File class.
Definition: bdb_blob.hpp:58
Berkeley DB Large Object File class.
Definition: bdb_blob.hpp:256
BDB errno exception class.
Definition: bdb_expt.hpp:82
EDiagSev SetDiagPostLevel(EDiagSev post_sev=eDiag_Error)
Set the threshold severity for posting the messages.
Definition: ncbidiag.cpp:5409
#define NcbiEndl
Definition: ncbistre.hpp:411
EBDB_ErrCode Fetch()
Fetch the record corresponding to the current key value.
Definition: bdb_blob.cpp:54
Warning message.
Definition: ncbidiag.hpp:640
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:492
X * release(void)
Release pointer.
Definition: ncbimisc.hpp:400
void PrintDemoDB()
Definition: demo4.cpp:177
EBDB_ErrCode GetData(void *buf, size_t size)
Copy LOB data into the 'buf'.
Definition: bdb_blob.cpp:129
EBDB_ErrCode Insert(unsigned int lob_id, const void *data, size_t size)
Insert BLOB data into the database, does nothing if key exists.
Definition: bdb_blob.cpp:466
void LoadDemoDB()
Definition: demo4.cpp:152
int i
size_t LobSize() const
Get LOB size. Becomes available right after successfull Fetch.
Definition: bdb_blob.cpp:581
EBDB_ErrCode GetData(void *buf, size_t size)
Copy LOB data into the 'buf'.
Definition: bdb_blob.cpp:550
string s_LobDBFileName
Definition: demo4.cpp:43
void SetDiagPostFlag(EDiagPostFlag flag)
Set the specified flag (globally).
Definition: ncbidiag.cpp:5350
int Run(void)
Run the application.
Definition: demo4.cpp:233
CBDB_FieldString subkey
Definition: demo4.cpp:142
auto_ptr –
Definition: ncbimisc.hpp:336
void Init(void)
Initialize the application.
Definition: demo4.cpp:222
EBDB_ErrCode Insert(const void *data, size_t size)
Insert BLOB into the database.
Definition: bdb_blob.cpp:135
int size
implies 'eReadWrite' too
Definition: bdb_file.hpp:81
CArgDescriptions –.
Definition: ncbiargs.hpp:509
BDB library exception.
Definition: bdb_expt.hpp:120
const int array2[]
Definition: demo4.cpp:48
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:724
String field type designed to work with C-strings (ASCIIZ)
Definition: bdb_types.hpp:1589
EBDB_ErrCode Fetch(unsigned int lob_id)
Fetch LOB record.
Definition: bdb_blob.hpp:328
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
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
const int array1[]
Definition: demo4.cpp:47
void LoadBLOB_Table()
Definition: demo4.cpp:58
CNcbiApplication –.
Definition: ncbiapp.hpp:120
void SetUsageContext(const string &usage_name, const string &usage_description, bool usage_sort_args=false, SIZE_TYPE usage_width=78)
Set extra info to be used by PrintUsage().
Definition: ncbiargs.cpp:3110
CBDB_FieldString key
Definition: demo4.cpp:141
BDB library BLOB support.
BLob Demo application.
Definition: demo4.cpp:214
exit(2)
string s_LobDBFileName2
Definition: demo4.cpp:44
EBDB_ErrCode
BDB Return codes.
Definition: bdb_file.hpp:57
int main(int argc, const char *argv[])
Definition: demo4.cpp:258
void PrintBLOB_Table()
Definition: demo4.cpp:78
USING_NCBI_SCOPE
Definition: demo4.cpp:40
Set by default if _DEBUG; else not set.
Definition: ncbidiag.hpp:680
static uschar * buffer
Definition: pcretest.c:187
void BindKey(const char *field_name, CBDB_Field *key_field, size_t buf_size=0)
Definition: bdb_file.cpp:1271
Modified on Tue May 03 18:01:01 2016 by modify_doxy.py rev. 426318