You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

leveldbwrapper.cpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2012-2013 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. #include "leveldbwrapper.h"
  5. #include "util.h"
  6. #include <boost/filesystem.hpp>
  7. #include <leveldb/cache.h>
  8. #include <leveldb/env.h>
  9. #include <leveldb/filter_policy.h>
  10. #include <memenv/memenv.h>
  11. void HandleError(const leveldb::Status &status) throw(leveldb_error) {
  12. if (status.ok())
  13. return;
  14. LogPrintf("%s\n", status.ToString().c_str());
  15. if (status.IsCorruption())
  16. throw leveldb_error("Database corrupted");
  17. if (status.IsIOError())
  18. throw leveldb_error("Database I/O error");
  19. if (status.IsNotFound())
  20. throw leveldb_error("Database entry missing");
  21. throw leveldb_error("Unknown database error");
  22. }
  23. static leveldb::Options GetOptions(size_t nCacheSize) {
  24. leveldb::Options options;
  25. options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
  26. options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
  27. options.filter_policy = leveldb::NewBloomFilterPolicy(10);
  28. options.compression = leveldb::kNoCompression;
  29. options.max_open_files = 64;
  30. return options;
  31. }
  32. CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory, bool fWipe) {
  33. penv = NULL;
  34. readoptions.verify_checksums = true;
  35. iteroptions.verify_checksums = true;
  36. iteroptions.fill_cache = false;
  37. syncoptions.sync = true;
  38. options = GetOptions(nCacheSize);
  39. options.create_if_missing = true;
  40. if (fMemory) {
  41. penv = leveldb::NewMemEnv(leveldb::Env::Default());
  42. options.env = penv;
  43. } else {
  44. if (fWipe) {
  45. LogPrintf("Wiping LevelDB in %s\n", path.string().c_str());
  46. leveldb::DestroyDB(path.string(), options);
  47. }
  48. boost::filesystem::create_directory(path);
  49. LogPrintf("Opening LevelDB in %s\n", path.string().c_str());
  50. }
  51. leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
  52. HandleError(status);
  53. LogPrintf("Opened LevelDB successfully\n");
  54. }
  55. CLevelDBWrapper::~CLevelDBWrapper() {
  56. delete pdb;
  57. pdb = NULL;
  58. delete options.filter_policy;
  59. options.filter_policy = NULL;
  60. delete options.block_cache;
  61. options.block_cache = NULL;
  62. delete penv;
  63. options.env = NULL;
  64. }
  65. bool CLevelDBWrapper::WriteBatch(CLevelDBBatch &batch, bool fSync) throw(leveldb_error) {
  66. leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
  67. HandleError(status);
  68. return true;
  69. }