1 // Copyright (c) 2012 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 #include <leveldb/env.h>
9 #include <leveldb/cache.h>
10 #include <leveldb/filter_policy.h>
12 #include <boost/filesystem.hpp>
14 static leveldb::Options GetOptions() {
15 leveldb::Options options;
16 int nCacheSizeMB = GetArg("-dbcache", 25);
17 options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
18 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
19 options.compression = leveldb::kNoCompression;
23 CLevelDB::CLevelDB(const boost::filesystem::path &path) {
25 readoptions.verify_checksums = true;
26 iteroptions.verify_checksums = true;
27 iteroptions.fill_cache = false;
28 syncoptions.sync = true;
29 options = GetOptions();
30 options.create_if_missing = true;
31 boost::filesystem::create_directory(path);
32 printf("Opening LevelDB in %s\n", path.string().c_str());
33 leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
35 throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str()));
36 printf("Opened LevelDB successfully\n");
39 CLevelDB::~CLevelDB() {
42 delete options.filter_policy;
43 options.filter_policy = NULL;
44 delete options.block_cache;
45 options.block_cache = NULL;
50 bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) {
51 leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
53 printf("LevelDB write failure: %s\n", status.ToString().c_str());