Skip to content

Expiry works only during runtime and is lost on shrink #112

@boguslaw-wojcik

Description

@boguslaw-wojcik

Version: v1.3.0

If I add key first without expiry and then re-set it with expiry, but the expiry is not concluded during runtime, they key is never expired and on shrinking expiry is lost. To reproduce it:

Run this code:

func main()  {
	db, err := buntdb.Open("data.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	err = db.Update(func(tx *buntdb.Tx) error {
		_, _, err := tx.Set("my_key", "my_val", nil)

		return err
	})

	err = db.Update(func(tx *buntdb.Tx) error {
		_, _, err := tx.Set("my_key", "my_val", &buntdb.SetOptions{Expires: true, TTL: 5 * time.Second})

		return err
	})
}

The data.db will look like this:

*3
$3
set
$6
my_key
$6
my_val
*5
$3
set
$6
my_key
$6
my_val
$2
ae
$10
1701350403

After more than 5 seconds, run this code:

func main() {
	db, err := buntdb.Open("data.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	db.Shrink()

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

		return err
	})
}

It will print:

key: my_key, value: my_val

The data.db file looks like this:

*3
$3
set
$6
my_key
$6
my_val

I guess the expiry is not set when reconstructing the state from the log if there were previous versions of the same key without expiry.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions