Commit a80ab64a authored by acud's avatar acud Committed by GitHub

localstore: dont store chunk if it already exists (#979)

parent 09507fc3
...@@ -165,26 +165,18 @@ func (db *DB) put(mode storage.ModePut, chs ...swarm.Chunk) (exist []bool, err e ...@@ -165,26 +165,18 @@ func (db *DB) put(mode storage.ModePut, chs ...swarm.Chunk) (exist []bool, err e
// The batch can be written to the database. // The batch can be written to the database.
// Provided batch and binID map are updated. // Provided batch and binID map are updated.
func (db *DB) putRequest(batch *leveldb.Batch, binIDs map[uint8]uint64, item shed.Item) (exists bool, gcSizeChange int64, err error) { func (db *DB) putRequest(batch *leveldb.Batch, binIDs map[uint8]uint64, item shed.Item) (exists bool, gcSizeChange int64, err error) {
i, err := db.retrievalDataIndex.Get(item) has, err := db.retrievalDataIndex.Has(item)
switch { if err != nil {
case err == nil:
exists = true
item.StoreTimestamp = i.StoreTimestamp
item.BinID = i.BinID
case errors.Is(err, leveldb.ErrNotFound):
// no chunk accesses
exists = false
default:
return false, 0, err return false, 0, err
} }
if item.StoreTimestamp == 0 { if has {
item.StoreTimestamp = now() return true, 0, nil
} }
if item.BinID == 0 {
item.BinID, err = db.incBinID(binIDs, db.po(swarm.NewAddress(item.Address))) item.StoreTimestamp = now()
if err != nil { item.BinID, err = db.incBinID(binIDs, db.po(swarm.NewAddress(item.Address)))
return false, 0, err if err != nil {
} return false, 0, err
} }
gcSizeChange, err = db.setGC(batch, item) gcSizeChange, err = db.setGC(batch, item)
...@@ -197,7 +189,7 @@ func (db *DB) putRequest(batch *leveldb.Batch, binIDs map[uint8]uint64, item she ...@@ -197,7 +189,7 @@ func (db *DB) putRequest(batch *leveldb.Batch, binIDs map[uint8]uint64, item she
return false, 0, err return false, 0, err
} }
return exists, gcSizeChange, nil return false, gcSizeChange, nil
} }
// putUpload adds an Item to the batch by updating required indexes: // putUpload adds an Item to the batch by updating required indexes:
......
...@@ -73,7 +73,7 @@ func TestModePutRequest(t *testing.T) { ...@@ -73,7 +73,7 @@ func TestModePutRequest(t *testing.T) {
} }
for _, ch := range chunks { for _, ch := range chunks {
newRetrieveIndexesTestWithAccess(db, ch, storeTimestamp, wantTimestamp)(t) newRetrieveIndexesTestWithAccess(db, ch, storeTimestamp, storeTimestamp)(t)
} }
newItemsCountTest(db.gcIndex, tc.count)(t) newItemsCountTest(db.gcIndex, tc.count)(t)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment