1 /* Copyright (c) 2009, 2010 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"
26 #include "ovsdb-data.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
34 #include "transaction.h"
36 struct ovsdb_execution {
38 struct ovsdb_txn *txn;
39 struct ovsdb_symbol_table *symtab;
43 long long int elapsed_msec;
44 long long int timeout_msec;
47 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
48 struct ovsdb_parser *,
51 static ovsdb_operation_executor ovsdb_execute_insert;
52 static ovsdb_operation_executor ovsdb_execute_select;
53 static ovsdb_operation_executor ovsdb_execute_update;
54 static ovsdb_operation_executor ovsdb_execute_mutate;
55 static ovsdb_operation_executor ovsdb_execute_delete;
56 static ovsdb_operation_executor ovsdb_execute_wait;
57 static ovsdb_operation_executor ovsdb_execute_commit;
58 static ovsdb_operation_executor ovsdb_execute_abort;
59 static ovsdb_operation_executor ovsdb_execute_comment;
61 static ovsdb_operation_executor *
62 lookup_executor(const char *name)
64 struct ovsdb_operation {
66 ovsdb_operation_executor *executor;
69 static const struct ovsdb_operation operations[] = {
70 { "insert", ovsdb_execute_insert },
71 { "select", ovsdb_execute_select },
72 { "update", ovsdb_execute_update },
73 { "mutate", ovsdb_execute_mutate },
74 { "delete", ovsdb_execute_delete },
75 { "wait", ovsdb_execute_wait },
76 { "commit", ovsdb_execute_commit },
77 { "abort", ovsdb_execute_abort },
78 { "comment", ovsdb_execute_comment },
83 for (i = 0; i < ARRAY_SIZE(operations); i++) {
84 const struct ovsdb_operation *c = &operations[i];
85 if (!strcmp(c->name, name)) {
93 ovsdb_execute(struct ovsdb *db, const struct json *params,
94 long long int elapsed_msec, long long int *timeout_msec)
96 struct ovsdb_execution x;
97 struct ovsdb_error *error;
102 if (params->type != JSON_ARRAY) {
103 struct ovsdb_error *error;
105 error = ovsdb_syntax_error(params, NULL, "array expected");
106 results = ovsdb_error_to_json(error);
107 ovsdb_error_destroy(error);
112 x.txn = ovsdb_txn_create(db);
113 x.symtab = ovsdb_symbol_table_create();
115 x.elapsed_msec = elapsed_msec;
116 x.timeout_msec = LLONG_MAX;
119 results = json_array_create_empty();
120 n_operations = params->u.array.n;
122 for (i = 0; i < n_operations; i++) {
123 struct json *operation = params->u.array.elems[i];
124 struct ovsdb_error *parse_error;
125 struct ovsdb_parser parser;
127 const struct json *op;
129 /* Parse and execute operation. */
130 ovsdb_parser_init(&parser, operation,
131 "ovsdb operation %zu of %zu", i + 1, n_operations);
132 op = ovsdb_parser_member(&parser, "op", OP_ID);
133 result = json_object_create();
135 const char *op_name = json_string(op);
136 ovsdb_operation_executor *executor = lookup_executor(op_name);
138 error = executor(&x, &parser, result);
140 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
144 assert(ovsdb_parser_has_error(&parser));
147 /* A parse error overrides any other error.
148 * An error overrides any other result. */
149 parse_error = ovsdb_parser_finish(&parser);
151 ovsdb_error_destroy(error);
155 json_destroy(result);
156 result = ovsdb_error_to_json(error);
158 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
160 ovsdb_txn_abort(x.txn);
161 *timeout_msec = x.timeout_msec;
163 json_destroy(result);
164 json_destroy(results);
169 /* Add result to array. */
170 json_array_add(results, result);
177 error = ovsdb_txn_commit(x.txn, x.durable);
179 json_array_add(results, ovsdb_error_to_json(error));
182 ovsdb_txn_abort(x.txn);
185 while (json_array(results)->n < n_operations) {
186 json_array_add(results, json_null_create());
190 ovsdb_error_destroy(error);
191 ovsdb_symbol_table_destroy(x.symtab);
197 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
198 struct json *result UNUSED)
200 const struct json *durable;
202 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
203 if (durable && json_boolean(durable)) {
209 static struct ovsdb_error *
210 ovsdb_execute_abort(struct ovsdb_execution *x UNUSED,
211 struct ovsdb_parser *parser UNUSED,
212 struct json *result UNUSED)
214 return ovsdb_error("aborted", "aborted by request");
217 static struct ovsdb_table *
218 parse_table(struct ovsdb_execution *x,
219 struct ovsdb_parser *parser, const char *member)
221 struct ovsdb_table *table;
222 const char *table_name;
223 const struct json *json;
225 json = ovsdb_parser_member(parser, member, OP_ID);
229 table_name = json_string(json);
231 table = shash_find_data(&x->db->tables, table_name);
233 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
238 static WARN_UNUSED_RESULT struct ovsdb_error *
239 parse_row(struct ovsdb_parser *parser, const char *member,
240 const struct ovsdb_table *table,
241 struct ovsdb_symbol_table *symtab,
242 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
244 struct ovsdb_error *error;
245 const struct json *json;
246 struct ovsdb_row *row;
251 return OVSDB_BUG("null table");
253 json = ovsdb_parser_member(parser, member, OP_OBJECT);
255 return OVSDB_BUG("null row member");
258 row = ovsdb_row_create(table);
259 error = ovsdb_row_from_json(row, json, symtab, columns);
261 ovsdb_row_destroy(row);
270 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
273 struct ovsdb_table *table;
274 struct ovsdb_row *row = NULL;
275 const struct json *uuid_name;
276 struct ovsdb_error *error;
277 struct uuid row_uuid;
279 table = parse_table(x, parser, "table");
280 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
281 error = ovsdb_parser_get_error(parser);
284 struct ovsdb_symbol *symbol;
286 symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
288 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
289 "This \"uuid-name\" appeared on an "
290 "earlier \"insert\" operation.");
292 row_uuid = symbol->uuid;
295 uuid_generate(&row_uuid);
299 error = parse_row(parser, "row", table, x->symtab, &row, NULL);
302 /* Check constraints for columns not included in "row", in case the
303 * default values do not satisfy the constraints. We could check only
304 * the columns that have their default values by supplying an
305 * ovsdb_column_set to parse_row() above, but I suspect that this is
307 const struct shash_node *node;
309 SHASH_FOR_EACH (node, &table->schema->columns) {
310 const struct ovsdb_column *column = node->data;
311 const struct ovsdb_datum *datum = &row->fields[column->index];
313 /* If there are 0 keys or pairs, there's nothing to check.
314 * If there is 1, it might be a default value.
315 * If there are more, it can't be a default value, so the value has
316 * already been checked. */
318 error = ovsdb_datum_check_constraints(datum, &column->type);
320 ovsdb_row_destroy(row);
327 *ovsdb_row_get_uuid_rw(row) = row_uuid;
328 ovsdb_txn_row_insert(x->txn, row);
329 json_object_put(result, "uuid",
330 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
337 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
340 struct ovsdb_table *table;
341 const struct json *where, *columns_json, *sort_json;
342 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
343 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
344 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
345 struct ovsdb_error *error;
347 table = parse_table(x, parser, "table");
348 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
349 columns_json = ovsdb_parser_member(parser, "columns",
350 OP_ARRAY | OP_OPTIONAL);
351 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
353 error = ovsdb_parser_get_error(parser);
355 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
359 error = ovsdb_column_set_from_json(columns_json, table, &columns);
362 error = ovsdb_column_set_from_json(sort_json, table, &sort);
365 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
367 ovsdb_query_distinct(table, &condition, &columns, &rows);
368 ovsdb_row_set_sort(&rows, &sort);
369 json_object_put(result, "rows",
370 ovsdb_row_set_to_json(&rows, &columns));
372 ovsdb_row_set_destroy(&rows);
375 ovsdb_column_set_destroy(&columns);
376 ovsdb_column_set_destroy(&sort);
377 ovsdb_condition_destroy(&condition);
382 struct update_row_cbdata {
384 struct ovsdb_txn *txn;
385 const struct ovsdb_row *row;
386 const struct ovsdb_column_set *columns;
390 update_row_cb(const struct ovsdb_row *row, void *ur_)
392 struct update_row_cbdata *ur = ur_;
395 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
396 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
397 ur->row, ur->columns);
404 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
407 struct ovsdb_table *table;
408 const struct json *where;
409 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
410 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
411 struct ovsdb_row *row = NULL;
412 struct update_row_cbdata ur;
413 struct ovsdb_error *error;
415 table = parse_table(x, parser, "table");
416 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
417 error = ovsdb_parser_get_error(parser);
419 error = parse_row(parser, "row", table, x->symtab, &row, &columns);
422 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
429 ur.columns = &columns;
430 ovsdb_query(table, &condition, update_row_cb, &ur);
431 json_object_put(result, "count", json_integer_create(ur.n_matches));
434 ovsdb_row_destroy(row);
435 ovsdb_column_set_destroy(&columns);
436 ovsdb_condition_destroy(&condition);
441 struct mutate_row_cbdata {
443 struct ovsdb_txn *txn;
444 const struct ovsdb_mutation_set *mutations;
448 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
450 struct mutate_row_cbdata *mr = mr_;
453 ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
460 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
463 struct ovsdb_table *table;
464 const struct json *where;
465 const struct json *mutations_json;
466 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
467 struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
468 struct ovsdb_row *row = NULL;
469 struct mutate_row_cbdata mr;
470 struct ovsdb_error *error;
472 table = parse_table(x, parser, "table");
473 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
474 mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
475 error = ovsdb_parser_get_error(parser);
477 error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
478 x->symtab, &mutations);
481 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
487 mr.mutations = &mutations;
488 ovsdb_query(table, &condition, mutate_row_cb, &mr);
489 json_object_put(result, "count", json_integer_create(mr.n_matches));
492 ovsdb_row_destroy(row);
493 ovsdb_mutation_set_destroy(&mutations);
494 ovsdb_condition_destroy(&condition);
499 struct delete_row_cbdata {
501 const struct ovsdb_table *table;
502 struct ovsdb_txn *txn;
506 delete_row_cb(const struct ovsdb_row *row, void *dr_)
508 struct delete_row_cbdata *dr = dr_;
511 ovsdb_txn_row_delete(dr->txn, row);
517 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
520 struct ovsdb_table *table;
521 const struct json *where;
522 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
523 struct ovsdb_error *error;
525 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
526 table = parse_table(x, parser, "table");
527 error = ovsdb_parser_get_error(parser);
529 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
533 struct delete_row_cbdata dr;
538 ovsdb_query(table, &condition, delete_row_cb, &dr);
540 json_object_put(result, "count", json_integer_create(dr.n_matches));
543 ovsdb_condition_destroy(&condition);
548 struct wait_auxdata {
549 struct ovsdb_row_hash *actual;
550 struct ovsdb_row_hash *expected;
555 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
557 struct wait_auxdata *aux = aux_;
559 if (ovsdb_row_hash_contains(aux->expected, row)) {
560 ovsdb_row_hash_insert(aux->actual, row);
563 /* The query row isn't in the expected result set, so the actual and
564 * expected results sets definitely differ and we can short-circuit the
565 * rest of the query. */
571 static struct ovsdb_error *
572 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
573 struct json *result UNUSED)
575 struct ovsdb_table *table;
576 const struct json *timeout, *where, *columns_json, *until, *rows;
577 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
578 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
579 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
580 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
581 struct ovsdb_error *error;
582 struct wait_auxdata aux;
583 long long int timeout_msec = 0;
586 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
587 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
588 columns_json = ovsdb_parser_member(parser, "columns",
589 OP_ARRAY | OP_OPTIONAL);
590 until = ovsdb_parser_member(parser, "until", OP_STRING);
591 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
592 table = parse_table(x, parser, "table");
593 error = ovsdb_parser_get_error(parser);
595 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
599 error = ovsdb_column_set_from_json(columns_json, table, &columns);
603 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
604 if (timeout_msec < 0) {
605 error = ovsdb_syntax_error(timeout, NULL,
606 "timeout must be nonnegative");
607 } else if (timeout_msec < x->timeout_msec) {
608 x->timeout_msec = timeout_msec;
611 timeout_msec = LLONG_MAX;
613 if (strcmp(json_string(until), "==")
614 && strcmp(json_string(until), "!=")) {
615 error = ovsdb_syntax_error(until, NULL,
616 "\"until\" must be \"==\" or \"!=\"");
620 /* Parse "rows" into 'expected'. */
621 ovsdb_row_hash_init(&expected, &columns);
622 for (i = 0; i < rows->u.array.n; i++) {
623 struct ovsdb_error *error;
624 struct ovsdb_row *row;
626 row = ovsdb_row_create(table);
627 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
633 if (!ovsdb_row_hash_insert(&expected, row)) {
634 /* XXX Perhaps we should abort with an error or log a
636 ovsdb_row_destroy(row);
643 ovsdb_row_hash_init(&actual, &columns);
644 aux.actual = &actual;
645 aux.expected = &expected;
647 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
649 /* We know that every row in 'actual' is also in 'expected'. We
650 * also know that all of the rows in 'actual' are distinct and that
651 * all of the rows in 'expected' are distinct. Therefore, if
652 * 'actual' and 'expected' have the same number of rows, then they
653 * have the same content. */
654 size_t n_actual = ovsdb_row_hash_count(&actual);
655 size_t n_expected = ovsdb_row_hash_count(&expected);
656 equal = n_actual == n_expected;
658 if (!strcmp(json_string(until), "==") != equal) {
659 if (timeout && x->elapsed_msec >= timeout_msec) {
660 if (x->elapsed_msec) {
661 error = ovsdb_error("timed out",
662 "\"wait\" timed out after %lld ms",
665 error = ovsdb_error("timed out", "\"wait\" timed out");
668 /* ovsdb_execute() will change this, if triggers really are
670 error = ovsdb_error("not supported", "triggers not supported");
676 ovsdb_row_hash_destroy(&expected, true);
677 ovsdb_row_hash_destroy(&actual, false);
678 ovsdb_column_set_destroy(&columns);
679 ovsdb_condition_destroy(&condition);
684 static struct ovsdb_error *
685 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
686 struct json *result UNUSED)
688 const struct json *comment;
690 comment = ovsdb_parser_member(parser, "comment", OP_STRING);
694 ovsdb_txn_add_comment(x->txn, json_string(comment));