@@ -643,7 +643,7 @@ func (s *ColumnStore) DropDB(name string) error {
643
643
s .mtx .Lock ()
644
644
defer s .mtx .Unlock ()
645
645
delete (s .dbs , name )
646
- return os .Remove (filepath .Join (s .DatabasesDir (), name ))
646
+ return os .RemoveAll (filepath .Join (s .DatabasesDir (), name ))
647
647
}
648
648
649
649
func (db * DB ) openWAL (ctx context.Context , opts ... wal.TestingOption ) (WAL , error ) {
@@ -668,6 +668,8 @@ func (db *DB) openWAL(ctx context.Context, opts ...wal.TestingOption) (WAL, erro
668
668
const (
669
669
walPath = "wal"
670
670
snapshotsPath = "snapshots"
671
+ indexPath = "index"
672
+ trashPath = "trash"
671
673
)
672
674
673
675
func (db * DB ) walDir () string {
@@ -679,11 +681,11 @@ func (db *DB) snapshotsDir() string {
679
681
}
680
682
681
683
func (db * DB ) trashDir () string {
682
- return filepath .Join (db .storagePath , "trash" )
684
+ return filepath .Join (db .storagePath , trashPath )
683
685
}
684
686
685
687
func (db * DB ) indexDir () string {
686
- return filepath .Join (db .storagePath , "index" )
688
+ return filepath .Join (db .storagePath , indexPath )
687
689
}
688
690
689
691
// recover attempts to recover database state from a combination of snapshots and the WAL.
@@ -1319,10 +1321,22 @@ func validateName(name string) bool {
1319
1321
return ! strings .Contains (name , "/" )
1320
1322
}
1321
1323
1322
- // dropStorage removes snapshots and WAL data from the storage directory.
1324
+ // dropStorage removes all data from the storage directory, but leaves the empty
1325
+ // storage directory.
1323
1326
func (db * DB ) dropStorage () error {
1324
1327
trashDir := db .trashDir ()
1325
1328
1329
+ entries , err := os .ReadDir (db .storagePath )
1330
+ if err != nil {
1331
+ if os .IsNotExist (err ) {
1332
+ // Nothing to drop.
1333
+ return nil
1334
+ }
1335
+ return err
1336
+ }
1337
+ // Try to rename all entries as this is O(1) per entry. We want to preserve
1338
+ // the storagePath for future opens of this database. Callers that want to
1339
+ // drop the DB remove storagePath themselves.
1326
1340
if moveErr := func () error {
1327
1341
if err := os .MkdirAll (trashDir , os .FileMode (0o755 )); err != nil {
1328
1342
return fmt .Errorf ("making trash dir: %w" , err )
@@ -1334,23 +1348,23 @@ func (db *DB) dropStorage() error {
1334
1348
if err != nil {
1335
1349
return err
1336
1350
}
1337
- if err := os . Rename ( db . snapshotsDir (), filepath . Join ( tmpPath , snapshotsPath )); err != nil && ! os . IsNotExist ( err ) {
1338
- return err
1339
- }
1340
- if err := os . Rename ( db . walDir (), filepath . Join ( tmpPath , walPath )); err != nil && ! os . IsNotExist ( err ) {
1341
- return err
1351
+ errs := make ([] error , 0 , len ( entries ))
1352
+ for _ , e := range entries {
1353
+ if err := os . Rename ( filepath . Join ( db . storagePath , e . Name ()), filepath . Join ( tmpPath , e . Name ())); err != nil && ! os . IsNotExist ( err ) {
1354
+ errs = append ( errs , err )
1355
+ }
1342
1356
}
1343
- return nil
1357
+ return errors . Join ( errs ... )
1344
1358
}(); moveErr != nil {
1345
- // If we failed to move the wal/snapshots to the trash dir, fall
1346
- // back to attempting to remove them with RemoveAll.
1347
- if err := os . RemoveAll ( db . snapshotsDir ()); err != nil {
1348
- return fmt . Errorf ( "%v: %v" , moveErr , err )
1349
- }
1350
- if err := os . RemoveAll ( db . walDir ()); err != nil {
1351
- return fmt . Errorf ( "%v: %v" , moveErr , err )
1359
+ // If we failed to move storage path entries to the trash dir, fall back
1360
+ // to attempting to remove them with RemoveAll.
1361
+ errs := make ([] error , 0 , len ( entries ))
1362
+ for _ , e := range entries {
1363
+ if err := os . RemoveAll ( filepath . Join ( db . storagePath , e . Name ())); err != nil {
1364
+ errs = append ( errs , err )
1365
+ }
1352
1366
}
1353
- return moveErr
1367
+ return errors . Join ( errs ... )
1354
1368
}
1355
1369
return os .RemoveAll (trashDir )
1356
1370
}
0 commit comments