From ce95d9e0ce4ec43765cb2f2b110953b75b5b1ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Gill=C3=A9?= Date: Thu, 21 Mar 2024 23:48:59 +0100 Subject: [PATCH] WIP No need to create dirs ahead of time, when persist does it as well. Maybe this could go into a separate PR. Also comment in NewPersistentDB that we keep doing it to have an early error instead of user only getting one on first collection creation. --- collection.go | 8 +------- db.go | 9 +-------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/collection.go b/collection.go index 11bdc73..5c9c893 100644 --- a/collection.go +++ b/collection.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "os" "path/filepath" "slices" "sync" @@ -48,11 +47,6 @@ func newCollection(name string, metadata map[string]string, embed EmbeddingFunc, if dbDir != "" { safeName := hash2hex(name) c.persistDirectory = filepath.Join(dbDir, safeName) - // Create dir - err := os.MkdirAll(c.persistDirectory, 0o700) - if err != nil { - return nil, fmt.Errorf("couldn't create collection directory: %w", err) - } // Persist name and metadata metadataPath := filepath.Join(c.persistDirectory, metadataFileName) metadataPath += ".gob" @@ -63,7 +57,7 @@ func newCollection(name string, metadata map[string]string, embed EmbeddingFunc, Name: name, Metadata: m, } - err = persist(metadataPath, pc, false, "") + err := persist(metadataPath, pc, false, "") if err != nil { return nil, fmt.Errorf("couldn't persist collection metadata: %w", err) } diff --git a/db.go b/db.go index 46679ce..99910bf 100644 --- a/db.go +++ b/db.go @@ -251,13 +251,6 @@ func (db *DB) Export(filePath string, compress bool, encryptionKey string) error } } - // Create parent dir if it doesn't exist - parentDir := filepath.Dir(filePath) - err := os.MkdirAll(parentDir, 0o700) - if err != nil { - return fmt.Errorf("couldn't create parent directory: %w", err) - } - // Create persistence structs with exported fields so that they can be encoded // as gob. type persistenceCollection struct { @@ -282,7 +275,7 @@ func (db *DB) Export(filePath string, compress bool, encryptionKey string) error } } - err = persist(filePath, persistenceDB, compress, encryptionKey) + err := persist(filePath, persistenceDB, compress, encryptionKey) if err != nil { return fmt.Errorf("couldn't export DB: %w", err) }