Revision d277b075 htools/Ganeti/Query/Filter.hs

b/htools/Ganeti/Query/Filter.hs
144 144
-- | Verifies if a given item passes a filter. The runtime context
145 145
-- might be missing, in which case most of the filters will consider
146 146
-- this as passing the filter.
147
--
148
-- Note: we use explicit recursion to reduce unneeded memory use;
149
-- 'any' and 'all' do not play nice with monadic values, resulting in
150
-- either too much memory use or in too many thunks being created.
147 151
evaluateFilter :: ConfigData -> Maybe b -> a
148 152
               -> Filter (FieldGetter a b)
149 153
               -> ErrorResult Bool
150 154
evaluateFilter _ _  _ EmptyFilter = Ok True
151
evaluateFilter c mb a (AndFilter flts) =
152
  all id <$> mapM (evaluateFilter c mb a) flts
153
evaluateFilter c mb a (OrFilter flts)  =
154
  any id <$> mapM (evaluateFilter c mb a) flts
155
evaluateFilter c mb a (AndFilter flts) = helper flts
156
  where helper [] = Ok True
157
        helper (f:fs) = do
158
          v <- evaluateFilter c mb a f
159
          if v
160
            then helper fs
161
            else Ok False
162
evaluateFilter c mb a (OrFilter flts) = helper flts
163
  where helper [] = Ok False
164
        helper (f:fs) = do
165
          v <- evaluateFilter c mb a f
166
          if v
167
            then Ok True
168
            else helper fs
155 169
evaluateFilter c mb a (NotFilter flt)  =
156 170
  not <$> evaluateFilter c mb a flt
157 171
evaluateFilter c mb a (TrueFilter getter)  =

Also available in: Unified diff