From ce30cb740eb9d4c55d7872bd00386a79d4fb22bf Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 3 Oct 2024 01:44:28 +0100 Subject: [PATCH] Remove session id from filter match key --- internal/storage/timed.go | 8 +++---- internal/storage/timed_test.go | 42 +++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/internal/storage/timed.go b/internal/storage/timed.go index cf05dbb..256bd9d 100644 --- a/internal/storage/timed.go +++ b/internal/storage/timed.go @@ -44,12 +44,10 @@ func(tib *TimedDb) checkPrefix(pfx uint8, key []byte) bool { } for _, v = range(tib.matchPfx[pfx]) { l := len(v) - k := append(tib.parentSession, key...) - if l > len(k) { + if l > len(key) { continue } - logg.Debugf("check the prefix", "v", v, "k", k, "l", l ) - if bytes.Equal(v, k[:l]) { + if bytes.Equal(v, key[:l]) { return true } } @@ -98,7 +96,7 @@ func(tib *TimedDb) Stale(ctx context.Context, pfx uint8, sessionId string, key [ b = append([]byte{pfx}, b...) v, err := tib.tdb.Get(ctx, b) 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 } t_now := time.Now() diff --git a/internal/storage/timed_test.go b/internal/storage/timed_test.go index 38fb63f..a5c7980 100644 --- a/internal/storage/timed_test.go +++ b/internal/storage/timed_test.go @@ -44,7 +44,7 @@ func TestFilteredStaleDb(t *testing.T) { k := []byte("foo") 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.SetSession("inky") err = tdb.Put(ctx, k, []byte("bar")) @@ -83,3 +83,43 @@ func TestFilteredStaleDb(t *testing.T) { 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") + } +}