2024-09-19 14:57:11 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"git.defalsify.org/vise.git/db"
|
|
|
|
fsdb "git.defalsify.org/vise.git/db/fs"
|
|
|
|
gdbmdb "git.defalsify.org/vise.git/db/gdbm"
|
|
|
|
"git.defalsify.org/vise.git/persist"
|
|
|
|
"git.defalsify.org/vise.git/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StorageService interface {
|
2024-09-21 19:49:11 +02:00
|
|
|
GetPersister(ctx context.Context) (*persist.Persister, error)
|
|
|
|
GetUserdataDb(ctx context.Context) db.Db
|
|
|
|
GetResource(ctx context.Context) (resource.Resource, error)
|
|
|
|
EnsureDbDir() error
|
2024-09-19 14:57:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
type MenuStorageService struct{
|
|
|
|
dbDir string
|
|
|
|
resourceDir string
|
|
|
|
}
|
2024-09-19 14:57:11 +02:00
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService {
|
|
|
|
return &MenuStorageService{
|
|
|
|
dbDir: dbDir,
|
|
|
|
resourceDir: resourceDir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
|
2024-09-19 14:57:11 +02:00
|
|
|
store := gdbmdb.NewGdbmDb()
|
2024-09-21 19:49:11 +02:00
|
|
|
storeFile := path.Join(ms.dbDir, "state.gdbm")
|
2024-09-19 14:57:11 +02:00
|
|
|
store.Connect(ctx, storeFile)
|
|
|
|
pr := persist.NewPersister(store)
|
|
|
|
return pr, nil
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) db.Db {
|
2024-09-19 14:57:11 +02:00
|
|
|
store := gdbmdb.NewGdbmDb()
|
2024-09-21 19:49:11 +02:00
|
|
|
storeFile := path.Join(ms.dbDir, "userdata.gdbm")
|
2024-09-19 14:57:11 +02:00
|
|
|
store.Connect(ctx, storeFile)
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
func (ms *MenuStorageService) GetResource(ctx context.Context) (resource.Resource, error) {
|
2024-09-19 14:57:11 +02:00
|
|
|
store := fsdb.NewFsDb()
|
2024-09-21 19:49:11 +02:00
|
|
|
err := store.Connect(ctx, ms.resourceDir)
|
2024-09-19 14:57:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rfs := resource.NewDbResource(store)
|
|
|
|
return rfs, nil
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error) {
|
2024-09-19 14:57:11 +02:00
|
|
|
store := gdbmdb.NewGdbmDb()
|
2024-09-21 19:49:11 +02:00
|
|
|
storeFile := path.Join(ms.dbDir, "state.gdbm")
|
2024-09-19 14:57:11 +02:00
|
|
|
store.Connect(ctx, storeFile)
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
func (ms *MenuStorageService) EnsureDbDir() error {
|
|
|
|
err := os.MkdirAll(ms.dbDir, 0700)
|
2024-09-19 14:57:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|