101 lines
1.8 KiB
Go
101 lines
1.8 KiB
Go
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
|
|
}
|