Copyright header updates s/2013/2014 on files whose last git commit was done in 2014.
[VerusCoin.git] / src / leveldbwrapper.cpp
CommitLineData
57702541 1// Copyright (c) 2012-2014 The Bitcoin developers
43b7905e
PW
2// Distributed under the MIT/X11 software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
b64187d0 5#include "leveldbwrapper.h"
51ed9ec9 6
43b7905e
PW
7#include "util.h"
8
51ed9ec9 9#include <boost/filesystem.hpp>
43b7905e 10#include <leveldb/cache.h>
51ed9ec9 11#include <leveldb/env.h>
43b7905e 12#include <leveldb/filter_policy.h>
eb12a14d 13#include <memenv.h>
43b7905e 14
421218d3
PW
15void HandleError(const leveldb::Status &status) throw(leveldb_error) {
16 if (status.ok())
17 return;
7d9d134b 18 LogPrintf("%s\n", status.ToString());
421218d3
PW
19 if (status.IsCorruption())
20 throw leveldb_error("Database corrupted");
21 if (status.IsIOError())
22 throw leveldb_error("Database I/O error");
23 if (status.IsNotFound())
24 throw leveldb_error("Database entry missing");
25 throw leveldb_error("Unknown database error");
26}
27
1c83b0a3 28static leveldb::Options GetOptions(size_t nCacheSize) {
43b7905e 29 leveldb::Options options;
1c83b0a3
PW
30 options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
31 options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
43b7905e
PW
32 options.filter_policy = leveldb::NewBloomFilterPolicy(10);
33 options.compression = leveldb::kNoCompression;
2233fc53 34 options.max_open_files = 64;
43b7905e
PW
35 return options;
36}
37
b64187d0 38CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory, bool fWipe) {
43b7905e
PW
39 penv = NULL;
40 readoptions.verify_checksums = true;
41 iteroptions.verify_checksums = true;
42 iteroptions.fill_cache = false;
43 syncoptions.sync = true;
1c83b0a3 44 options = GetOptions(nCacheSize);
43b7905e 45 options.create_if_missing = true;
e1bfbab8
PW
46 if (fMemory) {
47 penv = leveldb::NewMemEnv(leveldb::Env::Default());
48 options.env = penv;
49 } else {
7fea4846 50 if (fWipe) {
7d9d134b 51 LogPrintf("Wiping LevelDB in %s\n", path.string());
7fea4846
PW
52 leveldb::DestroyDB(path.string(), options);
53 }
e1bfbab8 54 boost::filesystem::create_directory(path);
7d9d134b 55 LogPrintf("Opening LevelDB in %s\n", path.string());
e1bfbab8 56 }
43b7905e 57 leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
b35e9932 58 HandleError(status);
881a85a2 59 LogPrintf("Opened LevelDB successfully\n");
43b7905e
PW
60}
61
b64187d0 62CLevelDBWrapper::~CLevelDBWrapper() {
43b7905e
PW
63 delete pdb;
64 pdb = NULL;
65 delete options.filter_policy;
66 options.filter_policy = NULL;
67 delete options.block_cache;
68 options.block_cache = NULL;
69 delete penv;
70 options.env = NULL;
71}
72
b64187d0 73bool CLevelDBWrapper::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error) {
43b7905e 74 leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
b35e9932 75 HandleError(status);
43b7905e
PW
76 return true;
77}
This page took 0.08905 seconds and 4 git commands to generate.