-
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
fix: change pageItemCommand default format auto #596
Conversation
f42d8cb
to
f086276
Compare
cmd/bbolt/main_test.go
Outdated
func(tx int, k int) []byte { return convertInt64IntoBytes(t, 10001) }, | ||
func(tx int, k int) []byte { return convertInt64IntoBytes(t, 10002) }, |
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.
func(tx int, k int) []byte { return convertInt64IntoBytes(t, 10001) }, | |
func(tx int, k int) []byte { return convertInt64IntoBytes(t, 10002) }, | |
func(tx int, k int) []byte { return convertInt64IntoBytes(t, k+1) }, | |
func(tx int, k int) []byte { return convertInt64IntoBytes(t, k+2) }, |
cmd/bbolt/main_test.go
Outdated
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") { |
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.
This isn't correct. When the key or value isn't printable, the output will be hex format string.
For your reference:
|
f086276
to
14448c3
Compare
@ahrtr fixed 👍🏽 |
{ | ||
name: "non printable items", | ||
printable: false, | ||
itemId: "0", |
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,
for i:=0; i<100; i++ {
// check $i item
}
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 ?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
expectedKey: hex.EncodeToString(convertInt64IntoBytes(0 + 1)), | |
expectedKey: hex.EncodeToString(convertInt64IntoBytes(0 + 1)), |
Why we use key as 0+1
and value as 0+2
?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
same here, why we increment the i
? .. not the case in the printable
data above !
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Note that purpose of the requireDBNoChange
is to ensure the command doesn't change the db's data.
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.
so dbData
read the database file that is already on-disk .. then requireDBNoChange
will read the new data file after the test data has been inserted in the transaction above ..
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 comment
The 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 dbData(t, srcPath)
is executed right away. It reads current db's data. Note that the we have already finished inserting all data at the moment.
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 dbData(t, srcPath)
.
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.
The rough workflow:
- insert some sample data
- read the db's data using
dbData(t, srcPath)
- execute the command (the main part of the test);
- read the db's data again, and compare it with the data read at step 2.
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.
gotcha, makes perfect sense now :) .. thanks tonsss 🙏🏽
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()) | ||
} |
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.
If you want, you can also enhance the test case something like below,
for i:=0; i<100; i++ {
m := NewMain()
err = m.Run("page-item", db.Path(), fmt.Sprintf("%d", leafPageId), fmt.Sprintf("%d", i))
require.NoError(t, err)
if !strings.Contains(m.Stdout.String(), xxx) || !strings.Contains(m.Stdout.String(), yyy) {
t.Fatalf("Unexpected output:\n%s\n", m.Stdout.String())
}
}
Then you can remove
itemId string
expectedKey string
expectedValue string
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.
i like the anonymous struct style more, because it is easier to be read by others.. thanks for your answer 🙏🏽
I assume #596 (comment) answered all your questions. |
cmd/bbolt/main_test.go
Outdated
"fmt" | ||
"go.etcd.io/bbolt/internal/guts_cli" |
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.
Need to put all go.etcd.io/xxx
together.
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.
done 👍🏽
Signed-off-by: Mustafa Elbehery <[email protected]>
14448c3
to
f56cd26
Compare
One more minor comment, the readme doc https://github.com/etcd-io/bbolt/tree/master/cmd/bbolt#page-item also needs to be updated. The default format changed. This can be addressed in this PR or a followup PR. |
thanks for your comment .. I propose to change the Readme once in a follow up PR, because the CI is almost done on this PR .. wdyt ? |
OK. |
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.
LGTM
Congratulations on your first PR! Thanks.
This PR changes
pageItemCommand
default format fromascii-encoded
toauto
resolves ##588
cc @ahrtr