Files
thehub/hub/server/dbwrapper.cpp
T

116 lines
3.8 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (c) 2012-2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2012-09-03 19:05:30 +02:00
2015-10-22 21:33:06 -04:00
#include "dbwrapper.h"
2013-04-13 00:13:08 -05:00
#include <boost/filesystem.hpp>
2012-09-03 19:05:30 +02:00
#include <leveldb/cache.h>
2013-04-13 00:13:08 -05:00
#include <leveldb/env.h>
2012-09-03 19:05:30 +02:00
#include <leveldb/filter_policy.h>
#include <memenv.h>
2026-05-14 13:13:40 +02:00
#include <boost/scoped_ptr.hpp>
2012-09-03 19:05:30 +02:00
2022-09-07 11:19:11 +02:00
void HandleError(const leveldb::Status& status)
{
2013-01-29 01:44:19 +01:00
if (status.ok())
return;
2017-08-29 11:42:11 +02:00
logCritical(Log::DB) << status.ToString();
2013-01-29 01:44:19 +01:00
if (status.IsCorruption())
throw dbwrapper_error("Database corrupted");
2013-01-29 01:44:19 +01:00
if (status.IsIOError())
throw dbwrapper_error("Database I/O error");
2013-01-29 01:44:19 +01:00
if (status.IsNotFound())
throw dbwrapper_error("Database entry missing");
throw dbwrapper_error("Unknown database error");
2013-01-29 01:44:19 +01:00
}
static leveldb::Options GetOptions(size_t nCacheSize)
{
2012-09-03 19:05:30 +02:00
leveldb::Options options;
2012-11-04 17:11:48 +01:00
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
2012-09-03 19:05:30 +02:00
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
options.compression = leveldb::kNoCompression;
2013-04-24 00:10:23 +02:00
options.max_open_files = 64;
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
// LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
// on corruption in later versions.
options.paranoid_checks = true;
}
2012-09-03 19:05:30 +02:00
return options;
}
2018-11-02 20:42:45 +01:00
CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe)
{
2012-09-03 19:05:30 +02:00
penv = NULL;
readoptions.verify_checksums = true;
iteroptions.verify_checksums = true;
iteroptions.fill_cache = false;
syncoptions.sync = true;
2012-11-04 17:11:48 +01:00
options = GetOptions(nCacheSize);
2012-09-03 19:05:30 +02:00
options.create_if_missing = true;
2012-09-04 18:12:00 +02:00
if (fMemory) {
penv = leveldb::NewMemEnv(leveldb::Env::Default());
options.env = penv;
} else {
if (fWipe) {
2017-08-29 11:42:11 +02:00
logCritical(Log::DB) << "Wiping LevelDB in" << path.string();
leveldb::Status result = leveldb::DestroyDB(path.string(), options);
HandleError(result);
}
boost::system::error_code error;
boost::filesystem::create_directories(path, error);
2017-08-29 11:42:11 +02:00
logCritical(Log::DB) << "Opening LevelDB in" << path.string();
2012-09-04 18:12:00 +02:00
}
2012-09-03 19:05:30 +02:00
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
HandleError(status);
2017-08-29 11:42:11 +02:00
logInfo(Log::DB) << "Opened LevelDB successfully";
2012-09-03 19:05:30 +02:00
}
CDBWrapper::~CDBWrapper()
{
2012-09-03 19:05:30 +02:00
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;
}
2022-09-07 11:19:11 +02:00
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
{
2012-09-03 19:05:30 +02:00
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
HandleError(status);
2012-09-03 19:05:30 +02:00
return true;
}
bool CDBWrapper::IsEmpty()
{
boost::scoped_ptr<CDBIterator> it(NewIterator());
it->SeekToFirst();
return !(it->Valid());
}
CDBIterator::~CDBIterator() { delete piter; }
bool CDBIterator::Valid() { return piter->Valid(); }
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
void CDBIterator::Next() { piter->Next(); }