Skip to content

Commit

Permalink
revert the signature of tx.Check and add tx.CheckWithStringer
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <wachao@vmware.com>
  • Loading branch information
ahrtr committed Jan 29, 2023
1 parent afa8b89 commit 35c4569
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/bbolt/main.go
Expand Up @@ -213,7 +213,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(CmdKvStringer()) {
for err := range tx.CheckWithOptions(bolt.WithKVStringer(CmdKvStringer())) {
fmt.Fprintln(cmd.Stdout, err)
count++
}
Expand Down
4 changes: 2 additions & 2 deletions db_test.go
Expand Up @@ -398,7 +398,7 @@ func TestOpen_Check(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err = db.View(func(tx *bolt.Tx) error { return <-tx.Check(bolt.HexKVStringer()) }); err != nil {
if err = db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil {
t.Fatal(err)
}
if err = db.Close(); err != nil {
Expand All @@ -409,7 +409,7 @@ func TestOpen_Check(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *bolt.Tx) error { return <-tx.Check(bolt.HexKVStringer()) }); err != nil {
if err := db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil {
t.Fatal(err)
}
if err := db.Close(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/btesting/btesting.go
Expand Up @@ -119,7 +119,7 @@ func (db *DB) MustCheck() {
err := db.Update(func(tx *bolt.Tx) error {
// Collect all the errors.
var errors []error
for err := range tx.Check(bolt.HexKVStringer()) {
for err := range tx.Check() {
errors = append(errors, err)
if len(errors) > 10 {
break
Expand Down
4 changes: 2 additions & 2 deletions internal/tests/tx_check_test.go
Expand Up @@ -39,7 +39,7 @@ func TestTx_RecursivelyCheckPages_MisplacedPage(t *testing.T) {
require.NoError(t, db.Update(func(tx *bolt.Tx) error {
// Collect all the errors.
var errors []error
for err := range tx.Check(bolt.HexKVStringer()) {
for err := range tx.Check() {
errors = append(errors, err)
}
require.Len(t, errors, 1)
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestTx_RecursivelyCheckPages_CorruptedLeaf(t *testing.T) {
require.NoError(t, db.Update(func(tx *bolt.Tx) error {
// Collect all the errors.
var errors []error
for err := range tx.Check(bolt.HexKVStringer()) {
for err := range tx.Check() {
errors = append(errors, err)
}
require.Len(t, errors, 2)
Expand Down
2 changes: 1 addition & 1 deletion tx.go
Expand Up @@ -200,7 +200,7 @@ func (tx *Tx) Commit() error {

// If strict mode is enabled then perform a consistency check.
if tx.db.StrictMode {
ch := tx.Check(HexKVStringer())
ch := tx.Check()
var errs []string
for {
err, ok := <-ch
Expand Down
29 changes: 27 additions & 2 deletions tx_check.go
Expand Up @@ -13,9 +13,22 @@ import (
// because of caching. This overhead can be removed if running on a read-only
// transaction, however, it is not safe to execute other writer transactions at
// the same time.
func (tx *Tx) Check(kvStringer KVStringer) <-chan error {
func (tx *Tx) Check() <-chan error {
return tx.CheckWithOptions()
}

// CheckWithOptions allows users to provide a customized `KVStringer` implementation,
// so that bolt can generate human-readable diagnostic messages.
func (tx *Tx) CheckWithOptions(options ...CheckOption) <-chan error {
chkConfig := checkConfig{
kvStringer: HexKVStringer(),
}
for _, op := range options {
op(&chkConfig)
}

ch := make(chan error)
go tx.check(kvStringer, ch)
go tx.check(chkConfig.kvStringer, ch)
return ch
}

Expand Down Expand Up @@ -179,6 +192,18 @@ func verifyKeyOrder(pgId pgid, pageType string, index int, key []byte, previousK

// ===========================================================================================

type checkConfig struct {
kvStringer KVStringer
}

type CheckOption func(options *checkConfig)

func WithKVStringer(kvStringer KVStringer) CheckOption {
return func(c *checkConfig) {
c.kvStringer = kvStringer
}
}

// KVStringer allows to prepare human-readable diagnostic messages.
type KVStringer interface {
KeyToString([]byte) string
Expand Down
2 changes: 1 addition & 1 deletion tx_test.go
Expand Up @@ -50,7 +50,7 @@ func TestTx_Check_ReadOnly(t *testing.T) {
numChecks := 2
errc := make(chan error, numChecks)
check := func() {
errc <- <-tx.Check(bolt.HexKVStringer())
errc <- <-tx.Check()
}
// Ensure the freelist is not reloaded and does not race.
for i := 0; i < numChecks; i++ {
Expand Down

0 comments on commit 35c4569

Please sign in to comment.