From f42d8cb814495d8f00f9488c837a91ab028aceeb Mon Sep 17 00:00:00 2001 From: Mustafa Elbehery Date: Mon, 6 Nov 2023 19:19:12 +0100 Subject: [PATCH] fix: change pageItemCommand default format auto Signed-off-by: Mustafa Elbehery --- cmd/bbolt/main.go | 2 +- cmd/bbolt/main_test.go | 92 ++++++++++++++++++++++++++++++------------ 2 files changed, 68 insertions(+), 26 deletions(-) diff --git a/cmd/bbolt/main.go b/cmd/bbolt/main.go index 7115471c0..a0e13ce3f 100644 --- a/cmd/bbolt/main.go +++ b/cmd/bbolt/main.go @@ -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 diff --git a/cmd/bbolt/main_test.go b/cmd/bbolt/main_test.go index dbc7de762..12267c772 100644 --- a/cmd/bbolt/main_test.go +++ b/cmd/bbolt/main_test.go @@ -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. @@ -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] +}