From 26f6a509c7b9c630649ca824c54fd3a41e745e49 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 14 Nov 2018 19:43:13 +0100 Subject: [PATCH] swarm/storage: Fix iterator condition --- swarm/storage/ldbstore.go | 9 +++------ swarm/storage/ldbstore_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/swarm/storage/ldbstore.go b/swarm/storage/ldbstore.go index 717fbe9361..c163f9b9f3 100644 --- a/swarm/storage/ldbstore.go +++ b/swarm/storage/ldbstore.go @@ -604,15 +604,15 @@ func (s *LDBStore) CleanGCIndex() error { it.Release() - //var idx dpaDBIndex var poPtrs [256]uint64 var doneIterating bool + lastIdxKey := []byte{keyIndex} for !doneIterating { var idxs []dpaDBIndex var chunkHashes [][]byte var pos []uint8 it := s.db.NewIterator() - it.Seek([]byte{keyIndex}) + it.Seek(lastIdxKey) for i := 0; i < 4096; i++ { if !it.Valid() { doneIterating = true @@ -630,6 +630,7 @@ func (s *LDBStore) CleanGCIndex() error { return fmt.Errorf("corrupt index: %v", err) } po := s.po(chunkHash) + lastIdxKey = it.Key() // if we don't find the data key, remove the entry dataKey := getDataKey(idx.Idx, po) @@ -637,9 +638,6 @@ func (s *LDBStore) CleanGCIndex() error { if err != nil { log.Warn("deleting inconsistent index (missing data)", "key", chunkHash) batch.Delete(it.Key()) - // if err := s.db.Delete(it.Key()); err != nil { - // return err - // } } else { idxs = append(idxs, idx) chunkHashes = append(chunkHashes, chunkHash) @@ -669,7 +667,6 @@ func (s *LDBStore) CleanGCIndex() error { if err != nil { return err } - } log.Debug("gc cleanup entries", "ok", okEntryCount, "total", totalEntryCount, "batchlen", batch.Len()) diff --git a/swarm/storage/ldbstore_test.go b/swarm/storage/ldbstore_test.go index 07557980c6..9c10b36290 100644 --- a/swarm/storage/ldbstore_test.go +++ b/swarm/storage/ldbstore_test.go @@ -761,6 +761,38 @@ func TestCleanIndex(t *testing.T) { t.Fatalf("expected sum of bin indices to be 3, was %d", binTotal) } } + + // check that the iterator quits properly + chunks, err = mputRandomChunks(ldb, 4100, 4096) + if err != nil { + t.Fatal(err) + } + + po = ldb.po(chunks[4099].Address()[:]) + dataKey = make([]byte, 10) + dataKey[0] = keyData + dataKey[1] = byte(po) + binary.BigEndian.PutUint64(dataKey[2:], 4099+3) + if _, err := ldb.db.Get(dataKey); err != nil { + t.Fatal(err) + } + if err := ldb.db.Delete(dataKey); err != nil { + t.Fatal(err) + } + + if err := ldb.CleanGCIndex(); err != nil { + t.Fatal(err) + } + + // entrycount should now be one less of added chunks + c, err = ldb.db.Get(keyEntryCnt) + if err != nil { + t.Fatalf("expected gc 2 idx to be present: %v", idxKey) + } + entryCount = binary.BigEndian.Uint64(c) + if entryCount != 4099+2 { + t.Fatalf("expected entrycnt to be 2, was %d", c) + } } func waitGc(ctx context.Context, ldb *LDBStore) {