Skip to content

Commit

Permalink
Merge pull request #26 from philippgille/persistence-delete
Browse files Browse the repository at this point in the history
Implement deletion of persisted data
  • Loading branch information
philippgille authored Feb 28, 2024
2 parents 2b8a849 + 11087cb commit d728d41
Showing 1 changed file with 36 additions and 2 deletions.
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
}

0 comments on commit d728d41

Please sign in to comment.