forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move CDBBase into seperated file for persistence
This file might be used for additional consolidation of database and persistence related blocks, as noted in the TODO comment block.
- Loading branch information
Showing
6 changed files
with
183 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "mastercore_persistence.h" | ||
|
||
#include "mastercore_log.h" | ||
|
||
#include "util.h" | ||
|
||
#include "leveldb/db.h" | ||
#include "leveldb/write_batch.h" | ||
|
||
#include <boost/filesystem/path.hpp> | ||
|
||
#include <stdint.h> | ||
|
||
/** | ||
* Opens or creates a LevelDB based database. | ||
*/ | ||
leveldb::Status CDBBase::Open(const boost::filesystem::path& path, bool fWipe) | ||
{ | ||
if (fWipe) { | ||
if (msc_debug_persistence) file_log("Wiping LevelDB in %s\n", path.string()); | ||
leveldb::DestroyDB(path.string(), options); | ||
} | ||
TryCreateDirectory(path); | ||
if (msc_debug_persistence) file_log("Opening LevelDB in %s\n", path.string()); | ||
|
||
return leveldb::DB::Open(options, path.string(), &pdb); | ||
} | ||
|
||
/** | ||
* Deletes all entries of the database, and resets the counters. | ||
*/ | ||
void CDBBase::Clear() | ||
{ | ||
int64_t nTimeStart = GetTimeMicros(); | ||
unsigned int n = 0; | ||
leveldb::WriteBatch batch; | ||
leveldb::Iterator* it = NewIterator(); | ||
|
||
for (it->SeekToFirst(); it->Valid(); it->Next()) { | ||
batch.Delete(it->key()); | ||
++n; | ||
} | ||
|
||
delete it; | ||
|
||
leveldb::Status status = pdb->Write(writeoptions, &batch); | ||
nRead = 0; | ||
nWritten = 0; | ||
|
||
int64_t nTime = GetTimeMicros() - nTimeStart; | ||
if (msc_debug_persistence) | ||
file_log("Removed %d entries: %s [%.3f ms/entry, %.3f ms total]\n", | ||
n, status.ToString(), (n > 0 ? (0.001 * nTime / n) : 0), 0.001 * nTime); | ||
} | ||
|
||
/** | ||
* Deinitializes and closes the database. | ||
*/ | ||
void CDBBase::Close() | ||
{ | ||
if (pdb) { | ||
delete pdb; | ||
pdb = NULL; | ||
} | ||
} | ||
|
||
|
||
/** | ||
@todo Move initialization and deinitialization of databases into this file (?) | ||
@todo Move file based storage into this file | ||
@todo Replace file based storage with LevelDB based storage | ||
@todo Wrap global state objects and prevent direct access: | ||
static CMPTradeList* ptradedb = new CMPTradeList(); | ||
CMPTradeList& TradeDB() { | ||
assert(ptradedb); | ||
return *ptradedb; | ||
} | ||
// before: | ||
t_tradelistdb->recordTrade(); | ||
// after: | ||
TradeDB().recordTrade(); | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#ifndef MASTERCORE_PERSISTENCE_H | ||
#define MASTERCORE_PERSISTENCE_H | ||
|
||
#include "leveldb/db.h" | ||
|
||
#include <boost/filesystem/path.hpp> | ||
|
||
/** Base class for LevelDB based storage. | ||
*/ | ||
class CDBBase | ||
{ | ||
private: | ||
//! Options used when iterating over values of the database | ||
leveldb::ReadOptions iteroptions; | ||
|
||
protected: | ||
//! Database options used | ||
leveldb::Options options; | ||
|
||
//! Options used when reading from the database | ||
leveldb::ReadOptions readoptions; | ||
|
||
//! Options used when writing to the database | ||
leveldb::WriteOptions writeoptions; | ||
|
||
//! Options used when sync writing to the database | ||
leveldb::WriteOptions syncoptions; | ||
|
||
//! The database itself | ||
leveldb::DB* pdb; | ||
|
||
//! Number of entries read | ||
unsigned int nRead; | ||
|
||
//! Number of entries written | ||
unsigned int nWritten; | ||
|
||
CDBBase() : nRead(0), nWritten(0) | ||
{ | ||
options.paranoid_checks = true; | ||
options.create_if_missing = true; | ||
options.compression = leveldb::kNoCompression; | ||
options.max_open_files = 64; | ||
readoptions.verify_checksums = true; | ||
iteroptions.verify_checksums = true; | ||
iteroptions.fill_cache = false; | ||
syncoptions.sync = true; | ||
} | ||
|
||
virtual ~CDBBase() | ||
{ | ||
Close(); | ||
} | ||
|
||
leveldb::Iterator* NewIterator() const | ||
{ | ||
return pdb->NewIterator(iteroptions); | ||
} | ||
|
||
/** | ||
* Opens or creates a LevelDB based database. | ||
* | ||
* If the database is wiped before opening, it's content is destroyed, including | ||
* all log files and meta data. | ||
* | ||
* @param path The path of the database to open | ||
* @param fWipe Whether to wipe the database before opening | ||
* @return A Status object, indicating success or failure | ||
*/ | ||
leveldb::Status Open(const boost::filesystem::path& path, bool fWipe = false); | ||
|
||
public: | ||
/** | ||
* Deletes all entries of the database, and resets the counters. | ||
*/ | ||
void Clear(); | ||
|
||
/** | ||
* Deinitializes and closes the database. | ||
*/ | ||
void Close(); | ||
}; | ||
|
||
|
||
#endif // MASTERCORE_PERSISTENCE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters