1 /* Copyright (c) 2009 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "condition.h"
24 #include "ovsdb-error.h"
29 ovsdb_function_from_string(const char *name, enum ovsdb_function *function)
31 #define OVSDB_FUNCTION(ENUM, NAME) \
32 if (!strcmp(name, NAME)) { \
39 return ovsdb_syntax_error(NULL, "unknown function",
40 "No function named %s.", name);
44 ovsdb_function_to_string(enum ovsdb_function function)
47 #define OVSDB_FUNCTION(ENUM, NAME) case ENUM: return NAME;
56 static WARN_UNUSED_RESULT struct ovsdb_error *
57 ovsdb_clause_from_json(const struct ovsdb_table_schema *ts,
58 const struct json *json,
59 const struct ovsdb_symbol_table *symtab,
60 struct ovsdb_clause *clause)
62 const struct json_array *array;
63 struct ovsdb_error *error;
64 const char *function_name;
65 const char *column_name;
66 struct ovsdb_type type;
68 if (json->type != JSON_ARRAY
69 || json->u.array.n != 3
70 || json->u.array.elems[0]->type != JSON_STRING
71 || json->u.array.elems[1]->type != JSON_STRING) {
72 return ovsdb_syntax_error(json, NULL, "Parse error in condition.");
74 array = json_array(json);
76 column_name = json_string(array->elems[0]);
77 clause->column = ovsdb_table_schema_get_column(ts, column_name);
78 if (!clause->column) {
79 return ovsdb_syntax_error(json, "unknown column",
80 "No column %s in table %s.",
81 column_name, ts->name);
83 type = clause->column->type;
85 function_name = json_string(array->elems[1]);
86 error = ovsdb_function_from_string(function_name, &clause->function);
91 /* Type-check and relax restrictions on 'type' if appropriate. */
92 switch (clause->function) {
97 /* XXX should we also allow these operators for types with n_min == 0,
98 * n_max == 1? (They would always be "false" if the value was
100 if (!ovsdb_type_is_scalar(&type)
101 || (type.key_type != OVSDB_TYPE_INTEGER
102 && type.key_type != OVSDB_TYPE_REAL)) {
103 char *s = ovsdb_type_to_english(&type);
104 error = ovsdb_syntax_error(
105 json, NULL, "Type mismatch: \"%s\" operator may not be "
106 "applied to column %s of type %s.",
107 ovsdb_function_to_string(clause->function),
108 clause->column->name, s);
118 case OVSDB_F_EXCLUDES:
119 if (!ovsdb_type_is_scalar(&type)) {
121 type.n_max = UINT_MAX;
125 case OVSDB_F_INCLUDES:
126 if (!ovsdb_type_is_scalar(&type)) {
131 return ovsdb_datum_from_json(&clause->arg, &type, array->elems[2], symtab);
135 ovsdb_clause_free(struct ovsdb_clause *clause)
137 ovsdb_datum_destroy(&clause->arg, &clause->column->type);
141 compare_clauses_3way(const void *a_, const void *b_)
143 const struct ovsdb_clause *a = a_;
144 const struct ovsdb_clause *b = b_;
146 if (a->function != b->function) {
147 /* Bring functions to the front based on the fraction of table rows
148 * that they are (heuristically) expected to leave in the query
149 * results. Note that "enum ovsdb_function" is intentionally ordered
150 * to make this trivial. */
151 return a->function < b->function ? -1 : 1;
152 } else if (a->column->index != b->column->index) {
153 if (a->column->index < OVSDB_N_STD_COLUMNS
154 || b->column->index < OVSDB_N_STD_COLUMNS) {
155 /* Bring the standard columns and in particular the UUID column
156 * (since OVSDB_COL_UUID has value 0) to the front. We have an
157 * index on the UUID column, so that makes our queries cheaper. */
158 return a->column->index < b->column->index ? -1 : 1;
160 /* Order clauses predictably to make testing easier. */
161 return strcmp(a->column->name, b->column->name);
169 ovsdb_condition_from_json(const struct ovsdb_table_schema *ts,
170 const struct json *json,
171 const struct ovsdb_symbol_table *symtab,
172 struct ovsdb_condition *cnd)
174 const struct json_array *array = json_array(json);
177 cnd->clauses = xmalloc(array->n * sizeof *cnd->clauses);
179 for (i = 0; i < array->n; i++) {
180 struct ovsdb_error *error;
181 error = ovsdb_clause_from_json(ts, array->elems[i], symtab,
184 ovsdb_condition_destroy(cnd);
192 /* A real database would have a query optimizer here. */
193 qsort(cnd->clauses, cnd->n_clauses, sizeof *cnd->clauses,
194 compare_clauses_3way);
200 ovsdb_clause_to_json(const struct ovsdb_clause *clause)
202 return json_array_create_3(
203 json_string_create(clause->column->name),
204 json_string_create(ovsdb_function_to_string(clause->function)),
205 ovsdb_datum_to_json(&clause->arg, &clause->column->type));
209 ovsdb_condition_to_json(const struct ovsdb_condition *cnd)
211 struct json **clauses;
214 clauses = xmalloc(cnd->n_clauses * sizeof *clauses);
215 for (i = 0; i < cnd->n_clauses; i++) {
216 clauses[i] = ovsdb_clause_to_json(&cnd->clauses[i]);
218 return json_array_create(clauses, cnd->n_clauses);
222 ovsdb_condition_evaluate(const struct ovsdb_row *row,
223 const struct ovsdb_condition *cnd)
227 for (i = 0; i < cnd->n_clauses; i++) {
228 const struct ovsdb_clause *c = &cnd->clauses[i];
229 const struct ovsdb_datum *field = &row->fields[c->column->index];
230 const struct ovsdb_datum *arg = &cnd->clauses[i].arg;
231 const struct ovsdb_type *type = &c->column->type;
233 if (ovsdb_type_is_scalar(type)) {
234 int cmp = ovsdb_atom_compare_3way(&field->keys[0], &arg->keys[0],
236 switch (c->function) {
242 case OVSDB_F_INCLUDES:
245 case OVSDB_F_EXCLUDES:
253 switch (c->function) {
255 return ovsdb_datum_equals(field, arg, type);
257 return !ovsdb_datum_equals(field, arg, type);
258 case OVSDB_F_INCLUDES:
259 return ovsdb_datum_includes_all(arg, field, type);
260 case OVSDB_F_EXCLUDES:
261 return ovsdb_datum_excludes_all(arg, field, type);
276 ovsdb_condition_destroy(struct ovsdb_condition *cnd)
280 for (i = 0; i < cnd->n_clauses; i++) {
281 ovsdb_clause_free(&cnd->clauses[i]);