impl RAM db

main
Bel LaPointe 2024-04-12 09:19:48 -06:00
parent 82fc7526d0
commit c51e580e09
2 changed files with 59 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"io/ioutil"
"path"
"sync"
"time"
"go.etcd.io/bbolt"
@ -16,6 +17,60 @@ type Driver interface {
Set(context.Context, string, string, []byte) error
}
type RAM struct {
m map[string]map[string][]byte
lock *sync.RWMutex
}
func NewRAM() RAM {
return RAM{
m: make(map[string]map[string][]byte),
lock: &sync.RWMutex{},
}
}
func (ram RAM) Close() error {
return nil
}
func (ram RAM) ForEach(ctx context.Context, ns string, cb func(string, []byte) error) error {
ram.lock.RLock()
defer ram.lock.RUnlock()
for k, v := range ram.m[ns] {
if ctx.Err() != nil {
break
}
if err := cb(k, v); err != nil {
return err
}
}
return ctx.Err()
}
func (ram RAM) Get(_ context.Context, ns, id string) ([]byte, error) {
ram.lock.RLock()
defer ram.lock.RUnlock()
if _, ok := ram.m[ns]; !ok {
return nil, nil
}
return ram.m[ns][id], nil
}
func (ram RAM) Set(_ context.Context, ns, id string, v []byte) error {
ram.lock.Lock()
defer ram.lock.Unlock()
if _, ok := ram.m[ns]; !ok {
ram.m[ns] = map[string][]byte{}
}
ram.m[ns][id] = v
return nil
}
type BBolt struct {
db *bbolt.DB
}

View File

@ -7,6 +7,10 @@ import (
"testing"
)
func TestDriverRAM(t *testing.T) {
testDriver(t, NewRAM())
}
func TestDriverBBolt(t *testing.T) {
testDriver(t, NewTestDBIn(t.TempDir()))
}