dropdead per-remote metadata

Had to refactor pure code into separate modules so it is accessible
inside Annex.Branch.Transitions.

This commit was sponsored by Peter on Patreon.
This commit is contained in:
Joey Hess 2018-09-05 13:20:10 -04:00
parent f1e5dfb7c7
commit 0a7c5a9982
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
9 changed files with 197 additions and 109 deletions

View file

@ -11,46 +11,20 @@
- Licensed under the GNU GPL version 3 or higher.
-}
module Logs.SingleValue where
module Logs.SingleValue (
module Logs.SingleValue.Pure,
readLog,
getLog,
setLog,
) where
import Annex.Common
import qualified Annex.Branch
import Logs.Line
import Logs.SingleValue.Pure
import Annex.VectorClock
import qualified Data.Set as S
class SingleValueSerializable v where
serialize :: v -> String
deserialize :: String -> Maybe v
data LogEntry v = LogEntry
{ changed :: VectorClock
, value :: v
} deriving (Eq, Ord)
type Log v = S.Set (LogEntry v)
showLog :: (SingleValueSerializable v) => Log v -> String
showLog = unlines . map showline . S.toList
where
showline (LogEntry c v) = unwords [formatVectorClock c, serialize v]
parseLog :: (Ord v, SingleValueSerializable v) => String -> Log v
parseLog = S.fromList . mapMaybe parse . splitLines
where
parse line = do
let (sc, s) = splitword line
c <- parseVectorClock sc
v <- deserialize s
Just (LogEntry c v)
splitword = separate (== ' ')
newestValue :: Log v -> Maybe v
newestValue s
| S.null s = Nothing
| otherwise = Just (value $ S.findMax s)
readLog :: (Ord v, SingleValueSerializable v) => FilePath -> Annex (Log v)
readLog = parseLog <$$> Annex.Branch.get