package object import ( "fmt" "os" "path/filepath" "gopkg.in/yaml.v3" "thehouseoficarus/internal/behavior" ) type ObjectStore struct { dataDir string pathIndex map[string]string cache map[string]*ObjectDef } func NewObjectStore(dataDir string) *ObjectStore { return &ObjectStore{ dataDir: dataDir, pathIndex: behavior.BuildPathIndex(filepath.Join(dataDir, "objects")), cache: make(map[string]*ObjectDef), } } func (s *ObjectStore) Load(id string) (*ObjectDef, error) { if def, ok := s.cache[id]; ok { return def, nil } path, ok := s.pathIndex[id] if !ok { return nil, fmt.Errorf("read object %s: no such object", id) } data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("read object %s: %w", id, err) } var def ObjectDef if err := yaml.Unmarshal(data, &def); err != nil { return nil, fmt.Errorf("parse object %s: %w", id, err) } def.ID = id s.cache[id] = &def return &def, nil } func (s *ObjectStore) PathIndex() map[string]string { return s.pathIndex } func (s *ObjectStore) IDSet() map[string]bool { ids := make(map[string]bool) for id := range s.pathIndex { ids[id] = true } return ids } func (s *ObjectStore) Reload(dataDir string) { s.cache = make(map[string]*ObjectDef) s.pathIndex = behavior.BuildPathIndex(filepath.Join(dataDir, "objects")) }