[Issue #4] Add SnapshotStore unit tests (#31)
All checks were successful
CI / build (push) Successful in 15s
All checks were successful
CI / build (push) Successful in 15s
This commit was merged in pull request #31.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.flowmade.one/flowmade-one/aether"
|
||||
@@ -8,14 +9,16 @@ import (
|
||||
|
||||
// InMemoryEventStore provides a simple in-memory event store for testing
|
||||
type InMemoryEventStore struct {
|
||||
mu sync.RWMutex
|
||||
events map[string][]*aether.Event // actorID -> events
|
||||
mu sync.RWMutex
|
||||
events map[string][]*aether.Event // actorID -> events
|
||||
snapshots map[string][]*aether.ActorSnapshot // actorID -> snapshots (sorted by version)
|
||||
}
|
||||
|
||||
// NewInMemoryEventStore creates a new in-memory event store
|
||||
func NewInMemoryEventStore() *InMemoryEventStore {
|
||||
return &InMemoryEventStore{
|
||||
events: make(map[string][]*aether.Event),
|
||||
events: make(map[string][]*aether.Event),
|
||||
snapshots: make(map[string][]*aether.ActorSnapshot),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,3 +73,39 @@ func (es *InMemoryEventStore) GetLatestVersion(actorID string) (int64, error) {
|
||||
|
||||
return latestVersion, nil
|
||||
}
|
||||
|
||||
// SaveSnapshot saves a snapshot to the in-memory store
|
||||
func (es *InMemoryEventStore) SaveSnapshot(snapshot *aether.ActorSnapshot) error {
|
||||
if snapshot == nil {
|
||||
return fmt.Errorf("snapshot cannot be nil")
|
||||
}
|
||||
|
||||
es.mu.Lock()
|
||||
defer es.mu.Unlock()
|
||||
|
||||
if _, exists := es.snapshots[snapshot.ActorID]; !exists {
|
||||
es.snapshots[snapshot.ActorID] = make([]*aether.ActorSnapshot, 0)
|
||||
}
|
||||
es.snapshots[snapshot.ActorID] = append(es.snapshots[snapshot.ActorID], snapshot)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLatestSnapshot returns the most recent snapshot for an actor
|
||||
func (es *InMemoryEventStore) GetLatestSnapshot(actorID string) (*aether.ActorSnapshot, error) {
|
||||
es.mu.RLock()
|
||||
defer es.mu.RUnlock()
|
||||
|
||||
snapshots, exists := es.snapshots[actorID]
|
||||
if !exists || len(snapshots) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var latest *aether.ActorSnapshot
|
||||
for _, snapshot := range snapshots {
|
||||
if latest == nil || snapshot.Version > latest.Version {
|
||||
latest = snapshot
|
||||
}
|
||||
}
|
||||
|
||||
return latest, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user