Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
leveldb.cpp
Go to the documentation of this file.
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 #include <memenv/memenv.h>
12 
13 #include <boost/filesystem.hpp>
14 
15 void HandleError(const leveldb::Status &status) throw(leveldb_error) {
16  if (status.ok())
17  return;
18  if (status.IsCorruption())
19  throw leveldb_error("Database corrupted");
20  if (status.IsIOError())
21  throw leveldb_error("Database I/O error");
22  if (status.IsNotFound())
23  throw leveldb_error("Database entry missing");
24  throw leveldb_error("Unknown database error");
25 }
26 
27 static leveldb::Options GetOptions(size_t nCacheSize) {
28  leveldb::Options options;
29  options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
30  options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
33  options.max_open_files = 64;
34  return options;
35 }
36 
37 CLevelDB::CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory, bool fWipe) {
38  penv = NULL;
41  iteroptions.fill_cache = false;
42  syncoptions.sync = true;
43  options = GetOptions(nCacheSize);
45  if (fMemory) {
47  options.env = penv;
48  } else {
49  if (fWipe) {
50  printf("Wiping LevelDB in %s\n", path.string().c_str());
51  leveldb::DestroyDB(path.string(), options);
52  }
53  boost::filesystem::create_directory(path);
54  printf("Opening LevelDB in %s\n", path.string().c_str());
55  }
56  leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
57  if (!status.ok())
58  throw std::runtime_error(strprintf("CLevelDB(): error opening database environment %s", status.ToString().c_str()));
59  printf("Opened LevelDB successfully\n");
60 }
61 
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 
73 bool CLevelDB::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error) {
74  leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
75  if (!status.ok()) {
76  printf("LevelDB write failure: %s\n", status.ToString().c_str());
77  HandleError(status);
78  return false;
79  }
80  return true;
81 }
CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: leveldb.cpp:37
#define strprintf(format,...)
Definition: util.h:169
Cache * block_cache
Definition: options.h:98
bool create_if_missing
Definition: options.h:45
leveldb::Env * penv
Definition: leveldb.h:59
bool WriteBatch(CLevelDBBatch &batch, bool fSync=false)
Definition: leveldb.cpp:73
Status DestroyDB(const std::string &dbname, const Options &options)
Definition: db_impl.cc:1467
leveldb::Options options
Definition: leveldb.h:62
#define printf
Definition: rpcdump.cpp:12
static Status Open(const Options &options, const std::string &name, DB **dbptr)
Definition: db_impl.cc:1430
const FilterPolicy * NewBloomFilterPolicy(int bits_per_key)
Definition: bloom.cc:91
leveldb::ReadOptions iteroptions
Definition: leveldb.h:68
int max_open_files
Definition: options.h:90
~CLevelDB()
Definition: leveldb.cpp:62
const FilterPolicy * filter_policy
Definition: options.h:136
Cache * NewLRUCache(size_t capacity)
Definition: cache.cc:321
CompressionType compression
Definition: options.h:129
leveldb::ReadOptions readoptions
Definition: leveldb.h:65
bool ok() const
Definition: status.h:52
size_t write_buffer_size
Definition: options.h:83
leveldb::DB * pdb
Definition: leveldb.h:77
void HandleError(const leveldb::Status &status)
Definition: leveldb.cpp:15
leveldb::WriteOptions syncoptions
Definition: leveldb.h:74
Env * NewMemEnv(Env *base_env)
Definition: memenv.cc:380
static Env * Default()
Definition: env_posix.cc:800
std::string ToString() const
Definition: status.cc:36