Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Container.hs @ 7ec2f76b

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 ebf38064 Iustin Pop
  ( -- * Types
31 ebf38064 Iustin Pop
    Container
32 ebf38064 Iustin Pop
  , Key
33 ebf38064 Iustin Pop
  -- * Creation
34 ebf38064 Iustin Pop
  , IntMap.empty
35 ebf38064 Iustin Pop
  , IntMap.singleton
36 ebf38064 Iustin Pop
  , IntMap.fromList
37 ebf38064 Iustin Pop
  -- * Query
38 ebf38064 Iustin Pop
  , IntMap.size
39 ebf38064 Iustin Pop
  , IntMap.null
40 ebf38064 Iustin Pop
  , find
41 ebf38064 Iustin Pop
  , IntMap.findMax
42 ebf38064 Iustin Pop
  , IntMap.member
43 ebf38064 Iustin Pop
  -- * Update
44 ebf38064 Iustin Pop
  , add
45 ebf38064 Iustin Pop
  , addTwo
46 ebf38064 Iustin Pop
  , IntMap.map
47 ebf38064 Iustin Pop
  , IntMap.mapAccum
48 ebf38064 Iustin Pop
  , IntMap.filter
49 ebf38064 Iustin Pop
  -- * Conversion
50 ebf38064 Iustin Pop
  , IntMap.elems
51 ebf38064 Iustin Pop
  , IntMap.keys
52 ebf38064 Iustin Pop
  -- * Element functions
53 ebf38064 Iustin Pop
  , nameOf
54 ebf38064 Iustin Pop
  , findByName
55 ebf38064 Iustin Pop
  ) where
56 e4f08c46 Iustin Pop
57 e4f08c46 Iustin Pop
import qualified Data.IntMap as IntMap
58 e4f08c46 Iustin Pop
59 262a08a2 Iustin Pop
import qualified Ganeti.HTools.Types as T
60 262a08a2 Iustin Pop
61 179c0828 Iustin Pop
-- | Our key type.
62 179c0828 Iustin Pop
63 e4f08c46 Iustin Pop
type Key = IntMap.Key
64 179c0828 Iustin Pop
65 179c0828 Iustin Pop
-- | Our container type.
66 e4f08c46 Iustin Pop
type Container = IntMap.IntMap
67 e4f08c46 Iustin Pop
68 e4f08c46 Iustin Pop
-- | Locate a key in the map (must exist).
69 e4f08c46 Iustin Pop
find :: Key -> Container a -> a
70 3a3c1eb4 Iustin Pop
find k = (IntMap.! k)
71 e4f08c46 Iustin Pop
72 e4f08c46 Iustin Pop
-- | Add or update one element to the map.
73 e4f08c46 Iustin Pop
add :: Key -> a -> Container a -> Container a
74 9f6dcdea Iustin Pop
add = IntMap.insert
75 e4f08c46 Iustin Pop
76 e4f08c46 Iustin Pop
-- | Add or update two elements of the map.
77 e4f08c46 Iustin Pop
addTwo :: Key -> a -> Key -> a -> Container a -> Container a
78 9f6dcdea Iustin Pop
addTwo k1 v1 k2 v2 = add k1 v1 . add k2 v2
79 262a08a2 Iustin Pop
80 9188aeef Iustin Pop
-- | Compute the name of an element in a container.
81 262a08a2 Iustin Pop
nameOf :: (T.Element a) => Container a -> Key -> String
82 262a08a2 Iustin Pop
nameOf c k = T.nameOf $ find k c
83 262a08a2 Iustin Pop
84 9188aeef Iustin Pop
-- | Find an element by name in a Container; this is a very slow function.
85 262a08a2 Iustin Pop
findByName :: (T.Element a, Monad m) =>
86 e7724ccc Iustin Pop
              Container a -> String -> m a
87 262a08a2 Iustin Pop
findByName c n =
88 ebf38064 Iustin Pop
  let all_elems = IntMap.elems c
89 ebf38064 Iustin Pop
      result = filter ((n `elem`) . T.allNames) all_elems
90 ebf38064 Iustin Pop
  in case result of
91 ebf38064 Iustin Pop
       [item] -> return item
92 ebf38064 Iustin Pop
       _ -> fail $ "Wrong number of elems found with name " ++ n