sim: Add metadata command

Only really needed for completeness, preferred content expressions can
match against metadata.
This commit is contained in:
Joey Hess 2024-09-26 12:20:37 -04:00
parent b492eb051b
commit 783e910d0c
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
5 changed files with 46 additions and 16 deletions

View file

@ -27,6 +27,7 @@ import Annex.Link
import Annex.Wanted
import Annex.CatFile
import Annex.Action (quiesce)
import Annex.MetaData
import Logs.Group
import Logs.Trust
import Logs.PreferredContent
@ -36,6 +37,7 @@ import Logs.MaxSize
import Logs.Difference
import Logs.UUID
import Logs.Location
import Logs.MetaData
import Utility.Env
import qualified Annex
import qualified Remote
@ -67,6 +69,7 @@ data SimState t = SimState
, simNumCopies :: NumCopies
, simMinCopies :: MinCopies
, simGroups :: M.Map UUID (S.Set Group)
, simMetaData :: M.Map Key MetaData
, simWanted :: M.Map UUID PreferredContentExpression
, simRequired :: M.Map UUID PreferredContentExpression
, simGroupWanted :: M.Map Group PreferredContentExpression
@ -91,6 +94,7 @@ emptySimState rngseed rootdir = SimState
, simNumCopies = configuredNumCopies 1
, simMinCopies = configuredMinCopies 1
, simGroups = mempty
, simMetaData = mempty
, simWanted = mempty
, simRequired = mempty
, simGroupWanted = mempty
@ -245,6 +249,7 @@ data SimCommand
| CommandTrustLevel RepoName TrustLevel
| CommandGroup RepoName Group
| CommandUngroup RepoName Group
| CommandMetaData RawFilePath String
| CommandWanted RepoName PreferredContentExpression
| CommandRequired RepoName PreferredContentExpression
| CommandGroupWanted Group PreferredContentExpression
@ -508,6 +513,19 @@ applySimCommand' (CommandUngroup repo groupname) st _ =
Right $ Right $ st
{ simGroups = M.adjust (S.delete groupname) u (simGroups st)
}
applySimCommand' (CommandMetaData file modmetaexpr) st _ =
case parseModMeta modmetaexpr of
Left err -> Left err
Right modmeta -> case M.lookup file (simFiles st) of
Nothing -> Left $ "Cannot set metadata of unknown file " ++ fromRawFilePath file
Just k -> Right $ Right $ st
{ simMetaData = M.alter (addmeta modmeta) k
(simMetaData st)
}
where
addmeta modmeta (Just metadata) = Just $ unionMetaData metadata $
modMeta metadata modmeta
addmeta modmeta Nothing = Just $ modMeta emptyMetaData modmeta
applySimCommand' (CommandWanted repo expr) st _ =
checkKnownRepo repo st $ \u ->
checkValidPreferredContentExpression [expr] $ Right $ st
@ -1229,6 +1247,12 @@ updateSimRepoState newst sr = do
, addDiff = \u -> groupChange u . const
, removeDiff = const . flip groupChange (const mempty)
}
updateField oldst newst simMetaData $ DiffUpdate
{ replaceDiff = replaceNew addMetaData
, addDiff = addMetaData
, removeDiff = \k old -> addMetaData k $
modMeta old DelAllMeta
}
updateField oldst newst simWanted $ DiffUpdate
{ replaceDiff = replaceNew preferredContentSet
, addDiff = preferredContentSet

View file

@ -73,6 +73,8 @@ generateSimFile = unlines . map unwords . go
["group", repo, fromGroup group] : go rest
go (CommandUngroup (RepoName repo) group : rest) =
["ungroup", repo, fromGroup group] : go rest
go (CommandMetaData f modmeta : rest) =
["metadata", fromRawFilePath f, modmeta] : go rest
go (CommandWanted (RepoName repo) expr : rest) =
["wanted", repo, expr] : go rest
go (CommandRequired (RepoName repo) expr : rest) =
@ -188,6 +190,8 @@ parseSimCommand ("group":repo:group:[]) =
Right $ CommandGroup (RepoName repo) (toGroup group)
parseSimCommand ("ungroup":repo:group:[]) =
Right $ CommandUngroup (RepoName repo) (toGroup group)
parseSimCommand ("metadata":file:modmeta:[]) =
Right $ CommandMetaData (toRawFilePath file) modmeta
parseSimCommand ("wanted":repo:expr) =
Right $ CommandWanted (RepoName repo) (unwords expr)
parseSimCommand ("required":repo:expr) =

View file

@ -62,7 +62,7 @@ import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
newtype MetaData = MetaData (M.Map MetaField (S.Set MetaValue))
deriving (Show, Eq, Ord)
deriving (Read, Show, Eq, Ord)
instance ToJSON' MetaData where
toJSON' (MetaData m) = object $ map go (M.toList m)

View file

@ -309,16 +309,6 @@ as passed to "git annex sim" while a simulation is running.
Sets the trust level of the repository. This is equivilant to
[[git-annex-trust]](1), [[git-annex-untrust]](1), etc.
* `group repo group`
Add a repository to a group. This is equivilant to
[[git-annex-group]](1).
* `ungroup repo group`
Remove a repository from a group. This is equivilant to
[[git-annex-ungroup]](1).
* `wanted repo expression`
Configure the preferred content of a repository. This is equivilant
@ -352,6 +342,22 @@ as passed to "git annex sim" while a simulation is running.
Configure the groupwanted to a random expression.
* `group repo group`
Add a repository to a group. This is equivilant to
[[git-annex-group]](1).
* `ungroup repo group`
Remove a repository from a group. This is equivilant to
[[git-annex-ungroup]](1).
* `metadata filename expression`
Change the metadata of the simulated file. The expression is
in the same format as the --set option of the [[git-annex-metadata]]
command. For example: `metadata foo year=2025`
* `maxsize repo size`
Configure the maximum size of a repository. This is equivilant to

View file

@ -28,11 +28,7 @@ Planned schedule of work:
## work notes
* Currently working in [[todo/proving_preferred_content_behavior]]
in the `sim` branch.
* sim: Add support for metadata, so preferred content that matches on it
will work
* Currently working in [[todo/proving_preferred_content_behavior]].
* The sim cannot be safely interrupted, or two processes be run
concurrently. Both unlike other git-annex commands. Either document these