ovsdb: add conditions utilities to support monitor_cond
[cascardo/ovs.git] / ovsdb / condition.h
1 /* Copyright (c) 2009, 2010 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef OVSDB_CONDITION_H
17 #define OVSDB_CONDITION_H 1
18
19 #include <stddef.h>
20 #include "compiler.h"
21 #include "ovsdb-data.h"
22 #include "bitmap.h"
23
24 struct json;
25 struct ovsdb_table_schema;
26 struct ovsdb_row;
27
28 /* These list is ordered first with boolean functions and then in
29  * ascending order of the fraction of tables row that they are
30  * (heuristically) expected to leave in query results. */
31 #define OVSDB_FUNCTIONS                         \
32     OVSDB_FUNCTION(OVSDB_F_FALSE, "false")            \
33     OVSDB_FUNCTION(OVSDB_F_TRUE, "true")              \
34     OVSDB_FUNCTION(OVSDB_F_EQ, "==")                  \
35     OVSDB_FUNCTION(OVSDB_F_INCLUDES, "includes")      \
36     OVSDB_FUNCTION(OVSDB_F_LE, "<=")                  \
37     OVSDB_FUNCTION(OVSDB_F_LT, "<")                   \
38     OVSDB_FUNCTION(OVSDB_F_GE, ">=")                  \
39     OVSDB_FUNCTION(OVSDB_F_GT, ">")                   \
40     OVSDB_FUNCTION(OVSDB_F_EXCLUDES, "excludes")      \
41     OVSDB_FUNCTION(OVSDB_F_NE, "!=")
42
43 enum ovsdb_function {
44 #define OVSDB_FUNCTION(ENUM, NAME) ENUM,
45     OVSDB_FUNCTIONS
46 #undef OVSDB_FUNCTION
47     OVSDB_F_LAST = OVSDB_F_NE
48 };
49
50 struct ovsdb_error *ovsdb_function_from_string(const char *,
51                                                enum ovsdb_function *)
52     OVS_WARN_UNUSED_RESULT;
53 const char *ovsdb_function_to_string(enum ovsdb_function);
54
55 struct ovsdb_clause {
56     enum ovsdb_function function;
57     const struct ovsdb_column *column;
58     unsigned int index;
59     struct ovsdb_datum arg;
60 };
61
62 struct ovsdb_condition {
63     struct ovsdb_clause *clauses;
64     size_t n_clauses;
65 };
66
67 #define OVSDB_CONDITION_INITIALIZER { NULL, 0}
68
69 void ovsdb_condition_init(struct ovsdb_condition *);
70 bool ovsdb_condition_empty(const struct ovsdb_condition *);
71 struct ovsdb_error *ovsdb_condition_from_json(
72     const struct ovsdb_table_schema *,
73     const struct json *, struct ovsdb_symbol_table *,
74     struct ovsdb_condition *) OVS_WARN_UNUSED_RESULT;
75 struct json *ovsdb_condition_to_json(const struct ovsdb_condition *);
76 void ovsdb_condition_destroy(struct ovsdb_condition *);
77 bool ovsdb_condition_match_every_clause(const struct ovsdb_row *,
78                                         const struct ovsdb_condition *);
79 bool ovsdb_condition_match_any_clause(const struct ovsdb_datum *,
80                                       const struct ovsdb_condition *,
81                                       unsigned int index_map[]);
82 int ovsdb_condition_cmp_3way(const struct ovsdb_condition *a,
83                              const struct ovsdb_condition *b);
84 void ovsdb_condition_clone(struct ovsdb_condition *to,
85                            const struct ovsdb_condition *from);
86 bool ovsdb_condition_is_true(const struct ovsdb_condition *cond);
87 bool ovsdb_condition_is_false(const struct ovsdb_condition *cond);
88 const struct ovsdb_column **
89 ovsdb_condition_get_columns(const struct ovsdb_condition *cond,
90                             size_t *n_columns);
91
92 static inline bool
93 ovsdb_condition_empty_or_match_any(const struct ovsdb_datum *row_datum,
94                                    const struct ovsdb_condition *cnd,
95                                    unsigned int index_map[])
96 {
97     return (ovsdb_condition_empty(cnd) ||
98             ovsdb_condition_match_any_clause(row_datum, cnd, index_map));
99 }
100
101 #endif /* ovsdb/condition.h */