Skip to content

Commit

Permalink
Added flag -page-Id for check sub-command of bbolt CLI.
Browse files Browse the repository at this point in the history
Signed-off-by: ishan16696 <ishan.tyagi@sap.com>
  • Loading branch information
ishan16696 committed Feb 25, 2024
1 parent a6ec26b commit 86ffdcb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
11 changes: 9 additions & 2 deletions cmd/bbolt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,19 @@

- `check` opens a database at a given `[PATH]` and runs an exhaustive check to verify that all pages are accessible or are marked as freed. It also verifies that no pages are double referenced.
- usage:
`bbolt check [path to the bbolt database]`

```bash
bbolt check [options] [path to the bbolt database]
Additional option include:
-page-Id [PageID]
check opens a database at PATH and runs an exhaustive check to verify sub-tree starting from given pageId.
```

Example:

```bash
$bbolt check ~/default.etcd/member/snap/db
$bbolt check -page-Id 0 ~/default.etcd/member/snap/db
ok
```

Expand Down
18 changes: 15 additions & 3 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,17 @@ func newCheckCommand(m *Main) *checkCommand {
return c
}

type checkOptions struct {
pageId uint64
}

// Run executes the command.
func (cmd *checkCommand) Run(args ...string) error {
options := &checkOptions{}

// Parse flags.
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Uint64Var(&options.pageId, "page-Id", 0, "pageID to check sub-tree")
help := fs.Bool("h", false, "")
if err := fs.Parse(args); err != nil {
return err
Expand Down Expand Up @@ -225,7 +232,7 @@ func (cmd *checkCommand) Run(args ...string) error {
// Perform consistency check.
return db.View(func(tx *bolt.Tx) error {
var count int
for err := range tx.Check(bolt.WithKVStringer(CmdKvStringer())) {
for err := range tx.Check(bolt.WithKVStringer(CmdKvStringer(), options.pageId)) {
fmt.Fprintln(cmd.Stdout, err)
count++
}
Expand All @@ -245,11 +252,16 @@ func (cmd *checkCommand) Run(args ...string) error {
// Usage returns the help message.
func (cmd *checkCommand) Usage() string {
return strings.TrimLeft(`
usage: bolt check PATH
usage: bolt check [option] PATH
Check opens a database at PATH and runs an exhaustive check to verify that
all pages are accessible or are marked as freed. It also verifies that no
pages are accessible or are marked as freed. It also verifies that no
pages are double referenced.
Note: If no options is provided in "check" then it will run an exhaustive check on all pages.
Additional option include:
-page-Id [PageID]
check opens a database at PATH and runs an exhaustive check to verify sub-tree starting from given pageId.
Verification errors will stream out as they are found and the process will
return after all pages have been checked.
Expand Down
9 changes: 5 additions & 4 deletions tx_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (tx *Tx) check(cfg checkConfig, ch chan error) {
}
} else {
// Check the db file starting from a specified pageId.
if cfg.pageId < 2 || cfg.pageId >= uint(tx.meta.Pgid()) {
if cfg.pageId < 2 || cfg.pageId >= uint64(tx.meta.Pgid()) {
ch <- fmt.Errorf("page ID (%d) out of range [%d, %d)", cfg.pageId, 2, tx.meta.Pgid())
return
}
Expand Down Expand Up @@ -250,19 +250,20 @@ func verifyKeyOrder(pgId common.Pgid, pageType string, index int, key []byte, pr

type checkConfig struct {
kvStringer KVStringer
pageId uint
pageId uint64
}

type CheckOption func(options *checkConfig)

func WithKVStringer(kvStringer KVStringer) CheckOption {
func WithKVStringer(kvStringer KVStringer, pageID uint64) CheckOption {
return func(c *checkConfig) {
c.kvStringer = kvStringer
c.pageId = pageID
}
}

// WithPageId sets a page ID from which the check command starts to check
func WithPageId(pageId uint) CheckOption {
func WithPageId(pageId uint64) CheckOption {
return func(c *checkConfig) {
c.pageId = pageId
}
Expand Down
6 changes: 3 additions & 3 deletions tx_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestTx_Check_CorruptPage(t *testing.T) {
var cErrs []error

t.Log("Check corrupted page.")
errChan := tx.Check(bbolt.WithPageId(uint(victimPageId)))
errChan := tx.Check(bbolt.WithPageId(uint64(victimPageId)))
for cErr := range errChan {
cErrs = append(cErrs, cErr)
}
Expand All @@ -44,7 +44,7 @@ func TestTx_Check_CorruptPage(t *testing.T) {
t.Log("Check valid pages.")
cErrs = cErrs[:0]
for _, pgId := range validPageIds {
errChan = tx.Check(bbolt.WithPageId(uint(pgId)))
errChan = tx.Check(bbolt.WithPageId(uint64(pgId)))
for cErr := range errChan {
cErrs = append(cErrs, cErr)
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestTx_Check_WithNestBucket(t *testing.T) {
vErr := db.View(func(tx *bbolt.Tx) error {
var cErrs []error

errChan := tx.Check(bbolt.WithPageId(uint(bucketRootPageId)))
errChan := tx.Check(bbolt.WithPageId(uint64(bucketRootPageId)))
for cErr := range errChan {
cErrs = append(cErrs, cErr)
}
Expand Down

0 comments on commit 86ffdcb

Please sign in to comment.