github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/internal/internal.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 internal 6 7 import ( 8 "bytes" 9 "context" 10 11 "github.com/ethersphere/bee/v2/pkg/storage" 12 "github.com/ethersphere/bee/v2/pkg/storage/inmemchunkstore" 13 "github.com/ethersphere/bee/v2/pkg/storage/inmemstore" 14 "github.com/ethersphere/bee/v2/pkg/storer/internal/transaction" 15 "github.com/ethersphere/bee/v2/pkg/swarm" 16 ) 17 18 // PutterCloserWithReference provides a Putter which can be closed with a root 19 // swarm reference associated with this session. 20 type PutterCloserWithReference interface { 21 Put(context.Context, transaction.Store, swarm.Chunk) error 22 Close(storage.IndexStore, swarm.Address) error 23 Cleanup(transaction.Storage) error 24 } 25 26 var emptyAddr = make([]byte, swarm.HashSize) 27 28 // AddressOrZero returns swarm.ZeroAddress if the buf is of zero bytes. The Zero byte 29 // buffer is used by the items to serialize their contents and if valid swarm.ZeroAddress 30 // entries are allowed. 31 func AddressOrZero(buf []byte) swarm.Address { 32 if bytes.Equal(buf, emptyAddr) { 33 return swarm.ZeroAddress 34 } 35 return swarm.NewAddress(append(make([]byte, 0, swarm.HashSize), buf...)) 36 } 37 38 // AddressBytesOrZero is a helper which creates a zero buffer of swarm.HashSize. This 39 // is required during storing the items in the Store as their serialization formats 40 // are strict. 41 func AddressBytesOrZero(addr swarm.Address) []byte { 42 if addr.IsZero() { 43 return make([]byte, swarm.HashSize) 44 } 45 return addr.Bytes() 46 } 47 48 // NewInmemStorage constructs a inmem Storage implementation which can be used 49 // for the tests in the internal packages. 50 func NewInmemStorage() transaction.Storage { 51 ts := &inmemStorage{ 52 indexStore: inmemstore.New(), 53 chunkStore: inmemchunkstore.New(), 54 } 55 56 return ts 57 } 58 59 type inmemStorage struct { 60 indexStore storage.IndexStore 61 chunkStore storage.ChunkStore 62 } 63 64 func (t *inmemStorage) NewTransaction(ctx context.Context) (transaction.Transaction, func()) { 65 return &inmemTrx{t.indexStore, t.chunkStore}, func() {} 66 } 67 68 type inmemTrx struct { 69 indexStore storage.IndexStore 70 chunkStore storage.ChunkStore 71 } 72 73 func (t *inmemStorage) IndexStore() storage.Reader { return t.indexStore } 74 func (t *inmemStorage) ChunkStore() storage.ReadOnlyChunkStore { return t.chunkStore } 75 76 func (t *inmemTrx) IndexStore() storage.IndexStore { return t.indexStore } 77 func (t *inmemTrx) ChunkStore() storage.ChunkStore { return t.chunkStore } 78 func (t *inmemTrx) Commit() error { return nil } 79 80 func (t *inmemStorage) Close() error { return nil } 81 func (t *inmemStorage) Run(ctx context.Context, f func(s transaction.Store) error) error { 82 trx, done := t.NewTransaction(ctx) 83 defer done() 84 return f(trx) 85 }