From 01ce9b5c44fa531fd6e81e7c93ada3daaea64552 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 11 Mar 2021 11:45:05 +0200 Subject: [PATCH] add Compact to badger blockstore --- blockstore/badger/blockstore.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/blockstore/badger/blockstore.go b/blockstore/badger/blockstore.go index 2c00f4240..7c1615711 100644 --- a/blockstore/badger/blockstore.go +++ b/blockstore/badger/blockstore.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "runtime" "sync/atomic" "github.com/dgraph-io/badger/v2" @@ -150,6 +151,20 @@ func (b *Blockstore) CollectGarbage() error { return err } +// Compact runs a synchronous compaction +func (b *Blockstore) Compact() error { + if atomic.LoadInt64(&b.state) != stateOpen { + return ErrBlockstoreClosed + } + + nworkers := runtime.NumCPU() / 2 + if nworkers < 2 { + nworkers = 2 + } + + return b.DB.Flatten(nworkers) +} + // View implements blockstore.Viewer, which leverages zero-copy read-only // access to values. func (b *Blockstore) View(cid cid.Cid, fn func([]byte) error) error {