Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

truncate-database command line utility #107

Merged
merged 5 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/util/cmd/common/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import (
)

func InitStorage(datadir string) *badger.DB {
return InitStorageWithTruncate(datadir, false)
}

func InitStorageWithTruncate(datadir string, truncate bool) *badger.DB {
opts := badger.
DefaultOptions(datadir).
WithKeepL0InMemory(true).
WithLogger(nil)
WithLogger(nil).
WithTruncate(truncate)

db, err := badger.Open(opts)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/util/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
extract "github.com/onflow/flow-go/cmd/util/cmd/execution-state-extract"
"github.com/onflow/flow-go/cmd/util/cmd/find-block"
"github.com/onflow/flow-go/cmd/util/cmd/read-execution-state"
truncate_database "github.com/onflow/flow-go/cmd/util/cmd/truncate-database"
)

var (
Expand Down Expand Up @@ -52,6 +53,7 @@ func addCommands() {
rootCmd.AddCommand(find.Cmd)
rootCmd.AddCommand(read.Cmd)
rootCmd.AddCommand(checkpoint_list_tries.Cmd)
rootCmd.AddCommand(truncate_database.Cmd)
}

func initConfig() {
Expand Down
37 changes: 37 additions & 0 deletions cmd/util/cmd/truncate-database/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package truncate_database

import (
"github.com/rs/zerolog/log"

"github.com/spf13/cobra"

"github.com/onflow/flow-go/cmd/util/cmd/common"
)

var (
flagDatadir string
)

var Cmd = &cobra.Command{
Use: "truncate-database",
Short: "Truncates protocol state database (Possible data loss!)",
Run: run,
}

func init() {

Cmd.Flags().StringVar(&flagDatadir, "datadir", "",
"directory that stores the protocol state")
_ = Cmd.MarkFlagRequired("datadir")

}

func run(*cobra.Command, []string) {

log.Info().Msg("Opening database with truncate")

db := common.InitStorageWithTruncate(flagDatadir, true)
defer db.Close()

log.Info().Msg("Truncated")
}