2014-02-13 01:12:22 +00:00
|
|
|
{- git-annex general metadata storage log
|
|
|
|
-
|
|
|
|
- A line of the log will look like "timestamp field [+-]value [...]"
|
|
|
|
-
|
|
|
|
- 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.
|
|
|
|
-
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-02-13 01:12:22 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2014-02-21 15:30:31 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2014-02-13 01:12:22 +00:00
|
|
|
|
|
|
|
module Logs.MetaData (
|
|
|
|
getCurrentMetaData,
|
|
|
|
addMetaData,
|
2014-02-13 05:57:43 +00:00
|
|
|
addMetaData',
|
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
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.MetaData
|
2014-03-18 22:55:43 +00:00
|
|
|
import Annex.MetaData.StandardFields
|
2014-02-13 01:12:22 +00:00
|
|
|
import qualified Annex.Branch
|
|
|
|
import Logs
|
|
|
|
import Logs.SingleValue
|
|
|
|
|
|
|
|
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
|
|
|
import Data.Time.Clock.POSIX
|
2014-03-18 22:55:43 +00:00
|
|
|
import Data.Time.Format
|
|
|
|
import System.Locale
|
2014-02-13 01:12:22 +00:00
|
|
|
|
|
|
|
instance SingleValueSerializable MetaData where
|
|
|
|
serialize = Types.MetaData.serialize
|
|
|
|
deserialize = Types.MetaData.deserialize
|
|
|
|
|
2014-03-18 22:55:43 +00:00
|
|
|
getMetaDataLog :: Key -> Annex (Log MetaData)
|
|
|
|
getMetaDataLog = readLog . metaDataLogFile
|
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
|
2014-03-18 22:55:43 +00:00
|
|
|
getCurrentMetaData k = do
|
|
|
|
ls <- S.toAscList <$> getMetaDataLog k
|
|
|
|
let loggedmeta = currentMetaData $ combineMetaData $ map value ls
|
|
|
|
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
|
2014-03-19 23:10:35 +00:00
|
|
|
lastchangedval l = S.singleton $ toMetaValue $ showts $ changed l
|
2014-03-18 23:01:50 +00:00
|
|
|
showts = formatTime defaultTimeLocale "%F@%H-%M-%S" . posixSecondsToUTCTime
|
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 ()
|
2014-02-13 05:57:43 +00:00
|
|
|
addMetaData k metadata = addMetaData' k metadata =<< liftIO getPOSIXTime
|
|
|
|
|
|
|
|
{- Reusing the same timestamp when making changes to the metadata
|
|
|
|
- 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. -}
|
|
|
|
addMetaData' :: Key -> MetaData -> POSIXTime -> Annex ()
|
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
|
|
|
addMetaData' k d@(MetaData m) now
|
|
|
|
| d == emptyMetaData = noop
|
|
|
|
| otherwise = Annex.Branch.change (metaDataLogFile k) $
|
|
|
|
showLog . simplifyLog
|
|
|
|
. S.insert (LogEntry now metadata)
|
|
|
|
. 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
|
|
|
|
|
|
|
{- Simplify a log, removing historical values that are no longer
|
|
|
|
- needed.
|
|
|
|
-
|
|
|
|
- This is not as simple as just making a single log line with the newest
|
|
|
|
- state of all metadata. Consider this case:
|
|
|
|
-
|
|
|
|
- We have:
|
|
|
|
-
|
|
|
|
- 100 foo +x bar +y
|
|
|
|
- 200 foo -x
|
|
|
|
-
|
|
|
|
- An unmerged remote has:
|
|
|
|
-
|
2014-02-13 03:24:04 +00:00
|
|
|
- 150 bar -y baz +w
|
2014-02-13 01:12:22 +00:00
|
|
|
-
|
|
|
|
- If what we have were simplified to "200 foo -x bar +y" then when the line
|
|
|
|
- from the remote became available, it would be older than the simplified
|
|
|
|
- line, and its change to bar would not take effect. That is wrong.
|
|
|
|
-
|
2014-02-13 02:36:16 +00:00
|
|
|
- Instead, simplify it to:
|
2014-02-13 01:12:22 +00:00
|
|
|
-
|
2014-02-13 02:36:16 +00:00
|
|
|
- 100 bar +y
|
2014-02-13 01:12:22 +00:00
|
|
|
- 200 foo -x
|
|
|
|
-
|
2014-02-13 03:24:04 +00:00
|
|
|
- (Note that this ends up with the same number of lines as the
|
|
|
|
- unsimplified version, so there's really no point in updating
|
|
|
|
- the log to this version. Doing so would only add data to git,
|
|
|
|
- with little benefit.)
|
2014-02-13 02:36:16 +00:00
|
|
|
-
|
2014-02-13 01:12:22 +00:00
|
|
|
- Now merging with the remote yields:
|
|
|
|
-
|
2014-02-13 02:36:16 +00:00
|
|
|
- 100 bar +y
|
2014-02-13 03:24:04 +00:00
|
|
|
- 150 bar -y baz +w
|
2014-02-13 01:12:22 +00:00
|
|
|
- 200 foo -x
|
|
|
|
-
|
|
|
|
- Simplifying again:
|
|
|
|
-
|
|
|
|
- 150 bar +z baz +w
|
|
|
|
- 200 foo -x
|
|
|
|
-}
|
|
|
|
simplifyLog :: Log MetaData -> Log MetaData
|
2014-02-13 03:24:04 +00:00
|
|
|
simplifyLog s = case sl of
|
|
|
|
(newest:rest) ->
|
|
|
|
let sl' = go [newest] (value newest) rest
|
|
|
|
in if length sl' < length sl
|
|
|
|
then S.fromList sl'
|
|
|
|
else s
|
2014-02-13 01:12:22 +00:00
|
|
|
_ -> s
|
|
|
|
where
|
2014-02-21 15:30:31 +00:00
|
|
|
#if MIN_VERSION_containers(0,5,0)
|
2014-02-13 03:24:04 +00:00
|
|
|
sl = S.toDescList s
|
2014-02-21 15:30:31 +00:00
|
|
|
#else
|
|
|
|
sl = reverse (S.toAscList s)
|
|
|
|
#endif
|
2014-02-13 03:24:04 +00:00
|
|
|
|
2014-02-13 01:12:22 +00:00
|
|
|
go c _ [] = c
|
|
|
|
go c newer (l:ls)
|
2014-02-23 04:08:29 +00:00
|
|
|
| unique == emptyMetaData = go c newer ls
|
2014-02-13 02:36:16 +00:00
|
|
|
| otherwise = go (l { value = unique } : c)
|
|
|
|
(unionMetaData unique newer) ls
|
2014-02-13 01:12:22 +00:00
|
|
|
where
|
|
|
|
older = value l
|
2014-02-13 02:36:16 +00:00
|
|
|
unique = older `differenceMetaData` newer
|
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.
|
|
|
|
-}
|
|
|
|
copyMetaData :: Key -> Key -> Annex ()
|
|
|
|
copyMetaData oldkey newkey
|
|
|
|
| oldkey == newkey = noop
|
|
|
|
| otherwise = do
|
2014-03-18 22:55:43 +00:00
|
|
|
l <- getMetaDataLog oldkey
|
2014-02-24 18:41:33 +00:00
|
|
|
unless (S.null l) $
|
|
|
|
Annex.Branch.change (metaDataLogFile newkey) $
|
|
|
|
const $ showLog l
|