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

DescendKeys works incorrectly #83

Open
jekabolt opened this issue Feb 19, 2022 · 3 comments
Open

DescendKeys works incorrectly #83

jekabolt opened this issue Feb 19, 2022 · 3 comments

Comments

@jekabolt
Copy link

db, _ := buntdb.Open(":memory:")
	db.CreateIndex("name", "*", buntdb.IndexString)
	db.Update(func(tx *buntdb.Tx) error {
		tx.Set("user:1", "Tom", nil)
		tx.Set("user:2", "Tom", nil)
		tx.Set("user:3", "Tom", nil)
		tx.Set("user:4", "Tom", nil)
		tx.Set("user:5", "Tom", nil)
		tx.Set("user:6", "Tom", nil)
		tx.Set("user:7", "Tom", nil)
		tx.Set("user:8", "Tom", nil)
		tx.Set("user:9", "Tom", nil)
		tx.Set("user:10", "Tom", nil)
		tx.Set("user:11", "Tom", nil)
		tx.Set("user:12", "Tom", nil)
		tx.Set("user:13", "Tom", nil)
		tx.Set("user:14", "Tom", nil)
		tx.Set("user:15", "Tom", nil)

		return nil
	})
	db.View(func(tx *buntdb.Tx) error {
		tx.DescendKeys("user:*",
			func(key, value string) bool {
				fmt.Printf("%s: %s\n", key, value)
				return true
			})
		return nil
	})

output

user:9: Tom
user:8: Tom
user:7: Tom
user:6: Tom
user:5: Tom
user:4: Tom
user:3: Tom
user:2: Tom
user:15: Tom
user:14: Tom
user:13: Tom
user:12: Tom
user:11: Tom
user:10: Tom
user:1: Tom

is that correct behavior?

@tidwall
Copy link
Owner

tidwall commented Feb 19, 2022

Yes. This behavior is correct.

The "user:1" is ordered ahead of "user:10" when ascending and ordered behind when descending. This is because the keys in buntdb are in lexicographical order.

A popular option to keep order is to pad the numbers with zeros so that every number is of equal length. user:1 becomes user:0000000001

Makes sure, however, that the length of padded numbers remain the same for all keys.

@jekabolt
Copy link
Author

thank you

@gebv
Copy link

gebv commented Feb 19, 2022

@jekabolt recently faced a similar problem
Try this project https://github.com/gebv/lex. It is fork. Added go mod and minor code fixes.
It is project looks complete. Maybe as part of other projects (sub packages) there is something more complete.

PS There's a very interesting article https://www.zanopha.com/docs/elen.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants