efficient but unsafe journal file append
authorJoey Hess <joeyh@joeyh.name>
Mon, 18 Jul 2022 17:47:56 +0000 (13:47 -0400)
committerJoey Hess <joeyh@joeyh.name>
Mon, 18 Jul 2022 18:17:12 +0000 (14:17 -0400)
This is only for checking performance, it's not safe.

Sponsored-by: Dartmouth College's DANDI project
Annex/Branch.hs
Annex/Journal.hs

index 4c84430b3c8a8852488de28bf72ec923f8fc8b37..93dae25617b95c61b855bea4d29a013369d0ea53 100644 (file)
@@ -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
index 11ea671faba6d698fb70a51c3d867e6eba5840a4..d02c42355d09ab951458c1082de471dbb9da537d 100644 (file)
@@ -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