Skip to content

Commit

Permalink
Ensure expired items do not load
Browse files Browse the repository at this point in the history
See #112
  • Loading branch information
tidwall committed May 17, 2024
1 parent 4ac2e32 commit e0e630f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
7 changes: 7 additions & 0 deletions buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down Expand Up @@ -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
Expand Down
48 changes: 44 additions & 4 deletions buntdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
*/
Expand Down Expand Up @@ -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
})
}

0 comments on commit e0e630f

Please sign in to comment.