Commit e7724ccc authored by Iustin Pop's avatar Iustin Pop
Browse files

Change the Container.findByName function

This patch changes the signature and implementation of the function;
returning the item makes more sense (saves a lookup later again in the
container, and applying idx is cheap), and the previous implementation
was ugly.
parent bbd8efd2
...@@ -105,14 +105,10 @@ maxNameLen = maximum . map (length . T.nameOf) . elems ...@@ -105,14 +105,10 @@ maxNameLen = maximum . map (length . T.nameOf) . elems
-- | Find an element by name in a Container; this is a very slow function. -- | Find an element by name in a Container; this is a very slow function.
findByName :: (T.Element a, Monad m) => findByName :: (T.Element a, Monad m) =>
Container a -> String -> m Key Container a -> String -> m a
findByName c n = findByName c n =
let all_elems = elems c let all_elems = elems c
result = filter ((== n) . T.nameOf) all_elems result = filter ((== n) . T.nameOf) all_elems
nems = length result in case result of
in [item] -> return item
if nems /= 1 then _ -> fail $ "Wrong number of elems found with name " ++ n
fail $ "Wrong number of elems (" ++ show nems ++
") found with name " ++ n
else
return $ T.idxOf $ head result
...@@ -126,7 +126,7 @@ parseData body = do ...@@ -126,7 +126,7 @@ parseData body = do
ex_nodes <- fromObj "relocate_from" request ex_nodes <- fromObj "relocate_from" request
let ex_nodes' = map (stripSuffix $ length csf) ex_nodes let ex_nodes' = map (stripSuffix $ length csf) ex_nodes
ex_idex <- mapM (Container.findByName map_n) ex_nodes' ex_idex <- mapM (Container.findByName map_n) ex_nodes'
return $ Relocate ridx req_nodes ex_idex return $ Relocate ridx req_nodes (map Node.idx ex_idex)
other -> fail ("Invalid request type '" ++ other ++ "'") other -> fail ("Invalid request type '" ++ other ++ "'")
return $ Request rqtype map_n map_i csf return $ Request rqtype map_n map_i csf
......
...@@ -139,10 +139,9 @@ mergeData :: [(String, DynUtil)] -- ^ Instance utilisation data ...@@ -139,10 +139,9 @@ mergeData :: [(String, DynUtil)] -- ^ Instance utilisation data
mergeData um (nl, il) = do mergeData um (nl, il) = do
let il2 = Container.fromAssocList il let il2 = Container.fromAssocList il
il3 <- foldM (\im (name, n_util) -> do il3 <- foldM (\im (name, n_util) -> do
idx <- Container.findByName im name inst <- Container.findByName im name
let inst = Container.find idx im let new_i = inst { Instance.util = n_util }
new_i = inst { Instance.util = n_util } return $ Container.add (Instance.idx inst) new_i im
return $ Container.add idx new_i im
) il2 um ) il2 um
let nl2 = foldl' fixNodes nl (Container.elems il3) let nl2 = foldl' fixNodes nl (Container.elems il3)
let nl3 = Container.fromAssocList let nl3 = Container.fromAssocList
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment