Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement deletion of persisted data #26

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,49 @@ func (db *DB) GetOrCreateCollection(name string, metadata map[string]string, emb

// DeleteCollection deletes the collection with the given name.
// If the collection doesn't exist, this is a no-op.
func (db *DB) DeleteCollection(name string) {
// If the DB is persistent, it also removes the collection's directory.
// You shouldn't hold any references to the collection after calling this method.
func (db *DB) DeleteCollection(name string) error {
db.collectionsLock.Lock()
defer db.collectionsLock.Unlock()

col, ok := db.collections[name]
if !ok {
return nil
}

if db.persistDirectory != "" {
collectionPath := col.persistDirectory
err := os.RemoveAll(collectionPath)
if err != nil {
return fmt.Errorf("couldn't delete collection directory: %w", err)
}
}

delete(db.collections, name)
return nil
}

// Reset removes all collections from the DB.
func (db *DB) Reset() {
// If the DB is persistent, it also removes all contents of the DB directory.
// You shouldn't hold any references to old collections after calling this method.
func (db *DB) Reset() error {
db.collectionsLock.Lock()
defer db.collectionsLock.Unlock()

if db.persistDirectory != "" {
err := os.RemoveAll(db.persistDirectory)
if err != nil {
return fmt.Errorf("couldn't delete persistence directory: %w", err)
}
// Recreate empty root level directory
err = os.MkdirAll(db.persistDirectory, 0o700)
if err != nil {
return fmt.Errorf("couldn't recreate persistence directory: %w", err)
}
}

// Just assign a new map, the GC will take care of the rest.
db.collections = make(map[string]*Collection)
return nil
}