mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/storage: HasChunk -> Has and implementation with no access count change
This commit is contained in:
parent
fc919f562c
commit
9d4f27277e
7 changed files with 29 additions and 25 deletions
|
|
@ -266,8 +266,8 @@ func (m *MapChunkStore) Get(_ context.Context, ref Address) (Chunk, error) {
|
||||||
return chunk, nil
|
return chunk, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Need to implement HasChunk from SyncChunkStore
|
// Need to implement Has from SyncChunkStore
|
||||||
func (m *MapChunkStore) HasChunk(ctx context.Context, ref Address) bool {
|
func (m *MapChunkStore) Has(ctx context.Context, ref Address) bool {
|
||||||
m.mu.RLock()
|
m.mu.RLock()
|
||||||
defer m.mu.RUnlock()
|
defer m.mu.RUnlock()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -969,14 +969,18 @@ func (s *LDBStore) Get(_ context.Context, addr Address) (chunk Chunk, err error)
|
||||||
return s.get(addr)
|
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
|
// 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()
|
s.lock.RLock()
|
||||||
defer s.lock.RUnlock()
|
defer s.lock.RUnlock()
|
||||||
proximity := s.po(addr)
|
|
||||||
_, found := s.tryAccessIdx(addr, proximity)
|
ikey := getIndexKey(addr)
|
||||||
return found
|
_, 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
|
// TODO: To conform with other private methods of this object indices should not be updated
|
||||||
|
|
|
||||||
|
|
@ -132,11 +132,11 @@ func (ls *LocalStore) Put(ctx context.Context, chunk Chunk) error {
|
||||||
return err
|
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.
|
// is being stored there.
|
||||||
// Returns true if it is stored, false if not
|
// Returns true if it is stored, false if not
|
||||||
func (ls *LocalStore) HasChunk(ctx context.Context, addr Address) bool {
|
func (ls *LocalStore) Has(ctx context.Context, addr Address) bool {
|
||||||
return ls.DbStore.HasChunk(ctx, addr)
|
return ls.DbStore.Has(ctx, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get(chunk *Chunk) looks up a chunk in the local stores
|
// Get(chunk *Chunk) looks up a chunk in the local stores
|
||||||
|
|
|
||||||
|
|
@ -210,16 +210,16 @@ func setupLocalStore(t *testing.T, ldbCap int) (ls *LocalStore, cleanup func())
|
||||||
return store, cleanup
|
return store, cleanup
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHasChunk(t *testing.T) {
|
func TestHas(t *testing.T) {
|
||||||
ldbCap := defaultGCRatio
|
ldbCap := defaultGCRatio
|
||||||
store, cleanup := setupLocalStore(t, ldbCap)
|
store, cleanup := setupLocalStore(t, ldbCap)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
nonStoredAddr := GenerateRandomChunk(128).Address()
|
nonStoredAddr := GenerateRandomChunk(128).Address()
|
||||||
|
|
||||||
has := store.HasChunk(context.Background(), nonStoredAddr)
|
has := store.Has(context.Background(), nonStoredAddr)
|
||||||
if has {
|
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)
|
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)
|
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 {
|
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
|
//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 {
|
if has {
|
||||||
t.Fatal("Expected HasChunk() to return false, but returned true!")
|
t.Fatal("Expected Has() to return false, but returned true!")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,8 @@ func NewMemStore(params *StoreParams, _ *LDBStore) (m *MemStore) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasChunk needed to implement SyncChunkStore
|
// Has needed to implement SyncChunkStore
|
||||||
func (m *MemStore) HasChunk(_ context.Context, addr Address) bool {
|
func (m *MemStore) Has(_ context.Context, addr Address) bool {
|
||||||
return m.cache.Contains(addr)
|
return m.cache.Contains(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -158,11 +158,11 @@ func (n *NetStore) get(ctx context.Context, ref Address) (Chunk, func(context.Co
|
||||||
return chunk, nil, nil
|
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.
|
// database to return if it has a chunk or not.
|
||||||
// Called from the DebugAPI
|
// Called from the DebugAPI
|
||||||
func (n *NetStore) HasChunk(ctx context.Context, ref Address) bool {
|
func (n *NetStore) Has(ctx context.Context, ref Address) bool {
|
||||||
return n.store.HasChunk(ctx, ref)
|
return n.store.Has(ctx, ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getOrCreateFetcher attempts at retrieving an existing fetchers
|
// getOrCreateFetcher attempts at retrieving an existing fetchers
|
||||||
|
|
|
||||||
|
|
@ -292,7 +292,7 @@ func (v *ContentAddressValidator) Validate(chunk Chunk) bool {
|
||||||
type ChunkStore interface {
|
type ChunkStore interface {
|
||||||
Put(ctx context.Context, ch Chunk) (err error)
|
Put(ctx context.Context, ch Chunk) (err error)
|
||||||
Get(rctx context.Context, ref Address) (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()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -315,8 +315,8 @@ func (f *FakeChunkStore) Put(_ context.Context, ch Chunk) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasChunk doesn't do anything it is just here to implement ChunkStore
|
// Has doesn't do anything it is just here to implement ChunkStore
|
||||||
func (f *FakeChunkStore) HasChunk(_ context.Context, ref Address) bool {
|
func (f *FakeChunkStore) Has(_ context.Context, ref Address) bool {
|
||||||
panic("FakeChunkStore doesn't support HasChunk")
|
panic("FakeChunkStore doesn't support HasChunk")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue