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
// The batch can be written to the database.
// 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) {
i, err := db.retrievalDataIndex.Get(item)
switch {
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:
has, err := db.retrievalDataIndex.Has(item)
if err != nil {
return false, 0, err
}
if item.StoreTimestamp == 0 {
item.StoreTimestamp = now()
if has {
return true, 0, nil
}
if item.BinID == 0 {
item.BinID, err = db.incBinID(binIDs, db.po(swarm.NewAddress(item.Address)))
if err != nil {
return false, 0, err
}
item.StoreTimestamp = now()
item.BinID, err = db.incBinID(binIDs, db.po(swarm.NewAddress(item.Address)))
if err != nil {
return false, 0, err
}
gcSizeChange, err = db.setGC(batch, item)
......@@ -197,7 +189,7 @@ func (db *DB) putRequest(batch *leveldb.Batch, binIDs map[uint8]uint64, item she
return false, 0, err
}
return exists, gcSizeChange, nil
return false, gcSizeChange, nil
}
// putUpload adds an Item to the batch by updating required indexes:
......
......@@ -73,7 +73,7 @@ func TestModePutRequest(t *testing.T) {
}
for _, ch := range chunks {
newRetrieveIndexesTestWithAccess(db, ch, storeTimestamp, wantTimestamp)(t)
newRetrieveIndexesTestWithAccess(db, ch, storeTimestamp, storeTimestamp)(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