root / Ganeti / HTools / Container.hs @ 0ca66853
History | View | Annotate | Download (2.8 kB)
1 |
{-| Module abstracting the node and instance container implementation. |
---|---|
2 |
|
3 |
This is currently implemented on top of an 'IntMap', which seems to |
4 |
give the best performance for our workload. |
5 |
|
6 |
-} |
7 |
|
8 |
{- |
9 |
|
10 |
Copyright (C) 2009 Google Inc. |
11 |
|
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. |
16 |
|
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. |
21 |
|
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 |
25 |
02110-1301, USA. |
26 |
|
27 |
-} |
28 |
|
29 |
module Ganeti.HTools.Container |
30 |
( |
31 |
-- * Types |
32 |
Container |
33 |
, Key |
34 |
-- * Creation |
35 |
, empty |
36 |
, fromAssocList |
37 |
-- * Query |
38 |
, size |
39 |
, find |
40 |
-- * Update |
41 |
, add |
42 |
, addTwo |
43 |
, remove |
44 |
, IntMap.map |
45 |
, IntMap.mapAccum |
46 |
-- * Conversion |
47 |
, elems |
48 |
, keys |
49 |
-- * Element functions |
50 |
, nameOf |
51 |
, findByName |
52 |
) where |
53 |
|
54 |
import qualified Data.IntMap as IntMap |
55 |
|
56 |
import qualified Ganeti.HTools.Types as T |
57 |
|
58 |
type Key = IntMap.Key |
59 |
type Container = IntMap.IntMap |
60 |
|
61 |
-- | Create an empty container. |
62 |
empty :: Container a |
63 |
empty = IntMap.empty |
64 |
|
65 |
-- | Returns the number of elements in the map. |
66 |
size :: Container a -> Int |
67 |
size = IntMap.size |
68 |
|
69 |
-- | Locate a key in the map (must exist). |
70 |
find :: Key -> Container a -> a |
71 |
find k = (IntMap.! k) |
72 |
|
73 |
-- | Add or update one element to the map. |
74 |
add :: Key -> a -> Container a -> Container a |
75 |
add = IntMap.insert |
76 |
|
77 |
-- | Remove an element from the map. |
78 |
remove :: Key -> Container a -> Container a |
79 |
remove = IntMap.delete |
80 |
|
81 |
-- | Return the list of values in the map. |
82 |
elems :: Container a -> [a] |
83 |
elems = IntMap.elems |
84 |
|
85 |
-- | Return the list of keys in the map. |
86 |
keys :: Container a -> [Key] |
87 |
keys = IntMap.keys |
88 |
|
89 |
-- | Create a map from an association list. |
90 |
fromAssocList :: [(Key, a)] -> Container a |
91 |
fromAssocList = IntMap.fromList |
92 |
|
93 |
-- | Add or update two elements of the map. |
94 |
addTwo :: Key -> a -> Key -> a -> Container a -> Container a |
95 |
addTwo k1 v1 k2 v2 = add k1 v1 . add k2 v2 |
96 |
|
97 |
-- | Compute the name of an element in a container. |
98 |
nameOf :: (T.Element a) => Container a -> Key -> String |
99 |
nameOf c k = T.nameOf $ find k c |
100 |
|
101 |
-- | Find an element by name in a Container; this is a very slow function. |
102 |
findByName :: (T.Element a, Monad m) => |
103 |
Container a -> String -> m a |
104 |
findByName c n = |
105 |
let all_elems = elems c |
106 |
result = filter ((n `elem`) . T.allNames) all_elems |
107 |
in case result of |
108 |
[item] -> return item |
109 |
_ -> fail $ "Wrong number of elems found with name " ++ n |