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;
269 struct uuid row_uuid;
271 table = parse_table(x, parser, "table");
272 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
273 error = ovsdb_parser_get_error(parser);
275 uuid_generate(&row_uuid);
277 ovsdb_symbol_table_put(x->symtab, json_string(uuid_name), &row_uuid);
281 error = parse_row(parser, "row", table, x->symtab, &row, NULL);
284 *ovsdb_row_get_uuid_rw(row) = row_uuid;
285 ovsdb_txn_row_insert(x->txn, row);
286 json_object_put(result, "uuid",
287 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
295 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
298 struct ovsdb_table *table;
299 const struct json *where, *columns_json, *sort_json;
300 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
301 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
302 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
303 struct ovsdb_error *error;
305 table = parse_table(x, parser, "table");
306 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
307 columns_json = ovsdb_parser_member(parser, "columns",
308 OP_ARRAY | OP_OPTIONAL);
309 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
311 error = ovsdb_parser_get_error(parser);
313 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
317 error = ovsdb_column_set_from_json(columns_json, table, &columns);
320 error = ovsdb_column_set_from_json(sort_json, table, &sort);
323 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
325 ovsdb_query_distinct(table, &condition, &columns, &rows);
326 ovsdb_row_set_sort(&rows, &sort);
327 json_object_put(result, "rows",
328 ovsdb_row_set_to_json(&rows, &columns));
330 ovsdb_row_set_destroy(&rows);
333 ovsdb_column_set_destroy(&columns);
334 ovsdb_column_set_destroy(&sort);
335 ovsdb_condition_destroy(&condition);
340 struct update_row_cbdata {
342 struct ovsdb_txn *txn;
343 const struct ovsdb_row *row;
344 const struct ovsdb_column_set *columns;
348 update_row_cb(const struct ovsdb_row *row, void *ur_)
350 struct update_row_cbdata *ur = ur_;
353 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
354 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
355 ur->row, ur->columns);
362 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
365 struct ovsdb_table *table;
366 const struct json *where;
367 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
368 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
369 struct ovsdb_row *row = NULL;
370 struct update_row_cbdata ur;
371 struct ovsdb_error *error;
373 table = parse_table(x, parser, "table");
374 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
375 error = ovsdb_parser_get_error(parser);
377 error = parse_row(parser, "row", table, x->symtab, &row, &columns);
380 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
387 ur.columns = &columns;
388 ovsdb_query(table, &condition, update_row_cb, &ur);
389 json_object_put(result, "count", json_integer_create(ur.n_matches));
392 ovsdb_row_destroy(row);
393 ovsdb_column_set_destroy(&columns);
394 ovsdb_condition_destroy(&condition);
399 struct delete_row_cbdata {
401 const struct ovsdb_table *table;
402 struct ovsdb_txn *txn;
406 delete_row_cb(const struct ovsdb_row *row, void *dr_)
408 struct delete_row_cbdata *dr = dr_;
411 ovsdb_txn_row_delete(dr->txn, row);
417 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
420 struct ovsdb_table *table;
421 const struct json *where;
422 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
423 struct ovsdb_error *error;
425 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
426 table = parse_table(x, parser, "table");
427 error = ovsdb_parser_get_error(parser);
429 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
433 struct delete_row_cbdata dr;
438 ovsdb_query(table, &condition, delete_row_cb, &dr);
440 json_object_put(result, "count", json_integer_create(dr.n_matches));
443 ovsdb_condition_destroy(&condition);
448 struct wait_auxdata {
449 struct ovsdb_row_hash *actual;
450 struct ovsdb_row_hash *expected;
455 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
457 struct wait_auxdata *aux = aux_;
459 if (ovsdb_row_hash_contains(aux->expected, row)) {
460 ovsdb_row_hash_insert(aux->actual, row);
463 /* The query row isn't in the expected result set, so the actual and
464 * expected results sets definitely differ and we can short-circuit the
465 * rest of the query. */
471 static struct ovsdb_error *
472 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
473 struct json *result UNUSED)
475 struct ovsdb_table *table;
476 const struct json *timeout, *where, *columns_json, *until, *rows;
477 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
478 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
479 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
480 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
481 struct ovsdb_error *error;
482 struct wait_auxdata aux;
483 long long int timeout_msec = 0;
486 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
487 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
488 columns_json = ovsdb_parser_member(parser, "columns",
489 OP_ARRAY | OP_OPTIONAL);
490 until = ovsdb_parser_member(parser, "until", OP_STRING);
491 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
492 table = parse_table(x, parser, "table");
493 error = ovsdb_parser_get_error(parser);
495 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
499 error = ovsdb_column_set_from_json(columns_json, table, &columns);
503 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
504 if (timeout_msec < 0) {
505 error = ovsdb_syntax_error(timeout, NULL,
506 "timeout must be nonnegative");
507 } else if (timeout_msec < x->timeout_msec) {
508 x->timeout_msec = timeout_msec;
511 timeout_msec = LLONG_MAX;
513 if (strcmp(json_string(until), "==")
514 && strcmp(json_string(until), "!=")) {
515 error = ovsdb_syntax_error(until, NULL,
516 "\"until\" must be \"==\" or \"!=\"");
520 /* Parse "rows" into 'expected'. */
521 ovsdb_row_hash_init(&expected, &columns);
522 for (i = 0; i < rows->u.array.n; i++) {
523 struct ovsdb_error *error;
524 struct ovsdb_row *row;
526 row = ovsdb_row_create(table);
527 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
533 if (!ovsdb_row_hash_insert(&expected, row)) {
534 /* XXX Perhaps we should abort with an error or log a
536 ovsdb_row_destroy(row);
543 ovsdb_row_hash_init(&actual, &columns);
544 aux.actual = &actual;
545 aux.expected = &expected;
547 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
549 /* We know that every row in 'actual' is also in 'expected'. We
550 * also know that all of the rows in 'actual' are distinct and that
551 * all of the rows in 'expected' are distinct. Therefore, if
552 * 'actual' and 'expected' have the same number of rows, then they
553 * have the same content. */
554 size_t n_actual = ovsdb_row_hash_count(&actual);
555 size_t n_expected = ovsdb_row_hash_count(&expected);
556 equal = n_actual == n_expected;
558 if (!strcmp(json_string(until), "==") != equal) {
559 if (timeout && x->elapsed_msec >= timeout_msec) {
560 if (x->elapsed_msec) {
561 error = ovsdb_error("timed out",
562 "\"wait\" timed out after %lld ms",
565 error = ovsdb_error("timed out", "\"wait\" timed out");
568 /* ovsdb_execute() will change this, if triggers really are
570 error = ovsdb_error("not supported", "triggers not supported");
576 ovsdb_row_hash_destroy(&expected, true);
577 ovsdb_row_hash_destroy(&actual, false);
578 ovsdb_column_set_destroy(&columns);
579 ovsdb_condition_destroy(&condition);