Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Container.hs @ f5ed8632

History | View | Annotate | Download (2.8 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 e2fa2baf Iustin Pop
Copyright (C) 2009 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 e4f08c46 Iustin Pop
    , empty
36 e4f08c46 Iustin Pop
    , fromAssocList
37 e4f08c46 Iustin Pop
     -- * Query
38 e4f08c46 Iustin Pop
    , size
39 e4f08c46 Iustin Pop
    , find
40 e4f08c46 Iustin Pop
     -- * Update
41 e4f08c46 Iustin Pop
    , add
42 e4f08c46 Iustin Pop
    , addTwo
43 e4f08c46 Iustin Pop
    , remove
44 ec18dca9 Iustin Pop
    , IntMap.map
45 5d1baf63 Iustin Pop
    , IntMap.mapAccum
46 e4f08c46 Iustin Pop
    -- * Conversion
47 e4f08c46 Iustin Pop
    , elems
48 1fd47ca2 Iustin Pop
    , keys
49 262a08a2 Iustin Pop
    -- * Element functions
50 262a08a2 Iustin Pop
    , nameOf
51 262a08a2 Iustin Pop
    , findByName
52 e4f08c46 Iustin Pop
    ) where
53 e4f08c46 Iustin Pop
54 e4f08c46 Iustin Pop
import qualified Data.IntMap as IntMap
55 e4f08c46 Iustin Pop
56 262a08a2 Iustin Pop
import qualified Ganeti.HTools.Types as T
57 262a08a2 Iustin Pop
58 e4f08c46 Iustin Pop
type Key = IntMap.Key
59 e4f08c46 Iustin Pop
type Container = IntMap.IntMap
60 e4f08c46 Iustin Pop
61 e4f08c46 Iustin Pop
-- | Create an empty container.
62 e4f08c46 Iustin Pop
empty :: Container a
63 e4f08c46 Iustin Pop
empty = IntMap.empty
64 e4f08c46 Iustin Pop
65 e4f08c46 Iustin Pop
-- | Returns the number of elements in the map.
66 e4f08c46 Iustin Pop
size :: Container a -> Int
67 e4f08c46 Iustin Pop
size = IntMap.size
68 e4f08c46 Iustin Pop
69 e4f08c46 Iustin Pop
-- | Locate a key in the map (must exist).
70 e4f08c46 Iustin Pop
find :: Key -> Container a -> a
71 3a3c1eb4 Iustin Pop
find k = (IntMap.! k)
72 e4f08c46 Iustin Pop
73 e4f08c46 Iustin Pop
-- | Add or update one element to the map.
74 e4f08c46 Iustin Pop
add :: Key -> a -> Container a -> Container a
75 9f6dcdea Iustin Pop
add = IntMap.insert
76 e4f08c46 Iustin Pop
77 e4f08c46 Iustin Pop
-- | Remove an element from the map.
78 e4f08c46 Iustin Pop
remove :: Key -> Container a -> Container a
79 e4f08c46 Iustin Pop
remove = IntMap.delete
80 e4f08c46 Iustin Pop
81 e4f08c46 Iustin Pop
-- | Return the list of values in the map.
82 e4f08c46 Iustin Pop
elems :: Container a -> [a]
83 e4f08c46 Iustin Pop
elems = IntMap.elems
84 e4f08c46 Iustin Pop
85 1fd47ca2 Iustin Pop
-- | Return the list of keys in the map.
86 1fd47ca2 Iustin Pop
keys :: Container a -> [Key]
87 1fd47ca2 Iustin Pop
keys = IntMap.keys
88 1fd47ca2 Iustin Pop
89 e4f08c46 Iustin Pop
-- | Create a map from an association list.
90 e4f08c46 Iustin Pop
fromAssocList :: [(Key, a)] -> Container a
91 e4f08c46 Iustin Pop
fromAssocList = IntMap.fromList
92 e4f08c46 Iustin Pop
93 e4f08c46 Iustin Pop
-- | Add or update two elements of the map.
94 e4f08c46 Iustin Pop
addTwo :: Key -> a -> Key -> a -> Container a -> Container a
95 9f6dcdea Iustin Pop
addTwo k1 v1 k2 v2 = add k1 v1 . add k2 v2
96 262a08a2 Iustin Pop
97 9188aeef Iustin Pop
-- | Compute the name of an element in a container.
98 262a08a2 Iustin Pop
nameOf :: (T.Element a) => Container a -> Key -> String
99 262a08a2 Iustin Pop
nameOf c k = T.nameOf $ find k c
100 262a08a2 Iustin Pop
101 9188aeef Iustin Pop
-- | Find an element by name in a Container; this is a very slow function.
102 262a08a2 Iustin Pop
findByName :: (T.Element a, Monad m) =>
103 e7724ccc Iustin Pop
              Container a -> String -> m a
104 262a08a2 Iustin Pop
findByName c n =
105 262a08a2 Iustin Pop
    let all_elems = elems c
106 c854092b Iustin Pop
        result = filter ((n `elem`) . T.allNames) all_elems
107 e7724ccc Iustin Pop
    in case result of
108 e7724ccc Iustin Pop
         [item] -> return item
109 e7724ccc Iustin Pop
         _ -> fail $ "Wrong number of elems found with name " ++ n