aboutsummaryrefslogtreecommitdiff
path: root/internal/object/store.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/object/store.go')
-rw-r--r--internal/object/store.go19
1 files changed, 17 insertions, 2 deletions
diff --git a/internal/object/store.go b/internal/object/store.go
index 61cb007..997c324 100644
--- a/internal/object/store.go
+++ b/internal/object/store.go
@@ -4,12 +4,14 @@ import (
"fmt"
"os"
"path/filepath"
+ "sync"
"gopkg.in/yaml.v3"
"thehouseoficarus/internal/behavior"
)
type ObjectStore struct {
+ mu sync.Mutex
dataDir string
pathIndex map[string]string
cache map[string]*ObjectDef
@@ -24,6 +26,8 @@ func NewObjectStore(dataDir string) *ObjectStore {
}
func (s *ObjectStore) Load(id string) (*ObjectDef, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
if def, ok := s.cache[id]; ok {
return def, nil
}
@@ -44,12 +48,21 @@ func (s *ObjectStore) Load(id string) (*ObjectDef, error) {
return &def, nil
}
+// PathIndex returns a copy of the id→path index, safe to iterate concurrently.
func (s *ObjectStore) PathIndex() map[string]string {
- return s.pathIndex
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ out := make(map[string]string, len(s.pathIndex))
+ for id, p := range s.pathIndex {
+ out[id] = p
+ }
+ return out
}
func (s *ObjectStore) IDSet() map[string]bool {
- ids := make(map[string]bool)
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ ids := make(map[string]bool, len(s.pathIndex))
for id := range s.pathIndex {
ids[id] = true
}
@@ -57,6 +70,8 @@ func (s *ObjectStore) IDSet() map[string]bool {
}
func (s *ObjectStore) Reload(dataDir string) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
s.cache = make(map[string]*ObjectDef)
s.pathIndex = behavior.BuildPathIndex(filepath.Join(dataDir, "objects"))
}