diff options
Diffstat (limited to 'Logs')
-rw-r--r-- | Logs/RemoteState.hs | 33 | ||||
-rw-r--r-- | Logs/UUIDBased.hs | 25 |
2 files changed, 57 insertions, 1 deletions
diff --git a/Logs/RemoteState.hs b/Logs/RemoteState.hs new file mode 100644 index 000000000..95e51832e --- /dev/null +++ b/Logs/RemoteState.hs @@ -0,0 +1,33 @@ +{- Remote state logs. + - + - Copyright 2013 Joey Hess <joey@kitenet.net> + - + - Licensed under the GNU GPL version 3 or higher. + -} + +module Logs.RemoteState ( + getRemoteState, + setRemoteState, +) where + +import Common.Annex +import Logs +import Logs.UUIDBased +import qualified Annex.Branch + +import qualified Data.Map as M +import Data.Time.Clock.POSIX + +type RemoteState = String + +setRemoteState :: UUID -> Key -> RemoteState -> Annex () +setRemoteState u k s = do + ts <- liftIO getPOSIXTime + Annex.Branch.change (remoteStateLogFile k) $ + showLogNew id . changeLog ts u s . parseLogNew Just + +getRemoteState :: UUID -> Key -> Annex (Maybe RemoteState) +getRemoteState u k = extract . parseLogNew Just + <$> Annex.Branch.get (remoteStateLogFile k) + where + extract m = value <$> M.lookup u m diff --git a/Logs/UUIDBased.hs b/Logs/UUIDBased.hs index 10b3bf55d..430c92d55 100644 --- a/Logs/UUIDBased.hs +++ b/Logs/UUIDBased.hs @@ -6,8 +6,10 @@ - A line of the log will look like: "UUID[ INFO[ timestamp=foo]]" - The timestamp is last for backwards compatability reasons, - and may not be present on old log lines. + - + - New uuid based logs instead use the form: "timestamp UUID INFO" - - - Copyright 2011 Joey Hess <joey@kitenet.net> + - Copyright 2011-2013 Joey Hess <joey@kitenet.net> - - Licensed under the GNU GPL version 3 or higher. -} @@ -17,8 +19,10 @@ module Logs.UUIDBased ( LogEntry(..), TimeStamp(..), parseLog, + parseLogNew, parseLogWithUUID, showLog, + showLogNew, changeLog, addLog, simpleMap, @@ -56,6 +60,14 @@ showLog shower = unlines . map showpair . M.toList showpair (k, LogEntry Unknown v) = unwords [fromUUID k, shower v] +showLogNew :: (a -> String) -> Log a -> String +showLogNew shower = unlines . map showpair . M.toList + where + showpair (k, LogEntry (Date p) v) = + unwords [show p, fromUUID k, shower v] + showpair (k, LogEntry Unknown v) = + unwords ["0", fromUUID k, shower v] + parseLog :: (String -> Maybe a) -> String -> Log a parseLog = parseLogWithUUID . const @@ -86,6 +98,17 @@ parseLogWithUUID parser = M.fromListWith best . mapMaybe parse . lines Nothing -> Unknown Just d -> Date $ utcTimeToPOSIXSeconds d +parseLogNew :: (String -> Maybe a) -> String -> Log a +parseLogNew parser = M.fromListWith best . mapMaybe parse . lines + where + parse line = do + let (ts, rest) = splitword line + (u, v) = splitword rest + date <- Date . utcTimeToPOSIXSeconds <$> parseTime defaultTimeLocale "%s%Qs" ts + val <- parser v + Just (toUUID u, LogEntry date val) + splitword = separate (== ' ') + changeLog :: POSIXTime -> UUID -> a -> Log a -> Log a changeLog t u v = M.insert u $ LogEntry (Date t) v |