Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ab4f9fb80 |
No files matched your search
+38
-70
@@ -127,45 +127,22 @@ void CClaimTrieData::reorderClaims(const supportEntryType& supports)
|
||||
CClaimTrie::CClaimTrie(bool fMemory, bool fWipe, int proportionalDelayFactor)
|
||||
{
|
||||
nProportionalDelayFactor = proportionalDelayFactor;
|
||||
if (fWipe) {
|
||||
fs::remove_all(GetDataDir() / "claimtrie"); // old folder
|
||||
}
|
||||
db_TRIE_NODE_BY_HASH.reset(new CDBWrapper(GetDataDir() / "claimtrie_TRIE_NODE_BY_HASH", 240 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_TRIE_NODE_BY_NAME.reset(new CDBWrapper(GetDataDir() / "claimtrie_TRIE_NODE_BY_NAME", 120 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_CLAIM_BY_ID.reset(new CDBWrapper(GetDataDir() / "claimtrie_CLAIM_BY_ID", 20 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_CLAIM_EXP_QUEUE_ROW.reset(new CDBWrapper(GetDataDir() / "claimtrie_CLAIM_EXP_QUEUE_ROW", 16 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_CLAIM_QUEUE_ROW.reset(new CDBWrapper(GetDataDir() / "claimtrie_CLAIM_QUEUE_ROW", 16 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_CLAIM_QUEUE_NAME_ROW.reset(new CDBWrapper(GetDataDir() / "claimtrie_CLAIM_QUEUE_NAME_ROW", 32 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_SUPPORT_EXP_QUEUE_ROW.reset(new CDBWrapper(GetDataDir() / "claimtrie_SUPPORT_EXP_QUEUE_ROW", 12 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_SUPPORT_QUEUE_ROW.reset(new CDBWrapper(GetDataDir() / "claimtrie_SUPPORT_QUEUE_ROW", 12 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_SUPPORT_QUEUE_NAME_ROW.reset(new CDBWrapper(GetDataDir() / "claimtrie_SUPPORT_QUEUE_NAME_ROW", 24 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db_SUPPORT.reset(new CDBWrapper(GetDataDir() / "claimtrie_SUPPORT", 24 * 1024 * 1024, fMemory, fWipe, false));
|
||||
db.reset(new CDBWrapper(GetDataDir() / "claimtrie", 200 * 1024 * 1024, fMemory, fWipe, false));
|
||||
}
|
||||
|
||||
bool CClaimTrie::SyncToDisk()
|
||||
{
|
||||
auto success = true;
|
||||
success &= db_TRIE_NODE_BY_HASH->Sync();
|
||||
success &= db_TRIE_NODE_BY_NAME->Sync();
|
||||
success &= db_CLAIM_BY_ID->Sync();
|
||||
success &= db_CLAIM_EXP_QUEUE_ROW->Sync();
|
||||
success &= db_CLAIM_QUEUE_ROW->Sync();
|
||||
success &= db_CLAIM_QUEUE_NAME_ROW->Sync();
|
||||
success &= db_SUPPORT_EXP_QUEUE_ROW->Sync();
|
||||
success &= db_SUPPORT_QUEUE_ROW->Sync();
|
||||
success &= db_SUPPORT_QUEUE_NAME_ROW->Sync();
|
||||
success &= db_SUPPORT->Sync();
|
||||
return success;
|
||||
return db && db->Sync();
|
||||
}
|
||||
|
||||
template <typename Key, typename Container>
|
||||
typename Container::value_type* getQueue(CDBWrapper& db, const Key& key, Container& queue, bool create)
|
||||
typename Container::value_type* getQueue(CDBWrapper& db, uint8_t dbkey, const Key& key, Container& queue, bool create)
|
||||
{
|
||||
auto itQueue = queue.find(key);
|
||||
if (itQueue != queue.end())
|
||||
return &(*itQueue);
|
||||
typename Container::mapped_type row;
|
||||
if (db.Read(key, row) || create) {
|
||||
if (db.Read(std::make_pair(dbkey, key), row) || create) {
|
||||
auto ret = queue.insert(std::make_pair(key, row));
|
||||
assert(ret.second);
|
||||
return &(*ret.first);
|
||||
@@ -183,13 +160,13 @@ inline constexpr bool supportedType()
|
||||
template <>
|
||||
std::pair<const int, std::vector<queueEntryType<CClaimValue>>>* CClaimTrieCacheBase::getQueueCacheRow(int nHeight, bool createIfNotExists)
|
||||
{
|
||||
return getQueue(*(base->db_CLAIM_QUEUE_ROW), nHeight, claimQueueCache, createIfNotExists);
|
||||
return getQueue(*(base->db), CLAIM_QUEUE_ROW, nHeight, claimQueueCache, createIfNotExists);
|
||||
}
|
||||
|
||||
template <>
|
||||
std::pair<const int, std::vector<queueEntryType<CSupportValue>>>* CClaimTrieCacheBase::getQueueCacheRow(int nHeight, bool createIfNotExists)
|
||||
{
|
||||
return getQueue(*(base->db_SUPPORT_QUEUE_ROW), nHeight, supportQueueCache, createIfNotExists);
|
||||
return getQueue(*(base->db), SUPPORT_QUEUE_ROW, nHeight, supportQueueCache, createIfNotExists);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -202,13 +179,13 @@ std::pair<const int, std::vector<queueEntryType<T>>>* CClaimTrieCacheBase::getQu
|
||||
template <>
|
||||
typename queueNameType::value_type* CClaimTrieCacheBase::getQueueCacheNameRow<CClaimValue>(const std::string& name, bool createIfNotExists)
|
||||
{
|
||||
return getQueue(*(base->db_CLAIM_QUEUE_NAME_ROW), name, claimQueueNameCache, createIfNotExists);
|
||||
return getQueue(*(base->db), CLAIM_QUEUE_NAME_ROW, name, claimQueueNameCache, createIfNotExists);
|
||||
}
|
||||
|
||||
template <>
|
||||
typename queueNameType::value_type* CClaimTrieCacheBase::getQueueCacheNameRow<CSupportValue>(const std::string& name, bool createIfNotExists)
|
||||
{
|
||||
return getQueue(*(base->db_SUPPORT_QUEUE_NAME_ROW), name, supportQueueNameCache, createIfNotExists);
|
||||
return getQueue(*(base->db), SUPPORT_QUEUE_NAME_ROW, name, supportQueueNameCache, createIfNotExists);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -221,13 +198,13 @@ typename queueNameType::value_type* CClaimTrieCacheBase::getQueueCacheNameRow(co
|
||||
template <>
|
||||
typename expirationQueueType::value_type* CClaimTrieCacheBase::getExpirationQueueCacheRow<CClaimValue>(int nHeight, bool createIfNotExists)
|
||||
{
|
||||
return getQueue(*(base->db_CLAIM_EXP_QUEUE_ROW), nHeight, expirationQueueCache, createIfNotExists);
|
||||
return getQueue(*(base->db), CLAIM_EXP_QUEUE_ROW, nHeight, expirationQueueCache, createIfNotExists);
|
||||
}
|
||||
|
||||
template <>
|
||||
typename expirationQueueType::value_type* CClaimTrieCacheBase::getExpirationQueueCacheRow<CSupportValue>(int nHeight, bool createIfNotExists)
|
||||
{
|
||||
return getQueue(*(base->db_SUPPORT_EXP_QUEUE_ROW), nHeight, supportExpirationQueueCache, createIfNotExists);
|
||||
return getQueue(*(base->db), SUPPORT_EXP_QUEUE_ROW, nHeight, supportExpirationQueueCache, createIfNotExists);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -262,7 +239,7 @@ supportEntryType CClaimTrieCacheBase::getSupportsForName(const std::string& name
|
||||
return sit->second;
|
||||
|
||||
supportEntryType supports;
|
||||
if (base->db_SUPPORT->Read(name, supports)) // don't trust the try/catch in here
|
||||
if (base->db->Read(std::make_pair(SUPPORT, name), supports)) // don't trust the try/catch in here
|
||||
return supports;
|
||||
return {};
|
||||
}
|
||||
@@ -512,7 +489,7 @@ std::vector<std::pair<std::string, CClaimTrieDataNode>> CClaimTrie::nodes(const
|
||||
}
|
||||
|
||||
bool CClaimTrie::contains(const std::string &key) const {
|
||||
return db_TRIE_NODE_BY_NAME->Exists(key);
|
||||
return db->Exists(std::make_pair(TRIE_NODE_BY_NAME, key));
|
||||
}
|
||||
|
||||
bool CClaimTrie::empty() const {
|
||||
@@ -521,20 +498,20 @@ bool CClaimTrie::empty() const {
|
||||
|
||||
bool CClaimTrie::find(const std::string &key, CClaimTrieDataNode &node) const {
|
||||
uint256 hash;
|
||||
if (!db_TRIE_NODE_BY_NAME->Read(key, hash))
|
||||
if (!db->Read(std::make_pair(TRIE_NODE_BY_NAME, key), hash))
|
||||
return false;
|
||||
auto found = find(hash, node);
|
||||
return found;
|
||||
}
|
||||
|
||||
bool CClaimTrie::find(const uint256 &key, CClaimTrieDataNode &node) const {
|
||||
return db_TRIE_NODE_BY_HASH->Read(key, node);
|
||||
return db->Read(std::make_pair(TRIE_NODE_BY_HASH, key), node);
|
||||
}
|
||||
|
||||
bool CClaimTrieCacheBase::getClaimById(const uint160& claimId, std::string& name, CClaimValue& claim) const
|
||||
{
|
||||
CClaimIndexElement element;
|
||||
if (!base->db_CLAIM_BY_ID->Read(claimId, element))
|
||||
if (!base->db->Read(std::make_pair(CLAIM_BY_ID, claimId), element))
|
||||
return false;
|
||||
if (element.claim.claimId == claimId) {
|
||||
name = element.name;
|
||||
@@ -545,27 +522,25 @@ bool CClaimTrieCacheBase::getClaimById(const uint160& claimId, std::string& name
|
||||
}
|
||||
|
||||
template <typename K, typename T>
|
||||
void BatchWrite(CDBBatch& batch, const K& key, const std::vector<T>& value)
|
||||
void BatchWrite(CDBBatch& batch, uint8_t dbkey, const K& key, const std::vector<T>& value)
|
||||
{
|
||||
if (value.empty()) {
|
||||
batch.Erase(key);
|
||||
batch.Erase(std::make_pair(dbkey, key));
|
||||
} else {
|
||||
batch.Write(key, value);
|
||||
batch.Write(std::make_pair(dbkey, key), value);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
bool BatchWriteQueue(CDBWrapper& db, const Container& queue)
|
||||
void BatchWriteQueue(CDBBatch& batch, uint8_t dbkey, const Container& queue)
|
||||
{
|
||||
CDBBatch batch(db);
|
||||
for (auto& itQueue : queue)
|
||||
BatchWrite(batch, itQueue.first, itQueue.second);
|
||||
return db.WriteBatch(batch);
|
||||
BatchWrite(batch, dbkey, itQueue.first, itQueue.second);
|
||||
}
|
||||
|
||||
bool CClaimTrieCacheBase::flush()
|
||||
{
|
||||
CDBBatch batch_CLAIM_BY_ID(*(base->db_CLAIM_BY_ID));
|
||||
CDBBatch batch(*(base->db));
|
||||
|
||||
for (const auto& claim : claimsToDeleteFromByIdIndex) {
|
||||
auto it = std::find_if(claimsToAddToByIdIndex.begin(), claimsToAddToByIdIndex.end(),
|
||||
@@ -574,13 +549,11 @@ bool CClaimTrieCacheBase::flush()
|
||||
}
|
||||
);
|
||||
if (it == claimsToAddToByIdIndex.end())
|
||||
batch_CLAIM_BY_ID.Erase(claim.claimId);
|
||||
batch.Erase(std::make_pair(CLAIM_BY_ID, claim.claimId));
|
||||
}
|
||||
|
||||
for (const auto& e : claimsToAddToByIdIndex)
|
||||
batch_CLAIM_BY_ID.Write(e.claim.claimId, e);
|
||||
|
||||
auto success = base->db_CLAIM_BY_ID->WriteBatch(batch_CLAIM_BY_ID);
|
||||
batch.Write(std::make_pair(CLAIM_BY_ID, e.claim.claimId), e);
|
||||
|
||||
getMerkleHash();
|
||||
|
||||
@@ -593,8 +566,6 @@ bool CClaimTrieCacheBase::flush()
|
||||
forDeletion.insert(node.first);
|
||||
}
|
||||
|
||||
CDBBatch batch_TRIE_NODE_BY_HASH(*(base->db_TRIE_NODE_BY_HASH));
|
||||
CDBBatch batch_TRIE_NODE_BY_NAME(*(base->db_TRIE_NODE_BY_NAME));
|
||||
for (auto it = nodesToAddOrUpdate.begin(); it != nodesToAddOrUpdate.end(); ++it) {
|
||||
forDeletion.erase(it.key());
|
||||
if (!dirtyNodes.count(it.key()))
|
||||
@@ -605,35 +576,32 @@ bool CClaimTrieCacheBase::flush()
|
||||
for (auto &child: it.children()) // ordering here is important
|
||||
node.children.emplace_back(child.key().substr(it.key().size()), child->hash);
|
||||
|
||||
batch_TRIE_NODE_BY_HASH.Write(it->hash, node);
|
||||
batch_TRIE_NODE_BY_NAME.Write(it.key(), it->hash);
|
||||
batch.Write(std::make_pair(TRIE_NODE_BY_HASH, it->hash), node);
|
||||
batch.Write(std::make_pair(TRIE_NODE_BY_NAME, it.key()), it->hash);
|
||||
}
|
||||
|
||||
success &= base->db_TRIE_NODE_BY_HASH->WriteBatch(batch_TRIE_NODE_BY_HASH);
|
||||
|
||||
for (auto& name: forDeletion) {
|
||||
batch_TRIE_NODE_BY_NAME.Erase(name);
|
||||
batch.Erase(std::make_pair(TRIE_NODE_BY_NAME, name));
|
||||
}
|
||||
|
||||
success &= base->db_TRIE_NODE_BY_NAME->WriteBatch(batch_TRIE_NODE_BY_NAME);
|
||||
BatchWriteQueue(batch, SUPPORT, supportCache);
|
||||
|
||||
success &= BatchWriteQueue(*(base->db_SUPPORT), supportCache);
|
||||
BatchWriteQueue(batch, CLAIM_QUEUE_ROW, claimQueueCache);
|
||||
BatchWriteQueue(batch, CLAIM_QUEUE_NAME_ROW, claimQueueNameCache);
|
||||
BatchWriteQueue(batch, CLAIM_EXP_QUEUE_ROW, expirationQueueCache);
|
||||
|
||||
success &= BatchWriteQueue(*(base->db_CLAIM_QUEUE_ROW), claimQueueCache);
|
||||
success &= BatchWriteQueue(*(base->db_CLAIM_QUEUE_NAME_ROW), claimQueueNameCache);
|
||||
success &= BatchWriteQueue(*(base->db_CLAIM_EXP_QUEUE_ROW), expirationQueueCache);
|
||||
|
||||
success &= BatchWriteQueue(*(base->db_SUPPORT_QUEUE_ROW), supportQueueCache);
|
||||
success &= BatchWriteQueue(*(base->db_SUPPORT_QUEUE_NAME_ROW), supportQueueNameCache);
|
||||
success &= BatchWriteQueue(*(base->db_SUPPORT_EXP_QUEUE_ROW), supportExpirationQueueCache);
|
||||
BatchWriteQueue(batch, SUPPORT_QUEUE_ROW, supportQueueCache);
|
||||
BatchWriteQueue(batch, SUPPORT_QUEUE_NAME_ROW, supportQueueNameCache);
|
||||
BatchWriteQueue(batch, SUPPORT_EXP_QUEUE_ROW, supportExpirationQueueCache);
|
||||
|
||||
base->nNextHeight = nNextHeight;
|
||||
if (!nodesToAddOrUpdate.empty() && (LogAcceptCategory(BCLog::CLAIMS) || LogAcceptCategory(BCLog::BENCH))) {
|
||||
LogPrintf("TrieCache size: %zu nodes on block %d.\n",
|
||||
nodesToAddOrUpdate.height(), nNextHeight);
|
||||
LogPrintf("TrieCache size: %zu nodes on block %d, batch writes %zu bytes.\n",
|
||||
nodesToAddOrUpdate.height(), nNextHeight, batch.SizeEstimate());
|
||||
}
|
||||
auto ret = base->db->WriteBatch(batch);
|
||||
clear();
|
||||
return success;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool CClaimTrieCacheBase::validateTrieConsistency(const CBlockIndex* tip)
|
||||
@@ -655,7 +623,7 @@ bool CClaimTrieCacheBase::ReadFromDisk(const CBlockIndex* tip)
|
||||
base->nNextHeight = nNextHeight = tip ? tip->nHeight + 1 : 0;
|
||||
clear();
|
||||
|
||||
if (tip && fs::is_directory(GetDataDir() / "claimtrie")) {
|
||||
if (tip && (base->db->Exists(std::make_pair(TRIE_NODE, std::string())) || !base->db->Exists(std::make_pair(TRIE_NODE_BY_HASH, tip->hashClaimTrie)))) {
|
||||
LogPrintf("The claim trie database contains deprecated data and will need to be rebuilt");
|
||||
return false;
|
||||
}
|
||||
|
||||
+14
-10
@@ -17,6 +17,19 @@
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
// leveldb keys
|
||||
#define TRIE_NODE 'n' // deprecated
|
||||
#define TRIE_NODE_BY_HASH 'h'
|
||||
#define TRIE_NODE_BY_NAME 'g'
|
||||
#define CLAIM_BY_ID 'i'
|
||||
#define CLAIM_QUEUE_ROW 'r'
|
||||
#define CLAIM_QUEUE_NAME_ROW 'm'
|
||||
#define CLAIM_EXP_QUEUE_ROW 'e'
|
||||
#define SUPPORT 's'
|
||||
#define SUPPORT_QUEUE_ROW 'u'
|
||||
#define SUPPORT_QUEUE_NAME_ROW 'p'
|
||||
#define SUPPORT_EXP_QUEUE_ROW 'x'
|
||||
|
||||
uint256 getValueHash(const COutPoint& outPoint, int nHeightOfLastTakeover);
|
||||
|
||||
struct CClaimValue
|
||||
@@ -308,16 +321,7 @@ class CClaimTrie
|
||||
{
|
||||
int nNextHeight = 0;
|
||||
int nProportionalDelayFactor = 0;
|
||||
std::unique_ptr<CDBWrapper> db_TRIE_NODE_BY_HASH;
|
||||
std::unique_ptr<CDBWrapper> db_TRIE_NODE_BY_NAME;
|
||||
std::unique_ptr<CDBWrapper> db_CLAIM_BY_ID;
|
||||
std::unique_ptr<CDBWrapper> db_CLAIM_EXP_QUEUE_ROW;
|
||||
std::unique_ptr<CDBWrapper> db_CLAIM_QUEUE_ROW;
|
||||
std::unique_ptr<CDBWrapper> db_CLAIM_QUEUE_NAME_ROW;
|
||||
std::unique_ptr<CDBWrapper> db_SUPPORT_EXP_QUEUE_ROW;
|
||||
std::unique_ptr<CDBWrapper> db_SUPPORT_QUEUE_ROW;
|
||||
std::unique_ptr<CDBWrapper> db_SUPPORT_QUEUE_NAME_ROW;
|
||||
std::unique_ptr<CDBWrapper> db_SUPPORT;
|
||||
std::unique_ptr<CDBWrapper> db;
|
||||
|
||||
public:
|
||||
CClaimTrie() = default;
|
||||
|
||||
+24
-25
@@ -61,31 +61,29 @@ bool CClaimTrieCacheExpirationFork::forkForExpirationChange(bool increment)
|
||||
*/
|
||||
|
||||
//look through db for expiration queues, if we haven't already found it in dirty expiration queue
|
||||
boost::scoped_ptr<CDBIterator> pcursor_ceqr(base->db_CLAIM_EXP_QUEUE_ROW->NewIterator());
|
||||
for (pcursor_ceqr->SeekToFirst(); pcursor_ceqr->Valid(); pcursor_ceqr->Next()) {
|
||||
int height;
|
||||
if (!pcursor_ceqr->GetKey(height))
|
||||
boost::scoped_ptr<CDBIterator> pcursor(base->db->NewIterator());
|
||||
for (pcursor->SeekToFirst(); pcursor->Valid(); pcursor->Next()) {
|
||||
std::pair<uint8_t, int> key;
|
||||
if (!pcursor->GetKey(key))
|
||||
continue;
|
||||
expirationQueueRowType row;
|
||||
if (pcursor_ceqr->GetValue(row)) {
|
||||
reactivateClaim(row, height, increment);
|
||||
} else {
|
||||
return error("%s(): error reading expiration queue rows from disk", __func__);
|
||||
int height = key.second;
|
||||
if (key.first == CLAIM_EXP_QUEUE_ROW) {
|
||||
expirationQueueRowType row;
|
||||
if (pcursor->GetValue(row)) {
|
||||
reactivateClaim(row, height, increment);
|
||||
} else {
|
||||
return error("%s(): error reading expiration queue rows from disk", __func__);
|
||||
}
|
||||
} else if (key.first == SUPPORT_EXP_QUEUE_ROW) {
|
||||
expirationQueueRowType row;
|
||||
if (pcursor->GetValue(row)) {
|
||||
reactivateSupport(row, height, increment);
|
||||
} else {
|
||||
return error("%s(): error reading support expiration queue rows from disk", __func__);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boost::scoped_ptr<CDBIterator> pcursor_seqr(base->db_SUPPORT_EXP_QUEUE_ROW->NewIterator());
|
||||
for (pcursor_seqr->SeekToFirst(); pcursor_seqr->Valid(); pcursor_seqr->Next()) {
|
||||
int height;
|
||||
if (!pcursor_seqr->GetKey(height))
|
||||
continue;
|
||||
expirationQueueRowType row;
|
||||
if (pcursor_seqr->GetValue(row)) {
|
||||
reactivateSupport(row, height, increment);
|
||||
} else {
|
||||
return error("%s(): error reading support expiration queue rows from disk", __func__);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -163,14 +161,15 @@ bool CClaimTrieCacheNormalizationFork::normalizeAllNamesInTrieIfNecessary(insert
|
||||
// run the one-time upgrade of all names that need to change
|
||||
// it modifies the (cache) trie as it goes, so we need to grab everything to be modified first
|
||||
|
||||
boost::scoped_ptr<CDBIterator> pcursor(base->db_TRIE_NODE_BY_NAME->NewIterator());
|
||||
boost::scoped_ptr<CDBIterator> pcursor(base->db->NewIterator());
|
||||
for (pcursor->SeekToFirst(); pcursor->Valid(); pcursor->Next()) {
|
||||
std::string name;
|
||||
if (!pcursor->GetKey(name))
|
||||
std::pair<uint8_t, std::string> key;
|
||||
if (!pcursor->GetKey(key) || key.first != TRIE_NODE_BY_NAME)
|
||||
continue;
|
||||
|
||||
const auto& name = key.second;
|
||||
const std::string normalized = normalizeClaimName(name, true);
|
||||
if (normalized == name)
|
||||
if (normalized == key.second)
|
||||
continue;
|
||||
|
||||
auto supports = getSupportsForName(name);
|
||||
|
||||
+1
-6
@@ -116,7 +116,7 @@ static leveldb::Options GetOptions(size_t nCacheSize)
|
||||
}
|
||||
|
||||
CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
|
||||
: m_name(fs::basename(path)), ssKey(SER_DISK, CLIENT_VERSION), ssValue(SER_DISK, CLIENT_VERSION)
|
||||
: m_name(fs::basename(path))
|
||||
{
|
||||
penv = nullptr;
|
||||
readoptions.verify_checksums = true;
|
||||
@@ -239,11 +239,6 @@ bool CDBIterator::Valid() const { return piter->Valid(); }
|
||||
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
|
||||
void CDBIterator::Next() { piter->Next(); }
|
||||
|
||||
bool CDBWrapper::Sync() {
|
||||
CDBBatch batch(*this);
|
||||
return WriteBatch(batch, true);
|
||||
}
|
||||
|
||||
namespace dbwrapper_private {
|
||||
|
||||
void HandleError(const leveldb::Status& status)
|
||||
|
||||
+103
-94
@@ -16,6 +16,9 @@
|
||||
#include <leveldb/db.h>
|
||||
#include <leveldb/write_batch.h>
|
||||
|
||||
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
|
||||
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
|
||||
|
||||
class dbwrapper_error : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
@@ -40,6 +43,77 @@ const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w);
|
||||
|
||||
};
|
||||
|
||||
/** Batch of changes queued to be written to a CDBWrapper */
|
||||
class CDBBatch
|
||||
{
|
||||
friend class CDBWrapper;
|
||||
|
||||
private:
|
||||
const CDBWrapper &parent;
|
||||
leveldb::WriteBatch batch;
|
||||
|
||||
CDataStream ssKey;
|
||||
CDataStream ssValue;
|
||||
|
||||
size_t size_estimate;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @param[in] _parent CDBWrapper that this batch is to be submitted to
|
||||
*/
|
||||
explicit CDBBatch(const CDBWrapper &_parent) : parent(_parent), ssKey(SER_DISK, CLIENT_VERSION), ssValue(SER_DISK, CLIENT_VERSION), size_estimate(0) { };
|
||||
|
||||
void Clear()
|
||||
{
|
||||
batch.Clear();
|
||||
size_estimate = 0;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void Write(const K& key, const V& value)
|
||||
{
|
||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey << key;
|
||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
||||
|
||||
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
|
||||
ssValue << value;
|
||||
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
||||
leveldb::Slice slValue(ssValue.data(), ssValue.size());
|
||||
|
||||
batch.Put(slKey, slValue);
|
||||
// LevelDB serializes writes as:
|
||||
// - byte: header
|
||||
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
|
||||
// - byte[]: key
|
||||
// - varint: value length
|
||||
// - byte[]: value
|
||||
// The formula below assumes the key and value are both less than 16k.
|
||||
size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
|
||||
ssKey.clear();
|
||||
ssValue.clear();
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
void Erase(const K& key)
|
||||
{
|
||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey << key;
|
||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
||||
|
||||
batch.Delete(slKey);
|
||||
// LevelDB serializes erases as:
|
||||
// - byte: header
|
||||
// - varint: key length
|
||||
// - byte[]: key
|
||||
// The formula below assumes the key is less than 16kB.
|
||||
size_estimate += 2 + (slKey.size() > 127) + slKey.size();
|
||||
ssKey.clear();
|
||||
}
|
||||
|
||||
size_t SizeEstimate() const { return size_estimate; }
|
||||
};
|
||||
|
||||
class CDBIterator
|
||||
{
|
||||
private:
|
||||
@@ -61,10 +135,10 @@ public:
|
||||
void SeekToFirst();
|
||||
|
||||
template<typename K> void Seek(const K& key) {
|
||||
CDataStream ssKey1(SER_DISK, CLIENT_VERSION);
|
||||
// ssKey1.reserve(parent.ssKey.capacity());
|
||||
ssKey1 << key;
|
||||
leveldb::Slice slKey(ssKey1.data(), ssKey1.size());
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey << key;
|
||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
||||
piter->Seek(slKey);
|
||||
}
|
||||
|
||||
@@ -99,8 +173,6 @@ public:
|
||||
|
||||
};
|
||||
|
||||
class CDBBatch;
|
||||
|
||||
class CDBWrapper
|
||||
{
|
||||
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
|
||||
@@ -141,8 +213,6 @@ private:
|
||||
std::vector<unsigned char> CreateObfuscateKey() const;
|
||||
|
||||
public:
|
||||
mutable CDataStream ssKey, ssValue;
|
||||
|
||||
/**
|
||||
* @param[in] path Location in the filesystem where leveldb data will be stored.
|
||||
* @param[in] nCacheSize Configures various leveldb cache settings.
|
||||
@@ -160,12 +230,13 @@ public:
|
||||
template <typename K, typename V>
|
||||
bool Read(const K& key, V& value) const
|
||||
{
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey << key;
|
||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
||||
|
||||
std::string strValue;
|
||||
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
||||
ssKey.clear();
|
||||
if (!status.ok()) {
|
||||
if (status.IsNotFound())
|
||||
return false;
|
||||
@@ -183,17 +254,23 @@ public:
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
bool Write(const K& key, const V& value, bool fSync = false);
|
||||
bool Write(const K& key, const V& value, bool fSync = false)
|
||||
{
|
||||
CDBBatch batch(*this);
|
||||
batch.Write(key, value);
|
||||
return WriteBatch(batch, fSync);
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
bool Exists(const K& key) const
|
||||
{
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey << key;
|
||||
leveldb::Slice slKey(ssKey.data(), ssKey.size());
|
||||
|
||||
std::string strValue;
|
||||
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
|
||||
ssKey.clear();
|
||||
if (!status.ok()) {
|
||||
if (status.IsNotFound())
|
||||
return false;
|
||||
@@ -204,7 +281,12 @@ public:
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
bool Erase(const K& key, bool fSync = false);
|
||||
bool Erase(const K& key, bool fSync = false)
|
||||
{
|
||||
CDBBatch batch(*this);
|
||||
batch.Erase(key);
|
||||
return WriteBatch(batch, fSync);
|
||||
}
|
||||
|
||||
bool WriteBatch(CDBBatch& batch, bool fSync = false);
|
||||
|
||||
@@ -217,7 +299,11 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Sync();
|
||||
bool Sync()
|
||||
{
|
||||
CDBBatch batch(*this);
|
||||
return WriteBatch(batch, true);
|
||||
}
|
||||
|
||||
CDBIterator *NewIterator()
|
||||
{
|
||||
@@ -233,6 +319,8 @@ public:
|
||||
size_t EstimateSize(const K& key_begin, const K& key_end) const
|
||||
{
|
||||
CDataStream ssKey1(SER_DISK, CLIENT_VERSION), ssKey2(SER_DISK, CLIENT_VERSION);
|
||||
ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey1 << key_begin;
|
||||
ssKey2 << key_end;
|
||||
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
|
||||
@@ -250,8 +338,8 @@ public:
|
||||
void CompactRange(const K& key_begin, const K& key_end) const
|
||||
{
|
||||
CDataStream ssKey1(SER_DISK, CLIENT_VERSION), ssKey2(SER_DISK, CLIENT_VERSION);
|
||||
ssKey1.reserve(ssKey.capacity());
|
||||
ssKey2.reserve(ssKey.capacity());
|
||||
ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||
ssKey1 << key_begin;
|
||||
ssKey2 << key_end;
|
||||
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
|
||||
@@ -261,83 +349,4 @@ public:
|
||||
|
||||
};
|
||||
|
||||
/** Batch of changes queued to be written to a CDBWrapper */
|
||||
class CDBBatch
|
||||
{
|
||||
friend class CDBWrapper;
|
||||
const CDBWrapper &parent;
|
||||
leveldb::WriteBatch batch;
|
||||
|
||||
size_t size_estimate;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @param[in] _parent CDBWrapper that this batch is to be submitted to
|
||||
*/
|
||||
explicit CDBBatch(const CDBWrapper &_parent) : parent(_parent), size_estimate(0) { };
|
||||
|
||||
void Clear()
|
||||
{
|
||||
batch.Clear();
|
||||
size_estimate = 0;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void Write(const K& key, const V& value)
|
||||
{
|
||||
assert(parent.ssKey.empty());
|
||||
parent.ssKey << key;
|
||||
leveldb::Slice slKey(parent.ssKey.data(), parent.ssKey.size());
|
||||
|
||||
assert(parent.ssValue.empty());
|
||||
parent.ssValue << value;
|
||||
parent.ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
||||
leveldb::Slice slValue(parent.ssValue.data(), parent.ssValue.size());
|
||||
|
||||
batch.Put(slKey, slValue);
|
||||
// LevelDB serializes writes as:
|
||||
// - byte: header
|
||||
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
|
||||
// - byte[]: key
|
||||
// - varint: value length
|
||||
// - byte[]: value
|
||||
// The formula below assumes the key and value are both less than 16k.
|
||||
size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size();
|
||||
parent.ssKey.clear();
|
||||
parent.ssValue.clear();
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
void Erase(const K& key)
|
||||
{
|
||||
parent.ssKey << key;
|
||||
leveldb::Slice slKey(parent.ssKey.data(), parent.ssKey.size());
|
||||
|
||||
batch.Delete(slKey);
|
||||
// LevelDB serializes erases as:
|
||||
// - byte: header
|
||||
// - varint: key length
|
||||
// - byte[]: key
|
||||
// The formula below assumes the key is less than 16kB.
|
||||
size_estimate += 2 + (slKey.size() > 127) + slKey.size();
|
||||
parent.ssKey.clear();
|
||||
}
|
||||
|
||||
size_t SizeEstimate() const { return size_estimate; }
|
||||
};
|
||||
|
||||
template<typename K>
|
||||
bool CDBWrapper::Erase(const K &key, bool fSync) {
|
||||
CDBBatch batch(*this);
|
||||
batch.Erase(key);
|
||||
return WriteBatch(batch, fSync);
|
||||
}
|
||||
|
||||
template<typename K, typename V>
|
||||
bool CDBWrapper::Write(const K &key, const V &value, bool fSync) {
|
||||
CDBBatch batch(*this);
|
||||
batch.Write(key, value);
|
||||
return WriteBatch(batch, fSync);
|
||||
}
|
||||
|
||||
#endif // BITCOIN_DBWRAPPER_H
|
||||
+11
-11
@@ -222,8 +222,8 @@ std::shared_ptr<typename CPrefixTrie<TKey, TData>::Node>& CPrefixTrie<TKey, TDat
|
||||
}
|
||||
if (count == 0) {
|
||||
++size;
|
||||
it = children.emplace(key, std::make_shared<Node>()).first;
|
||||
it->second->data = std::make_shared<TData>();
|
||||
it = children.emplace(key, std::allocate_shared<Node>(nodePool)).first;
|
||||
it->second->data = std::allocate_shared<TData>(dataPool);
|
||||
return it->second;
|
||||
}
|
||||
if (count < it->first.size()) {
|
||||
@@ -232,9 +232,9 @@ std::shared_ptr<typename CPrefixTrie<TKey, TData>::Node>& CPrefixTrie<TKey, TDat
|
||||
auto nodes = std::move(it->second);
|
||||
children.erase(it);
|
||||
++size;
|
||||
it = children.emplace(prefix, std::make_shared<Node>()).first;
|
||||
it = children.emplace(prefix, std::allocate_shared<Node>(nodePool)).first;
|
||||
it->second->children.emplace(postfix, std::move(nodes));
|
||||
it->second->data = std::make_shared<TData>();
|
||||
it->second->data = std::allocate_shared<TData>(dataPool);
|
||||
if (key.size() == count)
|
||||
return it->second;
|
||||
}
|
||||
@@ -253,7 +253,7 @@ void CPrefixTrie<TKey, TData>::erase(const TKey& key, std::shared_ptr<Node>& nod
|
||||
if (!find(key, node, cb))
|
||||
return;
|
||||
|
||||
nodes.back().second->data = std::make_shared<TData>();
|
||||
nodes.back().second->data = std::allocate_shared<TData>(dataPool);
|
||||
for (; nodes.size() > 1; nodes.pop_back()) {
|
||||
// if we have only one child and no data ourselves, bring them up to our level
|
||||
auto& cNode = nodes.back().second;
|
||||
@@ -284,9 +284,9 @@ void CPrefixTrie<TKey, TData>::erase(const TKey& key, std::shared_ptr<Node>& nod
|
||||
}
|
||||
|
||||
template <typename TKey, typename TData>
|
||||
CPrefixTrie<TKey, TData>::CPrefixTrie() : size(0), root(std::make_shared<Node>())
|
||||
CPrefixTrie<TKey, TData>::CPrefixTrie() : size(0), root(std::allocate_shared<Node>(nodePool))
|
||||
{
|
||||
root->data = std::make_shared<TData>();
|
||||
root->data = std::allocate_shared<TData>(dataPool);
|
||||
}
|
||||
|
||||
template <typename TKey, typename TData>
|
||||
@@ -294,7 +294,7 @@ template <typename TDataUni>
|
||||
typename CPrefixTrie<TKey, TData>::iterator CPrefixTrie<TKey, TData>::insert(const TKey& key, TDataUni&& data)
|
||||
{
|
||||
auto& node = key.empty() ? root : insert(key, root);
|
||||
node->data = std::make_shared<TData>(std::forward<TDataUni>(data));
|
||||
node->data = std::allocate_shared<TData>(dataPool, std::forward<TDataUni>(data));
|
||||
return key.empty() ? begin() : iterator{key, node};
|
||||
}
|
||||
|
||||
@@ -320,7 +320,7 @@ typename CPrefixTrie<TKey, TData>::iterator CPrefixTrie<TKey, TData>::insert(CPr
|
||||
auto& node = insert(key, shared);
|
||||
copy = iterator{name, node};
|
||||
}
|
||||
copy.node.lock()->data = std::make_shared<TData>(std::forward<TDataUni>(data));
|
||||
copy.node.lock()->data = std::allocate_shared<TData>(dataPool, std::forward<TDataUni>(data));
|
||||
return copy;
|
||||
}
|
||||
|
||||
@@ -393,7 +393,7 @@ bool CPrefixTrie<TKey, TData>::erase(const TKey& key)
|
||||
{
|
||||
auto size_was = height();
|
||||
if (key.empty()) {
|
||||
root->data = std::make_shared<TData>();
|
||||
root->data = std::allocate_shared<TData>(dataPool);
|
||||
} else {
|
||||
erase(key, root);
|
||||
}
|
||||
@@ -404,7 +404,7 @@ template <typename TKey, typename TData>
|
||||
void CPrefixTrie<TKey, TData>::clear()
|
||||
{
|
||||
size = 0;
|
||||
root->data = std::make_shared<TData>();
|
||||
root->data = std::allocate_shared<TData>(dataPool);
|
||||
root->children.clear();
|
||||
}
|
||||
|
||||
|
||||
+8
-2
@@ -9,18 +9,21 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/pool/pool_alloc.hpp>
|
||||
|
||||
template <typename TKey, typename TData>
|
||||
class CPrefixTrie
|
||||
{
|
||||
class Node
|
||||
{
|
||||
static boost::pool_allocator<std::pair<const TKey, std::shared_ptr<Node>>> selfPool;
|
||||
template <bool>
|
||||
friend class Iterator;
|
||||
friend class CPrefixTrie<TKey, TData>;
|
||||
std::map<TKey, std::shared_ptr<Node>> children;
|
||||
std::map<TKey, std::shared_ptr<Node>, std::less<TKey>, decltype(selfPool)> children;
|
||||
|
||||
public:
|
||||
Node() = default;
|
||||
Node() : children(selfPool) {}
|
||||
Node(const Node&) = delete;
|
||||
Node(Node&& o) noexcept = default;
|
||||
Node& operator=(Node&& o) noexcept = default;
|
||||
@@ -102,6 +105,9 @@ class CPrefixTrie
|
||||
size_t size;
|
||||
std::shared_ptr<Node> root;
|
||||
|
||||
static boost::pool_allocator<Node> nodePool;
|
||||
static boost::pool_allocator<TData> dataPool;
|
||||
|
||||
template <typename TNode>
|
||||
using callback = std::function<void(const TKey&, TNode)>;
|
||||
|
||||
|
||||
@@ -227,11 +227,6 @@ public:
|
||||
return (std::string(begin(), end()));
|
||||
}
|
||||
|
||||
std::size_t capacity() const
|
||||
{
|
||||
return vch.capacity();
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Vector subset
|
||||
|
||||
@@ -319,13 +319,30 @@ struct ClaimTrieChainFixture: public CClaimTrieCacheExpirationFork
|
||||
DecrementBlocks(chainActive.Height() - mark);
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
bool keyTypeEmpty(uint8_t keyType)
|
||||
{
|
||||
boost::scoped_ptr<CDBIterator> pcursor(base->db->NewIterator());
|
||||
pcursor->SeekToFirst();
|
||||
|
||||
while (pcursor->Valid()) {
|
||||
std::pair<uint8_t, K> key;
|
||||
if (pcursor->GetKey(key)) {
|
||||
if (key.first == keyType)
|
||||
return false;
|
||||
}
|
||||
pcursor->Next();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool queueEmpty()
|
||||
{
|
||||
for (const auto& claimQueue: claimQueueCache) {
|
||||
if (!claimQueue.second.empty())
|
||||
return false;
|
||||
}
|
||||
return base->db_CLAIM_QUEUE_ROW->IsEmpty();
|
||||
return keyTypeEmpty<int>(CLAIM_QUEUE_ROW);
|
||||
}
|
||||
|
||||
bool expirationQueueEmpty()
|
||||
@@ -334,7 +351,7 @@ struct ClaimTrieChainFixture: public CClaimTrieCacheExpirationFork
|
||||
if (!expirationQueue.second.empty())
|
||||
return false;
|
||||
}
|
||||
return base->db_CLAIM_EXP_QUEUE_ROW->IsEmpty();
|
||||
return keyTypeEmpty<int>(CLAIM_EXP_QUEUE_ROW);
|
||||
}
|
||||
|
||||
bool supportEmpty()
|
||||
@@ -343,7 +360,7 @@ struct ClaimTrieChainFixture: public CClaimTrieCacheExpirationFork
|
||||
if (!entry.second.empty())
|
||||
return false;
|
||||
}
|
||||
return supportCache.empty() && base->db_SUPPORT->IsEmpty();
|
||||
return supportCache.empty() && keyTypeEmpty<std::string>(SUPPORT);
|
||||
}
|
||||
|
||||
bool supportQueueEmpty()
|
||||
@@ -352,7 +369,7 @@ struct ClaimTrieChainFixture: public CClaimTrieCacheExpirationFork
|
||||
if (!support.second.empty())
|
||||
return false;
|
||||
}
|
||||
return base->db_SUPPORT_QUEUE_ROW->IsEmpty();
|
||||
return keyTypeEmpty<int>(SUPPORT_QUEUE_ROW);
|
||||
}
|
||||
|
||||
int proportionalDelayFactor()
|
||||
|
||||
Reference in new issue
Block a user