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

Need to document the sorting order of the keys #317

Closed
h12w opened this issue Nov 12, 2017 · 2 comments
Closed

Need to document the sorting order of the keys #317

h12w opened this issue Nov 12, 2017 · 2 comments
Assignees
Labels
area/documentation Documentation related issues.

Comments

@h12w
Copy link

h12w commented Nov 12, 2017

There is no document describing how keys are sorted, so I have to test it with the code below.

It seems the ordering should be described as something like "byte-wise prefix order", similar to big endian but the keys are aligned from the least significant byte, and "no byte" is smaller than "byte zero" (the shorter the smaller).

I think the behavior should be clarified in the document so that we can depend on the sorting more confidently.

The test code:

func TestKeyOrder(t *testing.T) {
	dir := "test_order"
	if err := os.MkdirAll(dir, 0755); err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)
	db, err := openBadger(dir)
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close()
	if err := db.Update(func(txn *badger.Txn) error {
		if err := txn.Set([]byte{0, 0}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{0, 1}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{0, 2}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{1, 0}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{1, 1}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{1, 2}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{1, 2, 0}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{2, 0}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{2, 1}, nil); err != nil {
			return err
		}
		if err := txn.Set([]byte{2, 2}, nil); err != nil {
			return err
		}
		return nil
	}); err != nil {
		t.Fatal(err)
	}
	if err := db.View(func(txn *badger.Txn) error {
		opt := badger.DefaultIteratorOptions
		// opt.Reverse = true
		it := txn.NewIterator(opt)
		for it.Rewind(); it.Valid(); it.Next() {
			item := it.Item()
			k := item.Key()
			fmt.Println(k)
		}
		return nil
	}); err != nil {
		t.Fatal(err)
	}
}
@manishrjain
Copy link
Contributor

It's byte-wise lexicographical sorting. We can probably clarify that in the README.

https://golang.org/pkg/bytes/#Compare

@manishrjain manishrjain added the area/documentation Documentation related issues. label Nov 12, 2017
@h12w
Copy link
Author

h12w commented Nov 12, 2017

Yes, "lexicographic" is exactly the word I'm trying to say. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/documentation Documentation related issues.
Development

No branches or pull requests

3 participants