ProofedMesh/services/verification-service/internal/store/memory.go

38 lines
872 B
Go
Raw Permalink Normal View History

package store
import (
"context"
"sync"
)
// Memory is the Phase-1 in-process Store. Thread-safe via a single mutex.
type Memory struct {
mu sync.Mutex
decisions map[string]Decision
}
// NewMemory returns an empty Memory store.
func NewMemory() *Memory {
return &Memory{decisions: map[string]Decision{}}
}
// Reserve inserts d.DecisionID → d. On duplicate it returns ErrReplay
// and leaves the stored row untouched.
func (m *Memory) Reserve(_ context.Context, d Decision) error {
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.decisions[d.DecisionID]; exists {
return ErrReplay
}
m.decisions[d.DecisionID] = d
return nil
}
// Get returns the stored decision, if any.
func (m *Memory) Get(_ context.Context, decisionID string) (Decision, bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
d, ok := m.decisions[decisionID]
return d, ok, nil
}