Ensure expired items do not load

See #112
This commit is contained in:
tidwall 2024-05-16 20:04:06 -07:00
parent 4ac2e321b1
commit e0e630f20f
2 changed files with 51 additions and 4 deletions

View File

@ -922,6 +922,10 @@ func (db *DB) readLoad(rd io.Reader, modTime time.Time) (n int64, err error) {
exat: exat, exat: exat,
}, },
}) })
} else {
db.deleteFromDatabase(&dbItem{
key: parts[1],
})
} }
} else { } else {
db.insertIntoDatabase(&dbItem{key: parts[1], val: parts[2]}) db.insertIntoDatabase(&dbItem{key: parts[1], val: parts[2]})
@ -1637,6 +1641,9 @@ func (tx *Tx) scan(desc, gt, lt bool, index, start, stop string,
// wrap a btree specific iterator around the user-defined iterator. // wrap a btree specific iterator around the user-defined iterator.
iter := func(item interface{}) bool { iter := func(item interface{}) bool {
dbi := item.(*dbItem) dbi := item.(*dbItem)
if dbi.expired() {
return true
}
return iterator(dbi.key, dbi.val) return iterator(dbi.key, dbi.val)
} }
var tr *btree.BTree var tr *btree.BTree

View File

@ -2499,10 +2499,10 @@ func Benchmark_Descend_10000(t *testing.B) {
} }
/* /*
func Benchmark_Spatial_2D(t *testing.B) { func Benchmark_Spatial_2D(t *testing.B) {
N := 100000 N := 100000
db, _, _ := benchOpenFillData(t, N, true, true, false, true, 100) db, _, _ := benchOpenFillData(t, N, true, true, false, true, 100)
defer benchClose(t, false, db) defer benchClose(t, false, db)
} }
*/ */
@ -2918,3 +2918,43 @@ func TestWrappedError(t *testing.T) {
}() }()
panicErr(errors.New("my fake error")) panicErr(errors.New("my fake error"))
} }
func TestIssue112(t *testing.T) {
defer os.RemoveAll("data.db")
db, err := Open("data.db")
if err != nil {
panicErr(err)
}
err = db.Update(func(tx *Tx) error {
_, _, err := tx.Set("key:112", "value:112-old", nil)
return err
})
if err != nil {
panicErr(err)
}
err = db.Update(func(tx *Tx) error {
_, _, err := tx.Set("key:112", "value:112", &SetOptions{Expires: true, TTL: time.Second})
return err
})
if err != nil {
panicErr(err)
}
db.Close()
time.Sleep(time.Second)
db, err = Open("data.db")
if err != nil {
panicErr(err)
}
err = db.View(func(tx *Tx) error {
_, err := tx.Get("key:112")
return err
})
assert.Assert(err == ErrNotFound)
db.View(func(tx *Tx) error {
err := tx.Ascend("", func(key, value string) bool {
t.Fatalf("key: %s, value: %s\n", key, value)
return true
})
return err
})
}