buntdb icon indicating copy to clipboard operation
buntdb copied to clipboard

tx.Ascend doesn't work correctly

Open MarcelXia opened this issue 2 years ago • 1 comments

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?

MarcelXia avatar Jun 08 '22 06:06 MarcelXia

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

tidwall avatar Jun 08 '22 13:06 tidwall