Skip to content

Commit

Permalink
bytes and flushSize
Browse files Browse the repository at this point in the history
  • Loading branch information
ohcee authored Jul 3, 2024
1 parent 3d7b922 commit 8c5900f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ bool CZerocoinDB::WriteCoinMintBatch(const std::map<libzerocoin::PublicCoin, uin
const size_t FLUSH_SIZE = 950000000; // Set flush size to 950000000
std::vector<std::pair<std::pair<char, uint256>, uint256>> cache;
size_t count = 0;
size_t currentCacheSize = 0; // Track current cache size in bytes

auto it = mintInfo.begin(); // Iterator for traversing the map

Expand All @@ -547,7 +548,12 @@ bool CZerocoinDB::WriteCoinMintBatch(const std::map<libzerocoin::PublicCoin, uin
for (size_t i = 0; i < FLUSH_SIZE && it != mintInfo.end(); ++i, ++it) {
libzerocoin::PublicCoin pubCoin = it->first; // Get the public coin
uint256 hash = GetPubCoinHash(pubCoin.getValue()); // Calculate the hash of the coin's value
size_t elementSize = sizeof('m') + hash.size() + it->second.size(); // Approximate size of the element in bytes
if (currentCacheSize + elementSize > FLUSH_SIZE) {
break; // If adding the next element exceeds FLUSH_SIZE, break out of the loop
}
cache.emplace_back(std::make_pair('m', hash), it->second); // Add to cache
currentCacheSize += elementSize; // Update current cache size
++count; // Increment the counter
}

Expand All @@ -562,6 +568,7 @@ bool CZerocoinDB::WriteCoinMintBatch(const std::map<libzerocoin::PublicCoin, uin
return false; // If writing the batch fails, return false
}
cache.clear();
currentCacheSize = 0; // Reset current cache size
}
}

Expand Down Expand Up @@ -603,6 +610,7 @@ bool CZerocoinDB::WriteCoinSpendBatch(const std::map<libzerocoin::CoinSpend, uin
const size_t FLUSH_SIZE = 950000000; // Set flush size to 950000000
std::vector<std::pair<std::pair<char, uint256>, uint256>> cache;
size_t count = 0;
size_t currentCacheSize = 0;

auto it = spendInfo.begin(); // Iterator for traversing the map

Expand All @@ -614,7 +622,12 @@ bool CZerocoinDB::WriteCoinSpendBatch(const std::map<libzerocoin::CoinSpend, uin
CDataStream ss(SER_GETHASH, 0); // Create a data stream for hashing
ss << bnSerial; // Serialize the serial number into the data stream
uint256 hash = Hash(ss.begin(), ss.end()); // Compute the hash of the serialized data
size_t elementSize = sizeof('s') + hash.size() + it->second.size(); // Approximate size of the element in bytes
if (currentCacheSize + elementSize > FLUSH_SIZE) {
break; // If adding the next element exceeds FLUSH_SIZE, break out of the loop
}
cache.emplace_back(std::make_pair('s', hash), it->second); // Add to cache
currentCacheSize += elementSize; // Update current cache size
++count; // Increment the counter
}

Expand All @@ -629,6 +642,7 @@ bool CZerocoinDB::WriteCoinSpendBatch(const std::map<libzerocoin::CoinSpend, uin
return false; // If writing the batch fails, return false
}
cache.clear();
currentCacheSize = 0;
}
}

Expand Down Expand Up @@ -677,6 +691,7 @@ bool CZerocoinDB::WritePubcoinSpendBatch(std::map<uint256, uint256>& mapPubcoinS
const size_t FLUSH_SIZE = 950000000; // Set flush size to 950000000
std::vector<std::pair<std::pair<char, uint256>, std::pair<uint256, uint256>>> cache;
size_t count = 0;
size_t currentCacheSize = 0;

auto it = mapPubcoinSpends.begin(); // Iterator for traversing the map

Expand All @@ -686,7 +701,12 @@ bool CZerocoinDB::WritePubcoinSpendBatch(std::map<uint256, uint256>& mapPubcoinS
for (size_t i = 0; i < FLUSH_SIZE && it != mapPubcoinSpends.end(); ++i, ++it) {
const uint256& hashPubcoin = it->first; // Get the hash of the public coin
const uint256& txid = it->second; // Get the associated transaction ID
size_t elementSize = sizeof('l') + hashPubcoin.size() + txid.size() + hashBlock.size(); // Approximate size of the element in bytes
if (currentCacheSize + elementSize > FLUSH_SIZE) {
break; // If adding the next element exceeds FLUSH_SIZE, break out of the loop
}
cache.emplace_back(std::make_pair('l', hashPubcoin), std::make_pair(txid, hashBlock)); // Add to cache
currentCacheSize += elementSize;
++count; // Increment the counter
}

Expand Down

0 comments on commit 8c5900f

Please sign in to comment.