Compare commits

...

9 Commits

Author SHA1 Message Date
lash
df095f0873 Merge branch 'master' into lash/stalecache 2025-01-04 10:00:47 +00:00
lash
473a7fc480 Merge branch 'master' into lash/stalecache 2024-12-18 06:52:02 +00:00
lash
94551ba37f
Update govise dep 2024-10-31 20:51:02 +00:00
lash
973a69455e Merge remote-tracking branch 'origin/master' into lash/stalecache 2024-10-31 20:33:01 +00:00
lash
0af7379ae4 Merge branch 'master' into lash/stalecache 2024-10-14 14:55:16 +01:00
lash
ce30cb740e
Remove session id from filter match key 2024-10-03 01:44:28 +01:00
lash
659fd00c53
Remove noop get override in timed db 2024-10-02 23:59:41 +01:00
lash
9b3ed0d6ae
Add session to timed db 2024-10-02 23:58:00 +01:00
lash
fbcde2f322
Implement timed data row db 2024-10-02 22:06:43 +01:00
4 changed files with 246 additions and 2 deletions

View File

@ -17,6 +17,7 @@ var (
dbC map[string]chan db.Db dbC map[string]chan db.Db
) )
type ThreadGdbmDb struct { type ThreadGdbmDb struct {
db db.Db db db.Db
connStr string connStr string

View File

@ -6,6 +6,11 @@ import (
"git.defalsify.org/vise.git/db" "git.defalsify.org/vise.git/db"
) )
const (
DATATYPE_USERSUB = 64
SUBPREFIX_TIME = uint16(1)
)
// PrefixDb interface abstracts the database operations. // PrefixDb interface abstracts the database operations.
type PrefixDb interface { type PrefixDb interface {
Get(ctx context.Context, key []byte) ([]byte, error) Get(ctx context.Context, key []byte) ([]byte, error)
@ -26,7 +31,11 @@ func NewSubPrefixDb(store db.Db, pfx []byte) *SubPrefixDb {
} }
} }
func (s *SubPrefixDb) toKey(k []byte) []byte { func(s *SubPrefixDb) SetSession(sessionId string) {
s.store.SetSession(sessionId)
}
func(s *SubPrefixDb) toKey(k []byte) []byte {
return append(s.pfx, k...) return append(s.pfx, k...)
} }

109
internal/storage/timed.go Normal file
View File

@ -0,0 +1,109 @@
package storage
import (
"bytes"
"context"
"time"
"encoding/binary"
"git.defalsify.org/vise.git/db"
)
type TimedDb struct {
db.Db
tdb *SubPrefixDb
ttl time.Duration
parentPfx uint8
parentSession []byte
matchPfx map[uint8][][]byte
}
func NewTimedDb(db db.Db, ttl time.Duration) *TimedDb {
var b [2]byte
binary.BigEndian.PutUint16(b[:], SUBPREFIX_TIME)
sdb := NewSubPrefixDb(db, b[:])
return &TimedDb{
Db: db,
tdb: sdb,
ttl: ttl,
}
}
func(tib *TimedDb) WithMatch(pfx uint8, keyPart []byte) *TimedDb {
if tib.matchPfx == nil {
tib.matchPfx = make(map[uint8][][]byte)
}
tib.matchPfx[pfx] = append(tib.matchPfx[pfx], keyPart)
return tib
}
func(tib *TimedDb) checkPrefix(pfx uint8, key []byte) bool {
var v []byte
if tib.matchPfx == nil {
return true
}
for _, v = range(tib.matchPfx[pfx]) {
l := len(v)
if l > len(key) {
continue
}
if bytes.Equal(v, key[:l]) {
return true
}
}
return false
}
func(tib *TimedDb) SetPrefix(pfx uint8) {
tib.Db.SetPrefix(pfx)
tib.parentPfx = pfx
}
func(tib *TimedDb) SetSession(session string) {
tib.Db.SetSession(session)
tib.parentSession = []byte(session)
}
func(tib *TimedDb) Put(ctx context.Context, key []byte, val []byte) error {
t := time.Now()
b, err := t.MarshalBinary()
if err != nil {
return err
}
err = tib.Db.Put(ctx, key, val)
if err != nil {
return err
}
defer func() {
tib.parentPfx = 0
tib.parentSession = nil
}()
if tib.checkPrefix(tib.parentPfx, key) {
tib.tdb.SetSession("")
k := db.ToSessionKey(tib.parentPfx, []byte(tib.parentSession), key)
k = append([]byte{tib.parentPfx}, k...)
err = tib.tdb.Put(ctx, k, b)
if err != nil {
logg.ErrorCtxf(ctx, "failed to update timestamp of record", err)
}
}
return nil
}
func(tib *TimedDb) Stale(ctx context.Context, pfx uint8, sessionId string, key []byte) bool {
tib.tdb.SetSession("")
b := db.ToSessionKey(pfx, []byte(sessionId), key)
b = append([]byte{pfx}, b...)
v, err := tib.tdb.Get(ctx, b)
if err != nil {
logg.WarnCtxf(ctx, "no time entry", "key", key, "b", b)
return false
}
t_now := time.Now()
t_then := time.Time{}
err = t_then.UnmarshalBinary(v)
if err != nil {
return false
}
return t_now.After(t_then.Add(tib.ttl))
}

View File

@ -0,0 +1,125 @@
package storage
import (
"context"
"testing"
"time"
"git.defalsify.org/vise.git/db"
memdb "git.defalsify.org/vise.git/db/mem"
)
func TestStaleDb(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.SetPrefix(db.DATATYPE_USERDATA)
k := []byte("foo")
err = tdb.Put(ctx, k, []byte("bar"))
if err != nil {
t.Fatal(err)
}
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "", k) {
t.Fatal("expected not stale")
}
time.Sleep(time.Millisecond)
if !tdb.Stale(ctx, db.DATATYPE_USERDATA, "", k) {
t.Fatal("expected stale")
}
}
func TestFilteredStaleDb(t *testing.T) {
ctx := context.Background()
mdb := memdb.NewMemDb()
err := mdb.Connect(ctx, "")
if err != nil {
t.Fatal(err)
}
k := []byte("foo")
tdb := NewTimedDb(mdb, time.Duration(time.Millisecond))
tdb = tdb.WithMatch(db.DATATYPE_STATE, []byte("fo"))
tdb.SetPrefix(db.DATATYPE_USERDATA)
tdb.SetSession("inky")
err = tdb.Put(ctx, k, []byte("bar"))
if err != nil {
t.Fatal(err)
}
tdb.SetPrefix(db.DATATYPE_STATE)
tdb.SetSession("inky")
err = tdb.Put(ctx, k, []byte("pinky"))
if err != nil {
t.Fatal(err)
}
tdb.SetSession("blinky")
err = tdb.Put(ctx, k, []byte("clyde"))
if err != nil {
t.Fatal(err)
}
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "inky", k) {
t.Fatal("expected not stale")
}
if tdb.Stale(ctx, db.DATATYPE_STATE, "inky", k) {
t.Fatal("expected not stale")
}
if tdb.Stale(ctx, db.DATATYPE_STATE, "blinky", k) {
t.Fatal("expected not stale")
}
time.Sleep(time.Millisecond)
if tdb.Stale(ctx, db.DATATYPE_USERDATA, "inky", k) {
t.Fatal("expected not stale")
}
if !tdb.Stale(ctx, db.DATATYPE_STATE, "inky", k) {
t.Fatal("expected stale")
}
if tdb.Stale(ctx, db.DATATYPE_STATE, "blinky", k) {
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")
}
}