-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ledgerwatch/erigon-lib/kv" | ||
"github.com/ledgerwatch/erigon-lib/kv/mdbx" | ||
erigonlog "github.com/ledgerwatch/log/v3" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var WatchErigon = &cobra.Command{ | ||
Use: "erigon", | ||
Short: "watch erigon", | ||
Long: "watch erigon", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return watchErigon(args[0]) | ||
}, | ||
} | ||
var ( | ||
keyLimit = uint64(10) | ||
) | ||
|
||
func init() { | ||
WatchErigon.PersistentFlags().Uint64VarP(&keyLimit, "limit", "l", 10, "key limit") | ||
} | ||
|
||
func watchErigon(path string) error { | ||
fmt.Println("walking erigon", path) | ||
lg := erigonlog.New() | ||
lg.SetHandler(erigonlog.StdoutHandler) | ||
rw, err := mdbx.NewMDBX(lg).Path(path).WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg { | ||
defaultBuckets["erigonsystem"] = kv.TableCfgItem{} | ||
return defaultBuckets | ||
}).Readonly().Open(context.Background()) | ||
if err != nil { | ||
return errors.Wrapf(err, "open db: %s", path) | ||
} | ||
defer rw.Close() | ||
return walkdbkv(rw) | ||
} | ||
|
||
func walkdbkv(rw kv.RoDB) error { | ||
tx, err := rw.BeginRo(context.Background()) | ||
if err != nil { | ||
return errors.Wrap(err, "begin ro") | ||
} | ||
defer tx.Rollback() | ||
|
||
dbsize, err := tx.DBSize() | ||
if err != nil { | ||
return errors.Wrap(err, "db size") | ||
} | ||
fmt.Printf("db size: %d\n\n", dbsize) | ||
tables, err := tx.ListBuckets() | ||
if err != nil { | ||
return errors.Wrap(err, "list tables") | ||
} | ||
// fmt.Printf("tables: %v\n", tables) | ||
total := uint64(0) | ||
for _, table := range tables { | ||
tsize, err := tx.BucketSize(table) | ||
if err != nil { | ||
return errors.Wrapf(err, "table size: %s", table) | ||
} | ||
if tsize == 0 { | ||
continue | ||
} | ||
total += tsize | ||
keynum := uint64(0) | ||
err = tx.ForEach(table, nil, func(k, v []byte) error { | ||
if keyLimit == 0 || keynum < keyLimit { | ||
fmt.Printf("table: %s, key: %x, value: %x\n", table, k, v) | ||
} | ||
keynum++ | ||
return nil | ||
}) | ||
if err != nil { | ||
return errors.Wrapf(err, "for each table: %s", table) | ||
} | ||
fmt.Printf("table: %s, size: %d, keynum: %d\n\n", table, tsize, keynum) | ||
} | ||
fmt.Printf("total size used: %d\n", total) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// RootCmd represents the base command when called without any subcommands | ||
var RootCmd = &cobra.Command{ | ||
Use: "dbwatcher", | ||
Short: "Command-line interface for watch of IoTeX blockchain db file", | ||
Long: "dbwatcher is a command-line interface for watch of IoTeX blockchain db file.", | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
func Execute() { | ||
if err := RootCmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(WatchErigon) | ||
|
||
RootCmd.HelpFunc() | ||
} | ||
|
||
func main() { | ||
Execute() | ||
} |