2017-08-14 17:55:38 +00:00
|
|
|
{- git-annex vector clocks
|
|
|
|
-
|
|
|
|
- We don't have a way yet to keep true distributed vector clocks.
|
|
|
|
- The next best thing is a timestamp.
|
|
|
|
-
|
2020-12-23 19:21:33 +00:00
|
|
|
- Copyright 2017-2020 Joey Hess <id@joeyh.name>
|
2017-08-14 17:55:38 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2017-08-14 17:55:38 +00:00
|
|
|
-}
|
|
|
|
|
2020-12-23 19:21:33 +00:00
|
|
|
module Annex.VectorClock (
|
|
|
|
module Annex.VectorClock,
|
|
|
|
module Types.VectorClock,
|
|
|
|
) where
|
2017-08-14 17:55:38 +00:00
|
|
|
|
2020-12-23 19:21:33 +00:00
|
|
|
import Types.VectorClock
|
|
|
|
import Annex.Common
|
|
|
|
import qualified Annex
|
2018-10-30 03:13:36 +00:00
|
|
|
import Utility.TimeStamp
|
2020-12-23 19:21:33 +00:00
|
|
|
|
|
|
|
import Data.ByteString.Builder
|
2019-01-03 19:27:29 +00:00
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
2017-08-14 17:55:38 +00:00
|
|
|
|
2020-12-23 19:21:33 +00:00
|
|
|
currentVectorClock :: Annex VectorClock
|
|
|
|
currentVectorClock = liftIO =<< Annex.getState Annex.getvectorclock
|
2017-08-14 17:55:38 +00:00
|
|
|
|
2020-12-23 19:21:33 +00:00
|
|
|
-- Runs the action and uses the same vector clock throughout.
|
|
|
|
--
|
|
|
|
-- When the action modifies several files in the git-annex branch,
|
|
|
|
-- this can cause less space to be used, since the same vector clock
|
|
|
|
-- value is used, which can compress better.
|
|
|
|
--
|
|
|
|
-- However, this should not be used when running a long-duration action,
|
|
|
|
-- because the vector clock is based on the start of the action, and not on
|
|
|
|
-- the later points where it writes changes. For example, if this were
|
|
|
|
-- used across downloads of several files, the location log information
|
|
|
|
-- would have an earlier vector clock than necessary, which might cause it
|
|
|
|
-- to be disregarded in favor of other information that was collected
|
|
|
|
-- at an earlier point in time than when the transfers completted and the
|
|
|
|
-- log was written.
|
|
|
|
reuseVectorClockWhile :: Annex a -> Annex a
|
|
|
|
reuseVectorClockWhile = bracket setup cleanup . const
|
|
|
|
where
|
|
|
|
setup = do
|
|
|
|
origget <- Annex.getState Annex.getvectorclock
|
|
|
|
vc <- liftIO origget
|
|
|
|
use (pure vc)
|
|
|
|
return origget
|
2017-08-14 17:55:38 +00:00
|
|
|
|
2020-12-23 19:21:33 +00:00
|
|
|
cleanup origget = use origget
|
2017-08-14 17:55:38 +00:00
|
|
|
|
2020-12-23 19:21:33 +00:00
|
|
|
use vc = Annex.changeState $ \s ->
|
|
|
|
s { Annex.getvectorclock = vc }
|
2017-08-14 18:43:56 +00:00
|
|
|
|
|
|
|
formatVectorClock :: VectorClock -> String
|
2019-01-09 17:06:37 +00:00
|
|
|
formatVectorClock Unknown = "0"
|
2017-08-14 18:43:56 +00:00
|
|
|
formatVectorClock (VectorClock t) = show t
|
|
|
|
|
2019-01-09 17:06:37 +00:00
|
|
|
buildVectorClock :: VectorClock -> Builder
|
2019-01-09 18:17:00 +00:00
|
|
|
buildVectorClock = string7 . formatVectorClock
|
2019-01-09 17:06:37 +00:00
|
|
|
|
2017-08-14 18:43:56 +00:00
|
|
|
parseVectorClock :: String -> Maybe VectorClock
|
|
|
|
parseVectorClock t = VectorClock <$> parsePOSIXTime t
|
2019-01-03 19:27:29 +00:00
|
|
|
|
|
|
|
vectorClockParser :: A.Parser VectorClock
|
|
|
|
vectorClockParser = VectorClock <$> parserPOSIXTime
|