swarm/storage/localstore: use shed Iterate function

This commit is contained in:
Janos Guljas 2018-12-19 22:37:10 +01:00
parent d54d7ae0f8
commit 67473be38a
2 changed files with 16 additions and 10 deletions

View file

@ -78,7 +78,7 @@ func (db *DB) collectGarbage() (collectedCount int64, done bool, err error) {
target := db.gcTarget() target := db.gcTarget()
done = true done = true
err = db.gcIndex.IterateAll(func(item shed.Item) (stop bool, err error) { err = db.gcIndex.Iterate(func(item shed.Item) (stop bool, err error) {
gcSize := atomic.LoadInt64(&db.gcSize) gcSize := atomic.LoadInt64(&db.gcSize)
if gcSize-collectedCount <= target { if gcSize-collectedCount <= target {
return true, nil return true, nil
@ -96,7 +96,7 @@ func (db *DB) collectGarbage() (collectedCount int64, done bool, err error) {
return true, nil return true, nil
} }
return false, nil return false, nil
}) }, nil)
if err != nil { if err != nil {
return 0, false, err return 0, false, err
} }
@ -183,7 +183,7 @@ func (db *DB) writeGCSize(gcSize int64) (err error) {
// use only one iterator as it acquires its snapshot // use only one iterator as it acquires its snapshot
// not to remove hashes from index that are added // not to remove hashes from index that are added
// after stored gc size is written // after stored gc size is written
err = db.gcUncountedHashesIndex.IterateAll(func(item shed.Item) (stop bool, err error) { err = db.gcUncountedHashesIndex.Iterate(func(item shed.Item) (stop bool, err error) {
db.gcUncountedHashesIndex.DeleteInBatch(batch, item) db.gcUncountedHashesIndex.DeleteInBatch(batch, item)
batchSize++ batchSize++
if batchSize >= maxBatchSize { if batchSize >= maxBatchSize {
@ -195,7 +195,7 @@ func (db *DB) writeGCSize(gcSize int64) (err error) {
batchSize = 0 batchSize = 0
} }
return false, nil return false, nil
}) }, nil)
if err != nil { if err != nil {
return err return err
} }

View file

@ -381,10 +381,13 @@ func newGCIndexTest(db *DB, chunk storage.Chunk, storeTimestamp, accessTimestamp
func newItemsCountTest(i shed.Index, want int) func(t *testing.T) { func newItemsCountTest(i shed.Index, want int) func(t *testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
var c int var c int
i.IterateAll(func(item shed.Item) (stop bool, err error) { err := i.Iterate(func(item shed.Item) (stop bool, err error) {
c++ c++
return return
}) }, nil)
if err != nil {
t.Fatal(err)
}
if c != want { if c != want {
t.Errorf("got %v items in index, want %v", c, want) t.Errorf("got %v items in index, want %v", c, want)
} }
@ -396,10 +399,13 @@ func newItemsCountTest(i shed.Index, want int) func(t *testing.T) {
func newIndexGCSizeTest(db *DB) func(t *testing.T) { func newIndexGCSizeTest(db *DB) func(t *testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
var want int64 var want int64
db.gcIndex.IterateAll(func(item shed.Item) (stop bool, err error) { err := db.gcIndex.Iterate(func(item shed.Item) (stop bool, err error) {
want++ want++
return return
}) }, nil)
if err != nil {
t.Fatal(err)
}
got := atomic.LoadInt64(&db.gcSize) got := atomic.LoadInt64(&db.gcSize)
if got != want { if got != want {
t.Errorf("got gc size %v, want %v", got, want) t.Errorf("got gc size %v, want %v", got, want)
@ -424,7 +430,7 @@ func testItemsOrder(t *testing.T, i shed.Index, chunks []testIndexChunk, sortFun
} }
var cursor int var cursor int
err := i.IterateAll(func(item shed.Item) (stop bool, err error) { err := i.Iterate(func(item shed.Item) (stop bool, err error) {
want := chunks[cursor].Address() want := chunks[cursor].Address()
got := item.Address got := item.Address
if !bytes.Equal(got, want) { if !bytes.Equal(got, want) {
@ -432,7 +438,7 @@ func testItemsOrder(t *testing.T, i shed.Index, chunks []testIndexChunk, sortFun
} }
cursor++ cursor++
return false, nil return false, nil
}) }, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }