Add functions to check and fix cluster data
[ganeti-local] / Ganeti / HTools / Container.hs
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 module Ganeti.HTools.Container
9     (
10      -- * Types
11      Container
12      -- * Creation
13     , empty
14     , fromAssocList
15      -- * Query
16     , size
17     , find
18      -- * Update
19     , add
20     , addTwo
21     , remove
22     , IntMap.map
23     , IntMap.mapAccum
24     -- * Conversion
25     , elems
26     , keys
27     ) where
28
29 import qualified Data.IntMap as IntMap
30
31 type Key = IntMap.Key
32 type Container = IntMap.IntMap
33
34 -- | Create an empty container.
35 empty :: Container a
36 empty = IntMap.empty
37
38 -- | Returns the number of elements in the map.
39 size :: Container a -> Int
40 size = IntMap.size
41
42 -- | Locate a key in the map (must exist).
43 find :: Key -> Container a -> a
44 find k c = c IntMap.! k
45
46 -- | Locate a keyin the map returning a default value if not existing.
47 findWithDefault :: a -> Key -> Container a -> a
48 findWithDefault = IntMap.findWithDefault
49
50 -- | Add or update one element to the map.
51 add :: Key -> a -> Container a -> Container a
52 add k v c = IntMap.insert k v c
53
54 -- | Remove an element from the map.
55 remove :: Key -> Container a -> Container a
56 remove = IntMap.delete
57
58 -- | Return the list of values in the map.
59 elems :: Container a -> [a]
60 elems = IntMap.elems
61
62 -- | Return the list of keys in the map.
63 keys :: Container a -> [Key]
64 keys = IntMap.keys
65
66 -- | Create a map from an association list.
67 fromAssocList :: [(Key, a)] -> Container a
68 fromAssocList = IntMap.fromList
69
70 -- | Create a map from an association list with a combining function.
71 fromListWith :: (a -> a -> a) -> [(Key, a)] -> Container a
72 fromListWith = IntMap.fromListWith
73
74 -- | Fold over the values of the map.
75 fold :: (a -> b -> b) -> b -> Container a -> b
76 fold = IntMap.fold
77
78 -- | Add or update two elements of the map.
79 addTwo :: Key -> a -> Key -> a -> Container a -> Container a
80 addTwo k1 v1 k2 v2 c = add k1 v1 $ add k2 v2 c