Skip to content

Commit

Permalink
Move CDBBase into seperated file for persistence
Browse files Browse the repository at this point in the history
This file might be used for additional consolidation of database and persistence related blocks, as noted in the TODO comment block.
  • Loading branch information
dexX7 committed Apr 28, 2015
1 parent 6f030ac commit af1d7b4
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 105 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ OMNICORE_H = \
mastercore_log.h \
mastercore_mdex.h \
mastercore_parse_string.h \
mastercore_persistence.h \
mastercore_rpc.h \
mastercore_script.h \
mastercore_sp.h \
Expand All @@ -174,6 +175,7 @@ OMNICORE_CPP = \
mastercore_log.cpp \
mastercore_mdex.cpp \
mastercore_parse_string.cpp \
mastercore_persistence.cpp \
mastercore_rpc.cpp \
mastercore_script.cpp \
mastercore_sp.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/mastercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "mastercore_errors.h"
#include "mastercore_log.h"
#include "mastercore_mdex.h"
#include "mastercore_persistence.h"
#include "mastercore_script.h"
#include "mastercore_sp.h"
#include "mastercore_tx.h"
Expand Down
109 changes: 5 additions & 104 deletions src/mastercore.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class CBlockIndex;
class CTransaction;

#include "mastercore_log.h"
#include "mastercore_persistence.h"

#include "sync.h"
#include "uint256.h"
#include "util.h"

#include <boost/filesystem/path.hpp>

#include "leveldb/db.h"
#include "leveldb/write_batch.h"
#include "leveldb/status.h"

#include "json/json_spirit_value.h"

Expand Down Expand Up @@ -276,105 +276,6 @@ typedef struct
}
};

/** 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;
PrintToConsole("CDBBase created\n");
}

virtual ~CDBBase()
{
Close();
PrintToConsole("CDBBase destroyed\n");
}

leveldb::Iterator* NewIterator()
{
return pdb->NewIterator(iteroptions);
}

leveldb::Status Open(const boost::filesystem::path& path, bool fWipe = false)
{
if (fWipe) {
PrintToConsole("Wiping LevelDB in %s\n", path.string());
leveldb::DestroyDB(path.string(), options);
}
TryCreateDirectory(path);
PrintToConsole("Opening LevelDB in %s\n", path.string());
return leveldb::DB::Open(options, path.string(), &pdb);
}

public:
void 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;
PrintToConsole("Removed %d entries: %s [%.3f ms/entry, %.3f ms total]\n",
n, status.ToString(), (n > 0 ? (0.001 * nTime / n) : 0), 0.001 * nTime);
}

void Close()
{
if (pdb) {
delete pdb;
pdb = NULL;
}
PrintToConsole("CDBBase closed\n");
}
};

/** LevelDB based storage for STO recipients.
*/
class CMPSTOList : public CDBBase
Expand All @@ -388,7 +289,7 @@ class CMPSTOList : public CDBBase

virtual ~CMPSTOList()
{
PrintToConsole("CMPSTOList destroyed\n");
if (msc_debug_persistence) file_log("CMPSTOList closed\n");
}

void getRecipients(const uint256 txid, string filterAddress, Array *recipientArray, uint64_t *total, uint64_t *stoFee);
Expand All @@ -413,7 +314,7 @@ class CMPTradeList : public CDBBase

virtual ~CMPTradeList()
{
PrintToConsole("CMPTradeList destroyed\n");
if (msc_debug_persistence) file_log("CMPTradeList closed\n");
}

void recordTrade(const uint256 txid1, const uint256 txid2, string address1, string address2, unsigned int prop1, unsigned int prop2, uint64_t amount1, uint64_t amount2, int blockNum);
Expand All @@ -438,7 +339,7 @@ class CMPTxList : public CDBBase

virtual ~CMPTxList()
{
PrintToConsole("CMPTxList destroyed\n");
if (msc_debug_persistence) file_log("CMPTxList closed\n");
}

void recordTX(const uint256 &txid, bool fValid, int nBlock, unsigned int type, uint64_t nValue);
Expand Down
88 changes: 88 additions & 0 deletions src/mastercore_persistence.cpp
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();
*/

85 changes: 85 additions & 0 deletions src/mastercore_persistence.h
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
3 changes: 2 additions & 1 deletion src/mastercore_sp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "mastercore.h"
#include "mastercore_log.h"
#include "mastercore_persistence.h"

class CBlockIndex;

Expand Down Expand Up @@ -296,7 +297,7 @@ class CMPSPInfo : public CDBBase

virtual ~CMPSPInfo()
{
PrintToConsole("CMPSPInfo destroyed\n");
if (msc_debug_persistence) file_log("CMPSPInfo closed\n");
}

void init(unsigned int nextSPID = 0x3UL, unsigned int nextTestSPID = TEST_ECO_PROPERTY_1)
Expand Down

0 comments on commit af1d7b4

Please sign in to comment.