From 69e136156607e538fc78f3fe4505bd8206cbc0f2 Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Fri, 13 Apr 2018 12:08:35 +0300 Subject: [PATCH] swarm/storage: names fixup --- swarm/storage/ldbstore.go | 44 +++++++------------------ swarm/storage/ldbstore_test.go | 4 +-- swarm/storage/localstore.go | 2 +- swarm/storage/memstore_lrucache_test.go | 2 +- 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/swarm/storage/ldbstore.go b/swarm/storage/ldbstore.go index 6fe98aa1f8..691cf3eb7b 100644 --- a/swarm/storage/ldbstore.go +++ b/swarm/storage/ldbstore.go @@ -48,10 +48,8 @@ var ( ) const ( - defaultDbCapacity = 5000000 - defaultRadius = 0 // not yet used - gcArrayFreeRatio = 0.1 + maxGCitems = 5000 // max number of items to be gc'd per call to collectGarbage() ) var ( @@ -82,9 +80,6 @@ type LDBStore struct { capacity uint64 bucketCnt []uint64 - gcPos []byte - gcStartPos []byte - hashfunc SwarmHasher po func(Key) uint8 @@ -138,7 +133,6 @@ func NewLDBStore(path string, hash SwarmHasher, capacity uint64, po func(Key) ui data, _ := s.db.Get(keyEntryCnt) s.entryCnt = BytesToU64(data) s.entryCnt++ - log.Trace("NewLDBStore s.entryCnt++", "entryCnt", s.entryCnt) data, _ = s.db.Get(keyAccessCnt) s.accessCnt = BytesToU64(data) s.accessCnt++ @@ -146,12 +140,6 @@ func NewLDBStore(path string, hash SwarmHasher, capacity uint64, po func(Key) ui s.dataIdx = BytesToU64(data) s.dataIdx++ - s.gcStartPos = make([]byte, 1) - s.gcStartPos[0] = keyIndex - s.gcPos, _ = s.db.Get(keyGCPos) - if s.gcPos == nil { - s.gcPos = s.gcStartPos - } return s, nil } @@ -254,14 +242,18 @@ func (s *LDBStore) collectGarbage(ratio float32) { garbage := []*gcItem{} gcnt := 0 - for ok := it.Seek([]byte{keyIndex}); ok && (gcnt < 5000) && (uint64(gcnt) < s.entryCnt); ok = it.Next() { - key := it.Key() - val := it.Value() - if (key == nil) || (key[0] != keyIndex) { + for ok := it.Seek([]byte{keyIndex}); ok && (gcnt < maxGCitems) && (uint64(gcnt) < s.entryCnt); ok = it.Next() { + itkey := it.Key() + + if (itkey == nil) || (itkey[0] != keyIndex) { break } - log.Trace("iterator", "key", fmt.Sprintf("%x", key), "value", fmt.Sprintf("%x", val)) + // it.Key() contents change on next call to it.Next(), so we must copy it + key := make([]byte, len(it.Key())) + copy(key, it.Key()) + + val := it.Value() var index dpaDBIndex @@ -269,35 +261,23 @@ func (s *LDBStore) collectGarbage(ratio float32) { decodeIndex(val, &index) po := s.po(hash) - kkey := make([]byte, len(key)) - copy(kkey, key) - gci := &gcItem{ - idxKey: kkey, + idxKey: key, idx: index.Idx, - value: index.Access, + value: index.Access, // the smaller, the more likely to be gc'd. see sort comparator below. po: po, } - log.Trace("gci.idxKey", "gcnt", gcnt, "idxKey", fmt.Sprintf("%x", gci.idxKey), "idx", gci.idx, "gci.value", gci.value) - garbage = append(garbage, gci) gcnt++ } sort.Slice(garbage[:gcnt], func(i, j int) bool { return garbage[i].value < garbage[j].value }) - for k := 0; k < gcnt; k++ { - log.Trace("gcArray[]", "k", k, "idx", garbage[k].idx, "idxKey", fmt.Sprintf("%x", garbage[k].idxKey), "value", garbage[k].value) - } - cutoff := int(float32(gcnt) * ratio) - log.Trace("cutoff", "cut", cutoff, "gcnt", gcnt) for i := 0; i < cutoff; i++ { s.delete(garbage[i].idx, garbage[i].idxKey, garbage[i].po) } - - //s.db.Put(keyGCPos, s.gcPos) } // Export writes all chunks from the store to a tar archive, returning the diff --git a/swarm/storage/ldbstore_test.go b/swarm/storage/ldbstore_test.go index 928d8dc94a..c0ae54bd68 100644 --- a/swarm/storage/ldbstore_test.go +++ b/swarm/storage/ldbstore_test.go @@ -49,9 +49,9 @@ func newTestDbStore(mock bool) (*testDbStore, error) { addr := common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") mockStore := globalStore.NewNodeStore(addr) - db, err = NewMockDbStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc, mockStore) + db, err = NewMockDbStore(dir, MakeHashFunc(SHA3Hash), defaultLDBCapacity, testPoFunc, mockStore) } else { - db, err = NewLDBStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc) + db, err = NewLDBStore(dir, MakeHashFunc(SHA3Hash), defaultLDBCapacity, testPoFunc) } return &testDbStore{db, dir}, err diff --git a/swarm/storage/localstore.go b/swarm/storage/localstore.go index b2d4ef7616..53a6e9f90b 100644 --- a/swarm/storage/localstore.go +++ b/swarm/storage/localstore.go @@ -40,7 +40,7 @@ type StoreParams struct { //create params with default values func NewDefaultStoreParams() (self *StoreParams) { return &StoreParams{ - DbCapacity: defaultDbCapacity, + DbCapacity: defaultLDBCapacity, CacheCapacity: defaultCacheCapacity, } } diff --git a/swarm/storage/memstore_lrucache_test.go b/swarm/storage/memstore_lrucache_test.go index 285f797724..08f9920520 100644 --- a/swarm/storage/memstore_lrucache_test.go +++ b/swarm/storage/memstore_lrucache_test.go @@ -34,7 +34,7 @@ func newLDBStore(t *testing.T) (*LDBStore, func()) { } log.Trace("memstore.tempdir", "dir", dir) - db, err := NewLDBStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc) + db, err := NewLDBStore(dir, MakeHashFunc(SHA3Hash), defaultLDBCapacity, testPoFunc) if err != nil { t.Fatal(err) }