Skip to content

Commit

Permalink
fix: change pageItemCommand default format auto
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Elbehery <[email protected]>
  • Loading branch information
Elbehery committed Nov 6, 2023
1 parent 89a5a72 commit f42d8cb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (cmd *pageItemCommand) Run(args ...string) error {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.BoolVar(&options.keyOnly, "key-only", false, "Print only the key")
fs.BoolVar(&options.valueOnly, "value-only", false, "Print only the value")
fs.StringVar(&options.format, "format", "ascii-encoded", "Output format. One of: "+FORMAT_MODES)
fs.StringVar(&options.format, "format", "auto", "Output format. One of: "+FORMAT_MODES)
fs.BoolVar(&options.help, "h", false, "")
if err := fs.Parse(args); err != nil {
return err
Expand Down
92 changes: 67 additions & 25 deletions cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,70 @@ func TestPageCommand_Run(t *testing.T) {
}

func TestPageItemCommand_Run(t *testing.T) {
db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})
srcPath := db.Path()

// Insert some sample data
t.Log("Insert some sample data")
err := db.Fill([]byte("data"), 1, 100,
func(tx int, k int) []byte { return []byte(fmt.Sprintf("key_%d", k)) },
func(tx int, k int) []byte { return []byte(fmt.Sprintf("value_%d", k)) },
)
require.NoError(t, err)
t.Run("test data in string format", func(t *testing.T) {
db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})
srcPath := db.Path()

// Insert some sample data
t.Log("Insert some sample data")
err := db.Fill([]byte("data"), 1, 100,
func(tx int, k int) []byte { return []byte(fmt.Sprintf("key_%d", k)) },
func(tx int, k int) []byte { return []byte(fmt.Sprintf("value_%d", k)) },
)
require.NoError(t, err)

defer requireDBNoChange(t, dbData(t, srcPath), srcPath)
defer requireDBNoChange(t, dbData(t, srcPath), srcPath)

meta := readMetaPage(t, srcPath)
leafPageId := 0
for i := 2; i < int(meta.Pgid()); i++ {
p, _, err := guts_cli.ReadPage(srcPath, uint64(i))
require.NoError(t, err)
if p.IsLeafPage() && p.Count() > 1 {
leafPageId = int(p.Id())
}
}
require.NotEqual(t, 0, leafPageId)

meta := readMetaPage(t, srcPath)
leafPageId := 0
for i := 2; i < int(meta.Pgid()); i++ {
p, _, err := guts_cli.ReadPage(srcPath, uint64(i))
m := NewMain()
err = m.Run("page-item", db.Path(), fmt.Sprintf("%d", leafPageId), "0")
require.NoError(t, err)
if p.IsLeafPage() && p.Count() > 1 {
leafPageId = int(p.Id())
if !strings.Contains(m.Stdout.String(), "key_0") || !strings.Contains(m.Stdout.String(), "value_0") {
t.Fatalf("Unexpected output:\n%s\n", m.Stdout.String())
}
}
require.NotEqual(t, 0, leafPageId)
})

m := NewMain()
err = m.Run("page-item", db.Path(), fmt.Sprintf("%d", leafPageId), "0")
require.NoError(t, err)
if !strings.Contains(m.Stdout.String(), "key_0") || !strings.Contains(m.Stdout.String(), "value_0") {
t.Fatalf("Unexpected output:\n%s\n", m.Stdout.String())
}
t.Run("test data in int64 format", func(t *testing.T) {
db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})
srcPath := db.Path()

// Insert some sample data
t.Log("Insert some sample data")
err := db.Fill([]byte("data"), 1, 100,
func(tx int, k int) []byte { return convertInt64IntoBytes(t, 10001) },
func(tx int, k int) []byte { return convertInt64IntoBytes(t, 10002) },
)
require.NoError(t, err)

defer requireDBNoChange(t, dbData(t, srcPath), srcPath)

meta := readMetaPage(t, srcPath)
leafPageId := 0
for i := 2; i < int(meta.Pgid()); i++ {
p, _, err := guts_cli.ReadPage(srcPath, uint64(i))
require.NoError(t, err)
if p.IsLeafPage() && p.Count() > 1 {
leafPageId = int(p.Id())
}
}
require.NotEqual(t, 0, leafPageId)
m := NewMain()
err = m.Run("page-item", db.Path(), fmt.Sprintf("%d", leafPageId), "0")
require.NoError(t, err)
if !strings.Contains(m.Stdout.String(), "10001") || !strings.Contains(m.Stdout.String(), "10002") {
t.Fatalf("Unexpected output:\n%s\n", m.Stdout.String())
}
})
}

// Ensure the "stats" command can execute correctly.
Expand Down Expand Up @@ -629,3 +663,11 @@ func requireDBNoChange(t *testing.T, oldData []byte, filePath string) {
noChange := bytes.Equal(oldData, newData)
require.True(t, noChange)
}

func convertInt64IntoBytes(t testing.TB, num int64) []byte {
t.Helper()

buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutVarint(buf, num)
return buf[:n]
}

0 comments on commit f42d8cb

Please sign in to comment.