-
Notifications
You must be signed in to change notification settings - Fork 649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: change pageItemCommand default format auto #596
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |||||
"bytes" | ||||||
crypto "crypto/rand" | ||||||
"encoding/binary" | ||||||
"encoding/hex" | ||||||
"fmt" | ||||||
"io" | ||||||
"math/rand" | ||||||
|
@@ -14,12 +15,12 @@ import ( | |||||
"testing" | ||||||
|
||||||
"go.etcd.io/bbolt/internal/btesting" | ||||||
"go.etcd.io/bbolt/internal/guts_cli" | ||||||
|
||||||
"github.com/stretchr/testify/require" | ||||||
|
||||||
bolt "go.etcd.io/bbolt" | ||||||
main "go.etcd.io/bbolt/cmd/bbolt" | ||||||
"go.etcd.io/bbolt/internal/guts_cli" | ||||||
) | ||||||
|
||||||
// Ensure the "info" command can print information about a database. | ||||||
|
@@ -135,35 +136,76 @@ 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) | ||||||
|
||||||
defer requireDBNoChange(t, dbData(t, srcPath), srcPath) | ||||||
testCases := []struct { | ||||||
name string | ||||||
printable bool | ||||||
itemId string | ||||||
expectedKey string | ||||||
expectedValue string | ||||||
}{ | ||||||
{ | ||||||
name: "printable items", | ||||||
printable: true, | ||||||
itemId: "0", | ||||||
expectedKey: "key_0", | ||||||
expectedValue: "value_0", | ||||||
}, | ||||||
{ | ||||||
name: "non printable items", | ||||||
printable: false, | ||||||
itemId: "0", | ||||||
expectedKey: hex.EncodeToString(convertInt64IntoBytes(0 + 1)), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Why we use key as |
||||||
expectedValue: hex.EncodeToString(convertInt64IntoBytes(0 + 2)), | ||||||
}, | ||||||
} | ||||||
|
||||||
for _, tc := range testCases { | ||||||
t.Run(tc.name, func(t *testing.T) { | ||||||
db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096}) | ||||||
srcPath := db.Path() | ||||||
|
||||||
t.Log("Insert some sample data") | ||||||
err := db.Update(func(tx *bolt.Tx) error { | ||||||
b, bErr := tx.CreateBucketIfNotExists([]byte("data")) | ||||||
if bErr != nil { | ||||||
return bErr | ||||||
} | ||||||
|
||||||
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) | ||||||
for i := 0; i < 100; i++ { | ||||||
if tc.printable { | ||||||
if bErr = b.Put([]byte(fmt.Sprintf("key_%d", i)), []byte(fmt.Sprintf("value_%d", i))); bErr != nil { | ||||||
return bErr | ||||||
} | ||||||
} else { | ||||||
k, v := convertInt64IntoBytes(int64(i+1)), convertInt64IntoBytes(int64(i+2)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, why we increment the |
||||||
if bErr = b.Put(k, v); bErr != nil { | ||||||
return bErr | ||||||
} | ||||||
} | ||||||
} | ||||||
return nil | ||||||
}) | ||||||
require.NoError(t, err) | ||||||
defer requireDBNoChange(t, dbData(t, srcPath), srcPath) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wonder, why the database should not change, since we have inserted sample data ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that purpose of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so My question is, how the database should not change, while we are inserting sample data ? please correct me if i am wrong There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a "defer" statement gets executed, its value and parameters to the call are evaluated as usual and saved anew, so But the actual function is not invoked until the surrounding function returns. When it gets really executed, it reads the db's data again, and compare it with the previous data read in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rough workflow:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gotcha, makes perfect sense now :) .. thanks tonsss 🙏🏽 |
||||||
|
||||||
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(), "key_0") || !strings.Contains(m.Stdout.String(), "value_0") { | ||||||
t.Fatalf("Unexpected output:\n%s\n", m.Stdout.String()) | ||||||
m := NewMain() | ||||||
err = m.Run("page-item", db.Path(), fmt.Sprintf("%d", leafPageId), tc.itemId) | ||||||
require.NoError(t, err) | ||||||
if !strings.Contains(m.Stdout.String(), tc.expectedKey) || !strings.Contains(m.Stdout.String(), tc.expectedValue) { | ||||||
t.Fatalf("Unexpected output:\n%s\n", m.Stdout.String()) | ||||||
} | ||||||
Comment on lines
+202
to
+207
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want, you can also enhance the test case something like below,
Then you can remove
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like the anonymous struct style more, because it is easier to be read by others.. thanks for your answer 🙏🏽 |
||||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -629,3 +671,9 @@ func requireDBNoChange(t *testing.T, oldData []byte, filePath string) { | |||||
noChange := bytes.Equal(oldData, newData) | ||||||
require.True(t, noChange) | ||||||
} | ||||||
|
||||||
func convertInt64IntoBytes(num int64) []byte { | ||||||
buf := make([]byte, binary.MaxVarintLen64) | ||||||
n := binary.PutVarint(buf, num) | ||||||
return buf[:n] | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q : why we use the same itemId as the printable item above ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good question. You can even check all items using a for loop,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but we are using only one
itemId
for all the data, no ?