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

tx.Ascend doesn't work correctly #90

Open
MarcelXia opened this issue Jun 8, 2022 · 1 comment
Open

tx.Ascend doesn't work correctly #90

MarcelXia opened this issue Jun 8, 2022 · 1 comment

Comments

@MarcelXia
Copy link

MarcelXia commented Jun 8, 2022

when I store keys todo:0000000001 until todo:0000000010 (so 10 different records)
I see the order being right.

if I store todo:0000000001 again after that, the order is broken, because that last "record" is presented last, while the documentation states:
All keys/value pairs are ordered in the database by the key. To iterate over the keys:

Extra info: I have created an index on todo:

	err = db.CreateIndex("todo", "todo:*", buntdb.IndexString)
	if err != nil && err.Error() != "index exists" {
		log.Println(strings.ReplaceAll(fmt.Sprintf(`save.go:97 err %#v`, err), `, `, ",\n"))
	}

The insert goes like this:

	err = db.Update(func(tx *buntdb.Tx) error {
		fmt.Println(`save key:`, dbkey)
		_, _, err = tx.Set(dbkey, val, nil)

		return err
	})

Result:

save key: todo:0000000001
save key: todo:0000000002
save key: todo:0000000003
save key: todo:0000000004
save key: todo:0000000005
save key: todo:0000000006
save key: todo:0000000007
save key: todo:0000000008
save key: todo:0000000009
save key: todo:0000000010
save key: todo:0000000001

I get all of my todos:

err = db.View(
		func(tx *buntdb.Tx) error {
			err := tx.Ascend("todo", func(key, value string) bool {
				fmt.Printf("ascend key: %s\n", key)
				return true // continue iteration
			})

			return err
		})

Result:

ascend key: todo:0000000002
ascend key: todo:0000000003
ascend key: todo:0000000004
ascend key: todo:0000000005
ascend key: todo:0000000006
ascend key: todo:0000000007
ascend key: todo:0000000008
ascend key: todo:0000000009
ascend key: todo:0000000010
ascend key: todo:0000000001

Am I doing something wrong?

I tried to do it with:

	err = db.CreateIndex(todoBucket, todoBucket+":*", buntdb.IndexInt)

and that works. But how come the indexString doesn't work?
In my book the order should be the same because of the leading zeros, right?

@tidwall
Copy link
Owner

tidwall commented Jun 8, 2022

Hi,

CreateIndex orders by the values, not the keys.
buntdb.IndexString will order values by lexicographical order, where "2" > "10".
buntdb.IndexInt will order values by numerical order, where "2" < "10".

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

2 participants