From e7724ccc46708250aeed86898c6f119bac5fe03d Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Mon, 19 Oct 2009 06:50:40 +0900 Subject: [PATCH] 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. --- Ganeti/HTools/Container.hs | 12 ++++-------- Ganeti/HTools/IAlloc.hs | 2 +- Ganeti/HTools/Loader.hs | 7 +++---- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Ganeti/HTools/Container.hs b/Ganeti/HTools/Container.hs index 937676ca9..9d8e56cc9 100644 --- a/Ganeti/HTools/Container.hs +++ b/Ganeti/HTools/Container.hs @@ -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. findByName :: (T.Element a, Monad m) => - Container a -> String -> m Key + Container a -> String -> m a findByName c n = let all_elems = elems c result = filter ((== n) . T.nameOf) all_elems - nems = length result - in - if nems /= 1 then - fail $ "Wrong number of elems (" ++ show nems ++ - ") found with name " ++ n - else - return $ T.idxOf $ head result + in case result of + [item] -> return item + _ -> fail $ "Wrong number of elems found with name " ++ n diff --git a/Ganeti/HTools/IAlloc.hs b/Ganeti/HTools/IAlloc.hs index af177f74b..2aef97c86 100644 --- a/Ganeti/HTools/IAlloc.hs +++ b/Ganeti/HTools/IAlloc.hs @@ -126,7 +126,7 @@ parseData body = do ex_nodes <- fromObj "relocate_from" request let ex_nodes' = map (stripSuffix $ length csf) 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 ++ "'") return $ Request rqtype map_n map_i csf diff --git a/Ganeti/HTools/Loader.hs b/Ganeti/HTools/Loader.hs index 87f11f092..2873688de 100644 --- a/Ganeti/HTools/Loader.hs +++ b/Ganeti/HTools/Loader.hs @@ -139,10 +139,9 @@ mergeData :: [(String, DynUtil)] -- ^ Instance utilisation data mergeData um (nl, il) = do let il2 = Container.fromAssocList il il3 <- foldM (\im (name, n_util) -> do - idx <- Container.findByName im name - let inst = Container.find idx im - new_i = inst { Instance.util = n_util } - return $ Container.add idx new_i im + inst <- Container.findByName im name + let new_i = inst { Instance.util = n_util } + return $ Container.add (Instance.idx inst) new_i im ) il2 um let nl2 = foldl' fixNodes nl (Container.elems il3) let nl3 = Container.fromAssocList -- GitLab