diff --git a/cmd/swarm/db.go b/cmd/swarm/db.go index 82db713f14..eee5d25cb9 100644 --- a/cmd/swarm/db.go +++ b/cmd/swarm/db.go @@ -35,7 +35,7 @@ func dbExport(ctx *cli.Context) { utils.Fatalf("invalid arguments, please specify both (path to a local chunk database), (path to write the tar archive to, - for stdout) and the base key") } - store, err := openDbStore(args[0], common.Hex2Bytes(args[2])) + store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) if err != nil { utils.Fatalf("error opening local chunk database: %s", err) } @@ -67,7 +67,7 @@ func dbImport(ctx *cli.Context) { utils.Fatalf("invalid arguments, please specify both (path to a local chunk database), (path to read the tar archive from, - for stdin) and the base key") } - store, err := openDbStore(args[0], common.Hex2Bytes(args[2])) + store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) if err != nil { utils.Fatalf("error opening local chunk database: %s", err) } @@ -99,7 +99,7 @@ func dbClean(ctx *cli.Context) { utils.Fatalf("invalid arguments, please specify (path to a local chunk database) and the base key") } - store, err := openDbStore(args[0], common.Hex2Bytes(args[1])) + store, err := openLDBStore(args[0], common.Hex2Bytes(args[1])) if err != nil { utils.Fatalf("error opening local chunk database: %s", err) } @@ -108,10 +108,10 @@ func dbClean(ctx *cli.Context) { store.Cleanup() } -func openDbStore(path string, basekey []byte) (*storage.DbStore, error) { +func openLDBStore(path string, basekey []byte) (*storage.LDBStore, error) { if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil { return nil, fmt.Errorf("invalid chunkdb path: %s", err) } hash := storage.MakeHashFunc("SHA3") - return storage.NewDbStore(path, hash, 10000000, func(k storage.Key) (ret uint8) { return uint8(storage.Proximity(basekey[:], k[:])) }) + return storage.NewLDBStore(path, hash, 10000000, func(k storage.Key) (ret uint8) { return uint8(storage.Proximity(basekey[:], k[:])) }) } diff --git a/swarm/storage/dbapi.go b/swarm/storage/dbapi.go index 69a659564a..e0fac87464 100644 --- a/swarm/storage/dbapi.go +++ b/swarm/storage/dbapi.go @@ -18,12 +18,12 @@ package storage // wrapper of db-s to provide mockable custom local chunk store access to syncer type DBAPI struct { - db *DbStore + db *LDBStore loc *LocalStore } func NewDBAPI(loc *LocalStore) *DBAPI { - return &DBAPI{loc.DbStore.(*DbStore), loc} + return &DBAPI{loc.DbStore.(*LDBStore), loc} } // to obtain the chunks from key or request db entry only diff --git a/swarm/storage/dpa.go b/swarm/storage/dpa.go index 2d60ca97d3..2a28fbcd03 100644 --- a/swarm/storage/dpa.go +++ b/swarm/storage/dpa.go @@ -71,7 +71,7 @@ func NewLocalDPA(datadir string, basekey []byte) (*DPA, error) { hash := MakeHashFunc("SHA3") - dbStore, err := NewDbStore(datadir, hash, singletonSwarmDbCapacity, func(k Key) (ret uint8) { return uint8(Proximity(basekey[:], k[:])) }) + dbStore, err := NewLDBStore(datadir, hash, singletonSwarmDbCapacity, func(k Key) (ret uint8) { return uint8(Proximity(basekey[:], k[:])) }) if err != nil { return nil, err } diff --git a/swarm/storage/dpa_test.go b/swarm/storage/dpa_test.go index 51b276d2e8..75f1448dc4 100644 --- a/swarm/storage/dpa_test.go +++ b/swarm/storage/dpa_test.go @@ -32,7 +32,7 @@ func TestDPArandom(t *testing.T) { t.Fatalf("init dbStore failed: %v", err) } defer tdb.close() - db := tdb.DbStore + db := tdb.LDBStore db.setCapacity(50000) memStore := NewMemStore(db, defaultCacheCapacity) localStore := &LocalStore{ @@ -91,7 +91,7 @@ func TestDPA_capacity(t *testing.T) { t.Fatalf("init dbStore failed: %v", err) } defer tdb.close() - db := tdb.DbStore + db := tdb.LDBStore memStore := NewMemStore(db, 0) localStore := &LocalStore{ memStore, diff --git a/swarm/storage/dbstore.go b/swarm/storage/ldbstore.go similarity index 92% rename from swarm/storage/dbstore.go rename to swarm/storage/ldbstore.go index c0dd0d4b0c..8bc080c1ba 100644 --- a/swarm/storage/dbstore.go +++ b/swarm/storage/ldbstore.go @@ -74,7 +74,7 @@ type gcItem struct { idxKey []byte } -type DbStore struct { +type LDBStore struct { db *LDBDatabase // this should be stored in db, accessed transactionally @@ -106,8 +106,8 @@ type DbStore struct { // TODO: Instead of passing the distance function, just pass the address from which distances are calculated // to avoid the appearance of a pluggable distance metric and opportunities of bugs associated with providing // a function different from the one that is actually used. -func NewDbStore(path string, hash SwarmHasher, capacity uint64, po func(Key) uint8) (s *DbStore, err error) { - s = new(DbStore) +func NewLDBStore(path string, hash SwarmHasher, capacity uint64, po func(Key) uint8) (s *LDBStore, err error) { + s = new(LDBStore) s.hashfunc = hash s.batchC = make(chan bool) @@ -158,8 +158,8 @@ func NewDbStore(path string, hash SwarmHasher, capacity uint64, po func(Key) uin // NewMockDbStore creates a new instance of DbStore with // mockStore set to a provided value. If mockStore argument is nil, // this function behaves exactly as NewDbStore. -func NewMockDbStore(path string, hash SwarmHasher, capacity uint64, po func(Key) uint8, mockStore *mock.NodeStore) (s *DbStore, err error) { - s, err = NewDbStore(path, hash, capacity, po) +func NewMockDbStore(path string, hash SwarmHasher, capacity uint64, po func(Key) uint8, mockStore *mock.NodeStore) (s *LDBStore, err error) { + s, err = NewLDBStore(path, hash, capacity, po) if err != nil { return nil, err } @@ -195,7 +195,7 @@ func getIndexGCValue(index *dpaDBIndex) uint64 { return index.Access } -func (s *DbStore) updateIndexAccess(index *dpaDBIndex) { +func (s *LDBStore) updateIndexAccess(index *dpaDBIndex) { index.Access = s.accessCnt } @@ -286,7 +286,7 @@ func gcListSelect(list []*gcItem, left int, right int, n int) int { } } -func (s *DbStore) collectGarbage(ratio float32) { +func (s *LDBStore) collectGarbage(ratio float32) { it := s.db.NewIterator() it.Seek(s.gcPos) if it.Valid() { @@ -345,7 +345,7 @@ func (s *DbStore) collectGarbage(ratio float32) { // Export writes all chunks from the store to a tar archive, returning the // number of chunks written. -func (s *DbStore) Export(out io.Writer) (int64, error) { +func (s *LDBStore) Export(out io.Writer) (int64, error) { tw := tar.NewWriter(out) defer tw.Close() @@ -387,7 +387,7 @@ func (s *DbStore) Export(out io.Writer) (int64, error) { } // of chunks read. -func (s *DbStore) Import(in io.Reader) (int64, error) { +func (s *LDBStore) Import(in io.Reader) (int64, error) { tr := tar.NewReader(in) var count int64 @@ -429,7 +429,7 @@ func (s *DbStore) Import(in io.Reader) (int64, error) { return count, nil } -func (s *DbStore) Cleanup() { +func (s *LDBStore) Cleanup() { //Iterates over the database and checks that there are no faulty chunks it := s.db.NewIterator() startPosition := []byte{kpIndex} @@ -468,7 +468,7 @@ func (s *DbStore) Cleanup() { log.Warn(fmt.Sprintf("Found %v errors out of %v entries", errorsFound, total)) } -func (s *DbStore) ReIndex() { +func (s *LDBStore) ReIndex() { //Iterates over the database and checks that there are no faulty chunks it := s.db.NewIterator() startPosition := []byte{keyOldData} @@ -511,7 +511,7 @@ func (s *DbStore) ReIndex() { log.Warn(fmt.Sprintf("Found %v errors out of %v entries", errorsFound, total)) } -func (s *DbStore) delete(idx uint64, idxKey []byte, po uint8) { +func (s *LDBStore) delete(idx uint64, idxKey []byte, po uint8) { batch := new(leveldb.Batch) batch.Delete(idxKey) batch.Delete(getDataKey(idx, po)) @@ -526,26 +526,26 @@ func (s *DbStore) delete(idx uint64, idxKey []byte, po uint8) { s.db.Write(batch) } -func (s *DbStore) CurrentBucketStorageIndex(po uint8) uint64 { +func (s *LDBStore) CurrentBucketStorageIndex(po uint8) uint64 { s.lock.RLock() defer s.lock.RUnlock() return s.bucketCnt[po] } -func (s *DbStore) Size() uint64 { +func (s *LDBStore) Size() uint64 { s.lock.Lock() defer s.lock.Unlock() return s.entryCnt } -func (s *DbStore) CurrentStorageIndex() uint64 { +func (s *LDBStore) CurrentStorageIndex() uint64 { s.lock.RLock() defer s.lock.RUnlock() return s.dataIdx } -func (s *DbStore) Put(chunk *Chunk) { +func (s *LDBStore) Put(chunk *Chunk) { ikey := getIndexKey(chunk.Key) var index dpaDBIndex @@ -577,7 +577,7 @@ func (s *DbStore) Put(chunk *Chunk) { } // force putting into db, does not check access index -func (s *DbStore) doPut(chunk *Chunk, ikey []byte, index *dpaDBIndex, po uint8) { +func (s *LDBStore) doPut(chunk *Chunk, ikey []byte, index *dpaDBIndex, po uint8) { data := s.encodeDataFunc(chunk) s.batch.Put(getDataKey(s.dataIdx, po), data) index.Idx = s.dataIdx @@ -592,7 +592,7 @@ func (s *DbStore) doPut(chunk *Chunk, ikey []byte, index *dpaDBIndex, po uint8) } -func (s *DbStore) writeBatches() { +func (s *LDBStore) writeBatches() { for range s.batchesC { s.lock.Lock() b := s.batch @@ -618,7 +618,7 @@ func (s *DbStore) writeBatches() { } // must be called non concurrently -func (s *DbStore) writeBatch(b *leveldb.Batch, entryCnt, dataIdx, accessCnt uint64) error { +func (s *LDBStore) writeBatch(b *leveldb.Batch, entryCnt, dataIdx, accessCnt uint64) error { b.Put(keyEntryCnt, U64ToBytes(entryCnt)) b.Put(keyDataIdx, U64ToBytes(dataIdx)) b.Put(keyAccessCnt, U64ToBytes(accessCnt)) @@ -644,7 +644,7 @@ func newMockEncodeDataFunc(mockStore *mock.NodeStore) func(chunk *Chunk) []byte } // try to find index; if found, update access cnt and return true -func (s *DbStore) tryAccessIdx(ikey []byte, index *dpaDBIndex) bool { +func (s *LDBStore) tryAccessIdx(ikey []byte, index *dpaDBIndex) bool { idata, err := s.db.Get(ikey) if err != nil { return false @@ -658,13 +658,13 @@ func (s *DbStore) tryAccessIdx(ikey []byte, index *dpaDBIndex) bool { return true } -func (s *DbStore) Get(key Key) (chunk *Chunk, err error) { +func (s *LDBStore) Get(key Key) (chunk *Chunk, err error) { s.lock.Lock() defer s.lock.Unlock() return s.get(key) } -func (s *DbStore) get(key Key) (chunk *Chunk, err error) { +func (s *LDBStore) get(key Key) (chunk *Chunk, err error) { var indx dpaDBIndex if s.tryAccessIdx(getIndexKey(key), &indx) { @@ -724,7 +724,7 @@ func newMockGetDataFunc(mockStore *mock.NodeStore) func(key Key) (data []byte, e } } -func (s *DbStore) updateAccessCnt(key Key) { +func (s *LDBStore) updateAccessCnt(key Key) { s.lock.Lock() defer s.lock.Unlock() @@ -734,7 +734,7 @@ func (s *DbStore) updateAccessCnt(key Key) { } -func (s *DbStore) setCapacity(c uint64) { +func (s *LDBStore) setCapacity(c uint64) { s.lock.Lock() defer s.lock.Unlock() @@ -755,12 +755,12 @@ func (s *DbStore) setCapacity(c uint64) { } } -func (s *DbStore) Close() { +func (s *LDBStore) Close() { s.db.Close() } // SyncIterator(start, stop, po, f) calls f on each hash of a bin po from start to stop -func (s *DbStore) SyncIterator(since uint64, until uint64, po uint8, f func(Key, uint64) bool) error { +func (s *LDBStore) SyncIterator(since uint64, until uint64, po uint8, f func(Key, uint64) bool) error { sincekey := getDataKey(since, po) untilkey := getDataKey(until, po) it := s.db.NewIterator() diff --git a/swarm/storage/dbstore_test.go b/swarm/storage/ldbstore_test.go similarity index 98% rename from swarm/storage/dbstore_test.go rename to swarm/storage/ldbstore_test.go index 39f976442a..6caf556c2d 100644 --- a/swarm/storage/dbstore_test.go +++ b/swarm/storage/ldbstore_test.go @@ -30,7 +30,7 @@ import ( ) type testDbStore struct { - *DbStore + *LDBStore dir string } @@ -40,7 +40,7 @@ func newTestDbStore(mock bool) (*testDbStore, error) { return nil, err } - var db *DbStore + var db *LDBStore if mock { globalStore := mem.NewGlobalStore() addr := common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") @@ -48,7 +48,7 @@ func newTestDbStore(mock bool) (*testDbStore, error) { db, err = NewMockDbStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc, mockStore) } else { - db, err = NewDbStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc) + db, err = NewLDBStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, testPoFunc) } return &testDbStore{db, dir}, err diff --git a/swarm/storage/localstore.go b/swarm/storage/localstore.go index 6e3d00ee08..02bcd2385e 100644 --- a/swarm/storage/localstore.go +++ b/swarm/storage/localstore.go @@ -66,7 +66,7 @@ func NewLocalStore(hash SwarmHasher, params *StoreParams, basekey []byte, mockSt func NewTestLocalStore(path string) (*LocalStore, error) { basekey := make([]byte, 32) hasher := MakeHashFunc("SHA3") - dbStore, err := NewDbStore(path, hasher, singletonSwarmDbCapacity, func(k Key) (ret uint8) { return uint8(Proximity(basekey[:], k[:])) }) + dbStore, err := NewLDBStore(path, hasher, singletonSwarmDbCapacity, func(k Key) (ret uint8) { return uint8(Proximity(basekey[:], k[:])) }) if err != nil { return nil, err } @@ -79,7 +79,7 @@ func NewTestLocalStore(path string) (*LocalStore, error) { func NewTestLocalStoreForAddr(path string, basekey []byte) (*LocalStore, error) { hasher := MakeHashFunc("SHA3") - dbStore, err := NewDbStore(path, hasher, singletonSwarmDbCapacity, func(k Key) (ret uint8) { return uint8(Proximity(basekey[:], k[:])) }) + dbStore, err := NewLDBStore(path, hasher, singletonSwarmDbCapacity, func(k Key) (ret uint8) { return uint8(Proximity(basekey[:], k[:])) }) if err != nil { return nil, err } diff --git a/swarm/storage/memstore.go b/swarm/storage/memstore.go index 85303478ed..4de561e02a 100644 --- a/swarm/storage/memstore.go +++ b/swarm/storage/memstore.go @@ -44,7 +44,7 @@ type MemStore struct { entryCnt, capacity uint // stored entries accessCnt uint64 // access counter; oldest is thrown away when full dbAccessCnt uint64 - dbStore *DbStore + ldbStore *LDBStore lock sync.Mutex } @@ -61,10 +61,10 @@ a hash prefix subtree containing subtrees or one storage entry (but never both) (access[] is a binary tree inside the multi-bit leveled hash tree) */ -func NewMemStore(d *DbStore, capacity uint) (m *MemStore) { +func NewMemStore(d *LDBStore, capacity uint) (m *MemStore) { m = &MemStore{} m.memtree = newMemTree(memTreeFLW, nil, 0) - m.dbStore = d + m.ldbStore = d m.setCapacity(capacity) return } @@ -240,8 +240,8 @@ func (s *MemStore) Get(hash Key) (chunk *Chunk, err error) { if s.dbAccessCnt-node.lastDBaccess > dbForceUpdateAccessCnt { s.dbAccessCnt++ node.lastDBaccess = s.dbAccessCnt - if s.dbStore != nil { - s.dbStore.updateAccessCnt(hash) + if s.ldbStore != nil { + s.ldbStore.updateAccessCnt(hash) } } } else { diff --git a/swarm/storage/resource.go b/swarm/storage/resource.go index 2519ea4ed1..f2567a762b 100644 --- a/swarm/storage/resource.go +++ b/swarm/storage/resource.go @@ -870,7 +870,7 @@ func NewTestResourceHandler(datadir string, ethClient headerGetter, validator Re path := filepath.Join(datadir, DbDirName) basekey := make([]byte, 32) hasher := MakeHashFunc(SHA3Hash) - dbStore, err := NewDbStore(path, hasher, singletonSwarmDbCapacity, func(k Key) (ret uint8) { return uint8(Proximity(basekey[:], k[:])) }) + dbStore, err := NewLDBStore(path, hasher, singletonSwarmDbCapacity, func(k Key) (ret uint8) { return uint8(Proximity(basekey[:], k[:])) }) if err != nil { return nil, err }