2014-03-15 17:44:31 +00:00
|
|
|
{- git-annex Map log
|
|
|
|
-
|
|
|
|
- This is used to store a Map, in a way that can be union merged.
|
|
|
|
-
|
|
|
|
- A line of the log will look like: "timestamp field value"
|
|
|
|
-
|
2019-01-10 17:23:42 +00:00
|
|
|
- The field names cannot contain whitespace.
|
|
|
|
-
|
2019-01-09 17:06:37 +00:00
|
|
|
- Copyright 2014, 2019 Joey Hess <id@joeyh.name>
|
2014-03-15 17:44:31 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-03-15 17:44:31 +00:00
|
|
|
-}
|
|
|
|
|
2017-08-14 17:55:38 +00:00
|
|
|
module Logs.MapLog (
|
|
|
|
module Logs.MapLog,
|
|
|
|
VectorClock,
|
|
|
|
currentVectorClock,
|
|
|
|
) where
|
2014-10-09 19:09:26 +00:00
|
|
|
|
2014-03-15 17:44:31 +00:00
|
|
|
import Common
|
2017-08-14 17:55:38 +00:00
|
|
|
import Annex.VectorClock
|
2016-05-27 15:45:13 +00:00
|
|
|
import Logs.Line
|
2019-02-21 16:22:50 +00:00
|
|
|
import Utility.QuickCheck
|
2014-03-15 17:44:31 +00:00
|
|
|
|
2019-01-10 17:23:42 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2018-04-22 17:28:31 +00:00
|
|
|
import qualified Data.Map.Strict as M
|
2019-01-10 17:23:42 +00:00
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A8
|
2019-01-09 17:06:37 +00:00
|
|
|
import Data.ByteString.Builder
|
2014-03-15 17:44:31 +00:00
|
|
|
|
|
|
|
data LogEntry v = LogEntry
|
2017-08-14 17:55:38 +00:00
|
|
|
{ changed :: VectorClock
|
2014-03-15 17:44:31 +00:00
|
|
|
, value :: v
|
2019-01-10 17:23:42 +00:00
|
|
|
} deriving (Eq, Show)
|
2014-03-15 17:44:31 +00:00
|
|
|
|
2019-02-21 16:22:50 +00:00
|
|
|
instance Arbitrary v => Arbitrary (LogEntry v) where
|
|
|
|
arbitrary = LogEntry <$> arbitrary <*> arbitrary
|
|
|
|
|
2014-03-15 17:44:31 +00:00
|
|
|
type MapLog f v = M.Map f (LogEntry v)
|
|
|
|
|
2019-01-09 17:06:37 +00:00
|
|
|
buildMapLog :: (f -> Builder) -> (v -> Builder) -> MapLog f v -> Builder
|
|
|
|
buildMapLog fieldbuilder valuebuilder = mconcat . map genline . M.toList
|
2014-03-15 17:44:31 +00:00
|
|
|
where
|
2019-01-09 17:06:37 +00:00
|
|
|
genline (f, LogEntry c v) =
|
|
|
|
buildVectorClock c <> sp
|
|
|
|
<> fieldbuilder f <> sp
|
|
|
|
<> valuebuilder v <> nl
|
|
|
|
sp = charUtf8 ' '
|
|
|
|
nl = charUtf8 '\n'
|
|
|
|
|
2019-01-10 17:23:42 +00:00
|
|
|
parseMapLog :: Ord f => A.Parser f -> A.Parser v -> L.ByteString -> MapLog f v
|
|
|
|
parseMapLog fieldparser valueparser = fromMaybe M.empty . A.maybeResult
|
|
|
|
. A.parse (mapLogParser fieldparser valueparser)
|
|
|
|
|
|
|
|
mapLogParser :: Ord f => A.Parser f -> A.Parser v -> A.Parser (MapLog f v)
|
|
|
|
mapLogParser fieldparser valueparser = M.fromListWith best <$> parseLogLines go
|
2014-03-15 17:44:31 +00:00
|
|
|
where
|
2019-01-10 17:23:42 +00:00
|
|
|
go = do
|
|
|
|
c <- vectorClockParser
|
|
|
|
_ <- A8.char ' '
|
|
|
|
w <- A8.takeTill (== ' ')
|
|
|
|
f <- either fail return $
|
|
|
|
A.parseOnly (fieldparser <* A.endOfInput) w
|
|
|
|
_ <- A8.char ' '
|
|
|
|
v <- valueparser
|
|
|
|
A.endOfInput
|
|
|
|
return (f, LogEntry c v)
|
2014-03-15 17:44:31 +00:00
|
|
|
|
2017-08-14 17:55:38 +00:00
|
|
|
changeMapLog :: Ord f => VectorClock -> f -> v -> MapLog f v -> MapLog f v
|
|
|
|
changeMapLog c f v = M.insert f $ LogEntry c v
|
2014-03-15 17:44:31 +00:00
|
|
|
|
|
|
|
{- Only add an LogEntry if it's newer (or at least as new as) than any
|
|
|
|
- existing LogEntry for a field. -}
|
|
|
|
addMapLog :: Ord f => f -> LogEntry v -> MapLog f v -> MapLog f v
|
2018-04-22 17:28:31 +00:00
|
|
|
addMapLog = M.insertWith best
|
2014-03-15 17:44:31 +00:00
|
|
|
|
|
|
|
{- Converts a MapLog into a simple Map without the timestamp information.
|
|
|
|
- This is a one-way trip, but useful for code that never needs to change
|
|
|
|
- the log. -}
|
|
|
|
simpleMap :: MapLog f v -> M.Map f v
|
|
|
|
simpleMap = M.map value
|
|
|
|
|
|
|
|
best :: LogEntry v -> LogEntry v -> LogEntry v
|
|
|
|
best new old
|
|
|
|
| changed old > changed new = old
|
|
|
|
| otherwise = new
|
|
|
|
|
|
|
|
prop_addMapLog_sane :: Bool
|
|
|
|
prop_addMapLog_sane = newWins && newestWins
|
|
|
|
where
|
2017-08-14 17:55:38 +00:00
|
|
|
newWins = addMapLog ("foo") (LogEntry (VectorClock 1) "new") l == l2
|
|
|
|
newestWins = addMapLog ("foo") (LogEntry (VectorClock 1) "newest") l2 /= l2
|
2014-03-15 17:44:31 +00:00
|
|
|
|
2017-08-14 17:55:38 +00:00
|
|
|
l = M.fromList [("foo", LogEntry (VectorClock 0) "old")]
|
|
|
|
l2 = M.fromList [("foo", LogEntry (VectorClock 1) "new")]
|