Skip to content

Commit 5cd3f98

Browse files
committed
db, blockchain : remove last pool from index
1 parent fccf6b5 commit 5cd3f98

7 files changed

Lines changed: 52 additions & 1 deletion

File tree

csdb/include/csdb/database.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Database {
4747

4848
virtual bool putToTransIndex(const cs::Bytes& key, const cs::Bytes& value) = 0;
4949
virtual bool getFromTransIndex(const cs::Bytes& key, cs::Bytes* value) = 0;
50+
virtual bool removeLastFromTrxIndex(const cs::Bytes& key) = 0;
5051
virtual void truncateTransIndex() = 0;
5152

5253
virtual bool updateContractData(const cs::Bytes& key, const cs::Bytes& data) = 0;

csdb/include/csdb/database_berkeleydb.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class DatabaseBerkeleyDB : public Database {
4040

4141
bool putToTransIndex(const cs::Bytes& key, const cs::Bytes& value) override final;
4242
bool getFromTransIndex(const cs::Bytes& key, cs::Bytes* value) override final;
43+
bool removeLastFromTrxIndex(const cs::Bytes& key) override final;
4344
void truncateTransIndex() override final;
4445

4546
bool updateContractData(const cs::Bytes& key, const cs::Bytes& data) override;

csdb/include/csdb/storage.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class Storage final {
227227
// And now for something completely different
228228
cs::Sequence get_previous_transaction_block(const Address&, cs::Sequence) const;
229229
void set_previous_transaction_block(const Address&, cs::Sequence currTransBlock, cs::Sequence prevTransBlock);
230+
void remove_last_from_trx_index(const Address&, cs::Sequence lastIndexed);
230231
void truncate_trxs_index();
231232

232233
/**

csdb/src/database_berkeleydb.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,24 @@ bool DatabaseBerkeleyDB::putToTransIndex(const cs::Bytes &key, const cs::Bytes &
497497
return true;
498498
}
499499

500+
bool DatabaseBerkeleyDB::removeLastFromTrxIndex(const cs::Bytes &key) {
501+
if (!db_trans_idx_) {
502+
set_last_error(NotOpen);
503+
return false;
504+
}
505+
506+
Dbt_copy<cs::Bytes> db_key(key);
507+
508+
int status = db_trans_idx_->del(nullptr, &db_key, 0);
509+
if (status != 0) {
510+
set_last_error_from_berkeleydb(status);
511+
return false;
512+
}
513+
514+
set_last_error();
515+
return true;
516+
}
517+
500518
bool DatabaseBerkeleyDB::getFromTransIndex(const cs::Bytes &key, cs::Bytes *value) {
501519
if (!db_trans_idx_) {
502520
set_last_error(NotOpen);

csdb/src/storage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,10 @@ void Storage::set_previous_transaction_block(const Address& addr, cs::Sequence c
871871
d->db->putToTransIndex(key, os.buffer());
872872
}
873873

874+
void Storage::remove_last_from_trx_index(const Address& addr, cs::Sequence lastIndexed) {
875+
d->db->removeLastFromTrxIndex(get_trans_index_key(addr, lastIndexed));
876+
}
877+
874878
void Storage::truncate_trxs_index() {
875879
d->db->truncateTransIndex();
876880
}

csnode/include/csnode/blockchain.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class BlockChain {
115115
}
116116

117117
void removeWalletsInPoolFromCache(const csdb::Pool& pool);
118+
void removeLastBlockFromTrxIndex(const csdb::Pool&);
118119
void removeLastBlock();
119120

120121
// updates fees in every transaction

csnode/src/blockchain.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ void BlockChain::removeLastBlock() {
444444
--lastSequence_;
445445
total_transactions_count_ -= pool.transactions().size();
446446
removeWalletsInPoolFromCache(pool);
447+
removeLastBlockFromTrxIndex(pool);
447448

448449
emit removeBlockEvent(pool.sequence());
449450

@@ -461,7 +462,31 @@ csdb::Address BlockChain::getAddressFromKey(const std::string& key) {
461462
return res;
462463
}
463464
}
464-
465+
466+
void BlockChain::removeLastBlockFromTrxIndex(const csdb::Pool& pool) {
467+
std::set<csdb::Address> uniqueAddresses;
468+
469+
auto lbd = [&uniqueAddresses, this](const csdb::Address& addr, cs::Sequence sq) {
470+
auto key = getAddressByType(addr, AddressType::PublicKey);
471+
if (uniqueAddresses.insert(key).second) {
472+
std::lock_guard<decltype(dbLock_)> l(dbLock_);
473+
storage_.remove_last_from_trx_index(key, sq);
474+
}
475+
};
476+
477+
for (const auto& t : pool.transactions()) {
478+
lbd(t.source(), lastIndexedPool);
479+
lbd(t.target(), lastIndexedPool);
480+
}
481+
--lastIndexedPool;
482+
updateLastIndFile();
483+
484+
if (lastNonEmptyBlock_.poolSeq == pool.sequence()) {
485+
lastNonEmptyBlock_ = previousNonEmpty_[lastNonEmptyBlock_.poolSeq];
486+
previousNonEmpty_.erase(pool.sequence());
487+
}
488+
}
489+
465490
void BlockChain::removeWalletsInPoolFromCache(const csdb::Pool& pool) {
466491
try {
467492
std::lock_guard lock(cacheMutex_);

0 commit comments

Comments
 (0)