storage dirs
This commit is contained in:
parent
c6a8fc3a24
commit
18d81c545d
|
@ -44,9 +44,9 @@ var AddCmd = &cli.Command{
|
|||
},
|
||||
}
|
||||
|
||||
func NewAddCmd(repo storage.EventRepo) *cli.Command {
|
||||
func NewAddCmd(_ storage.LocalIDRepo, eventRepo storage.EventRepo) *cli.Command {
|
||||
AddCmd.Action = func(cCtx *cli.Context) error {
|
||||
return Add(cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for"), repo)
|
||||
return Add(cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for"), eventRepo)
|
||||
}
|
||||
return AddCmd
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ var ListCmd = &cli.Command{
|
|||
Usage: "List everything",
|
||||
}
|
||||
|
||||
func NewListCmd(repo storage.EventRepo) *cli.Command {
|
||||
func NewListCmd(_ storage.LocalIDRepo, repo storage.EventRepo) *cli.Command {
|
||||
ListCmd.Action = NewListAction(repo)
|
||||
return ListCmd
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
repo, err := storage.NewSqlite(conf.DBPath)
|
||||
localIDRepo, eventRepo, err := storage.NewSqlites(conf.DBPath)
|
||||
if err != nil {
|
||||
fmt.Printf("could not open db file: %s\n", err)
|
||||
os.Exit(1)
|
||||
|
@ -33,8 +33,8 @@ func main() {
|
|||
Name: "plan",
|
||||
Usage: "Plan your day with events",
|
||||
Commands: []*cli.Command{
|
||||
command.NewAddCmd(repo),
|
||||
command.NewListCmd(repo),
|
||||
command.NewAddCmd(localIDRepo, eventRepo),
|
||||
command.NewListCmd(localIDRepo, eventRepo),
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
)
|
||||
|
||||
type Memory struct {
|
||||
events map[string]item.Event
|
||||
localIDs map[int]string
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemory() *Memory {
|
||||
return &Memory{
|
||||
events: make(map[string]item.Event),
|
||||
localIDs: make(map[int]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Memory) Find(id string) (item.Event, error) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
event, exists := r.events[id]
|
||||
if !exists {
|
||||
return item.Event{}, errors.New("event not found")
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (r *Memory) FindByLocal(localID int) (item.Event, error) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
id, exists := r.localIDs[localID]
|
||||
if !exists {
|
||||
return item.Event{}, errors.New("event not found")
|
||||
}
|
||||
|
||||
event, exists := r.events[id]
|
||||
if !exists {
|
||||
return item.Event{}, errors.New("id an localid mismatch")
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (r *Memory) FindAll() (map[int]string, []item.Event, error) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
events := make([]item.Event, 0, len(r.events))
|
||||
for _, event := range r.events {
|
||||
events = append(events, event)
|
||||
}
|
||||
sort.Slice(events, func(i, j int) bool {
|
||||
return events[i].ID < events[j].ID
|
||||
})
|
||||
|
||||
return r.localIDs, events, nil
|
||||
}
|
||||
|
||||
func (r *Memory) Store(e item.Event) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
if _, exists := r.events[e.ID]; !exists {
|
||||
cur := make([]int, 0, len(r.localIDs))
|
||||
for i := range r.localIDs {
|
||||
cur = append(cur, i)
|
||||
}
|
||||
localID := NextLocalID(cur)
|
||||
r.localIDs[localID] = e.ID
|
||||
}
|
||||
|
||||
r.events[e.ID] = e
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Memory) Delete(id string) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
if _, exists := r.events[id]; !exists {
|
||||
return errors.New("event not found")
|
||||
}
|
||||
delete(r.events, id)
|
||||
|
||||
for localID, eventID := range r.localIDs {
|
||||
if id == eventID {
|
||||
delete(r.localIDs, localID)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
)
|
||||
|
||||
type MemoryEvent struct {
|
||||
events map[string]item.Event
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemoryEvent() *MemoryEvent {
|
||||
return &MemoryEvent{
|
||||
events: make(map[string]item.Event),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MemoryEvent) Find(id string) (item.Event, error) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
event, exists := r.events[id]
|
||||
if !exists {
|
||||
return item.Event{}, errors.New("event not found")
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (r *MemoryEvent) FindAll() ([]item.Event, error) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
events := make([]item.Event, 0, len(r.events))
|
||||
for _, event := range r.events {
|
||||
events = append(events, event)
|
||||
}
|
||||
sort.Slice(events, func(i, j int) bool {
|
||||
return events[i].ID < events[j].ID
|
||||
})
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (r *MemoryEvent) Store(e item.Event) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
r.events[e.ID] = e
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MemoryEvent) Delete(id string) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
if _, exists := r.events[id]; !exists {
|
||||
return errors.New("event not found")
|
||||
}
|
||||
delete(r.events, id)
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package storage
|
||||
package memory
|
||||
|
||||
import (
|
||||
"testing"
|
|
@ -0,0 +1,64 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go-mod.ewintr.nl/planner/plan/storage"
|
||||
)
|
||||
|
||||
type MemoryLocalID struct {
|
||||
ids map[string]int
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemoryLocalID() *MemoryLocalID {
|
||||
return &MemoryLocalID{
|
||||
ids: make(map[string]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (ml *MemoryLocalID) FindAll() (map[string]int, error) {
|
||||
ml.mutex.RLock()
|
||||
defer ml.mutex.RUnlock()
|
||||
|
||||
return ml.ids, nil
|
||||
}
|
||||
|
||||
func (ml *MemoryLocalID) Next() (string, int, error) {
|
||||
ml.mutex.RLock()
|
||||
defer ml.mutex.RUnlock()
|
||||
|
||||
id := uuid.New().String()
|
||||
|
||||
cur := make([]int, 0, len(ml.ids))
|
||||
for _, i := range ml.ids {
|
||||
cur = append(cur, i)
|
||||
}
|
||||
|
||||
localID := storage.NextLocalID(cur)
|
||||
|
||||
return id, localID, nil
|
||||
}
|
||||
|
||||
func (ml *MemoryLocalID) Store(id string, localID int) error {
|
||||
ml.mutex.Lock()
|
||||
defer ml.mutex.Unlock()
|
||||
|
||||
ml.ids[id] = localID
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ml *MemoryLocalID) Delete(id string) error {
|
||||
ml.mutex.Lock()
|
||||
defer ml.mutex.Unlock()
|
||||
|
||||
if _, ok := ml.ids[id]; !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
delete(ml.ids, id)
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package storage
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
|
@ -1,4 +1,4 @@
|
|||
package storage
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
@ -17,6 +17,7 @@ var migrations = []string{
|
|||
`PRAGMA journal_mode=WAL`,
|
||||
`PRAGMA synchronous=NORMAL`,
|
||||
`PRAGMA cache_size=2000`,
|
||||
`CREATE TABLE localids ("id" TEXT UNIQUE, "local_id" INTEGER)`,
|
||||
}
|
||||
|
||||
var (
|
|
@ -1,13 +1,19 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
|
||||
"go-mod.ewintr.nl/planner/item"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
)
|
||||
|
||||
type LocalIDRepo interface {
|
||||
FindAll() (map[string]int, error)
|
||||
Next() (string, int, error)
|
||||
Store(id string, localID int) error
|
||||
Delete(id string) error
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue