Skip to content
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

Merged
merged 1 commit into from
Nov 7, 2023

Conversation

Elbehery
Copy link
Member

@Elbehery Elbehery commented Nov 6, 2023

This PR changes pageItemCommand default format from ascii-encoded to auto

resolves ##588

cc @ahrtr

@Elbehery Elbehery force-pushed the pageItemCommand_default_format_auto branch from f42d8cb to f086276 Compare November 6, 2023 18:22
Comment on lines 178 to 179
func(tx int, k int) []byte { return convertInt64IntoBytes(t, 10001) },
func(tx int, k int) []byte { return convertInt64IntoBytes(t, 10002) },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) },

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") {
Copy link
Member

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.

@ahrtr
Copy link
Member

ahrtr commented Nov 7, 2023

For your reference:

func TestPageItemCommand_Run(t *testing.T) {
	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(convertInt64ToBytes(0 + 1)),
			expectedValue: hex.EncodeToString(convertInt64ToBytes(0 + 2)),
		},
	}

	for _, tc := range testCases {
		tc := tc
		t.Run(tc.name, 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.Update(func(tx *bolt.Tx) error {
				b, berr := tx.CreateBucketIfNotExists([]byte("data"))
				if berr != nil {
					return berr
				}
				for i := 0; i < 100; i++ {
					// write printable key/value
					if tc.printable {
						if perr := b.Put([]byte(fmt.Sprintf("key_%d", i)), []byte(fmt.Sprintf("value_%d", i))); perr != nil {
							return perr
						}
					} else {
						k, v := convertInt64ToBytes(int64(i+1)), convertInt64ToBytes(int64(i+2))
						if perr := b.Put(k, v); perr != nil {
							return perr
						}
					}
				}
				return nil
			})
			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), 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())
			}
		})
	}
}

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

@Elbehery Elbehery force-pushed the pageItemCommand_default_format_auto branch from f086276 to 14448c3 Compare November 7, 2023 13:58
@Elbehery
Copy link
Member Author

Elbehery commented Nov 7, 2023

@ahrtr fixed 👍🏽

{
name: "non printable items",
printable: false,
itemId: "0",
Copy link
Member Author

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 ?

Copy link
Member

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
}

Copy link
Member Author

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)),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))
Copy link
Member Author

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)
Copy link
Member Author

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 ?

Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rough workflow:

  1. insert some sample data
  2. read the db's data using dbData(t, srcPath)
  3. execute the command (the main part of the test);
  4. read the db's data again, and compare it with the data read at step 2.

Copy link
Member Author

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 🙏🏽

Comment on lines +202 to +207
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())
}
Copy link
Member

@ahrtr ahrtr Nov 7, 2023

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

Copy link
Member Author

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 🙏🏽

@ahrtr
Copy link
Member

ahrtr commented Nov 7, 2023

I assume #596 (comment) answered all your questions.

"fmt"
"go.etcd.io/bbolt/internal/guts_cli"
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👍🏽

@Elbehery Elbehery force-pushed the pageItemCommand_default_format_auto branch from 14448c3 to f56cd26 Compare November 7, 2023 14:21
@ahrtr
Copy link
Member

ahrtr commented Nov 7, 2023

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.

@Elbehery
Copy link
Member Author

Elbehery commented Nov 7, 2023

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 ?

@ahrtr
Copy link
Member

ahrtr commented Nov 7, 2023

I propose to change the Readme once in a follow up PR, because the CI is almost done on this PR .. wdyt ?

OK.

Copy link
Member

@ahrtr ahrtr left a 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.

@ahrtr ahrtr merged commit 959ab0a into etcd-io:master Nov 7, 2023
9 checks passed
@Elbehery Elbehery deleted the pageItemCommand_default_format_auto branch November 7, 2023 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants