mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
swarm/storage/localstore: writeGCSize function with leveldb batch
This commit is contained in:
parent
5ebbcb58b6
commit
80c269b50d
2 changed files with 27 additions and 15 deletions
|
|
@ -148,7 +148,7 @@ func (db *DB) writeGCSizeWorker() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-db.writeGCSizeTrigger:
|
case <-db.writeGCSizeTrigger:
|
||||||
err := db.writeGCSize()
|
err := db.writeGCSize(atomic.LoadInt64(&db.gcSize))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("localstore write gc size", "err", err)
|
log.Error("localstore write gc size", "err", err)
|
||||||
}
|
}
|
||||||
|
|
@ -167,15 +167,33 @@ func (db *DB) writeGCSizeWorker() {
|
||||||
// It removes all hashes from gcUncountedHashesIndex
|
// It removes all hashes from gcUncountedHashesIndex
|
||||||
// not to include them on the next database initialization
|
// not to include them on the next database initialization
|
||||||
// when gcSize is counted.
|
// when gcSize is counted.
|
||||||
func (db *DB) writeGCSize() (err error) {
|
func (db *DB) writeGCSize(gcSize int64) (err error) {
|
||||||
gcSize := atomic.LoadInt64(&db.gcSize)
|
const maxBatchSize = 1000
|
||||||
err = db.storedGCSize.Put(uint64(gcSize))
|
|
||||||
|
batch := new(leveldb.Batch)
|
||||||
|
db.storedGCSize.PutInBatch(batch, uint64(gcSize))
|
||||||
|
batchSize := 1
|
||||||
|
|
||||||
|
// use only one iterator as it acquires its snapshot
|
||||||
|
// not to remove hashes from index that are added
|
||||||
|
// after stored gc size is written
|
||||||
|
err = db.gcUncountedHashesIndex.IterateAll(func(item shed.Item) (stop bool, err error) {
|
||||||
|
db.gcUncountedHashesIndex.DeleteInBatch(batch, item)
|
||||||
|
batchSize++
|
||||||
|
if batchSize >= maxBatchSize {
|
||||||
|
err = db.shed.WriteBatch(batch)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
batch.Reset()
|
||||||
|
batchSize = 0
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return db.gcUncountedHashesIndex.IterateAll(func(item shed.Item) (stop bool, err error) {
|
return db.shed.WriteBatch(batch)
|
||||||
return false, db.gcUncountedHashesIndex.Delete(item)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// testHookCollectGarbage is a hook that can provide
|
// testHookCollectGarbage is a hook that can provide
|
||||||
|
|
|
||||||
|
|
@ -325,15 +325,9 @@ func New(path string, baseKey []byte, o *Options) (db *DB, err error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
gcSize += uint64(gcUncountedSize)
|
gcSize += uint64(gcUncountedSize)
|
||||||
// remove uncounted hashes from the index
|
// remove uncounted hashes from the index and
|
||||||
err = db.gcUncountedHashesIndex.IterateAll(func(item shed.Item) (stop bool, err error) {
|
|
||||||
return false, db.gcUncountedHashesIndex.Delete(item)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// save the total gcSize after uncounted hashes are removed
|
// save the total gcSize after uncounted hashes are removed
|
||||||
err = db.storedGCSize.Put(gcSize)
|
err = db.writeGCSize(int64(gcSize))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue