Add db to enable stale cache record metdata #105

Open
lash wants to merge 8 commits from lash/stalecache into master
2 changed files with 44 additions and 6 deletions
Showing only changes of commit ce30cb740e - Show all commits

View File

@ -44,12 +44,10 @@ func(tib *TimedDb) checkPrefix(pfx uint8, key []byte) bool {
} }
for _, v = range(tib.matchPfx[pfx]) { for _, v = range(tib.matchPfx[pfx]) {
l := len(v) l := len(v)
k := append(tib.parentSession, key...) if l > len(key) {
if l > len(k) {
continue continue
} }
logg.Debugf("check the prefix", "v", v, "k", k, "l", l ) if bytes.Equal(v, key[:l]) {
if bytes.Equal(v, k[:l]) {
return true return true
} }
} }
@ -98,7 +96,7 @@ func(tib *TimedDb) Stale(ctx context.Context, pfx uint8, sessionId string, key [
b = append([]byte{pfx}, b...) b = append([]byte{pfx}, b...)
v, err := tib.tdb.Get(ctx, b) v, err := tib.tdb.Get(ctx, b)
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "no time entry", "key", key, "b", b) logg.WarnCtxf(ctx, "no time entry", "key", key, "b", b)
return false return false
} }
t_now := time.Now() t_now := time.Now()

View File

@ -44,7 +44,7 @@ func TestFilteredStaleDb(t *testing.T) {
k := []byte("foo") k := []byte("foo")
tdb := NewTimedDb(mdb, time.Duration(time.Millisecond)) tdb := NewTimedDb(mdb, time.Duration(time.Millisecond))
tdb = tdb.WithMatch(db.DATATYPE_STATE, []byte("in")) tdb = tdb.WithMatch(db.DATATYPE_STATE, []byte("fo"))
tdb.SetPrefix(db.DATATYPE_USERDATA) tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("inky") tdb.SetSession("inky")
err = tdb.Put(ctx, k, []byte("bar")) err = tdb.Put(ctx, k, []byte("bar"))
@ -83,3 +83,43 @@ func TestFilteredStaleDb(t *testing.T) {
t.Fatal("expected not stale") t.Fatal("expected not stale")
} }
} }
func TestFilteredSameKeypartStaleDb(t *testing.T) {
ctx := context.Background()
mdb := memdb.NewMemDb()
err := mdb.Connect(ctx, "")
if err != nil {
t.Fatal(err)
}
tdb := NewTimedDb(mdb, time.Duration(time.Millisecond))
tdb = tdb.WithMatch(db.DATATYPE_USERDATA, []byte("ba"))
tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("xyzzy")
err = tdb.Put(ctx, []byte("bar"), []byte("inky"))
if err != nil {
t.Fatal(err)
}
tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("xyzzy")
err = tdb.Put(ctx, []byte("baz"), []byte("pinky"))
if err != nil {
t.Fatal(err)
}
tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("xyzzy")
err = tdb.Put(ctx, []byte("foo"), []byte("blinky"))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond)
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("bar")) {
t.Fatal("expected stale")
}
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("baz")) {
t.Fatal("expected stale")
}
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "xyzzy", []byte("foo")) {
t.Fatal("expected not stale")
}
}