aboutsummaryrefslogtreecommitdiff
path: root/src/leveldb.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2012-09-03 19:05:30 +0200
committerPieter Wuille <pieter.wuille@gmail.com>2012-10-20 23:08:56 +0200
commit43b7905e98d827df45e970dbaf5f08561ecc6cda (patch)
treea2f90a07dfd16e3a3ec1ae418e3f65a2f2177579 /src/leveldb.cpp
parent3ff3a2bd60a905d830de157390facde14aa0ffe3 (diff)
LevelDB glue
Database-independent glue for supporting LevelDB databases. Based on code from earlier commits by Mike Hearn in his leveldb branch.
Diffstat (limited to 'src/leveldb.cpp')
-rw-r--r--src/leveldb.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/leveldb.cpp b/src/leveldb.cpp
new file mode 100644
index 0000000000..29e5e6a7fd
--- /dev/null
+++ b/src/leveldb.cpp
@@ -0,0 +1,58 @@
+// Copyright (c) 2012 The Bitcoin developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include "leveldb.h"
+#include "util.h"
+
+#include <leveldb/env.h>
+#include <leveldb/cache.h>
+#include <leveldb/filter_policy.h>
+
+#include <boost/filesystem.hpp>
+
+static leveldb::Options GetOptions() {
+ leveldb::Options options;
+ int nCacheSizeMB = GetArg("-dbcache", 25);
+ options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
+ options.filter_policy = leveldb::NewBloomFilterPolicy(10);
+ options.compression = leveldb::kNoCompression;
+ return options;
+}
+
+CLevelDB::CLevelDB(const boost::filesystem::path &path) {
+ penv = NULL;
+ readoptions.verify_checksums = true;
+ iteroptions.verify_checksums = true;
+ iteroptions.fill_cache = false;
+ syncoptions.sync = true;
+ options = GetOptions();
+ options.create_if_missing = true;
+ boost::filesystem::create_directory(path);
+ printf("Opening LevelDB in %s\n", path.string().c_str());
+ leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
+ if (!status.ok())
+ throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str()));
+ printf("Opened LevelDB successfully\n");
+}
+
+CLevelDB::~CLevelDB() {
+ delete pdb;
+ pdb = NULL;
+ delete options.filter_policy;
+ options.filter_policy = NULL;
+ delete options.block_cache;
+ options.block_cache = NULL;
+ delete penv;
+ options.env = NULL;
+}
+
+bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) {
+ leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
+ if (!status.ok()) {
+ printf("LevelDB write failure: %s\n", status.ToString().c_str());
+ return false;
+ }
+ return true;
+}
+