github.com/ethersphere/bee/v2@v2.2.0/pkg/storage/leveldbstore/batch.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 leveldbstore 6 7 import ( 8 "context" 9 "fmt" 10 "sync" 11 12 "github.com/ethersphere/bee/v2/pkg/storage" 13 ldb "github.com/syndtr/goleveldb/leveldb" 14 ) 15 16 // Batch implements storage.BatchedStore interface Batch method. 17 func (s *Store) Batch(ctx context.Context) storage.Batch { 18 return &Batch{ 19 ctx: ctx, 20 batch: new(ldb.Batch), 21 store: s, 22 } 23 } 24 25 type Batch struct { 26 ctx context.Context 27 28 mu sync.Mutex // mu guards batch and done. 29 batch *ldb.Batch 30 store *Store 31 done bool 32 } 33 34 // Put implements storage.Batch interface Put method. 35 func (i *Batch) Put(item storage.Item) error { 36 if err := i.ctx.Err(); err != nil { 37 return err 38 } 39 40 val, err := item.Marshal() 41 if err != nil { 42 return fmt.Errorf("unable to marshal item: %w", err) 43 } 44 45 i.mu.Lock() 46 i.batch.Put(key(item), val) 47 i.mu.Unlock() 48 49 return nil 50 } 51 52 // Delete implements storage.Batch interface Delete method. 53 func (i *Batch) Delete(item storage.Item) error { 54 if err := i.ctx.Err(); err != nil { 55 return err 56 } 57 58 i.mu.Lock() 59 i.batch.Delete(key(item)) 60 i.mu.Unlock() 61 62 return nil 63 } 64 65 // Commit implements storage.Batch interface Commit method. 66 func (i *Batch) Commit() error { 67 if err := i.ctx.Err(); err != nil { 68 return err 69 } 70 71 i.mu.Lock() 72 defer i.mu.Unlock() 73 74 if i.done { 75 return storage.ErrBatchCommitted 76 } 77 78 if err := i.store.db.Write(i.batch, nil); err != nil { 79 return fmt.Errorf("unable to commit batch: %w", err) 80 } 81 82 i.done = true 83 84 return nil 85 }