root / htools / Ganeti / HTools / Container.hs @ a86fbf36
History | View | Annotate | Download (2.4 kB)
1 | e4f08c46 | Iustin Pop | {-| Module abstracting the node and instance container implementation. |
---|---|---|---|
2 | e4f08c46 | Iustin Pop | |
3 | e4f08c46 | Iustin Pop | This is currently implemented on top of an 'IntMap', which seems to |
4 | e4f08c46 | Iustin Pop | give the best performance for our workload. |
5 | e4f08c46 | Iustin Pop | |
6 | e4f08c46 | Iustin Pop | -} |
7 | e4f08c46 | Iustin Pop | |
8 | e2fa2baf | Iustin Pop | {- |
9 | e2fa2baf | Iustin Pop | |
10 | 1f4ae205 | Iustin Pop | Copyright (C) 2009, 2010, 2011 Google Inc. |
11 | e2fa2baf | Iustin Pop | |
12 | e2fa2baf | Iustin Pop | This program is free software; you can redistribute it and/or modify |
13 | e2fa2baf | Iustin Pop | it under the terms of the GNU General Public License as published by |
14 | e2fa2baf | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
15 | e2fa2baf | Iustin Pop | (at your option) any later version. |
16 | e2fa2baf | Iustin Pop | |
17 | e2fa2baf | Iustin Pop | This program is distributed in the hope that it will be useful, but |
18 | e2fa2baf | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | e2fa2baf | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | e2fa2baf | Iustin Pop | General Public License for more details. |
21 | e2fa2baf | Iustin Pop | |
22 | e2fa2baf | Iustin Pop | You should have received a copy of the GNU General Public License |
23 | e2fa2baf | Iustin Pop | along with this program; if not, write to the Free Software |
24 | e2fa2baf | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 | e2fa2baf | Iustin Pop | 02110-1301, USA. |
26 | e2fa2baf | Iustin Pop | |
27 | e2fa2baf | Iustin Pop | -} |
28 | e2fa2baf | Iustin Pop | |
29 | 669d7e3d | Iustin Pop | module Ganeti.HTools.Container |
30 | e4f08c46 | Iustin Pop | ( |
31 | e4f08c46 | Iustin Pop | -- * Types |
32 | e4f08c46 | Iustin Pop | Container |
33 | 4333a887 | Iustin Pop | , Key |
34 | e4f08c46 | Iustin Pop | -- * Creation |
35 | 5ef78537 | Iustin Pop | , IntMap.empty |
36 | 5ef78537 | Iustin Pop | , IntMap.singleton |
37 | cb0c77ff | Iustin Pop | , IntMap.fromList |
38 | e4f08c46 | Iustin Pop | -- * Query |
39 | 5ef78537 | Iustin Pop | , IntMap.size |
40 | 5ef78537 | Iustin Pop | , IntMap.null |
41 | e4f08c46 | Iustin Pop | , find |
42 | 5ef78537 | Iustin Pop | , IntMap.findMax |
43 | 1f4ae205 | Iustin Pop | , IntMap.member |
44 | e4f08c46 | Iustin Pop | -- * Update |
45 | e4f08c46 | Iustin Pop | , add |
46 | e4f08c46 | Iustin Pop | , addTwo |
47 | ec18dca9 | Iustin Pop | , IntMap.map |
48 | 5d1baf63 | Iustin Pop | , IntMap.mapAccum |
49 | 5ef78537 | Iustin Pop | , IntMap.filter |
50 | e4f08c46 | Iustin Pop | -- * Conversion |
51 | 5ef78537 | Iustin Pop | , IntMap.elems |
52 | 5ef78537 | Iustin Pop | , IntMap.keys |
53 | 262a08a2 | Iustin Pop | -- * Element functions |
54 | 262a08a2 | Iustin Pop | , nameOf |
55 | 262a08a2 | Iustin Pop | , findByName |
56 | e4f08c46 | Iustin Pop | ) where |
57 | e4f08c46 | Iustin Pop | |
58 | e4f08c46 | Iustin Pop | import qualified Data.IntMap as IntMap |
59 | e4f08c46 | Iustin Pop | |
60 | 262a08a2 | Iustin Pop | import qualified Ganeti.HTools.Types as T |
61 | 262a08a2 | Iustin Pop | |
62 | e4f08c46 | Iustin Pop | type Key = IntMap.Key |
63 | e4f08c46 | Iustin Pop | type Container = IntMap.IntMap |
64 | e4f08c46 | Iustin Pop | |
65 | e4f08c46 | Iustin Pop | -- | Locate a key in the map (must exist). |
66 | e4f08c46 | Iustin Pop | find :: Key -> Container a -> a |
67 | 3a3c1eb4 | Iustin Pop | find k = (IntMap.! k) |
68 | e4f08c46 | Iustin Pop | |
69 | e4f08c46 | Iustin Pop | -- | Add or update one element to the map. |
70 | e4f08c46 | Iustin Pop | add :: Key -> a -> Container a -> Container a |
71 | 9f6dcdea | Iustin Pop | add = IntMap.insert |
72 | e4f08c46 | Iustin Pop | |
73 | e4f08c46 | Iustin Pop | -- | Add or update two elements of the map. |
74 | e4f08c46 | Iustin Pop | addTwo :: Key -> a -> Key -> a -> Container a -> Container a |
75 | 9f6dcdea | Iustin Pop | addTwo k1 v1 k2 v2 = add k1 v1 . add k2 v2 |
76 | 262a08a2 | Iustin Pop | |
77 | 9188aeef | Iustin Pop | -- | Compute the name of an element in a container. |
78 | 262a08a2 | Iustin Pop | nameOf :: (T.Element a) => Container a -> Key -> String |
79 | 262a08a2 | Iustin Pop | nameOf c k = T.nameOf $ find k c |
80 | 262a08a2 | Iustin Pop | |
81 | 9188aeef | Iustin Pop | -- | Find an element by name in a Container; this is a very slow function. |
82 | 262a08a2 | Iustin Pop | findByName :: (T.Element a, Monad m) => |
83 | e7724ccc | Iustin Pop | Container a -> String -> m a |
84 | 262a08a2 | Iustin Pop | findByName c n = |
85 | 5ef78537 | Iustin Pop | let all_elems = IntMap.elems c |
86 | c854092b | Iustin Pop | result = filter ((n `elem`) . T.allNames) all_elems |
87 | e7724ccc | Iustin Pop | in case result of |
88 | e7724ccc | Iustin Pop | [item] -> return item |
89 | e7724ccc | Iustin Pop | _ -> fail $ "Wrong number of elems found with name " ++ n |