Skip to content

Commit

Permalink
Add cmd for db flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
strokovok authored and spilin committed Jul 29, 2024
1 parent 4c41305 commit 66b9b3b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"log"
"os"
"runtime"
"strconv"

"github.com/aurora-is-near/relayer2-base/db/badger/core"
Expand Down Expand Up @@ -109,6 +111,42 @@ func GetBlockCmd() *cobra.Command {
return getLastBlockCmd
}

func FlattenDB() *cobra.Command {
return &cobra.Command{
Use: "flatten-db <dbPath>",
Short: "Command to flatten db (merge all LSM levels) to improve performance",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dbPath := args[0]

log.Printf("Opening database at %s...", dbPath)
config := core.Config{
BadgerConfig: badger.DefaultOptions(dbPath),
GcIntervalSeconds: 60 * 60 * 24 * 365, // We don't want GC to be running during the flattening
}
db, err := core.NewDB(config, codec.NewTinypackCodec())
if err != nil {
return fmt.Errorf("unable to open database: %w", err)
}

defer func() {
log.Printf("Closing database")
if err := db.Close(); err != nil {
log.Printf("Error: unable to close database normally: %v", err)
}
}()

log.Printf("Flattening database, that might take some time...")
threads := runtime.GOMAXPROCS(0)
if err := db.BadgerDB().Flatten(threads); err != nil {
return fmt.Errorf("can't flatten database: %w", err)
}

return nil
},
}
}

// dbView opens db in read-only mode and calls fn with ViewTxn
func dbView(dbPath string, fn func(txn *core.ViewTxn) error) error {
config := core.Config{
Expand Down

0 comments on commit 66b9b3b

Please sign in to comment.