@@ -207,6 +207,12 @@ class BerkeleyBatch
207
207
operator Dbt*();
208
208
};
209
209
210
+ private:
211
+ bool ReadKey (CDataStream& key, CDataStream& value);
212
+ bool WriteKey (CDataStream& key, CDataStream& value, bool overwrite=true );
213
+ bool EraseKey (CDataStream& key);
214
+ bool HasKey (CDataStream& key);
215
+
210
216
protected:
211
217
Db* pdb;
212
218
std::string strFile;
@@ -236,91 +242,65 @@ class BerkeleyBatch
236
242
template <typename K, typename T>
237
243
bool Read (const K& key, T& value)
238
244
{
239
- if (!pdb)
240
- return false ;
241
-
242
245
// Key
243
246
CDataStream ssKey (SER_DISK, CLIENT_VERSION);
244
247
ssKey.reserve (1000 );
245
248
ssKey << key;
246
- SafeDbt datKey (ssKey.data (), ssKey.size ());
247
249
248
- // Read
249
- SafeDbt datValue;
250
- int ret = pdb->get (activeTxn, datKey, datValue, 0 );
250
+ CDataStream ssValue (SER_DISK, CLIENT_VERSION);
251
251
bool success = false ;
252
- if (datValue.get_data () != nullptr ) {
252
+ bool ret = ReadKey (ssKey, ssValue);
253
+ if (ret) {
253
254
// Unserialize value
254
255
try {
255
- CDataStream ssValue ((char *)datValue.get_data (), (char *)datValue.get_data () + datValue.get_size (), SER_DISK, CLIENT_VERSION);
256
256
ssValue >> value;
257
257
success = true ;
258
258
} catch (const std::exception&) {
259
259
// In this case success remains 'false'
260
260
}
261
261
}
262
- return ret == 0 && success;
262
+ return ret && success;
263
263
}
264
264
265
265
template <typename K, typename T>
266
266
bool Write (const K& key, const T& value, bool fOverwrite = true )
267
267
{
268
- if (!pdb)
269
- return true ;
270
- if (fReadOnly )
271
- assert (!" Write called on database in read-only mode" );
272
-
273
268
// Key
274
269
CDataStream ssKey (SER_DISK, CLIENT_VERSION);
275
270
ssKey.reserve (1000 );
276
271
ssKey << key;
277
- SafeDbt datKey (ssKey.data (), ssKey.size ());
278
272
279
273
// Value
280
274
CDataStream ssValue (SER_DISK, CLIENT_VERSION);
281
275
ssValue.reserve (10000 );
282
276
ssValue << value;
283
- SafeDbt datValue (ssValue.data (), ssValue.size ());
284
277
285
278
// Write
286
- int ret = pdb->put (activeTxn, datKey, datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
287
- return (ret == 0 );
279
+ return WriteKey (ssKey, ssValue, fOverwrite );
288
280
}
289
281
290
282
template <typename K>
291
283
bool Erase (const K& key)
292
284
{
293
- if (!pdb)
294
- return false ;
295
- if (fReadOnly )
296
- assert (!" Erase called on database in read-only mode" );
297
-
298
285
// Key
299
286
CDataStream ssKey (SER_DISK, CLIENT_VERSION);
300
287
ssKey.reserve (1000 );
301
288
ssKey << key;
302
- SafeDbt datKey (ssKey.data (), ssKey.size ());
303
289
304
290
// Erase
305
- int ret = pdb->del (activeTxn, datKey, 0 );
306
- return (ret == 0 || ret == DB_NOTFOUND);
291
+ return EraseKey (ssKey);
307
292
}
308
293
309
294
template <typename K>
310
295
bool Exists (const K& key)
311
296
{
312
- if (!pdb)
313
- return false ;
314
-
315
297
// Key
316
298
CDataStream ssKey (SER_DISK, CLIENT_VERSION);
317
299
ssKey.reserve (1000 );
318
300
ssKey << key;
319
- SafeDbt datKey (ssKey.data (), ssKey.size ());
320
301
321
302
// Exists
322
- int ret = pdb->exists (activeTxn, datKey, 0 );
323
- return (ret == 0 );
303
+ return HasKey (ssKey);
324
304
}
325
305
326
306
Dbc* GetCursor ();
0 commit comments