2018-08-31 16:23:22 +00:00
|
|
|
{- git-annex general metadata storage log and per-remote metadata storage log.
|
2014-02-13 01:12:22 +00:00
|
|
|
-
|
|
|
|
- A line of the log will look like "timestamp field [+-]value [...]"
|
|
|
|
-
|
2018-08-31 16:23:22 +00:00
|
|
|
- (In the per-remote log, each field is prefixed with "uuid:")
|
|
|
|
-
|
2014-02-13 01:12:22 +00:00
|
|
|
- Note that unset values are preserved. Consider this case:
|
|
|
|
-
|
|
|
|
- We have:
|
|
|
|
-
|
|
|
|
- 100 foo +x
|
|
|
|
- 200 foo -x
|
|
|
|
-
|
|
|
|
- An unmerged remote has:
|
|
|
|
-
|
|
|
|
- 150 foo +x
|
|
|
|
-
|
|
|
|
- After union merge, because the foo -x was preserved, we know that
|
|
|
|
- after the other remote redundantly set foo +x, it was unset,
|
|
|
|
- and so foo currently has no value.
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.MetaData (
|
|
|
|
getCurrentMetaData,
|
2018-08-31 16:23:22 +00:00
|
|
|
getCurrentRemoteMetaData,
|
2014-02-13 01:12:22 +00:00
|
|
|
addMetaData,
|
2018-08-31 16:23:22 +00:00
|
|
|
addRemoteMetaData,
|
|
|
|
addMetaDataClocked,
|
2014-02-13 01:12:22 +00:00
|
|
|
currentMetaData,
|
2014-02-24 18:41:33 +00:00
|
|
|
copyMetaData,
|
2014-02-13 01:12:22 +00:00
|
|
|
) where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2014-02-13 01:12:22 +00:00
|
|
|
import Types.MetaData
|
2014-03-18 22:55:43 +00:00
|
|
|
import Annex.MetaData.StandardFields
|
2017-08-14 17:55:38 +00:00
|
|
|
import Annex.VectorClock
|
2014-02-13 01:12:22 +00:00
|
|
|
import qualified Annex.Branch
|
2015-01-28 21:17:26 +00:00
|
|
|
import qualified Annex
|
2014-02-13 01:12:22 +00:00
|
|
|
import Logs
|
2018-10-30 03:13:36 +00:00
|
|
|
import Utility.TimeStamp
|
2018-09-05 17:20:10 +00:00
|
|
|
import Logs.MetaData.Pure
|
2014-02-13 01:12:22 +00:00
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2014-03-18 22:55:43 +00:00
|
|
|
import qualified Data.Map as M
|
2014-02-13 01:12:22 +00:00
|
|
|
|
|
|
|
{- Go through the log from oldest to newest, and combine it all
|
2014-03-18 22:55:43 +00:00
|
|
|
- into a single MetaData representing the current state.
|
|
|
|
-
|
|
|
|
- Automatically generates a lastchanged metadata for each field that's
|
|
|
|
- currently set, based on timestamps in the log.
|
|
|
|
-}
|
2014-02-13 01:12:22 +00:00
|
|
|
getCurrentMetaData :: Key -> Annex MetaData
|
2018-08-31 16:23:22 +00:00
|
|
|
getCurrentMetaData = getCurrentMetaData' metaDataLogFile
|
|
|
|
|
|
|
|
getCurrentMetaData' :: (GitConfig -> Key -> FilePath) -> Key -> Annex MetaData
|
|
|
|
getCurrentMetaData' getlogfile k = do
|
|
|
|
config <- Annex.getGitConfig
|
|
|
|
ls <- S.toAscList <$> readLog (getlogfile config k)
|
2017-09-28 16:56:35 +00:00
|
|
|
let loggedmeta = logToCurrentMetaData ls
|
2014-03-18 22:55:43 +00:00
|
|
|
return $ currentMetaData $ unionMetaData loggedmeta
|
|
|
|
(lastchanged ls loggedmeta)
|
2014-02-13 01:12:22 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
lastchanged [] _ = emptyMetaData
|
2014-03-19 23:10:35 +00:00
|
|
|
lastchanged ls (MetaData currentlyset) =
|
2014-03-18 22:55:43 +00:00
|
|
|
let m = foldl' (flip M.union) M.empty (map genlastchanged ls)
|
2014-03-19 23:10:35 +00:00
|
|
|
in MetaData $
|
|
|
|
-- Add a overall lastchanged using the oldest log
|
|
|
|
-- item (log is in ascending order).
|
|
|
|
M.insert lastChangedField (lastchangedval $ Prelude.last ls) $
|
|
|
|
M.mapKeys mkLastChangedField $
|
2014-03-18 22:55:43 +00:00
|
|
|
-- Only include fields that are currently set.
|
2014-03-19 23:10:35 +00:00
|
|
|
m `M.intersection` currentlyset
|
2014-03-18 22:55:43 +00:00
|
|
|
-- Makes each field have the timestamp as its value.
|
|
|
|
genlastchanged l =
|
|
|
|
let MetaData m = value l
|
2014-03-19 23:10:35 +00:00
|
|
|
ts = lastchangedval l
|
2014-03-18 22:55:43 +00:00
|
|
|
in M.map (const ts) m
|
2017-08-14 17:55:38 +00:00
|
|
|
lastchangedval l = S.singleton $ toMetaValue $ showts $
|
|
|
|
case changed l of
|
|
|
|
VectorClock t -> t
|
|
|
|
Unknown -> 0
|
2015-05-10 19:36:58 +00:00
|
|
|
showts = formatPOSIXTime "%F@%H-%M-%S"
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2018-08-31 16:23:22 +00:00
|
|
|
getCurrentRemoteMetaData :: UUID -> Key -> Annex RemoteMetaData
|
2018-08-31 17:12:58 +00:00
|
|
|
getCurrentRemoteMetaData u k = extractRemoteMetaData u <$>
|
2018-08-31 16:23:22 +00:00
|
|
|
getCurrentMetaData' remoteMetaDataLogFile k
|
|
|
|
|
2014-02-13 01:12:22 +00:00
|
|
|
{- Adds in some metadata, which can override existing values, or unset
|
|
|
|
- them, but otherwise leaves any existing metadata as-is. -}
|
|
|
|
addMetaData :: Key -> MetaData -> Annex ()
|
2018-08-31 16:23:22 +00:00
|
|
|
addMetaData = addMetaData' metaDataLogFile
|
|
|
|
|
|
|
|
addMetaData' :: (GitConfig -> Key -> FilePath) -> Key -> MetaData -> Annex ()
|
|
|
|
addMetaData' getlogfile k metadata =
|
|
|
|
addMetaDataClocked' getlogfile k metadata =<< liftIO currentVectorClock
|
2014-02-13 05:57:43 +00:00
|
|
|
|
2017-08-14 17:55:38 +00:00
|
|
|
{- Reusing the same VectorClock when making changes to the metadata
|
2014-02-13 05:57:43 +00:00
|
|
|
- of multiple keys is a nice optimisation. The same metadata lines
|
|
|
|
- will tend to be generated across the different log files, and so
|
|
|
|
- git will be able to pack the data more efficiently. -}
|
2018-08-31 16:23:22 +00:00
|
|
|
addMetaDataClocked :: Key -> MetaData -> VectorClock -> Annex ()
|
|
|
|
addMetaDataClocked = addMetaDataClocked' metaDataLogFile
|
|
|
|
|
|
|
|
addMetaDataClocked' :: (GitConfig -> Key -> FilePath) -> Key -> MetaData -> VectorClock -> Annex ()
|
|
|
|
addMetaDataClocked' getlogfile k d@(MetaData m) c
|
import metadata from feeds
When annex.genmetadata is set, metadata from the feed is added to files
that are imported from it.
Reused the same feedtitle and itemtitle, feedauthor, itemauthor, etc names
that are used in --template.
Also added title and author, which are the item title/author if available,
falling back to the feed title/author. These are more likely to be common
metadata fields.
(There is a small bit of dupication here, but once git gets
around to packing the object, it will compress it away.)
The itempubdate field is not included in the metadata as a string; instead
it is used to generate year and month fields, same as is done when adding
files with annex.genmetadata set.
This commit was sponsored by Amitai Schlair, who cooincidentially
is responsible for ikiwiki generating nice feed metadata!
2014-07-03 17:46:09 +00:00
|
|
|
| d == emptyMetaData = noop
|
2015-01-28 21:17:26 +00:00
|
|
|
| otherwise = do
|
|
|
|
config <- Annex.getGitConfig
|
2018-08-31 16:23:22 +00:00
|
|
|
Annex.Branch.change (getlogfile config k) $
|
2015-01-28 21:17:26 +00:00
|
|
|
showLog . simplifyLog
|
2017-08-14 17:55:38 +00:00
|
|
|
. S.insert (LogEntry c metadata)
|
2015-01-28 21:17:26 +00:00
|
|
|
. parseLog
|
2014-03-18 22:55:43 +00:00
|
|
|
where
|
|
|
|
metadata = MetaData $ M.filterWithKey (\f _ -> not (isLastChangedField f)) m
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2018-08-31 16:23:22 +00:00
|
|
|
addRemoteMetaData :: Key -> RemoteMetaData -> Annex ()
|
|
|
|
addRemoteMetaData k m = do
|
|
|
|
addMetaData' remoteMetaDataLogFile k (fromRemoteMetaData m)
|
|
|
|
|
|
|
|
getMetaDataLog :: Key -> Annex (Log MetaData)
|
|
|
|
getMetaDataLog key = do
|
|
|
|
config <- Annex.getGitConfig
|
|
|
|
readLog $ metaDataLogFile config key
|
|
|
|
|
2014-02-24 18:41:33 +00:00
|
|
|
{- Copies the metadata from the old key to the new key.
|
|
|
|
-
|
|
|
|
- The exact content of the metadata file is copied, so that the timestamps
|
|
|
|
- remain the same, and because this is more space-efficient in the git
|
|
|
|
- repository.
|
|
|
|
-
|
|
|
|
- Any metadata already attached to the new key is not preserved.
|
2017-09-28 16:56:35 +00:00
|
|
|
-
|
|
|
|
- Returns True when metadata was copied.
|
2014-02-24 18:41:33 +00:00
|
|
|
-}
|
2017-09-28 16:56:35 +00:00
|
|
|
copyMetaData :: Key -> Key -> Annex Bool
|
2014-02-24 18:41:33 +00:00
|
|
|
copyMetaData oldkey newkey
|
2017-09-28 16:56:35 +00:00
|
|
|
| oldkey == newkey = return False
|
2014-02-24 18:41:33 +00:00
|
|
|
| otherwise = do
|
2014-03-18 22:55:43 +00:00
|
|
|
l <- getMetaDataLog oldkey
|
2017-09-28 16:56:35 +00:00
|
|
|
if logToCurrentMetaData (S.toAscList l) == emptyMetaData
|
|
|
|
then return False
|
|
|
|
else do
|
|
|
|
config <- Annex.getGitConfig
|
|
|
|
Annex.Branch.change (metaDataLogFile config newkey) $
|
|
|
|
const $ showLog l
|
|
|
|
return True
|
2018-09-05 17:20:10 +00:00
|
|
|
|
|
|
|
readLog :: FilePath -> Annex (Log MetaData)
|
|
|
|
readLog = parseLog <$$> Annex.Branch.get
|