avoid unncessary db queries when exported directory can't be empty

In rename foo/bar to foo/baz, foo can't be empty.

In delete zxyyz, there's no exported directory (top doesn't count).
This commit is contained in:
Joey Hess 2017-09-15 16:30:49 -04:00
parent af82b2229c
commit e54a05612e
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
3 changed files with 24 additions and 19 deletions

View file

@ -321,4 +321,6 @@ cleanupRename ea db ek src dest = do
removeExportLocation db (asKey ek) src removeExportLocation db (asKey ek) src
addExportLocation db (asKey ek) dest addExportLocation db (asKey ek) dest
flushDbQueue db flushDbQueue db
removeEmptyDirectories ea db src [asKey ek] if exportedDirectories src /= exportedDirectories dest
then removeEmptyDirectories ea db src [asKey ek]
else return True

View file

@ -151,18 +151,20 @@ adjustExportable r = case M.lookup "exporttree" (config r) of
-- exported file, and after calling removeExportLocation and flushing the -- exported file, and after calling removeExportLocation and flushing the
-- database. -- database.
removeEmptyDirectories :: ExportActions Annex -> ExportHandle -> ExportLocation -> [Key] -> Annex Bool removeEmptyDirectories :: ExportActions Annex -> ExportHandle -> ExportLocation -> [Key] -> Annex Bool
removeEmptyDirectories ea db loc ks = case removeExportDirectory ea of removeEmptyDirectories ea db loc ks
Nothing -> return True | null (exportedDirectories loc) = return True
Just removeexportdirectory -> do | otherwise = case removeExportDirectory ea of
ok <- allM (go removeexportdirectory) Nothing -> return True
(reverse (exportedDirectories loc)) Just removeexportdirectory -> do
unless ok $ liftIO $ do ok <- allM (go removeexportdirectory)
-- Add back to export database, so this is (reverse (exportedDirectories loc))
-- tried again next time. unless ok $ liftIO $ do
forM_ ks $ \k -> -- Add back to export database, so this is
addExportLocation db k loc -- tried again next time.
flushDbQueue db forM_ ks $ \k ->
return ok addExportLocation db k loc
flushDbQueue db
return ok
where where
go removeexportdirectory d = go removeexportdirectory d =
ifM (liftIO $ isExportDirectoryEmpty db d) ifM (liftIO $ isExportDirectoryEmpty db d)

View file

@ -27,8 +27,8 @@ module Types.Remote
where where
import qualified Data.Map as M import qualified Data.Map as M
import Data.Ord
import qualified System.FilePath.Posix as Posix import qualified System.FilePath.Posix as Posix
import Data.Ord
import qualified Git import qualified Git
import Types.Key import Types.Key
@ -201,13 +201,14 @@ data ExportActions a = ExportActions
, renameExport :: Key -> ExportLocation -> ExportLocation -> a Bool , renameExport :: Key -> ExportLocation -> ExportLocation -> a Bool
} }
-- | All directories down to the ExportLocation, with the deepest ones -- | All subdirectories down to the ExportLocation, with the deepest ones
-- last. -- last. Does not include the top of the export.
exportedDirectories :: ExportLocation -> [ExportDirectory] exportedDirectories :: ExportLocation -> [ExportDirectory]
exportedDirectories (ExportLocation f) = exportedDirectories (ExportLocation f) =
map (ExportDirectory . Posix.joinPath . reverse) $ map (ExportDirectory . Posix.joinPath . reverse) (subs [] dirs)
subs [] $ map Posix.dropTrailingPathSeparator $
Posix.splitPath $ Posix.takeDirectory f
where where
subs _ [] = [] subs _ [] = []
subs ps (d:ds) = (d:ps) : subs (d:ps) ds subs ps (d:ds) = (d:ps) : subs (d:ps) ds
dirs = map Posix.dropTrailingPathSeparator $
reverse $ drop 1 $ reverse $ Posix.splitPath f