2019-02-07 17:40:26 +00:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package localstore
|
|
|
|
|
|
|
|
import (
|
2019-04-10 14:50:58 +00:00
|
|
|
"context"
|
2019-04-25 08:22:57 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2019-04-10 14:50:58 +00:00
|
|
|
|
2019-04-25 08:22:57 +00:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
2019-02-26 15:09:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/chunk"
|
2019-02-07 17:40:26 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Set updates database indexes for a specific
|
|
|
|
// chunk represented by the address.
|
2019-04-10 14:50:58 +00:00
|
|
|
// Set is required to implement chunk.Store
|
|
|
|
// interface.
|
2019-04-25 08:22:57 +00:00
|
|
|
func (db *DB) Set(ctx context.Context, mode chunk.ModeSet, addr chunk.Address) (err error) {
|
|
|
|
metricName := fmt.Sprintf("localstore.Set.%s", mode)
|
|
|
|
|
|
|
|
metrics.GetOrRegisterCounter(metricName, nil).Inc(1)
|
|
|
|
defer totalTimeMetric(metricName, time.Now())
|
|
|
|
|
|
|
|
err = db.set(mode, addr)
|
|
|
|
if err != nil {
|
|
|
|
metrics.GetOrRegisterCounter(metricName+".error", nil).Inc(1)
|
|
|
|
}
|
|
|
|
return err
|
2019-02-07 17:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-04-10 14:50:58 +00:00
|
|
|
func (db *DB) set(mode chunk.ModeSet, addr chunk.Address) (err error) {
|
2019-02-07 17:40:26 +00:00
|
|
|
// protect parallel updates
|
2019-03-08 23:06:39 +00:00
|
|
|
db.batchMu.Lock()
|
|
|
|
defer db.batchMu.Unlock()
|
2019-02-07 17:40:26 +00:00
|
|
|
|
|
|
|
batch := new(leveldb.Batch)
|
|
|
|
|
|
|
|
// variables that provide information for operations
|
|
|
|
// to be done after write batch function successfully executes
|
|
|
|
var gcSizeChange int64 // number to add or subtract from gcSize
|
|
|
|
var triggerPullFeed bool // signal pull feed subscriptions to iterate
|
|
|
|
|
|
|
|
item := addressToItem(addr)
|
|
|
|
|
|
|
|
switch mode {
|
2019-04-10 14:50:58 +00:00
|
|
|
case chunk.ModeSetAccess:
|
2019-02-07 17:40:26 +00:00
|
|
|
// add to pull, insert to gc
|
|
|
|
|
|
|
|
// need to get access timestamp here as it is not
|
|
|
|
// provided by the access function, and it is not
|
|
|
|
// a property of a chunk provided to Accessor.Put.
|
|
|
|
|
|
|
|
i, err := db.retrievalDataIndex.Get(item)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
item.StoreTimestamp = i.StoreTimestamp
|
2019-04-10 14:50:58 +00:00
|
|
|
item.BinID = i.BinID
|
2019-02-07 17:40:26 +00:00
|
|
|
case leveldb.ErrNotFound:
|
|
|
|
db.pushIndex.DeleteInBatch(batch, item)
|
|
|
|
item.StoreTimestamp = now()
|
2019-04-10 14:50:58 +00:00
|
|
|
item.BinID, err = db.binIDs.Inc(uint64(db.po(item.Address)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-07 17:40:26 +00:00
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i, err = db.retrievalAccessIndex.Get(item)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
item.AccessTimestamp = i.AccessTimestamp
|
|
|
|
db.gcIndex.DeleteInBatch(batch, item)
|
|
|
|
gcSizeChange--
|
|
|
|
case leveldb.ErrNotFound:
|
|
|
|
// the chunk is not accessed before
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
item.AccessTimestamp = now()
|
|
|
|
db.retrievalAccessIndex.PutInBatch(batch, item)
|
|
|
|
db.pullIndex.PutInBatch(batch, item)
|
|
|
|
triggerPullFeed = true
|
|
|
|
db.gcIndex.PutInBatch(batch, item)
|
|
|
|
gcSizeChange++
|
|
|
|
|
2019-04-10 14:50:58 +00:00
|
|
|
case chunk.ModeSetSync:
|
2019-02-07 17:40:26 +00:00
|
|
|
// delete from push, insert to gc
|
|
|
|
|
|
|
|
// need to get access timestamp here as it is not
|
|
|
|
// provided by the access function, and it is not
|
|
|
|
// a property of a chunk provided to Accessor.Put.
|
|
|
|
i, err := db.retrievalDataIndex.Get(item)
|
|
|
|
if err != nil {
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
// chunk is not found,
|
|
|
|
// no need to update gc index
|
|
|
|
// just delete from the push index
|
|
|
|
// if it is there
|
|
|
|
db.pushIndex.DeleteInBatch(batch, item)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
item.StoreTimestamp = i.StoreTimestamp
|
2019-04-10 14:50:58 +00:00
|
|
|
item.BinID = i.BinID
|
2019-02-07 17:40:26 +00:00
|
|
|
|
|
|
|
i, err = db.retrievalAccessIndex.Get(item)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
item.AccessTimestamp = i.AccessTimestamp
|
|
|
|
db.gcIndex.DeleteInBatch(batch, item)
|
|
|
|
gcSizeChange--
|
|
|
|
case leveldb.ErrNotFound:
|
|
|
|
// the chunk is not accessed before
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
item.AccessTimestamp = now()
|
|
|
|
db.retrievalAccessIndex.PutInBatch(batch, item)
|
|
|
|
db.pushIndex.DeleteInBatch(batch, item)
|
|
|
|
db.gcIndex.PutInBatch(batch, item)
|
|
|
|
gcSizeChange++
|
|
|
|
|
2019-04-10 14:50:58 +00:00
|
|
|
case chunk.ModeSetRemove:
|
2019-02-07 17:40:26 +00:00
|
|
|
// delete from retrieve, pull, gc
|
|
|
|
|
|
|
|
// need to get access timestamp here as it is not
|
|
|
|
// provided by the access function, and it is not
|
|
|
|
// a property of a chunk provided to Accessor.Put.
|
|
|
|
|
|
|
|
i, err := db.retrievalAccessIndex.Get(item)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
item.AccessTimestamp = i.AccessTimestamp
|
|
|
|
case leveldb.ErrNotFound:
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i, err = db.retrievalDataIndex.Get(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
item.StoreTimestamp = i.StoreTimestamp
|
2019-04-10 14:50:58 +00:00
|
|
|
item.BinID = i.BinID
|
2019-02-07 17:40:26 +00:00
|
|
|
|
|
|
|
db.retrievalDataIndex.DeleteInBatch(batch, item)
|
|
|
|
db.retrievalAccessIndex.DeleteInBatch(batch, item)
|
|
|
|
db.pullIndex.DeleteInBatch(batch, item)
|
|
|
|
db.gcIndex.DeleteInBatch(batch, item)
|
|
|
|
// a check is needed for decrementing gcSize
|
|
|
|
// as delete is not reporting if the key/value pair
|
|
|
|
// is deleted or not
|
|
|
|
if _, err := db.gcIndex.Get(item); err == nil {
|
|
|
|
gcSizeChange = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ErrInvalidMode
|
|
|
|
}
|
|
|
|
|
2019-03-08 23:06:39 +00:00
|
|
|
err = db.incGCSizeInBatch(batch, gcSizeChange)
|
2019-02-07 17:40:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-08 23:06:39 +00:00
|
|
|
|
|
|
|
err = db.shed.WriteBatch(batch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-07 17:40:26 +00:00
|
|
|
}
|
|
|
|
if triggerPullFeed {
|
|
|
|
db.triggerPullSubscriptions(db.po(item.Address))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|