From 9d4f27277e221927a7630b6d183027e7dbc69f1c Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Sun, 3 Feb 2019 20:20:01 -0500 Subject: [PATCH] swarm/storage: HasChunk -> Has and implementation with no access count change --- swarm/storage/common_test.go | 4 ++-- swarm/storage/ldbstore.go | 14 +++++++++----- swarm/storage/localstore.go | 6 +++--- swarm/storage/localstore_test.go | 14 +++++++------- swarm/storage/memstore.go | 4 ++-- swarm/storage/netstore.go | 6 +++--- swarm/storage/types.go | 6 +++--- 7 files changed, 29 insertions(+), 25 deletions(-) diff --git a/swarm/storage/common_test.go b/swarm/storage/common_test.go index 74fb00c48d..8ad95bfbce 100644 --- a/swarm/storage/common_test.go +++ b/swarm/storage/common_test.go @@ -266,8 +266,8 @@ func (m *MapChunkStore) Get(_ context.Context, ref Address) (Chunk, error) { return chunk, nil } -// Need to implement HasChunk from SyncChunkStore -func (m *MapChunkStore) HasChunk(ctx context.Context, ref Address) bool { +// Need to implement Has from SyncChunkStore +func (m *MapChunkStore) Has(ctx context.Context, ref Address) bool { m.mu.RLock() defer m.mu.RUnlock() diff --git a/swarm/storage/ldbstore.go b/swarm/storage/ldbstore.go index de6dffa234..f2f32f35b4 100644 --- a/swarm/storage/ldbstore.go +++ b/swarm/storage/ldbstore.go @@ -969,14 +969,18 @@ func (s *LDBStore) Get(_ context.Context, addr Address) (chunk Chunk, err error) return s.get(addr) } -// HasChunk queries the underlying DB if a chunk with the given address is stored +// Has queries the underlying DB if a chunk with the given address is stored // Returns true if the chunk is found, false if not -func (s *LDBStore) HasChunk(_ context.Context, addr Address) bool { +func (s *LDBStore) Has(_ context.Context, addr Address) bool { s.lock.RLock() defer s.lock.RUnlock() - proximity := s.po(addr) - _, found := s.tryAccessIdx(addr, proximity) - return found + + ikey := getIndexKey(addr) + _, err := s.db.Get(ikey) + if err != nil { + return false + } + return true } // TODO: To conform with other private methods of this object indices should not be updated diff --git a/swarm/storage/localstore.go b/swarm/storage/localstore.go index c86dc93411..eefb7565a5 100644 --- a/swarm/storage/localstore.go +++ b/swarm/storage/localstore.go @@ -132,11 +132,11 @@ func (ls *LocalStore) Put(ctx context.Context, chunk Chunk) error { return err } -// HasChunk queries the underlying DbStore if a chunk with the given address +// Has queries the underlying DbStore if a chunk with the given address // is being stored there. // Returns true if it is stored, false if not -func (ls *LocalStore) HasChunk(ctx context.Context, addr Address) bool { - return ls.DbStore.HasChunk(ctx, addr) +func (ls *LocalStore) Has(ctx context.Context, addr Address) bool { + return ls.DbStore.Has(ctx, addr) } // Get(chunk *Chunk) looks up a chunk in the local stores diff --git a/swarm/storage/localstore_test.go b/swarm/storage/localstore_test.go index 6ce2ffe7dd..ec69951c4f 100644 --- a/swarm/storage/localstore_test.go +++ b/swarm/storage/localstore_test.go @@ -210,16 +210,16 @@ func setupLocalStore(t *testing.T, ldbCap int) (ls *LocalStore, cleanup func()) return store, cleanup } -func TestHasChunk(t *testing.T) { +func TestHas(t *testing.T) { ldbCap := defaultGCRatio store, cleanup := setupLocalStore(t, ldbCap) defer cleanup() nonStoredAddr := GenerateRandomChunk(128).Address() - has := store.HasChunk(context.Background(), nonStoredAddr) + has := store.Has(context.Background(), nonStoredAddr) if has { - t.Fatal("Expected HasChunk() to return false, but returned true!") + t.Fatal("Expected Has() to return false, but returned true!") } storeChunks := GenerateRandomChunks(128, 3) @@ -229,16 +229,16 @@ func TestHasChunk(t *testing.T) { t.Fatalf("Expected store to store chunk, but it failed: %v", err) } - has := store.HasChunk(context.Background(), ch.Address()) + has := store.Has(context.Background(), ch.Address()) if !has { - t.Fatal("Expected HasChunk() to return true, but returned false!") + t.Fatal("Expected Has() to return true, but returned false!") } } //let's be paranoic and test again that the non-existent chunk returns false - has = store.HasChunk(context.Background(), nonStoredAddr) + has = store.Has(context.Background(), nonStoredAddr) if has { - t.Fatal("Expected HasChunk() to return false, but returned true!") + t.Fatal("Expected Has() to return false, but returned true!") } } diff --git a/swarm/storage/memstore.go b/swarm/storage/memstore.go index 723bf70000..611ac3bc51 100644 --- a/swarm/storage/memstore.go +++ b/swarm/storage/memstore.go @@ -48,8 +48,8 @@ func NewMemStore(params *StoreParams, _ *LDBStore) (m *MemStore) { } } -// HasChunk needed to implement SyncChunkStore -func (m *MemStore) HasChunk(_ context.Context, addr Address) bool { +// Has needed to implement SyncChunkStore +func (m *MemStore) Has(_ context.Context, addr Address) bool { return m.cache.Contains(addr) } diff --git a/swarm/storage/netstore.go b/swarm/storage/netstore.go index f4ea928083..b24d08bc2d 100644 --- a/swarm/storage/netstore.go +++ b/swarm/storage/netstore.go @@ -158,11 +158,11 @@ func (n *NetStore) get(ctx context.Context, ref Address) (Chunk, func(context.Co return chunk, nil, nil } -// HasChunk is the storage layer entry point to query the underlying +// Has is the storage layer entry point to query the underlying // database to return if it has a chunk or not. // Called from the DebugAPI -func (n *NetStore) HasChunk(ctx context.Context, ref Address) bool { - return n.store.HasChunk(ctx, ref) +func (n *NetStore) Has(ctx context.Context, ref Address) bool { + return n.store.Has(ctx, ref) } // getOrCreateFetcher attempts at retrieving an existing fetchers diff --git a/swarm/storage/types.go b/swarm/storage/types.go index 67cfd994f3..7ec21328e2 100644 --- a/swarm/storage/types.go +++ b/swarm/storage/types.go @@ -292,7 +292,7 @@ func (v *ContentAddressValidator) Validate(chunk Chunk) bool { type ChunkStore interface { Put(ctx context.Context, ch Chunk) (err error) Get(rctx context.Context, ref Address) (ch Chunk, err error) - HasChunk(rctx context.Context, ref Address) bool + Has(rctx context.Context, ref Address) bool Close() } @@ -315,8 +315,8 @@ func (f *FakeChunkStore) Put(_ context.Context, ch Chunk) error { return nil } -// HasChunk doesn't do anything it is just here to implement ChunkStore -func (f *FakeChunkStore) HasChunk(_ context.Context, ref Address) bool { +// Has doesn't do anything it is just here to implement ChunkStore +func (f *FakeChunkStore) Has(_ context.Context, ref Address) bool { panic("FakeChunkStore doesn't support HasChunk") }