Transaction hash caching
[novacoin.git] / src / leveldb.cpp
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.
4
5 #include "leveldb.h"
6 #include "util.h"
7
8 #include <leveldb/env.h>
9 #include <leveldb/cache.h>
10 #include <leveldb/filter_policy.h>
11
12 #include <boost/filesystem.hpp>
13
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;
20     return options;
21 }
22
23 CLevelDB::CLevelDB(const boost::filesystem::path &path) {
24     penv = NULL;
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);
34     if (!status.ok())
35         throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str()));
36     printf("Opened LevelDB successfully\n");
37 }
38
39 CLevelDB::~CLevelDB() {
40     delete pdb;
41     pdb = NULL;
42     delete options.filter_policy;
43     options.filter_policy = NULL;
44     delete options.block_cache;
45     options.block_cache = NULL;
46     delete penv;
47     options.env = NULL;
48 }
49
50 bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) {
51     leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
52     if (!status.ok()) {
53         printf("LevelDB write failure: %s\n", status.ToString().c_str());
54         return false;
55     }
56     return true;
57 }
58