planner/sync/service/memory.go

47 lines
805 B
Go
Raw Normal View History

2024-09-18 07:40:50 +02:00
package main
2024-08-20 08:34:11 +02:00
2024-08-22 07:18:50 +02:00
import (
2024-09-11 07:33:22 +02:00
"slices"
2024-09-23 07:48:18 +02:00
"sync"
2024-08-22 07:18:50 +02:00
"time"
2024-09-18 07:55:14 +02:00
2024-09-20 07:09:30 +02:00
"go-mod.ewintr.nl/planner/item"
2024-08-22 07:18:50 +02:00
)
2024-08-16 14:25:06 +02:00
type Memory struct {
2024-09-18 07:55:14 +02:00
items map[string]item.Item
2024-09-23 07:48:18 +02:00
mutex sync.RWMutex
2024-08-16 14:25:06 +02:00
}
func NewMemory() *Memory {
return &Memory{
2024-09-18 07:55:14 +02:00
items: make(map[string]item.Item),
2024-08-16 14:25:06 +02:00
}
}
2024-09-18 07:55:14 +02:00
func (m *Memory) Update(item item.Item) error {
2024-09-23 07:48:18 +02:00
m.mutex.Lock()
defer m.mutex.Unlock()
2024-08-28 07:21:02 +02:00
m.items[item.ID] = item
2024-08-23 10:52:17 +02:00
return nil
}
2024-09-18 07:55:14 +02:00
func (m *Memory) Updated(kinds []item.Kind, timestamp time.Time) ([]item.Item, error) {
2024-09-23 07:48:18 +02:00
m.mutex.RLock()
defer m.mutex.RUnlock()
2024-09-18 07:55:14 +02:00
result := make([]item.Item, 0)
2024-08-22 07:18:50 +02:00
for _, i := range m.items {
2024-09-11 07:33:22 +02:00
timeOK := timestamp.IsZero() || i.Updated.Equal(timestamp) || i.Updated.After(timestamp)
kindOK := len(kinds) == 0 || slices.Contains(kinds, i.Kind)
if timeOK && kindOK {
2024-08-22 07:18:50 +02:00
result = append(result, i)
}
}
return result, nil
}