From de18d92de62e52f20267b52601edfa44e636b92f Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 18 Jul 2022 13:47:56 -0400 Subject: [PATCH] efficient but unsafe journal file append This is only for checking performance, it's not safe. Sponsored-by: Dartmouth College's DANDI project --- Annex/Branch.hs | 2 +- Annex/Journal.hs | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Annex/Branch.hs b/Annex/Branch.hs index 4c84430b3c..93dae25617 100644 --- a/Annex/Branch.hs +++ b/Annex/Branch.hs @@ -414,7 +414,7 @@ data ChangeOrAppend t = Change t | Append t -} changeOrAppend :: Journalable content => RegardingUUID -> RawFilePath -> (L.ByteString -> ChangeOrAppend content) -> Annex () changeOrAppend ru file f = lockJournal $ \jl -> do - oldc <- getToChange jl ru file + oldc <- getToChange ru file case f oldc of Change newc -> set jl ru file newc Append toappend -> append jl ru file oldc toappend diff --git a/Annex/Journal.hs b/Annex/Journal.hs index 11ea671fab..d02c42355d 100644 --- a/Annex/Journal.hs +++ b/Annex/Journal.hs @@ -92,21 +92,27 @@ setJournalFile _jl ru file content = withOtherTmp $ \tmp -> do -- exists write `catchIO` (const (createAnnexDirectory jd >> write)) -{- Appends content to a journal file. +{- Appends content to a journal file. - - - TODO: Inefficient! -} + - The oldcontent is whatever is in the git-annex branch. + - When the journal file does not yet exist, the oldcontent + - is first written to the journal file. + - + - TODO: Unsafe! Does not append atomically. -} appendJournalFile :: Journalable content => JournalLocked -> RegardingUUID -> RawFilePath -> L.ByteString -> content -> Annex () -appendJournalFile _jl ru file oldcontent toappend = withOtherTmp $ \tmp -> do +appendJournalFile _jl ru file oldcontent toappend = do jd <- fromRepo =<< ifM (regardingPrivateUUID ru) ( return gitAnnexPrivateJournalDir , return gitAnnexJournalDir ) - let jfile = journalFile file - let tmpfile = tmp P. jfile - let write = liftIO $ do - withFile (fromRawFilePath tmpfile) WriteMode $ \h -> do + let jfile = fromRawFilePath $ jd P. journalFile file + let write = liftIO $ ifM (doesFileExist jfile) + ( withFile jfile AppendMode $ \h -> + writeJournalHandle h toappend + , withFile jfile WriteMode $ \h -> do writeJournalHandle h oldcontent writeJournalHandle h toappend + ) write `catchIO` (const (createAnnexDirectory jd >> write)) data JournalledContent