1 {-| Module abstracting the node and instance container implementation.
3 This is currently implemented on top of an 'IntMap', which seems to
4 give the best performance for our workload.
10 Copyright (C) 2009 Google Inc.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 module Ganeti.HTools.Container
52 -- * Element functions
57 import qualified Data.IntMap as IntMap
59 import qualified Ganeti.HTools.Types as T
62 type Container = IntMap.IntMap
64 -- | Locate a key in the map (must exist).
65 find :: Key -> Container a -> a
68 -- | Add or update one element to the map.
69 add :: Key -> a -> Container a -> Container a
72 -- | Create a map from an association list.
73 fromAssocList :: [(Key, a)] -> Container a
74 fromAssocList = IntMap.fromList
76 -- | Add or update two elements of the map.
77 addTwo :: Key -> a -> Key -> a -> Container a -> Container a
78 addTwo k1 v1 k2 v2 = add k1 v1 . add k2 v2
80 -- | Compute the name of an element in a container.
81 nameOf :: (T.Element a) => Container a -> Key -> String
82 nameOf c k = T.nameOf $ find k c
84 -- | Find an element by name in a Container; this is a very slow function.
85 findByName :: (T.Element a, Monad m) =>
86 Container a -> String -> m a
88 let all_elems = IntMap.elems c
89 result = filter ((n `elem`) . T.allNames) all_elems
92 _ -> fail $ "Wrong number of elems found with name " ++ n