Skip to content

Commit

Permalink
Merge pull request #57 from philippgille/improve-persistent-db-loading
Browse files Browse the repository at this point in the history
Improve loading of persistent DB
  • Loading branch information
philippgille authored Mar 18, 2024
2 parents 420c3bc + 8484e27 commit 9ab9af9
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ func NewPersistentDB(path string) (*DB, error) {
return nil, fmt.Errorf("couldn't read collection directory: %w", err)
}
c := &Collection{
// We can fill Name, persistDirectory and metadata only after reading
persistDirectory: collectionPath,
documents: make(map[string]*Document),
// We can fill Name and metadata only after reading
// the metadata.
documents: make(map[string]*Document),
// We can fill embed only when the user calls DB.GetCollection() or
// DB.GetOrCreateCollection().
}
Expand All @@ -124,7 +125,6 @@ func NewPersistentDB(path string) (*DB, error) {
return nil, fmt.Errorf("couldn't read collection metadata: %w", err)
}
c.Name = pc.Name
c.persistDirectory = filepath.Dir(collectionPath)
c.metadata = pc.Metadata
} else if filepath.Ext(collectionDirEntry.Name()) == ".gob" {
// Read document
Expand All @@ -139,6 +139,16 @@ func NewPersistentDB(path string) (*DB, error) {
continue
}
}
// If we have neither name nor documents, it was likely a user-added
// directory, so skip it.
if c.Name == "" && len(c.documents) == 0 {
continue
}
// If we have no name, it means there was no metadata file
if c.Name == "" {
return nil, fmt.Errorf("collection metadata file not found: %s", collectionPath)
}

db.collections[c.Name] = c
}

Expand Down

0 comments on commit 9ab9af9

Please sign in to comment.