rosedblabs / rosedb

Lightweight, fast and reliable key/value storage engine based on Bitcask.
https://rosedblabs.github.io
Apache License 2.0
4.48k stars 620 forks source link

Bug: Is it a bug in *Batch.Put? #308

Closed Orlion closed 2 months ago

Orlion commented 2 months ago

We have the following code:

var key = []byte("key")
var value1 = []byte("v1")
var value2 = []byte("v2")
db.Put(key, value1)
duration, _ := db.TTL(key)
fmt.Printf("before expire: %d\n", duration)

batch := db.NewBatch(BatchOptions{})
err = batch.Expire(key, time.Second*10)
err = batch.Put(key, value2)
err = batch.Commit()
duration, _ = db.TTL(key)
fmt.Printf("after expire: %d\n", duration)

Its output is

before expire: -1
after expire: -1

We expected to add an expiration time of 10 seconds to the key through batch.Expire(key, time.Second*10), but batch.Put(key, value2) removed the expiration time incorrectly.

I found this line in the code: https://github.com/rosedblabs/rosedb/blob/a89ea823e5b9d3f65422671410f082e0b47a255e/batch.go#L131

So, is it a bug?

roseduan commented 2 months ago

First, you set the expiration time of the key in batch, The key is then immediately set to a new value, so any previous information will be overwritten, I think this is correct.

Orlion commented 2 months ago

First, you set the expiration time of the key in batch, The key is then immediately set to a new value, so any previous information will be overwritten, I think this is correct.

I got it.