2015-05-10 18:45:55 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
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"
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-03-15 17:44:31 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
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
|
2014-03-15 17:44:31 +00:00
|
|
|
|
2018-04-22 17:28:31 +00:00
|
|
|
import qualified Data.Map.Strict as M
|
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
|
2017-08-14 18:43:56 +00:00
|
|
|
} deriving (Eq)
|
2014-03-15 17:44:31 +00:00
|
|
|
|
|
|
|
type MapLog f v = M.Map f (LogEntry v)
|
|
|
|
|
|
|
|
showMapLog :: (f -> String) -> (v -> String) -> MapLog f v -> String
|
|
|
|
showMapLog fieldshower valueshower = unlines . map showpair . M.toList
|
|
|
|
where
|
2017-08-14 17:55:38 +00:00
|
|
|
showpair (f, LogEntry (VectorClock c) v) =
|
|
|
|
unwords [show c, fieldshower f, valueshower v]
|
2014-03-15 17:44:31 +00:00
|
|
|
showpair (f, LogEntry Unknown v) =
|
|
|
|
unwords ["0", fieldshower f, valueshower v]
|
|
|
|
|
|
|
|
parseMapLog :: Ord f => (String -> Maybe f) -> (String -> Maybe v) -> String -> MapLog f v
|
2016-05-27 15:45:13 +00:00
|
|
|
parseMapLog fieldparser valueparser = M.fromListWith best . mapMaybe parse . splitLines
|
2014-03-15 17:44:31 +00:00
|
|
|
where
|
|
|
|
parse line = do
|
2017-08-14 18:43:56 +00:00
|
|
|
let (sc, rest) = splitword line
|
2014-03-15 17:44:31 +00:00
|
|
|
(sf, sv) = splitword rest
|
2017-08-14 18:43:56 +00:00
|
|
|
c <- parseVectorClock sc
|
2014-03-15 17:44:31 +00:00
|
|
|
f <- fieldparser sf
|
|
|
|
v <- valueparser sv
|
2017-08-14 17:55:38 +00:00
|
|
|
Just (f, LogEntry c v)
|
2014-03-15 17:44:31 +00:00
|
|
|
splitword = separate (== ' ')
|
|
|
|
|
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")]
|