Anoncoin  0.9.4
P2P Digital Currency
leveldbwrapper.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2014 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "leveldbwrapper.h"
7 
8 #include "util.h"
9 
10 #include <boost/filesystem.hpp>
11 #include <leveldb/cache.h>
12 #include <leveldb/env.h>
13 #include <leveldb/filter_policy.h>
14 #include <memenv.h>
15 
16 void HandleError(const leveldb::Status &status) throw(leveldb_error) {
17  if (status.ok())
18  return;
19  LogPrintf("%s\n", status.ToString());
20  if (status.IsCorruption())
21  throw leveldb_error("Database corrupted");
22  if (status.IsIOError())
23  throw leveldb_error("Database I/O error");
24  if (status.IsNotFound())
25  throw leveldb_error("Database entry missing");
26  throw leveldb_error("Unknown database error");
27 }
28 
29 static leveldb::Options GetOptions(size_t nCacheSize) {
30  leveldb::Options options;
31  options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
32  options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
33  options.filter_policy = leveldb::NewBloomFilterPolicy(10);
34  options.compression = leveldb::kNoCompression;
35  options.max_open_files = 64;
36  return options;
37 }
38 
39 CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory, bool fWipe) {
40  penv = NULL;
41  readoptions.verify_checksums = true;
42  iteroptions.verify_checksums = true;
43  iteroptions.fill_cache = false;
44  syncoptions.sync = true;
45  options = GetOptions(nCacheSize);
46  options.create_if_missing = true;
47  if (fMemory) {
48  penv = leveldb::NewMemEnv(leveldb::Env::Default());
49  options.env = penv;
50  } else {
51  if (fWipe) {
52  LogPrintf("Wiping LevelDB in %s\n", path.string());
53  leveldb::DestroyDB(path.string(), options);
54  }
55  TryCreateDirectory(path);
56  LogPrintf("Opening LevelDB in %s\n", path.string());
57  }
58  leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
59  HandleError(status);
60  LogPrintf("Opened LevelDB successfully\n");
61 }
62 
64  delete pdb;
65  pdb = NULL;
66  delete options.filter_policy;
67  options.filter_policy = NULL;
68  delete options.block_cache;
69  options.block_cache = NULL;
70  delete penv;
71  options.env = NULL;
72 }
73 
74 bool CLevelDBWrapper::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error) {
75  leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
76  HandleError(status);
77  return true;
78 }
leveldb::Env * penv
leveldb::WriteOptions syncoptions
leveldb::DB * pdb
#define LogPrintf(...)
Definition: util.h:118
CLevelDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false)
void HandleError(const leveldb::Status &status)
bool TryCreateDirectory(const boost::filesystem::path &p)
Definition: util.cpp:1083
leveldb::ReadOptions iteroptions
bool WriteBatch(CLevelDBBatch &batch, bool fSync=false)
leveldb::Options options
leveldb::ReadOptions readoptions