]>
Commit | Line | Data |
---|---|---|
43b7905e PW |
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> | |
e1bfbab8 | 11 | #include <memenv/memenv.h> |
43b7905e PW |
12 | |
13 | #include <boost/filesystem.hpp> | |
14 | ||
1c83b0a3 | 15 | static leveldb::Options GetOptions(size_t nCacheSize) { |
43b7905e | 16 | leveldb::Options options; |
1c83b0a3 PW |
17 | options.block_cache = leveldb::NewLRUCache(nCacheSize / 2); |
18 | options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously | |
43b7905e PW |
19 | options.filter_policy = leveldb::NewBloomFilterPolicy(10); |
20 | options.compression = leveldb::kNoCompression; | |
21 | return options; | |
22 | } | |
23 | ||
7fea4846 | 24 | CLevelDB::CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory, bool fWipe) { |
43b7905e PW |
25 | penv = NULL; |
26 | readoptions.verify_checksums = true; | |
27 | iteroptions.verify_checksums = true; | |
28 | iteroptions.fill_cache = false; | |
29 | syncoptions.sync = true; | |
1c83b0a3 | 30 | options = GetOptions(nCacheSize); |
43b7905e | 31 | options.create_if_missing = true; |
e1bfbab8 PW |
32 | if (fMemory) { |
33 | penv = leveldb::NewMemEnv(leveldb::Env::Default()); | |
34 | options.env = penv; | |
35 | } else { | |
7fea4846 PW |
36 | if (fWipe) { |
37 | printf("Wiping LevelDB in %s\n", path.string().c_str()); | |
38 | leveldb::DestroyDB(path.string(), options); | |
39 | } | |
e1bfbab8 PW |
40 | boost::filesystem::create_directory(path); |
41 | printf("Opening LevelDB in %s\n", path.string().c_str()); | |
42 | } | |
43b7905e PW |
43 | leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb); |
44 | if (!status.ok()) | |
45 | throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str())); | |
46 | printf("Opened LevelDB successfully\n"); | |
47 | } | |
48 | ||
49 | CLevelDB::~CLevelDB() { | |
50 | delete pdb; | |
51 | pdb = NULL; | |
52 | delete options.filter_policy; | |
53 | options.filter_policy = NULL; | |
54 | delete options.block_cache; | |
55 | options.block_cache = NULL; | |
56 | delete penv; | |
57 | options.env = NULL; | |
58 | } | |
59 | ||
60 | bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) { | |
61 | leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch); | |
62 | if (!status.ok()) { | |
63 | printf("LevelDB write failure: %s\n", status.ToString().c_str()); | |
64 | return false; | |
65 | } | |
66 | return true; | |
67 | } | |
68 |