cmd/blockstore/yaml: accept --slots=all

Adds option to dump all slots
This commit is contained in:
Richard Patel 2022-09-11 09:01:29 +02:00
parent d7cd878bc9
commit 0773c47d05
1 changed files with 36 additions and 7 deletions

View File

@ -33,7 +33,13 @@ func init() {
Cmd.Run = run
}
func run(_ *cobra.Command, args []string) {
func run(c *cobra.Command, args []string) {
go func() {
// No need for clean shutdown, exit quickly
<-c.Context().Done()
os.Exit(0)
}()
rocksDB := args[0]
printColumnFamilies(rocksDB)
@ -46,12 +52,16 @@ func run(_ *cobra.Command, args []string) {
printRoot(db)
slots, ok := util.ParseInts(*flagSlots)
if !ok {
klog.Exitf("Invalid slots specifier: %s", *flagSlots)
}
if len(slots) > 0 {
dumpSlots(db, slots)
if *flagSlots == "all" {
dumpAllSlots(db)
} else {
slots, ok := util.ParseInts(*flagSlots)
if !ok {
klog.Exitf("Invalid slots specifier: %s", *flagSlots)
}
if len(slots) > 0 {
dumpSlots(db, slots)
}
}
if klog.Stats.Error.Lines() > 0 {
@ -81,6 +91,25 @@ func printRoot(db *blockstore.DB) {
fmt.Println("root:", root)
}
func dumpAllSlots(db *blockstore.DB) {
iter := db.DB.NewIteratorCF(grocksdb.NewDefaultReadOptions(), db.CfMeta)
iter.SeekToFirst()
defer iter.Close()
hasHeader := false
for iter.Valid() {
if !hasHeader {
fmt.Println("slots:")
hasHeader = true
}
slot, ok := blockstore.ParseSlotKey(iter.Key().Data())
if !ok {
continue
}
dumpSlot(db, slot)
iter.Next()
}
}
func dumpSlots(db *blockstore.DB, slots util.Ints) {
fmt.Println("slots:")
slots.Iter(func(slot uint64) bool {