From 60571122d5ee1a0ce5e8f66107b4c99bb65dbf7b Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Fri, 17 Aug 2018 14:54:08 +0200 Subject: [PATCH] swarm/storage: add Delete to LDBStore --- swarm/storage/ldbstore.go | 16 ++++++++++++++-- swarm/storage/ldbstore_test.go | 17 ++--------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/swarm/storage/ldbstore.go b/swarm/storage/ldbstore.go index f81bdc76ad..dd9e6414e6 100644 --- a/swarm/storage/ldbstore.go +++ b/swarm/storage/ldbstore.go @@ -462,6 +462,18 @@ func (s *LDBStore) ReIndex() { log.Warn(fmt.Sprintf("Found %v errors out of %v entries", errorsFound, total)) } +func (s *LDBStore) Delete(addr Address) { + s.lock.Lock() + defer s.lock.Unlock() + + ikey := getIndexKey(addr) + + var indx dpaDBIndex + s.tryAccessIdx(ikey, &indx) + + s.delete(indx.Idx, ikey, s.po(addr)) +} + func (s *LDBStore) delete(idx uint64, idxKey []byte, po uint8) { metrics.GetOrRegisterCounter("ldbstore.delete", nil).Inc(1) @@ -486,8 +498,8 @@ func (s *LDBStore) CurrentBucketStorageIndex(po uint8) uint64 { } func (s *LDBStore) Size() uint64 { - s.lock.Lock() - defer s.lock.Unlock() + s.lock.RLock() + defer s.lock.RUnlock() return s.entryCnt } diff --git a/swarm/storage/ldbstore_test.go b/swarm/storage/ldbstore_test.go index 16ac7ee2e4..51a5c5586f 100644 --- a/swarm/storage/ldbstore_test.go +++ b/swarm/storage/ldbstore_test.go @@ -416,14 +416,7 @@ func TestLDBStoreAddRemove(t *testing.T) { for i := 0; i < n; i++ { // delete all even index chunks if i%2 == 0 { - - key := chunks[i].Addr - ikey := getIndexKey(key) - - var indx dpaDBIndex - ldb.tryAccessIdx(ikey, &indx) - - ldb.delete(indx.Idx, ikey, ldb.po(key)) + ldb.Delete(chunks[i].Addr) } } @@ -477,13 +470,7 @@ func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) { // delete all chunks for i := 0; i < n; i++ { - key := chunks[i].Addr - ikey := getIndexKey(key) - - var indx dpaDBIndex - ldb.tryAccessIdx(ikey, &indx) - - ldb.delete(indx.Idx, ikey, ldb.po(key)) + ldb.Delete(chunks[i].Addr) } log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)