refactor: cleanup

This commit is contained in:
Mark Bailey 2025-08-31 06:41:53 -04:00
parent 42a063182b
commit e42b1d06e9
5 changed files with 43 additions and 35 deletions

View File

@ -12,27 +12,34 @@ import (
const defaultBaseDir = "./data/badger"
type BadgerOpts func(*BadgerStore)
type Storer interface {
Close() error
Delete(context.Context, string) error
Get(context.Context, string) ([]byte, error)
Put(context.Context, string, []byte, time.Duration) error
}
type BadgerOpts func(*badgerStore)
func WithBaseDir(baseDir string) BadgerOpts {
return func(b *BadgerStore) {
return func(b *badgerStore) {
b.baseDir = baseDir
}
}
func WithSubDir(subDir string) BadgerOpts {
return func(b *BadgerStore) {
return func(b *badgerStore) {
b.subDir = subDir
}
}
type BadgerStore struct {
type badgerStore struct {
baseDir string
subDir string
db *badger.DB
}
func (b *BadgerStore) Put(ctx context.Context, key string, val []byte, ttl time.Duration) error {
func (b *badgerStore) Put(ctx context.Context, key string, val []byte, ttl time.Duration) error {
done := make(chan error, 1)
go func() {
@ -51,7 +58,7 @@ func (b *BadgerStore) Put(ctx context.Context, key string, val []byte, ttl time.
}
}
func (b *BadgerStore) Get(ctx context.Context, key string) ([]byte, error) {
func (b *badgerStore) Get(ctx context.Context, key string) ([]byte, error) {
done := make(chan struct {
val []byte
err error
@ -81,7 +88,7 @@ func (b *BadgerStore) Get(ctx context.Context, key string) ([]byte, error) {
}
}
func (b *BadgerStore) Delete(ctx context.Context, key string) error {
func (b *badgerStore) Delete(ctx context.Context, key string) error {
done := make(chan error, 1)
go func() {
@ -99,11 +106,11 @@ func (b *BadgerStore) Delete(ctx context.Context, key string) error {
}
}
func (b *BadgerStore) Close() error {
func (b *badgerStore) Close() error {
return b.db.Close()
}
func (b *BadgerStore) dir() string {
func (b *badgerStore) dir() string {
if b.baseDir == "" {
b.baseDir = defaultBaseDir
}
@ -115,8 +122,8 @@ func (b *BadgerStore) dir() string {
return filepath.Join(b.baseDir, b.subDir)
}
func NewBadgerStore(opts ...BadgerOpts) (*BadgerStore, error) {
s := &BadgerStore{}
func NewBadgerStore(opts ...BadgerOpts) (*badgerStore, error) {
s := &badgerStore{}
for _, opt := range opts {
opt(s)

View File

@ -8,7 +8,7 @@ import (
"time"
)
func newTestStore(t *testing.T) *BadgerStore {
func newTestStore(t *testing.T) *badgerStore {
t.Helper()
dir := filepath.Join(os.TempDir(), "badger_test", time.Now().Format("20060102150405"))
@ -40,11 +40,11 @@ func runSubtests(t *testing.T, subs []subtest) {
func TestBadgerStore(t *testing.T) {
tests := []struct {
name string
test func(t *testing.T, store *BadgerStore)
test func(t *testing.T, store *badgerStore)
}{
{
name: "Put/Get/Delete roundtrip",
test: func(t *testing.T, store *BadgerStore) {
test: func(t *testing.T, store *badgerStore) {
ctx := context.Background()
key := "foo"
val := []byte("bar")
@ -92,7 +92,7 @@ func TestBadgerStore(t *testing.T) {
},
{
name: "PutWithTTL expires",
test: func(t *testing.T, store *BadgerStore) {
test: func(t *testing.T, store *badgerStore) {
ctx := context.Background()
key := "ttl"
val := []byte("value")
@ -117,7 +117,7 @@ func TestBadgerStore(t *testing.T) {
},
{
name: "ContextCancellation affects operations",
test: func(t *testing.T, store *BadgerStore) {
test: func(t *testing.T, store *badgerStore) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
@ -162,17 +162,17 @@ func TestBadgerStore(t *testing.T) {
func TestDirFunction(t *testing.T) {
tests := []struct {
name string
store *BadgerStore
store *badgerStore
want string
}{
{
name: "default directory",
store: &BadgerStore{},
store: &badgerStore{},
want: defaultBaseDir,
},
{
name: "with base and sub dir",
store: &BadgerStore{baseDir: "/tmp", subDir: "foo"},
store: &badgerStore{baseDir: "/tmp", subDir: "foo"},
want: filepath.Join("/tmp", "foo"),
},
}

View File

@ -9,9 +9,15 @@ import (
const suff = "route_cache"
type RouteCacher interface {
Store(string, string) error
GetPath(string) (string, error)
All() (map[string]string, error)
}
type routeCache struct {
rs map[string]string
s *BadgerStore
s Storer
}
var (
@ -20,13 +26,16 @@ var (
c *routeCache
)
func NewRouteCache() (*routeCache, error) {
func NewRouteCache(store Storer) (*routeCache, error) {
rcOnce.Do(func() {
store, err := NewBadgerStore(WithSubDir("route_cache"))
if err != nil {
log.Printf("failed to create badger store for route cache: %v", err)
c = nil
return
if store == nil {
st, err := NewBadgerStore(WithSubDir("route_cache"))
if err != nil {
log.Println(err)
return
}
store = st
}
c = &routeCache{
@ -53,7 +62,6 @@ func (r *routeCache) All() (map[string]string, error) {
}
func (r *routeCache) Store(name string, path string) error {
// TODO: implement
return nil
}

View File

@ -0,0 +1 @@
package internal

View File

@ -1,8 +0,0 @@
// Package joist implements the main Joist framework.
package joist
type RouteCacher interface {
Store(string, string) error
GetPath(string) (string, error)
All() (map[string]string, error)
}