2ddc811dd0426e2108c0e5051ee5917b44be39a5
[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     bool optimized;
66     struct shash o_columns;
67 };
68
69 #define OVSDB_CONDITION_INITIALIZER(COND) \
70     { NULL, 0, true, SHASH_INITIALIZER(&(COND)->o_columns)}
71
72 void ovsdb_condition_init(struct ovsdb_condition *);
73 bool ovsdb_condition_empty(const struct ovsdb_condition *);
74 struct ovsdb_error *ovsdb_condition_from_json(
75     const struct ovsdb_table_schema *,
76     const struct json *, struct ovsdb_symbol_table *,
77     struct ovsdb_condition *) OVS_WARN_UNUSED_RESULT;
78 struct json *ovsdb_condition_to_json(const struct ovsdb_condition *);
79 void ovsdb_condition_destroy(struct ovsdb_condition *);
80 bool ovsdb_condition_match_every_clause(const struct ovsdb_row *,
81                                         const struct ovsdb_condition *);
82 bool ovsdb_condition_match_any_clause(const struct ovsdb_datum *,
83                                       const struct ovsdb_condition *,
84                                       unsigned int index_map[]);
85 int ovsdb_condition_cmp_3way(const struct ovsdb_condition *a,
86                              const struct ovsdb_condition *b);
87 void ovsdb_condition_clone(struct ovsdb_condition *to,
88                            const struct ovsdb_condition *from);
89 bool ovsdb_condition_is_true(const struct ovsdb_condition *cond);
90 bool ovsdb_condition_is_false(const struct ovsdb_condition *cond);
91 const struct ovsdb_column **
92 ovsdb_condition_get_columns(const struct ovsdb_condition *cond,
93                             size_t *n_columns);
94
95 static inline bool
96 ovsdb_condition_empty_or_match_any(const struct ovsdb_datum *row_datum,
97                                    const struct ovsdb_condition *cnd,
98                                    unsigned int index_map[])
99 {
100     return (ovsdb_condition_empty(cnd) ||
101             ovsdb_condition_match_any_clause(row_datum, cnd, index_map));
102 }
103
104 #endif /* ovsdb/condition.h */