github.com/ethersphere/bee/v2@v2.2.0/pkg/storage/chunkstore.go (about) 1 // Copyright 2022 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package storage 6 7 import ( 8 "context" 9 10 "github.com/ethersphere/bee/v2/pkg/swarm" 11 ) 12 13 // Getter is the interface that wraps the basic Get method. 14 type Getter interface { 15 // Get a chunk by its swarm.Address. Returns the chunk associated with 16 // the address alongside with its postage stamp, or a storage.ErrNotFound 17 // if the chunk is not found. 18 // If the chunk has multiple stamps, then the first stamp is returned in this 19 // query. In order to deterministically get the stamp, use the GetterWithStamp 20 // interface. If the chunk is not found storage.ErrNotFound will be returned. 21 Get(context.Context, swarm.Address) (swarm.Chunk, error) 22 } 23 24 // Putter is the interface that wraps the basic Put method. 25 type Putter interface { 26 // Put a chunk into the store alongside with its postage stamp. 27 Put(context.Context, swarm.Chunk) error 28 } 29 30 // Deleter is the interface that wraps the basic Delete method. 31 type Deleter interface { 32 // Delete a chunk by the given swarm.Address. 33 Delete(context.Context, swarm.Address) error 34 } 35 36 // Hasser is the interface that wraps the basic Has method. 37 type Hasser interface { 38 // Has checks whether a chunk exists in the store. 39 Has(context.Context, swarm.Address) (bool, error) 40 } 41 42 // Replacer is the interface that wraps the basic Replace method. 43 type Replacer interface { 44 // Replace a chunk in the store. 45 Replace(context.Context, swarm.Chunk) error 46 } 47 48 // PutterFunc type is an adapter to allow the use of 49 // ChunkStore as Putter interface. If f is a function 50 // with the appropriate signature, PutterFunc(f) is a 51 // Putter that calls f. 52 type PutterFunc func(context.Context, swarm.Chunk) error 53 54 // Put calls f(ctx, chunk). 55 func (f PutterFunc) Put(ctx context.Context, chunk swarm.Chunk) error { 56 return f(ctx, chunk) 57 } 58 59 type GetterFunc func(context.Context, swarm.Address) (swarm.Chunk, error) 60 61 func (f GetterFunc) Get(ctx context.Context, address swarm.Address) (swarm.Chunk, error) { 62 return f(ctx, address) 63 } 64 65 type IterateChunkFn func(swarm.Chunk) (stop bool, err error) 66 67 // ChunkGetterDeleter is a storage that provides 68 // only read and delete operations for chunks. 69 type ChunkGetterDeleter interface { 70 Getter 71 Deleter 72 } 73 74 type ChunkStore interface { 75 Getter 76 Putter 77 Deleter 78 Hasser 79 Replacer 80 81 // Iterate over chunks in no particular order. 82 Iterate(context.Context, IterateChunkFn) error 83 } 84 85 type ReadOnlyChunkStore interface { 86 Getter 87 Hasser 88 }