ganeti
Safe HaskellNone

Ganeti.Query.Filter

Description

Implementation of the Ganeti Query2 filterning.

The filtering of results should be done in two phases.

In the first phase, before contacting any remote nodes for runtime data, the filtering should be executed with Nothing for the runtime context. This will make all non-runtime filters filter correctly, whereas all runtime filters will respond successfully. As described in the Python version too, this makes for example Or filters very inefficient if they contain runtime fields.

Once this first filtering phase has been done, we hopefully eliminated some remote nodes out of the list of candidates, we run the remote data gathering, and we evaluate the filter again, this time with a Just runtime context. This will make all filters work correctly.

Note that the second run will re-evaluate the config/simple fields, without caching; this is not perfect, but we consider config accesses very cheap (and the configuration snapshot we have won't change between the two runs, hence we will not get inconsistent results).

Synopsis

Documentation

compileFilter :: FieldMap a b -> Filter FilterField -> ErrorResult (Filter (FieldGetter a b, QffMode)) Source #

Compiles a filter based on field names to one based on getters.

qffField :: QffMode -> JSValue -> ErrorResult JSValue Source #

Processes a field value given a QffMode.

wrapGetter :: ConfigData -> Maybe b -> a -> (FieldGetter a b, QffMode) -> (JSValue -> ErrorResult Bool) -> MaybeT ErrorResult Bool Source #

Wraps a getter, filter pair. If the getter is FieldRuntime but we don't have a runtime context, we skip the filtering, returning Nothing in the MaybeT. Otherwise, we pass the actual value to the filter.

trueFilter :: JSValue -> ErrorResult Bool Source #

Helper to evaluate a filter getter (and the value it generates) in a boolean context.

type Comparator = forall a. (Eq a, Ord a) => a -> a -> Bool Source #

A type synonim for a rank-2 comparator function. This is used so that we can pass the usual <=, >, == functions to binOpFilter and for them to be used in multiple contexts.

eqFilter :: QffMode -> FilterValue -> JSValue -> ErrorResult Bool Source #

Equality checker.

This will handle hostnames correctly, if the mode is set to QffHostname.

binOpFilter :: Comparator -> FilterValue -> JSValue -> ErrorResult Bool Source #

Helper to evaluate a filder getter (and the value it generates) in a boolean context. Note the order of arguments is reversed from the filter definitions (due to the call chain), make sure to compare in the reverse order too!.

regexpFilter :: FilterRegex -> JSValue -> ErrorResult Bool Source #

Implements the RegexpFilter matching.

containsFilter :: FilterValue -> JSValue -> ErrorResult Bool Source #

Implements the ContainsFilter matching.

data Comparison Source #

Ways we can compare things in the filter language.

Constructors

Eq 
Lt 
Le 
Gt 
Ge 

Instances

Instances details
Eq Comparison # 
Instance details

Defined in Ganeti.Query.Filter

Methods

(==) :: Comparison -> Comparison -> Bool

(/=) :: Comparison -> Comparison -> Bool

Ord Comparison # 
Instance details

Defined in Ganeti.Query.Filter

Show Comparison # 
Instance details

Defined in Ganeti.Query.Filter

Methods

showsPrec :: Int -> Comparison -> ShowS

show :: Comparison -> String

showList :: [Comparison] -> ShowS

toCompFun :: Comparison -> Comparator Source #

Turns a comparison into the corresponding Haskell function.

data FilterOp field val where Source #

Operations in the leaves of the Ganeti filter language.

Constructors

Truth :: FilterOp field () 
Comp :: Comparison -> FilterOp field FilterValue 
Regex :: FilterOp field FilterRegex 
Contains :: FilterOp field FilterValue 

Instances

Instances details
Eq (FilterOp field val) # 
Instance details

Defined in Ganeti.Query.Filter

Methods

(==) :: FilterOp field val -> FilterOp field val -> Bool

(/=) :: FilterOp field val -> FilterOp field val -> Bool

Show (FilterOp field val) # 
Instance details

Defined in Ganeti.Query.Filter

Methods

showsPrec :: Int -> FilterOp field val -> ShowS

show :: FilterOp field val -> String

showList :: [FilterOp field val] -> ShowS

evaluateFilterM :: (Monad m, Applicative m) => (forall val. FilterOp field val -> field -> val -> m Bool) -> Filter field -> m Bool Source #

Checks if a filter matches.

The leaves of the filter are evaluated against an object using the passed opFun; that is why the object need not be passed in.

The field type describes the "accessors" that are used to query values from the object; those values are to be matched against the val type in the filter leaves.

Useful monads m for this are ErrorResult and Maybe.

evaluateQueryFilter :: ConfigData -> Maybe b -> a -> Filter (FieldGetter a b, QffMode) -> ErrorResult Bool Source #

Verifies if a given item passes a filter. The runtime context might be missing, in which case most of the filters will consider this as passing the filter.

evaluateFilterJSON :: Filter JSValue -> ErrorResult Bool Source #

Evaluates a Filter on a JSON object.

tryGetter :: ConfigData -> Maybe b -> a -> FieldGetter a b -> Maybe ResultEntry Source #

Runs a getter with potentially missing runtime context.

requestedNames :: FilterField -> Filter FilterField -> Maybe [FilterValue] Source #

Computes the requested names, if only names were requested (and with equality). Otherwise returns Nothing.

makeSimpleFilter :: String -> [Either String Integer] -> Filter FilterField Source #

Builds a simple filter from a list of names.