From e0e630f20f0f1945e5cedfe37ea3dfb222c8f7b9 Mon Sep 17 00:00:00 2001 From: tidwall Date: Thu, 16 May 2024 20:04:06 -0700 Subject: [PATCH] Ensure expired items do not load See #112 --- buntdb.go | 7 +++++++ buntdb_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/buntdb.go b/buntdb.go index 244748e..4f7a6d9 100644 --- a/buntdb.go +++ b/buntdb.go @@ -922,6 +922,10 @@ func (db *DB) readLoad(rd io.Reader, modTime time.Time) (n int64, err error) { exat: exat, }, }) + } else { + db.deleteFromDatabase(&dbItem{ + key: parts[1], + }) } } else { 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. iter := func(item interface{}) bool { dbi := item.(*dbItem) + if dbi.expired() { + return true + } return iterator(dbi.key, dbi.val) } var tr *btree.BTree diff --git a/buntdb_test.go b/buntdb_test.go index 4b37db2..d32584d 100644 --- a/buntdb_test.go +++ b/buntdb_test.go @@ -2499,10 +2499,10 @@ func Benchmark_Descend_10000(t *testing.B) { } /* -func Benchmark_Spatial_2D(t *testing.B) { - N := 100000 - db, _, _ := benchOpenFillData(t, N, true, true, false, true, 100) - defer benchClose(t, false, db) + func Benchmark_Spatial_2D(t *testing.B) { + N := 100000 + db, _, _ := benchOpenFillData(t, N, true, true, false, true, 100) + defer benchClose(t, false, db) } */ @@ -2918,3 +2918,43 @@ func TestWrappedError(t *testing.T) { }() 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 + }) +}