diff options
Diffstat (limited to 'internal/object/store.go')
| -rw-r--r-- | internal/object/store.go | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/internal/object/store.go b/internal/object/store.go index a884601..a11ff7a 100644 --- a/internal/object/store.go +++ b/internal/object/store.go @@ -5,18 +5,21 @@ import ( "os" "path/filepath" + "thehouseoficarus/internal/action" "gopkg.in/yaml.v3" ) type ObjectStore struct { - dataDir string - cache map[string]*ObjectDef + dataDir string + pathIndex map[string]string + cache map[string]*ObjectDef } func NewObjectStore(dataDir string) *ObjectStore { return &ObjectStore{ - dataDir: dataDir, - cache: make(map[string]*ObjectDef), + dataDir: dataDir, + pathIndex: action.BuildPathIndex(filepath.Join(dataDir, "objects")), + cache: make(map[string]*ObjectDef), } } @@ -24,7 +27,10 @@ func (s *ObjectStore) Load(id string) (*ObjectDef, error) { if def, ok := s.cache[id]; ok { return def, nil } - path := filepath.Join(s.dataDir, "objects", fmt.Sprintf("%s.yaml", id)) + 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) @@ -36,3 +42,15 @@ func (s *ObjectStore) Load(id string) (*ObjectDef, error) { 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 +} |
