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" 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 { func WithBaseDir(baseDir string) BadgerOpts {
return func(b *BadgerStore) { return func(b *badgerStore) {
b.baseDir = baseDir b.baseDir = baseDir
} }
} }
func WithSubDir(subDir string) BadgerOpts { func WithSubDir(subDir string) BadgerOpts {
return func(b *BadgerStore) { return func(b *badgerStore) {
b.subDir = subDir b.subDir = subDir
} }
} }
type BadgerStore struct { type badgerStore struct {
baseDir string baseDir string
subDir string subDir string
db *badger.DB 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) done := make(chan error, 1)
go func() { 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 { done := make(chan struct {
val []byte val []byte
err error 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) done := make(chan error, 1)
go func() { 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() return b.db.Close()
} }
func (b *BadgerStore) dir() string { func (b *badgerStore) dir() string {
if b.baseDir == "" { if b.baseDir == "" {
b.baseDir = defaultBaseDir b.baseDir = defaultBaseDir
} }
@ -115,8 +122,8 @@ func (b *BadgerStore) dir() string {
return filepath.Join(b.baseDir, b.subDir) return filepath.Join(b.baseDir, b.subDir)
} }
func NewBadgerStore(opts ...BadgerOpts) (*BadgerStore, error) { func NewBadgerStore(opts ...BadgerOpts) (*badgerStore, error) {
s := &BadgerStore{} s := &badgerStore{}
for _, opt := range opts { for _, opt := range opts {
opt(s) opt(s)

View File

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

View File

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