Skip to content

Commit 23d9b0f

Browse files
committed
cmd/geth: add db commands: stats, compact, put, get, delete
1 parent 7a800f9 commit 23d9b0f

File tree

3 files changed

+355
-126
lines changed

3 files changed

+355
-126
lines changed

cmd/geth/chaincmd.go

Lines changed: 3 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"os"
23-
"path/filepath"
2423
"runtime"
2524
"strconv"
2625
"sync/atomic"
2726
"time"
2827

2928
"github.com/ethereum/go-ethereum/cmd/utils"
3029
"github.com/ethereum/go-ethereum/common"
31-
"github.com/ethereum/go-ethereum/console/prompt"
3230
"github.com/ethereum/go-ethereum/core"
3331
"github.com/ethereum/go-ethereum/core/rawdb"
3432
"github.com/ethereum/go-ethereum/core/state"
@@ -170,18 +168,6 @@ The export-preimages command export hash preimages to an RLP encoded stream`,
170168
Category: "BLOCKCHAIN COMMANDS",
171169
Description: `
172170
The first argument must be the directory containing the blockchain to download from`,
173-
}
174-
removedbCommand = cli.Command{
175-
Action: utils.MigrateFlags(removeDB),
176-
Name: "removedb",
177-
Usage: "Remove blockchain and state databases",
178-
ArgsUsage: " ",
179-
Flags: []cli.Flag{
180-
utils.DataDirFlag,
181-
},
182-
Category: "BLOCKCHAIN COMMANDS",
183-
Description: `
184-
Remove blockchain and state databases`,
185171
}
186172
dumpCommand = cli.Command{
187173
Action: utils.MigrateFlags(dump),
@@ -202,25 +188,6 @@ Remove blockchain and state databases`,
202188
The arguments are interpreted as block numbers or hashes.
203189
Use "ethereum dump 0" to dump the genesis block.`,
204190
}
205-
inspectCommand = cli.Command{
206-
Action: utils.MigrateFlags(inspect),
207-
Name: "inspect",
208-
Usage: "Inspect the storage size for each type of data in the database",
209-
ArgsUsage: " ",
210-
Flags: []cli.Flag{
211-
utils.DataDirFlag,
212-
utils.AncientFlag,
213-
utils.CacheFlag,
214-
utils.MainnetFlag,
215-
utils.RopstenFlag,
216-
utils.RinkebyFlag,
217-
utils.GoerliFlag,
218-
utils.YoloV3Flag,
219-
utils.LegacyTestnetFlag,
220-
utils.SyncModeFlag,
221-
},
222-
Category: "BLOCKCHAIN COMMANDS",
223-
}
224191
)
225192

226193
// initGenesis will initialise the given JSON format genesis file and writes it as
@@ -323,17 +290,7 @@ func importChain(ctx *cli.Context) error {
323290
fmt.Printf("Import done in %v.\n\n", time.Since(start))
324291

325292
// Output pre-compaction stats mostly to see the import trashing
326-
stats, err := db.Stat("leveldb.stats")
327-
if err != nil {
328-
utils.Fatalf("Failed to read database stats: %v", err)
329-
}
330-
fmt.Println(stats)
331-
332-
ioStats, err := db.Stat("leveldb.iostats")
333-
if err != nil {
334-
utils.Fatalf("Failed to read database iostats: %v", err)
335-
}
336-
fmt.Println(ioStats)
293+
showLeveldbStats(db)
337294

338295
// Print the memory statistics used by the importing
339296
mem := new(runtime.MemStats)
@@ -351,22 +308,12 @@ func importChain(ctx *cli.Context) error {
351308
// Compact the entire database to more accurately measure disk io and print the stats
352309
start = time.Now()
353310
fmt.Println("Compacting entire database...")
354-
if err = db.Compact(nil, nil); err != nil {
311+
if err := db.Compact(nil, nil); err != nil {
355312
utils.Fatalf("Compaction failed: %v", err)
356313
}
357314
fmt.Printf("Compaction done in %v.\n\n", time.Since(start))
358315

359-
stats, err = db.Stat("leveldb.stats")
360-
if err != nil {
361-
utils.Fatalf("Failed to read database stats: %v", err)
362-
}
363-
fmt.Println(stats)
364-
365-
ioStats, err = db.Stat("leveldb.iostats")
366-
if err != nil {
367-
utils.Fatalf("Failed to read database iostats: %v", err)
368-
}
369-
fmt.Println(ioStats)
316+
showLeveldbStats(db)
370317
return importErr
371318
}
372319

@@ -499,66 +446,6 @@ func copyDb(ctx *cli.Context) error {
499446
return nil
500447
}
501448

502-
func removeDB(ctx *cli.Context) error {
503-
stack, config := makeConfigNode(ctx)
504-
505-
// Remove the full node state database
506-
path := stack.ResolvePath("chaindata")
507-
if common.FileExist(path) {
508-
confirmAndRemoveDB(path, "full node state database")
509-
} else {
510-
log.Info("Full node state database missing", "path", path)
511-
}
512-
// Remove the full node ancient database
513-
path = config.Eth.DatabaseFreezer
514-
switch {
515-
case path == "":
516-
path = filepath.Join(stack.ResolvePath("chaindata"), "ancient")
517-
case !filepath.IsAbs(path):
518-
path = config.Node.ResolvePath(path)
519-
}
520-
if common.FileExist(path) {
521-
confirmAndRemoveDB(path, "full node ancient database")
522-
} else {
523-
log.Info("Full node ancient database missing", "path", path)
524-
}
525-
// Remove the light node database
526-
path = stack.ResolvePath("lightchaindata")
527-
if common.FileExist(path) {
528-
confirmAndRemoveDB(path, "light node database")
529-
} else {
530-
log.Info("Light node database missing", "path", path)
531-
}
532-
return nil
533-
}
534-
535-
// confirmAndRemoveDB prompts the user for a last confirmation and removes the
536-
// folder if accepted.
537-
func confirmAndRemoveDB(database string, kind string) {
538-
confirm, err := prompt.Stdin.PromptConfirm(fmt.Sprintf("Remove %s (%s)?", kind, database))
539-
switch {
540-
case err != nil:
541-
utils.Fatalf("%v", err)
542-
case !confirm:
543-
log.Info("Database deletion skipped", "path", database)
544-
default:
545-
start := time.Now()
546-
filepath.Walk(database, func(path string, info os.FileInfo, err error) error {
547-
// If we're at the top level folder, recurse into
548-
if path == database {
549-
return nil
550-
}
551-
// Delete all the files, but not subfolders
552-
if !info.IsDir() {
553-
os.Remove(path)
554-
return nil
555-
}
556-
return filepath.SkipDir
557-
})
558-
log.Info("Database successfully deleted", "path", database, "elapsed", common.PrettyDuration(time.Since(start)))
559-
}
560-
}
561-
562449
func dump(ctx *cli.Context) error {
563450
stack, _ := makeConfigNode(ctx)
564451
defer stack.Close()
@@ -598,16 +485,6 @@ func dump(ctx *cli.Context) error {
598485
return nil
599486
}
600487

601-
func inspect(ctx *cli.Context) error {
602-
node, _ := makeConfigNode(ctx)
603-
defer node.Close()
604-
605-
_, chainDb := utils.MakeChain(ctx, node, true)
606-
defer chainDb.Close()
607-
608-
return rawdb.InspectDatabase(chainDb)
609-
}
610-
611488
// hashish returns true for strings that look like hashes.
612489
func hashish(x string) bool {
613490
_, err := strconv.Atoi(x)

0 commit comments

Comments
 (0)