1 /* Copyright (c) 2009, 2010, 2011 Nicira, Inc.
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;
55 static struct ovsdb_error *
56 ovsdb_clause_from_json(const struct ovsdb_table_schema *ts,
57 const struct json *json,
58 struct ovsdb_symbol_table *symtab,
59 struct ovsdb_clause *clause)
61 const struct json_array *array;
62 struct ovsdb_error *error;
63 const char *function_name;
64 const char *column_name;
65 struct ovsdb_type type;
67 if (json->type == JSON_TRUE || json->type == JSON_FALSE) {
69 json->type == JSON_TRUE ? OVSDB_F_TRUE : OVSDB_F_FALSE;
71 /* Column and arg fields are not being used with boolean functions.
73 clause->column = ovsdb_table_schema_get_column(ts, "_uuid");
74 clause->index = clause->column->index;
75 ovsdb_datum_init_default(&clause->arg, &clause->column->type);
79 if (json->type != JSON_ARRAY
80 || json->u.array.n != 3
81 || json->u.array.elems[0]->type != JSON_STRING
82 || json->u.array.elems[1]->type != JSON_STRING) {
83 return ovsdb_syntax_error(json, NULL, "Parse error in condition.");
85 array = json_array(json);
87 column_name = json_string(array->elems[0]);
88 clause->column = ovsdb_table_schema_get_column(ts, column_name);
89 if (!clause->column) {
90 return ovsdb_syntax_error(json, "unknown column",
91 "No column %s in table %s.",
92 column_name, ts->name);
94 clause->index = clause->column->index;
95 type = clause->column->type;
97 function_name = json_string(array->elems[1]);
98 error = ovsdb_function_from_string(function_name, &clause->function);
103 /* Type-check and relax restrictions on 'type' if appropriate. */
104 switch (clause->function) {
109 /* Allow these operators for types with n_min == 0, n_max == 1.
110 * (They will always be "false" if the value is missing.) */
111 if (!(ovsdb_type_is_scalar(&type)
112 || ovsdb_type_is_optional_scalar(&type))
113 || (type.key.type != OVSDB_TYPE_INTEGER
114 && type.key.type != OVSDB_TYPE_REAL)) {
115 char *s = ovsdb_type_to_english(&type);
116 error = ovsdb_syntax_error(
117 json, NULL, "Type mismatch: \"%s\" operator may not be "
118 "applied to column %s of type %s.",
119 ovsdb_function_to_string(clause->function),
120 clause->column->name, s);
129 case OVSDB_F_EXCLUDES:
130 if (!ovsdb_type_is_scalar(&type)) {
132 type.n_max = UINT_MAX;
136 case OVSDB_F_INCLUDES:
137 if (!ovsdb_type_is_scalar(&type)) {
145 return ovsdb_datum_from_json(&clause->arg, &type, array->elems[2], symtab);
149 ovsdb_clause_free(struct ovsdb_clause *clause)
151 ovsdb_datum_destroy(&clause->arg, &clause->column->type);
155 compare_clauses_3way(const void *a_, const void *b_)
157 const struct ovsdb_clause *a = a_;
158 const struct ovsdb_clause *b = b_;
160 if (a->function != b->function) {
161 /* Bring functions to the front based on the fraction of table rows
162 * that they are (heuristically) expected to leave in the query
163 * results. Note that "enum ovsdb_function" is intentionally ordered
164 * to make this trivial. */
165 return a->function < b->function ? -1 : 1;
166 } else if (a->column->index != b->column->index) {
167 if (a->column->index < OVSDB_N_STD_COLUMNS
168 || b->column->index < OVSDB_N_STD_COLUMNS) {
169 /* Bring the standard columns and in particular the UUID column
170 * (since OVSDB_COL_UUID has value 0) to the front. We have an
171 * index on the UUID column, so that makes our queries cheaper. */
172 return a->column->index < b->column->index ? -1 : 1;
174 /* Order clauses predictably to make testing easier. */
175 return strcmp(a->column->name, b->column->name);
183 compare_clauses_3way_with_data(const void *a_, const void *b_)
185 const struct ovsdb_clause *a = a_;
186 const struct ovsdb_clause *b = b_;
189 res = compare_clauses_3way(a, b);
190 return res ? res : ovsdb_datum_compare_3way(&a->arg,
196 ovsdb_condition_from_json(const struct ovsdb_table_schema *ts,
197 const struct json *json,
198 struct ovsdb_symbol_table *symtab,
199 struct ovsdb_condition *cnd)
201 const struct json_array *array = json_array(json);
204 cnd->clauses = xmalloc(array->n * sizeof *cnd->clauses);
206 for (i = 0; i < array->n; i++) {
207 struct ovsdb_error *error;
208 error = ovsdb_clause_from_json(ts, array->elems[i], symtab,
211 ovsdb_condition_destroy(cnd);
219 /* A real database would have a query optimizer here. */
220 qsort(cnd->clauses, cnd->n_clauses, sizeof *cnd->clauses,
221 compare_clauses_3way_with_data);
227 ovsdb_clause_to_json(const struct ovsdb_clause *clause)
229 if (clause->function != OVSDB_F_TRUE &&
230 clause->function != OVSDB_F_FALSE) {
231 return json_array_create_3(
232 json_string_create(clause->column->name),
233 json_string_create(ovsdb_function_to_string(clause->function)),
234 ovsdb_datum_to_json(&clause->arg, &clause->column->type));
237 return json_boolean_create(clause->function == OVSDB_F_TRUE);
241 ovsdb_condition_to_json(const struct ovsdb_condition *cnd)
243 struct json **clauses;
246 clauses = xmalloc(cnd->n_clauses * sizeof *clauses);
247 for (i = 0; i < cnd->n_clauses; i++) {
248 clauses[i] = ovsdb_clause_to_json(&cnd->clauses[i]);
250 return json_array_create(clauses, cnd->n_clauses);
254 ovsdb_clause_evaluate(const struct ovsdb_datum *fields,
255 const struct ovsdb_clause *c,
256 unsigned int index_map[])
258 const struct ovsdb_datum *field = &fields[index_map ?
259 index_map[c->column->index] :
261 const struct ovsdb_datum *arg = &c->arg;
262 const struct ovsdb_type *type = &c->column->type;
264 if (c->function == OVSDB_F_TRUE ||
265 c->function == OVSDB_F_FALSE) {
266 return c->function == OVSDB_F_TRUE;
268 if (ovsdb_type_is_optional_scalar(type) && field->n == 0) {
269 switch (c->function) {
275 case OVSDB_F_INCLUDES:
278 case OVSDB_F_EXCLUDES:
284 } else if (ovsdb_type_is_scalar(type)
285 || ovsdb_type_is_optional_scalar(type)) {
286 int cmp = ovsdb_atom_compare_3way(&field->keys[0], &arg->keys[0],
288 switch (c->function) {
294 case OVSDB_F_INCLUDES:
297 case OVSDB_F_EXCLUDES:
308 switch (c->function) {
310 return ovsdb_datum_equals(field, arg, type);
312 return !ovsdb_datum_equals(field, arg, type);
313 case OVSDB_F_INCLUDES:
314 return ovsdb_datum_includes_all(arg, field, type);
315 case OVSDB_F_EXCLUDES:
316 return ovsdb_datum_excludes_all(arg, field, type);
331 ovsdb_clause_clone(struct ovsdb_clause *new, struct ovsdb_clause *old)
333 new->function = old->function;
334 new->column = old->column;
335 ovsdb_datum_clone(&new->arg,
341 ovsdb_condition_match_every_clause(const struct ovsdb_row *row,
342 const struct ovsdb_condition *cnd)
346 for (i = 0; i < cnd->n_clauses; i++) {
347 if (!ovsdb_clause_evaluate(row->fields, &cnd->clauses[i], NULL)) {
355 /* Returns true if condition evaluation of one of the clauses is
356 * true. index_map[] is an optional array that if exists indicates a mapping
357 * between indexing row_datum to the indexes in ovsdb_column */
359 ovsdb_condition_match_any_clause(const struct ovsdb_datum *row_datum,
360 const struct ovsdb_condition *cnd,
361 unsigned int index_map[])
365 for (i = 0; i < cnd->n_clauses; i++) {
366 if (ovsdb_clause_evaluate(row_datum, &cnd->clauses[i], index_map)) {
375 ovsdb_condition_destroy(struct ovsdb_condition *cnd)
379 for (i = 0; i < cnd->n_clauses; i++) {
380 ovsdb_clause_free(&cnd->clauses[i]);
387 ovsdb_condition_init(struct ovsdb_condition *cnd)
394 ovsdb_condition_empty(const struct ovsdb_condition *cnd)
396 return cnd->n_clauses == 0;
400 ovsdb_condition_cmp_3way(const struct ovsdb_condition *a,
401 const struct ovsdb_condition *b)
406 if (a->n_clauses != b->n_clauses) {
407 return a->n_clauses < b->n_clauses ? -1 : 1;
410 /* We assume clauses are sorted */
411 for (i = 0; i < a->n_clauses; i++) {
412 res = (compare_clauses_3way_with_data(&a->clauses[i], &b->clauses[i]));
422 ovsdb_condition_clone(struct ovsdb_condition *to,
423 const struct ovsdb_condition *from)
427 to->clauses = xzalloc(from->n_clauses * sizeof *to->clauses);
429 for (i = 0; i < from->n_clauses; i++) {
430 ovsdb_clause_clone(&to->clauses[i], &from->clauses[i]);
432 to->n_clauses = from->n_clauses;
435 /* Return true if ovsdb_condition_match_any_clause() will return true on
438 ovsdb_condition_is_true(const struct ovsdb_condition *cond)
440 return (!cond->n_clauses ||
441 (cond->n_clauses >= 1 && (cond->clauses[0].function == OVSDB_F_TRUE)) ||
442 (cond->n_clauses >= 2 && (cond->clauses[1].function == OVSDB_F_TRUE)));
446 ovsdb_condition_is_false(const struct ovsdb_condition *cond)
448 return ((cond->n_clauses == 1) &&
449 (cond->clauses[0].function == OVSDB_F_FALSE));
452 const struct ovsdb_column **
453 ovsdb_condition_get_columns(const struct ovsdb_condition *cond,
456 const struct ovsdb_column **columns;
459 columns = xmalloc(cond->n_clauses * sizeof *columns);
460 for (i = 0; i < cond->n_clauses; i++) {
461 columns[i] = cond->clauses[i].column;