swarm/storage: Fix iterator condition

This commit is contained in:
lash 2018-11-14 19:43:13 +01:00
parent d4944a3883
commit 26f6a509c7
2 changed files with 35 additions and 6 deletions

View file

@ -604,15 +604,15 @@ func (s *LDBStore) CleanGCIndex() error {
it.Release() it.Release()
//var idx dpaDBIndex
var poPtrs [256]uint64 var poPtrs [256]uint64
var doneIterating bool var doneIterating bool
lastIdxKey := []byte{keyIndex}
for !doneIterating { for !doneIterating {
var idxs []dpaDBIndex var idxs []dpaDBIndex
var chunkHashes [][]byte var chunkHashes [][]byte
var pos []uint8 var pos []uint8
it := s.db.NewIterator() it := s.db.NewIterator()
it.Seek([]byte{keyIndex}) it.Seek(lastIdxKey)
for i := 0; i < 4096; i++ { for i := 0; i < 4096; i++ {
if !it.Valid() { if !it.Valid() {
doneIterating = true doneIterating = true
@ -630,6 +630,7 @@ func (s *LDBStore) CleanGCIndex() error {
return fmt.Errorf("corrupt index: %v", err) return fmt.Errorf("corrupt index: %v", err)
} }
po := s.po(chunkHash) po := s.po(chunkHash)
lastIdxKey = it.Key()
// if we don't find the data key, remove the entry // if we don't find the data key, remove the entry
dataKey := getDataKey(idx.Idx, po) dataKey := getDataKey(idx.Idx, po)
@ -637,9 +638,6 @@ func (s *LDBStore) CleanGCIndex() error {
if err != nil { if err != nil {
log.Warn("deleting inconsistent index (missing data)", "key", chunkHash) log.Warn("deleting inconsistent index (missing data)", "key", chunkHash)
batch.Delete(it.Key()) batch.Delete(it.Key())
// if err := s.db.Delete(it.Key()); err != nil {
// return err
// }
} else { } else {
idxs = append(idxs, idx) idxs = append(idxs, idx)
chunkHashes = append(chunkHashes, chunkHash) chunkHashes = append(chunkHashes, chunkHash)
@ -669,7 +667,6 @@ func (s *LDBStore) CleanGCIndex() error {
if err != nil { if err != nil {
return err return err
} }
} }
log.Debug("gc cleanup entries", "ok", okEntryCount, "total", totalEntryCount, "batchlen", batch.Len()) log.Debug("gc cleanup entries", "ok", okEntryCount, "total", totalEntryCount, "batchlen", batch.Len())

View file

@ -761,6 +761,38 @@ func TestCleanIndex(t *testing.T) {
t.Fatalf("expected sum of bin indices to be 3, was %d", binTotal) 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) { func waitGc(ctx context.Context, ldb *LDBStore) {