2018-09-05 17:20:10 +00:00
|
|
|
{- git-annex single-value log, pure operations
|
|
|
|
-
|
2019-01-07 19:51:05 +00:00
|
|
|
- Copyright 2014-2019 Joey Hess <id@joeyh.name>
|
2018-09-05 17:20:10 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.SingleValue.Pure where
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Logs.Line
|
|
|
|
import Annex.VectorClock
|
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2019-01-07 19:51:05 +00:00
|
|
|
import qualified Data.ByteString as B
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
|
|
|
import Data.Attoparsec.ByteString.Char8 (char)
|
|
|
|
import Data.ByteString.Builder
|
2018-09-05 17:20:10 +00:00
|
|
|
|
|
|
|
class SingleValueSerializable v where
|
2019-01-07 19:51:05 +00:00
|
|
|
serialize :: v -> B.ByteString
|
|
|
|
deserialize :: B.ByteString -> Maybe v
|
2018-09-05 17:20:10 +00:00
|
|
|
|
|
|
|
data LogEntry v = LogEntry
|
|
|
|
{ changed :: VectorClock
|
|
|
|
, value :: v
|
|
|
|
} deriving (Eq, Ord)
|
|
|
|
|
|
|
|
type Log v = S.Set (LogEntry v)
|
|
|
|
|
2019-01-07 19:51:05 +00:00
|
|
|
buildLog :: (SingleValueSerializable v) => Log v -> Builder
|
|
|
|
buildLog = mconcat . map genline . S.toList
|
2018-09-05 17:20:10 +00:00
|
|
|
where
|
2019-01-07 19:51:05 +00:00
|
|
|
genline (LogEntry c v) =
|
2019-01-09 17:06:37 +00:00
|
|
|
buildVectorClock c <> sp <> byteString (serialize v) <> nl
|
2019-01-07 19:51:05 +00:00
|
|
|
sp = charUtf8 ' '
|
|
|
|
nl = charUtf8 '\n'
|
|
|
|
|
|
|
|
parseLog :: (Ord v, SingleValueSerializable v) => L.ByteString -> Log v
|
|
|
|
parseLog = S.fromList . fromMaybe []
|
|
|
|
. A.maybeResult . A.parse (logParser <* A.endOfInput)
|
|
|
|
|
|
|
|
logParser :: SingleValueSerializable v => A.Parser [LogEntry v]
|
|
|
|
logParser = parseLogLines $ LogEntry
|
|
|
|
<$> vectorClockParser
|
|
|
|
<* char ' '
|
|
|
|
<*> (parsevalue =<< A.takeByteString)
|
2018-09-05 17:20:10 +00:00
|
|
|
where
|
2019-01-07 19:51:05 +00:00
|
|
|
parsevalue = maybe (fail "log line parse failure") return . deserialize
|
2018-09-05 17:20:10 +00:00
|
|
|
|
|
|
|
newestValue :: Log v -> Maybe v
|
|
|
|
newestValue s
|
|
|
|
| S.null s = Nothing
|
|
|
|
| otherwise = Just (value $ S.findMax s)
|