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.
22 #include "condition.h"
25 #include "ovsdb-data.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
33 #include "transaction.h"
35 struct ovsdb_execution {
37 struct ovsdb_txn *txn;
38 struct ovsdb_symbol_table *symtab;
42 long long int elapsed_msec;
43 long long int timeout_msec;
46 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
47 struct ovsdb_parser *,
50 static ovsdb_operation_executor ovsdb_execute_insert;
51 static ovsdb_operation_executor ovsdb_execute_select;
52 static ovsdb_operation_executor ovsdb_execute_update;
53 static ovsdb_operation_executor ovsdb_execute_delete;
54 static ovsdb_operation_executor ovsdb_execute_wait;
55 static ovsdb_operation_executor ovsdb_execute_commit;
56 static ovsdb_operation_executor ovsdb_execute_abort;
58 static ovsdb_operation_executor *
59 lookup_executor(const char *name)
61 struct ovsdb_operation {
63 ovsdb_operation_executor *executor;
66 static const struct ovsdb_operation operations[] = {
67 { "insert", ovsdb_execute_insert },
68 { "select", ovsdb_execute_select },
69 { "update", ovsdb_execute_update },
70 { "delete", ovsdb_execute_delete },
71 { "wait", ovsdb_execute_wait },
72 { "commit", ovsdb_execute_commit },
73 { "abort", ovsdb_execute_abort },
78 for (i = 0; i < ARRAY_SIZE(operations); i++) {
79 const struct ovsdb_operation *c = &operations[i];
80 if (!strcmp(c->name, name)) {
88 ovsdb_execute(struct ovsdb *db, const struct json *params,
89 long long int elapsed_msec, long long int *timeout_msec)
91 struct ovsdb_execution x;
92 struct ovsdb_error *error;
97 if (params->type != JSON_ARRAY) {
98 struct ovsdb_error *error;
100 error = ovsdb_syntax_error(params, NULL, "array expected");
101 results = ovsdb_error_to_json(error);
102 ovsdb_error_destroy(error);
107 x.txn = ovsdb_txn_create(db);
108 x.symtab = ovsdb_symbol_table_create();
110 x.elapsed_msec = elapsed_msec;
111 x.timeout_msec = LLONG_MAX;
114 results = json_array_create_empty();
115 n_operations = params->u.array.n;
117 for (i = 0; i < n_operations; i++) {
118 struct json *operation = params->u.array.elems[i];
119 struct ovsdb_error *parse_error;
120 struct ovsdb_parser parser;
122 const struct json *op;
124 /* Parse and execute operation. */
125 ovsdb_parser_init(&parser, operation,
126 "ovsdb operation %zu of %zu", i + 1, n_operations);
127 op = ovsdb_parser_member(&parser, "op", OP_ID);
128 result = json_object_create();
130 const char *op_name = json_string(op);
131 ovsdb_operation_executor *executor = lookup_executor(op_name);
133 error = executor(&x, &parser, result);
135 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
139 assert(ovsdb_parser_has_error(&parser));
142 /* A parse error overrides any other error.
143 * An error overrides any other result. */
144 parse_error = ovsdb_parser_finish(&parser);
146 ovsdb_error_destroy(error);
150 json_destroy(result);
151 result = ovsdb_error_to_json(error);
153 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
155 ovsdb_txn_abort(x.txn);
156 *timeout_msec = x.timeout_msec;
157 ovsdb_error_destroy(error);
158 json_destroy(results);
162 /* Add result to array. */
163 json_array_add(results, result);
170 error = ovsdb_txn_commit(x.txn, x.durable);
172 json_array_add(results, ovsdb_error_to_json(error));
175 ovsdb_txn_abort(x.txn);
178 while (json_array(results)->n < n_operations) {
179 json_array_add(results, json_null_create());
182 ovsdb_error_destroy(error);
183 ovsdb_symbol_table_destroy(x.symtab);
189 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
190 struct json *result UNUSED)
192 const struct json *durable;
194 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
195 if (durable && json_boolean(durable)) {
201 static struct ovsdb_error *
202 ovsdb_execute_abort(struct ovsdb_execution *x UNUSED,
203 struct ovsdb_parser *parser UNUSED,
204 struct json *result UNUSED)
206 return ovsdb_error("aborted", "aborted by request");
209 static struct ovsdb_table *
210 parse_table(struct ovsdb_execution *x,
211 struct ovsdb_parser *parser, const char *member)
213 struct ovsdb_table *table;
214 const char *table_name;
215 const struct json *json;
217 json = ovsdb_parser_member(parser, member, OP_ID);
221 table_name = json_string(json);
223 table = shash_find_data(&x->db->tables, table_name);
225 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
230 static WARN_UNUSED_RESULT struct ovsdb_error *
231 parse_row(struct ovsdb_parser *parser, const char *member,
232 const struct ovsdb_table *table,
233 const struct ovsdb_symbol_table *symtab,
234 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
236 struct ovsdb_error *error;
237 const struct json *json;
238 struct ovsdb_row *row;
243 return OVSDB_BUG("null table");
245 json = ovsdb_parser_member(parser, member, OP_OBJECT);
247 return OVSDB_BUG("null row member");
250 row = ovsdb_row_create(table);
251 error = ovsdb_row_from_json(row, json, symtab, columns);
253 ovsdb_row_destroy(row);
262 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
265 struct ovsdb_table *table;
266 struct ovsdb_row *row = NULL;
267 const struct json *uuid_name;
268 struct ovsdb_error *error;
270 table = parse_table(x, parser, "table");
271 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
272 error = ovsdb_parser_get_error(parser);
274 error = parse_row(parser, "row", table, x->symtab, &row, NULL);
277 uuid_generate(ovsdb_row_get_uuid_rw(row));
279 ovsdb_symbol_table_put(x->symtab, json_string(uuid_name),
280 ovsdb_row_get_uuid(row));
282 ovsdb_txn_row_insert(x->txn, row);
283 json_object_put(result, "uuid",
284 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
292 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
295 struct ovsdb_table *table;
296 const struct json *where, *columns_json, *sort_json;
297 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
298 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
299 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
300 struct ovsdb_error *error;
302 table = parse_table(x, parser, "table");
303 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
304 columns_json = ovsdb_parser_member(parser, "columns",
305 OP_ARRAY | OP_OPTIONAL);
306 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
308 error = ovsdb_parser_get_error(parser);
310 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
314 error = ovsdb_column_set_from_json(columns_json, table, &columns);
317 error = ovsdb_column_set_from_json(sort_json, table, &sort);
320 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
322 ovsdb_query_distinct(table, &condition, &columns, &rows);
323 ovsdb_row_set_sort(&rows, &sort);
324 json_object_put(result, "rows",
325 ovsdb_row_set_to_json(&rows, &columns));
327 ovsdb_row_set_destroy(&rows);
330 ovsdb_column_set_destroy(&columns);
331 ovsdb_column_set_destroy(&sort);
332 ovsdb_condition_destroy(&condition);
337 struct update_row_cbdata {
339 struct ovsdb_txn *txn;
340 const struct ovsdb_row *row;
341 const struct ovsdb_column_set *columns;
345 update_row_cb(const struct ovsdb_row *row, void *ur_)
347 struct update_row_cbdata *ur = ur_;
350 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
351 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
352 ur->row, ur->columns);
359 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
362 struct ovsdb_table *table;
363 const struct json *where;
364 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
365 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
366 struct ovsdb_row *row = NULL;
367 struct update_row_cbdata ur;
368 struct ovsdb_error *error;
370 table = parse_table(x, parser, "table");
371 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
372 error = ovsdb_parser_get_error(parser);
374 error = parse_row(parser, "row", table, x->symtab, &row, &columns);
377 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
384 ur.columns = &columns;
385 ovsdb_query(table, &condition, update_row_cb, &ur);
386 json_object_put(result, "count", json_integer_create(ur.n_matches));
389 ovsdb_row_destroy(row);
390 ovsdb_column_set_destroy(&columns);
391 ovsdb_condition_destroy(&condition);
396 struct delete_row_cbdata {
398 const struct ovsdb_table *table;
399 struct ovsdb_txn *txn;
403 delete_row_cb(const struct ovsdb_row *row, void *dr_)
405 struct delete_row_cbdata *dr = dr_;
408 ovsdb_txn_row_delete(dr->txn, row);
414 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
417 struct ovsdb_table *table;
418 const struct json *where;
419 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
420 struct ovsdb_error *error;
422 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
423 table = parse_table(x, parser, "table");
424 error = ovsdb_parser_get_error(parser);
426 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
430 struct delete_row_cbdata dr;
435 ovsdb_query(table, &condition, delete_row_cb, &dr);
437 json_object_put(result, "count", json_integer_create(dr.n_matches));
440 ovsdb_condition_destroy(&condition);
445 struct wait_auxdata {
446 struct ovsdb_row_hash *actual;
447 struct ovsdb_row_hash *expected;
452 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
454 struct wait_auxdata *aux = aux_;
456 if (ovsdb_row_hash_contains(aux->expected, row)) {
457 ovsdb_row_hash_insert(aux->actual, row);
460 /* The query row isn't in the expected result set, so the actual and
461 * expected results sets definitely differ and we can short-circuit the
462 * rest of the query. */
468 static struct ovsdb_error *
469 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
470 struct json *result UNUSED)
472 struct ovsdb_table *table;
473 const struct json *timeout, *where, *columns_json, *until, *rows;
474 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
475 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
476 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
477 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
478 struct ovsdb_error *error;
479 struct wait_auxdata aux;
480 long long int timeout_msec = 0;
483 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
484 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
485 columns_json = ovsdb_parser_member(parser, "columns",
486 OP_ARRAY | OP_OPTIONAL);
487 until = ovsdb_parser_member(parser, "until", OP_STRING);
488 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
489 table = parse_table(x, parser, "table");
490 error = ovsdb_parser_get_error(parser);
492 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
496 error = ovsdb_column_set_from_json(columns_json, table, &columns);
500 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
501 if (timeout_msec < 0) {
502 error = ovsdb_syntax_error(timeout, NULL,
503 "timeout must be nonnegative");
504 } else if (timeout_msec < x->timeout_msec) {
505 x->timeout_msec = timeout_msec;
508 timeout_msec = LLONG_MAX;
510 if (strcmp(json_string(until), "==")
511 && strcmp(json_string(until), "!=")) {
512 error = ovsdb_syntax_error(until, NULL,
513 "\"until\" must be \"==\" or \"!=\"");
517 /* Parse "rows" into 'expected'. */
518 ovsdb_row_hash_init(&expected, &columns);
519 for (i = 0; i < rows->u.array.n; i++) {
520 struct ovsdb_error *error;
521 struct ovsdb_row *row;
523 row = ovsdb_row_create(table);
524 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
530 if (!ovsdb_row_hash_insert(&expected, row)) {
531 /* XXX Perhaps we should abort with an error or log a
533 ovsdb_row_destroy(row);
540 ovsdb_row_hash_init(&actual, &columns);
541 aux.actual = &actual;
542 aux.expected = &expected;
544 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
546 /* We know that every row in 'actual' is also in 'expected'. We
547 * also know that all of the rows in 'actual' are distinct and that
548 * all of the rows in 'expected' are distinct. Therefore, if
549 * 'actual' and 'expected' have the same number of rows, then they
550 * have the same content. */
551 size_t n_actual = ovsdb_row_hash_count(&actual);
552 size_t n_expected = ovsdb_row_hash_count(&expected);
553 equal = n_actual == n_expected;
555 if (!strcmp(json_string(until), "==") != equal) {
556 if (timeout && x->elapsed_msec >= timeout_msec) {
557 if (x->elapsed_msec) {
558 error = ovsdb_error("timed out",
559 "\"wait\" timed out after %lld ms",
562 error = ovsdb_error("timed out", "\"wait\" timed out");
565 /* ovsdb_execute() will change this, if triggers really are
567 error = ovsdb_error("not supported", "triggers not supported");
573 ovsdb_row_hash_destroy(&expected, true);
574 ovsdb_row_hash_destroy(&actual, false);
575 ovsdb_column_set_destroy(&columns);
576 ovsdb_condition_destroy(&condition);