diff options
Diffstat (limited to 'swarm/storage/localstore/mode_set.go')
-rw-r--r-- | swarm/storage/localstore/mode_set.go | 63 |
1 files changed, 28 insertions, 35 deletions
diff --git a/swarm/storage/localstore/mode_set.go b/swarm/storage/localstore/mode_set.go index 83fcbea52..14b48a22e 100644 --- a/swarm/storage/localstore/mode_set.go +++ b/swarm/storage/localstore/mode_set.go @@ -17,51 +17,37 @@ package localstore import ( + "context" + "fmt" + "time" + + "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/swarm/chunk" "github.com/syndtr/goleveldb/leveldb" ) -// ModeSet enumerates different Setter modes. -type ModeSet int - -// Setter modes. -const ( - // ModeSetAccess: when an update request is received for a chunk or chunk is retrieved for delivery - ModeSetAccess ModeSet = iota - // ModeSetSync: when push sync receipt is received - ModeSetSync - // modeSetRemove: when GC-d - // unexported as no external packages should remove chunks from database - modeSetRemove -) +// Set updates database indexes for a specific +// chunk represented by the address. +// Set is required to implement chunk.Store +// interface. +func (db *DB) Set(ctx context.Context, mode chunk.ModeSet, addr chunk.Address) (err error) { + metricName := fmt.Sprintf("localstore.Set.%s", mode) -// Setter sets the state of a particular -// Chunk in database by changing indexes. -type Setter struct { - db *DB - mode ModeSet -} + metrics.GetOrRegisterCounter(metricName, nil).Inc(1) + defer totalTimeMetric(metricName, time.Now()) -// NewSetter returns a new Setter on database -// with a specific Mode. -func (db *DB) NewSetter(mode ModeSet) *Setter { - return &Setter{ - mode: mode, - db: db, + err = db.set(mode, addr) + if err != nil { + metrics.GetOrRegisterCounter(metricName+".error", nil).Inc(1) } -} - -// Set updates database indexes for a specific -// chunk represented by the address. -func (s *Setter) Set(addr chunk.Address) (err error) { - return s.db.set(s.mode, addr) + return err } // set updates database indexes for a specific // chunk represented by the address. // It acquires lockAddr to protect two calls // of this function for the same address in parallel. -func (db *DB) set(mode ModeSet, addr chunk.Address) (err error) { +func (db *DB) set(mode chunk.ModeSet, addr chunk.Address) (err error) { // protect parallel updates db.batchMu.Lock() defer db.batchMu.Unlock() @@ -76,7 +62,7 @@ func (db *DB) set(mode ModeSet, addr chunk.Address) (err error) { item := addressToItem(addr) switch mode { - case ModeSetAccess: + case chunk.ModeSetAccess: // add to pull, insert to gc // need to get access timestamp here as it is not @@ -87,9 +73,14 @@ func (db *DB) set(mode ModeSet, addr chunk.Address) (err error) { switch err { case nil: item.StoreTimestamp = i.StoreTimestamp + item.BinID = i.BinID case leveldb.ErrNotFound: db.pushIndex.DeleteInBatch(batch, item) item.StoreTimestamp = now() + item.BinID, err = db.binIDs.Inc(uint64(db.po(item.Address))) + if err != nil { + return err + } default: return err } @@ -112,7 +103,7 @@ func (db *DB) set(mode ModeSet, addr chunk.Address) (err error) { db.gcIndex.PutInBatch(batch, item) gcSizeChange++ - case ModeSetSync: + case chunk.ModeSetSync: // delete from push, insert to gc // need to get access timestamp here as it is not @@ -131,6 +122,7 @@ func (db *DB) set(mode ModeSet, addr chunk.Address) (err error) { return err } item.StoreTimestamp = i.StoreTimestamp + item.BinID = i.BinID i, err = db.retrievalAccessIndex.Get(item) switch err { @@ -149,7 +141,7 @@ func (db *DB) set(mode ModeSet, addr chunk.Address) (err error) { db.gcIndex.PutInBatch(batch, item) gcSizeChange++ - case modeSetRemove: + case chunk.ModeSetRemove: // delete from retrieve, pull, gc // need to get access timestamp here as it is not @@ -169,6 +161,7 @@ func (db *DB) set(mode ModeSet, addr chunk.Address) (err error) { return err } item.StoreTimestamp = i.StoreTimestamp + item.BinID = i.BinID db.retrievalDataIndex.DeleteInBatch(batch, item) db.retrievalAccessIndex.DeleteInBatch(batch, item) |