Skip to content

cmd/XDC: add db commands: stats, compact, put, get, delete #821

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

Merged
merged 1 commit into from
Jan 24, 2025
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
84 changes: 11 additions & 73 deletions cmd/XDC/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/XinFinOrg/XDPoSChain/cmd/utils"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/console"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types"
Expand Down Expand Up @@ -132,19 +131,6 @@ if already existing.`,
},
Description: `
The export-preimages command export hash preimages to an RLP encoded stream`,
}
removedbCommand = &cli.Command{
Action: removeDB,
Name: "removedb",
Usage: "Remove blockchain and state databases",
ArgsUsage: " ",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.XDCXDataDirFlag,
utils.LightModeFlag,
},
Description: `
Remove blockchain and state databases`,
}
dumpCommand = &cli.Command{
Action: dump,
Expand Down Expand Up @@ -224,8 +210,8 @@ func importChain(ctx *cli.Context) error {
// Start metrics export if enabled
utils.SetupMetrics(&cfg.Metrics)

chain, chainDb := utils.MakeChain(ctx, stack, false)
defer chainDb.Close()
chain, db := utils.MakeChain(ctx, stack, false)
defer db.Close()

// Start periodically gathering memory profiles
var peakMemAlloc, peakMemSys uint64
Expand All @@ -245,13 +231,17 @@ func importChain(ctx *cli.Context) error {
// Import the chain
start := time.Now()

var importErr error

if ctx.Args().Len() == 1 {
if err := utils.ImportChain(chain, ctx.Args().First()); err != nil {
importErr = err
log.Error("Import error", "err", err)
}
} else {
for _, arg := range ctx.Args().Slice() {
if err := utils.ImportChain(chain, arg); err != nil {
importErr = err
log.Error("Import error", "file", arg, "err", err)
}
}
Expand All @@ -260,19 +250,7 @@ func importChain(ctx *cli.Context) error {
fmt.Printf("Import done in %v.\n\n", time.Since(start))

// Output pre-compaction stats mostly to see the import trashing
db := chainDb

stats, err := db.Get([]byte("leveldb.stats"))
if err != nil {
utils.Fatalf("Failed to read database stats: %v", err)
}
fmt.Println(stats)

ioStats, err := db.Get([]byte("leveldb.iostats"))
if err != nil {
utils.Fatalf("Failed to read database iostats: %v", err)
}
fmt.Println(ioStats)
showLeveldbStats(db)

// Print the memory statistics used by the importing
mem := new(runtime.MemStats)
Expand All @@ -283,31 +261,20 @@ func importChain(ctx *cli.Context) error {
fmt.Printf("Allocations: %.3f million\n", float64(mem.Mallocs)/1000000)
fmt.Printf("GC pause: %v\n\n", time.Duration(mem.PauseTotalNs))

if ctx.IsSet(utils.NoCompactionFlag.Name) {
if ctx.Bool(utils.NoCompactionFlag.Name) {
return nil
}

// Compact the entire database to more accurately measure disk io and print the stats
start = time.Now()
fmt.Println("Compacting entire database...")
if err = db.Compact(nil, nil); err != nil {
if err := db.Compact(nil, nil); err != nil {
utils.Fatalf("Compaction failed: %v", err)
}
fmt.Printf("Compaction done in %v.\n\n", time.Since(start))

stats, err = db.Get([]byte("leveldb.stats"))
if err != nil {
utils.Fatalf("Failed to read database stats: %v", err)
}
fmt.Println(stats)

ioStats, err = db.Get([]byte("leveldb.iostats"))
if err != nil {
utils.Fatalf("Failed to read database iostats: %v", err)
}
fmt.Println(ioStats)

return nil
showLeveldbStats(db)
return importErr
}

func exportChain(ctx *cli.Context) error {
Expand Down Expand Up @@ -385,35 +352,6 @@ func exportPreimages(ctx *cli.Context) error {
return nil
}

func removeDB(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)

for _, name := range []string{"chaindata", "lightchaindata"} {
// Ensure the database exists in the first place
logger := log.New("database", name)

dbdir := stack.ResolvePath(name)
if !common.FileExist(dbdir) {
logger.Info("Database doesn't exist, skipping", "path", dbdir)
continue
}
// Confirm removal and execute
fmt.Println(dbdir)
confirm, err := console.Stdin.PromptConfirm("Remove this database?")
switch {
case err != nil:
utils.Fatalf("%v", err)
case !confirm:
logger.Warn("Database deletion aborted")
default:
start := time.Now()
os.RemoveAll(dbdir)
logger.Info("Database successfully deleted", "elapsed", common.PrettyDuration(time.Since(start)))
}
}
return nil
}

func dump(ctx *cli.Context) error {
stack, _ := makeFullNode(ctx)
defer stack.Close()
Expand Down
Loading
Loading