]>
Commit | Line | Data |
---|---|---|
19b48a81 | 1 | /* Copyright (c) 2009, 2010, 2011 Nicira Networks |
f85f8ebb BP |
2 | * |
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: | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
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. | |
14 | */ | |
15 | ||
16 | #include <config.h> | |
17 | ||
18 | #include <assert.h> | |
19 | #include <limits.h> | |
20 | ||
21 | #include "column.h" | |
22 | #include "condition.h" | |
bd06962a | 23 | #include "file.h" |
f85f8ebb | 24 | #include "json.h" |
e9f8f936 | 25 | #include "mutation.h" |
f85f8ebb BP |
26 | #include "ovsdb-data.h" |
27 | #include "ovsdb-error.h" | |
28 | #include "ovsdb-parser.h" | |
29 | #include "ovsdb.h" | |
30 | #include "query.h" | |
31 | #include "row.h" | |
da897f41 | 32 | #include "server.h" |
f85f8ebb BP |
33 | #include "table.h" |
34 | #include "timeval.h" | |
35 | #include "transaction.h" | |
36 | ||
37 | struct ovsdb_execution { | |
38 | struct ovsdb *db; | |
da897f41 | 39 | const struct ovsdb_session *session; |
f85f8ebb BP |
40 | struct ovsdb_txn *txn; |
41 | struct ovsdb_symbol_table *symtab; | |
42 | bool durable; | |
43 | ||
44 | /* Triggers. */ | |
45 | long long int elapsed_msec; | |
46 | long long int timeout_msec; | |
47 | }; | |
48 | ||
49 | typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *, | |
50 | struct ovsdb_parser *, | |
51 | struct json *result); | |
52 | ||
f85f8ebb BP |
53 | static ovsdb_operation_executor ovsdb_execute_insert; |
54 | static ovsdb_operation_executor ovsdb_execute_select; | |
55 | static ovsdb_operation_executor ovsdb_execute_update; | |
e9f8f936 | 56 | static ovsdb_operation_executor ovsdb_execute_mutate; |
f85f8ebb BP |
57 | static ovsdb_operation_executor ovsdb_execute_delete; |
58 | static ovsdb_operation_executor ovsdb_execute_wait; | |
59 | static ovsdb_operation_executor ovsdb_execute_commit; | |
60 | static ovsdb_operation_executor ovsdb_execute_abort; | |
d171b584 | 61 | static ovsdb_operation_executor ovsdb_execute_comment; |
da897f41 | 62 | static ovsdb_operation_executor ovsdb_execute_assert; |
f85f8ebb BP |
63 | |
64 | static ovsdb_operation_executor * | |
65 | lookup_executor(const char *name) | |
66 | { | |
67 | struct ovsdb_operation { | |
68 | const char *name; | |
69 | ovsdb_operation_executor *executor; | |
70 | }; | |
71 | ||
72 | static const struct ovsdb_operation operations[] = { | |
73 | { "insert", ovsdb_execute_insert }, | |
74 | { "select", ovsdb_execute_select }, | |
75 | { "update", ovsdb_execute_update }, | |
e9f8f936 | 76 | { "mutate", ovsdb_execute_mutate }, |
f85f8ebb BP |
77 | { "delete", ovsdb_execute_delete }, |
78 | { "wait", ovsdb_execute_wait }, | |
79 | { "commit", ovsdb_execute_commit }, | |
80 | { "abort", ovsdb_execute_abort }, | |
d171b584 | 81 | { "comment", ovsdb_execute_comment }, |
da897f41 | 82 | { "assert", ovsdb_execute_assert }, |
f85f8ebb BP |
83 | }; |
84 | ||
85 | size_t i; | |
86 | ||
87 | for (i = 0; i < ARRAY_SIZE(operations); i++) { | |
88 | const struct ovsdb_operation *c = &operations[i]; | |
89 | if (!strcmp(c->name, name)) { | |
90 | return c->executor; | |
91 | } | |
92 | } | |
93 | return NULL; | |
94 | } | |
95 | ||
96 | struct json * | |
da897f41 BP |
97 | ovsdb_execute(struct ovsdb *db, const struct ovsdb_session *session, |
98 | const struct json *params, | |
f85f8ebb BP |
99 | long long int elapsed_msec, long long int *timeout_msec) |
100 | { | |
101 | struct ovsdb_execution x; | |
102 | struct ovsdb_error *error; | |
103 | struct json *results; | |
104 | size_t n_operations; | |
105 | size_t i; | |
106 | ||
9cb53f26 BP |
107 | if (params->type != JSON_ARRAY |
108 | || !params->u.array.n | |
109 | || params->u.array.elems[0]->type != JSON_STRING | |
110 | || strcmp(params->u.array.elems[0]->u.string, db->schema->name)) { | |
9cb53f26 BP |
111 | if (params->type != JSON_ARRAY) { |
112 | error = ovsdb_syntax_error(params, NULL, "array expected"); | |
113 | } else { | |
114 | error = ovsdb_syntax_error(params, NULL, "database name expected " | |
115 | "as first parameter"); | |
116 | } | |
117 | ||
f85f8ebb BP |
118 | results = ovsdb_error_to_json(error); |
119 | ovsdb_error_destroy(error); | |
120 | return results; | |
121 | } | |
122 | ||
123 | x.db = db; | |
da897f41 | 124 | x.session = session; |
f85f8ebb BP |
125 | x.txn = ovsdb_txn_create(db); |
126 | x.symtab = ovsdb_symbol_table_create(); | |
127 | x.durable = false; | |
128 | x.elapsed_msec = elapsed_msec; | |
129 | x.timeout_msec = LLONG_MAX; | |
130 | results = NULL; | |
131 | ||
132 | results = json_array_create_empty(); | |
9cb53f26 | 133 | n_operations = params->u.array.n - 1; |
f85f8ebb | 134 | error = NULL; |
9cb53f26 | 135 | for (i = 1; i <= n_operations; i++) { |
f85f8ebb BP |
136 | struct json *operation = params->u.array.elems[i]; |
137 | struct ovsdb_error *parse_error; | |
138 | struct ovsdb_parser parser; | |
139 | struct json *result; | |
140 | const struct json *op; | |
141 | ||
142 | /* Parse and execute operation. */ | |
143 | ovsdb_parser_init(&parser, operation, | |
0cd85e1b | 144 | "ovsdb operation %zu of %zu", i, n_operations); |
f85f8ebb BP |
145 | op = ovsdb_parser_member(&parser, "op", OP_ID); |
146 | result = json_object_create(); | |
147 | if (op) { | |
148 | const char *op_name = json_string(op); | |
149 | ovsdb_operation_executor *executor = lookup_executor(op_name); | |
150 | if (executor) { | |
151 | error = executor(&x, &parser, result); | |
152 | } else { | |
5764c0ed BP |
153 | ovsdb_parser_raise_error(&parser, "No operation \"%s\"", |
154 | op_name); | |
f85f8ebb BP |
155 | } |
156 | } else { | |
157 | assert(ovsdb_parser_has_error(&parser)); | |
158 | } | |
159 | ||
160 | /* A parse error overrides any other error. | |
161 | * An error overrides any other result. */ | |
162 | parse_error = ovsdb_parser_finish(&parser); | |
163 | if (parse_error) { | |
164 | ovsdb_error_destroy(error); | |
165 | error = parse_error; | |
166 | } | |
167 | if (error) { | |
168 | json_destroy(result); | |
169 | result = ovsdb_error_to_json(error); | |
170 | } | |
171 | if (error && !strcmp(ovsdb_error_get_tag(error), "not supported") | |
172 | && timeout_msec) { | |
173 | ovsdb_txn_abort(x.txn); | |
174 | *timeout_msec = x.timeout_msec; | |
e084f690 BP |
175 | |
176 | json_destroy(result); | |
f85f8ebb | 177 | json_destroy(results); |
e084f690 BP |
178 | results = NULL; |
179 | goto exit; | |
f85f8ebb BP |
180 | } |
181 | ||
182 | /* Add result to array. */ | |
183 | json_array_add(results, result); | |
184 | if (error) { | |
185 | break; | |
186 | } | |
187 | } | |
188 | ||
189 | if (!error) { | |
bd06962a | 190 | error = ovsdb_txn_commit(x.txn, x.durable); |
f85f8ebb BP |
191 | if (error) { |
192 | json_array_add(results, ovsdb_error_to_json(error)); | |
193 | } | |
194 | } else { | |
195 | ovsdb_txn_abort(x.txn); | |
196 | } | |
197 | ||
198 | while (json_array(results)->n < n_operations) { | |
199 | json_array_add(results, json_null_create()); | |
200 | } | |
201 | ||
e084f690 | 202 | exit: |
f85f8ebb BP |
203 | ovsdb_error_destroy(error); |
204 | ovsdb_symbol_table_destroy(x.symtab); | |
205 | ||
206 | return results; | |
207 | } | |
208 | ||
d3d8f1f7 | 209 | static struct ovsdb_error * |
f85f8ebb | 210 | ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser, |
c69ee87c | 211 | struct json *result OVS_UNUSED) |
f85f8ebb BP |
212 | { |
213 | const struct json *durable; | |
214 | ||
215 | durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN); | |
216 | if (durable && json_boolean(durable)) { | |
217 | x->durable = true; | |
218 | } | |
219 | return NULL; | |
220 | } | |
221 | ||
222 | static struct ovsdb_error * | |
c69ee87c BP |
223 | ovsdb_execute_abort(struct ovsdb_execution *x OVS_UNUSED, |
224 | struct ovsdb_parser *parser OVS_UNUSED, | |
225 | struct json *result OVS_UNUSED) | |
f85f8ebb BP |
226 | { |
227 | return ovsdb_error("aborted", "aborted by request"); | |
228 | } | |
229 | ||
f85f8ebb BP |
230 | static struct ovsdb_table * |
231 | parse_table(struct ovsdb_execution *x, | |
232 | struct ovsdb_parser *parser, const char *member) | |
233 | { | |
234 | struct ovsdb_table *table; | |
235 | const char *table_name; | |
236 | const struct json *json; | |
237 | ||
238 | json = ovsdb_parser_member(parser, member, OP_ID); | |
239 | if (!json) { | |
240 | return NULL; | |
241 | } | |
242 | table_name = json_string(json); | |
243 | ||
244 | table = shash_find_data(&x->db->tables, table_name); | |
245 | if (!table) { | |
246 | ovsdb_parser_raise_error(parser, "No table named %s.", table_name); | |
247 | } | |
248 | return table; | |
249 | } | |
250 | ||
251 | static WARN_UNUSED_RESULT struct ovsdb_error * | |
19b48a81 | 252 | parse_row(const struct json *json, const struct ovsdb_table *table, |
fbf925e4 | 253 | struct ovsdb_symbol_table *symtab, |
f85f8ebb BP |
254 | struct ovsdb_row **rowp, struct ovsdb_column_set *columns) |
255 | { | |
256 | struct ovsdb_error *error; | |
f85f8ebb BP |
257 | struct ovsdb_row *row; |
258 | ||
259 | *rowp = NULL; | |
260 | ||
261 | if (!table) { | |
262 | return OVSDB_BUG("null table"); | |
263 | } | |
f85f8ebb | 264 | if (!json) { |
19b48a81 | 265 | return OVSDB_BUG("null row"); |
f85f8ebb BP |
266 | } |
267 | ||
268 | row = ovsdb_row_create(table); | |
269 | error = ovsdb_row_from_json(row, json, symtab, columns); | |
270 | if (error) { | |
271 | ovsdb_row_destroy(row); | |
272 | return error; | |
273 | } else { | |
274 | *rowp = row; | |
275 | return NULL; | |
276 | } | |
277 | } | |
278 | ||
d3d8f1f7 | 279 | static struct ovsdb_error * |
f85f8ebb BP |
280 | ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser, |
281 | struct json *result) | |
282 | { | |
283 | struct ovsdb_table *table; | |
284 | struct ovsdb_row *row = NULL; | |
19b48a81 | 285 | const struct json *uuid_name, *row_json; |
f85f8ebb | 286 | struct ovsdb_error *error; |
6e30ca63 | 287 | struct uuid row_uuid; |
f85f8ebb BP |
288 | |
289 | table = parse_table(x, parser, "table"); | |
290 | uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL); | |
19b48a81 | 291 | row_json = ovsdb_parser_member(parser, "row", OP_OBJECT); |
f85f8ebb | 292 | error = ovsdb_parser_get_error(parser); |
19b48a81 BP |
293 | if (error) { |
294 | return error; | |
295 | } | |
6e30ca63 | 296 | |
6e30ca63 | 297 | if (uuid_name) { |
2d2d6d4a BP |
298 | struct ovsdb_symbol *symbol; |
299 | ||
fbf925e4 | 300 | symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name)); |
e9387de4 | 301 | if (symbol->created) { |
fbf925e4 BP |
302 | return ovsdb_syntax_error(uuid_name, "duplicate uuid-name", |
303 | "This \"uuid-name\" appeared on an " | |
304 | "earlier \"insert\" operation."); | |
2d2d6d4a | 305 | } |
fbf925e4 | 306 | row_uuid = symbol->uuid; |
e9387de4 | 307 | symbol->created = true; |
2d2d6d4a BP |
308 | } else { |
309 | uuid_generate(&row_uuid); | |
6e30ca63 BP |
310 | } |
311 | ||
f85f8ebb | 312 | if (!error) { |
19b48a81 | 313 | error = parse_row(row_json, table, x->symtab, &row, NULL); |
f85f8ebb | 314 | } |
bd76d25d BP |
315 | if (!error) { |
316 | /* Check constraints for columns not included in "row", in case the | |
317 | * default values do not satisfy the constraints. We could check only | |
318 | * the columns that have their default values by supplying an | |
319 | * ovsdb_column_set to parse_row() above, but I suspect that this is | |
320 | * cheaper. */ | |
321 | const struct shash_node *node; | |
322 | ||
323 | SHASH_FOR_EACH (node, &table->schema->columns) { | |
324 | const struct ovsdb_column *column = node->data; | |
325 | const struct ovsdb_datum *datum = &row->fields[column->index]; | |
326 | ||
327 | /* If there are 0 keys or pairs, there's nothing to check. | |
328 | * If there is 1, it might be a default value. | |
329 | * If there are more, it can't be a default value, so the value has | |
330 | * already been checked. */ | |
331 | if (datum->n == 1) { | |
332 | error = ovsdb_datum_check_constraints(datum, &column->type); | |
333 | if (error) { | |
334 | ovsdb_row_destroy(row); | |
335 | break; | |
336 | } | |
337 | } | |
338 | } | |
339 | } | |
f85f8ebb | 340 | if (!error) { |
6e30ca63 | 341 | *ovsdb_row_get_uuid_rw(row) = row_uuid; |
f85f8ebb BP |
342 | ovsdb_txn_row_insert(x->txn, row); |
343 | json_object_put(result, "uuid", | |
344 | ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID], | |
345 | &ovsdb_type_uuid)); | |
f85f8ebb BP |
346 | } |
347 | return error; | |
348 | } | |
349 | ||
d3d8f1f7 | 350 | static struct ovsdb_error * |
f85f8ebb BP |
351 | ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser, |
352 | struct json *result) | |
353 | { | |
354 | struct ovsdb_table *table; | |
355 | const struct json *where, *columns_json, *sort_json; | |
356 | struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER; | |
357 | struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER; | |
358 | struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER; | |
359 | struct ovsdb_error *error; | |
360 | ||
361 | table = parse_table(x, parser, "table"); | |
362 | where = ovsdb_parser_member(parser, "where", OP_ARRAY); | |
363 | columns_json = ovsdb_parser_member(parser, "columns", | |
364 | OP_ARRAY | OP_OPTIONAL); | |
365 | sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL); | |
366 | ||
367 | error = ovsdb_parser_get_error(parser); | |
368 | if (!error) { | |
369 | error = ovsdb_condition_from_json(table->schema, where, x->symtab, | |
370 | &condition); | |
371 | } | |
372 | if (!error) { | |
1cb29ab0 BP |
373 | error = ovsdb_column_set_from_json(columns_json, table->schema, |
374 | &columns); | |
f85f8ebb BP |
375 | } |
376 | if (!error) { | |
1cb29ab0 | 377 | error = ovsdb_column_set_from_json(sort_json, table->schema, &sort); |
f85f8ebb BP |
378 | } |
379 | if (!error) { | |
380 | struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER; | |
381 | ||
382 | ovsdb_query_distinct(table, &condition, &columns, &rows); | |
383 | ovsdb_row_set_sort(&rows, &sort); | |
384 | json_object_put(result, "rows", | |
385 | ovsdb_row_set_to_json(&rows, &columns)); | |
386 | ||
387 | ovsdb_row_set_destroy(&rows); | |
388 | } | |
389 | ||
390 | ovsdb_column_set_destroy(&columns); | |
391 | ovsdb_column_set_destroy(&sort); | |
392 | ovsdb_condition_destroy(&condition); | |
393 | ||
394 | return error; | |
395 | } | |
396 | ||
397 | struct update_row_cbdata { | |
398 | size_t n_matches; | |
399 | struct ovsdb_txn *txn; | |
400 | const struct ovsdb_row *row; | |
401 | const struct ovsdb_column_set *columns; | |
402 | }; | |
403 | ||
404 | static bool | |
405 | update_row_cb(const struct ovsdb_row *row, void *ur_) | |
406 | { | |
407 | struct update_row_cbdata *ur = ur_; | |
408 | ||
409 | ur->n_matches++; | |
410 | if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) { | |
411 | ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row), | |
412 | ur->row, ur->columns); | |
413 | } | |
414 | ||
415 | return true; | |
416 | } | |
417 | ||
d3d8f1f7 | 418 | static struct ovsdb_error * |
f85f8ebb BP |
419 | ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser, |
420 | struct json *result) | |
421 | { | |
422 | struct ovsdb_table *table; | |
19b48a81 | 423 | const struct json *where, *row_json; |
f85f8ebb BP |
424 | struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER; |
425 | struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER; | |
426 | struct ovsdb_row *row = NULL; | |
427 | struct update_row_cbdata ur; | |
428 | struct ovsdb_error *error; | |
429 | ||
430 | table = parse_table(x, parser, "table"); | |
431 | where = ovsdb_parser_member(parser, "where", OP_ARRAY); | |
19b48a81 | 432 | row_json = ovsdb_parser_member(parser, "row", OP_OBJECT); |
f85f8ebb BP |
433 | error = ovsdb_parser_get_error(parser); |
434 | if (!error) { | |
19b48a81 | 435 | error = parse_row(row_json, table, x->symtab, &row, &columns); |
f85f8ebb BP |
436 | } |
437 | if (!error) { | |
438 | error = ovsdb_condition_from_json(table->schema, where, x->symtab, | |
439 | &condition); | |
440 | } | |
441 | if (!error) { | |
442 | ur.n_matches = 0; | |
443 | ur.txn = x->txn; | |
444 | ur.row = row; | |
445 | ur.columns = &columns; | |
446 | ovsdb_query(table, &condition, update_row_cb, &ur); | |
447 | json_object_put(result, "count", json_integer_create(ur.n_matches)); | |
448 | } | |
449 | ||
450 | ovsdb_row_destroy(row); | |
451 | ovsdb_column_set_destroy(&columns); | |
452 | ovsdb_condition_destroy(&condition); | |
453 | ||
454 | return error; | |
455 | } | |
456 | ||
e9f8f936 BP |
457 | struct mutate_row_cbdata { |
458 | size_t n_matches; | |
459 | struct ovsdb_txn *txn; | |
460 | const struct ovsdb_mutation_set *mutations; | |
b7585d1d | 461 | struct ovsdb_error **error; |
e9f8f936 BP |
462 | }; |
463 | ||
464 | static bool | |
465 | mutate_row_cb(const struct ovsdb_row *row, void *mr_) | |
466 | { | |
467 | struct mutate_row_cbdata *mr = mr_; | |
468 | ||
469 | mr->n_matches++; | |
b7585d1d BP |
470 | *mr->error = ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row), |
471 | mr->mutations); | |
472 | return *mr->error == NULL; | |
e9f8f936 BP |
473 | } |
474 | ||
d3d8f1f7 | 475 | static struct ovsdb_error * |
e9f8f936 BP |
476 | ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser, |
477 | struct json *result) | |
478 | { | |
479 | struct ovsdb_table *table; | |
480 | const struct json *where; | |
481 | const struct json *mutations_json; | |
482 | struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER; | |
483 | struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER; | |
484 | struct ovsdb_row *row = NULL; | |
485 | struct mutate_row_cbdata mr; | |
486 | struct ovsdb_error *error; | |
487 | ||
488 | table = parse_table(x, parser, "table"); | |
489 | where = ovsdb_parser_member(parser, "where", OP_ARRAY); | |
490 | mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY); | |
491 | error = ovsdb_parser_get_error(parser); | |
492 | if (!error) { | |
493 | error = ovsdb_mutation_set_from_json(table->schema, mutations_json, | |
494 | x->symtab, &mutations); | |
495 | } | |
496 | if (!error) { | |
497 | error = ovsdb_condition_from_json(table->schema, where, x->symtab, | |
498 | &condition); | |
499 | } | |
500 | if (!error) { | |
501 | mr.n_matches = 0; | |
502 | mr.txn = x->txn; | |
503 | mr.mutations = &mutations; | |
b7585d1d | 504 | mr.error = &error; |
e9f8f936 BP |
505 | ovsdb_query(table, &condition, mutate_row_cb, &mr); |
506 | json_object_put(result, "count", json_integer_create(mr.n_matches)); | |
507 | } | |
508 | ||
509 | ovsdb_row_destroy(row); | |
510 | ovsdb_mutation_set_destroy(&mutations); | |
511 | ovsdb_condition_destroy(&condition); | |
512 | ||
513 | return error; | |
514 | } | |
515 | ||
f85f8ebb BP |
516 | struct delete_row_cbdata { |
517 | size_t n_matches; | |
518 | const struct ovsdb_table *table; | |
519 | struct ovsdb_txn *txn; | |
520 | }; | |
521 | ||
522 | static bool | |
523 | delete_row_cb(const struct ovsdb_row *row, void *dr_) | |
524 | { | |
525 | struct delete_row_cbdata *dr = dr_; | |
526 | ||
527 | dr->n_matches++; | |
528 | ovsdb_txn_row_delete(dr->txn, row); | |
529 | ||
530 | return true; | |
531 | } | |
532 | ||
d3d8f1f7 | 533 | static struct ovsdb_error * |
f85f8ebb BP |
534 | ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser, |
535 | struct json *result) | |
536 | { | |
537 | struct ovsdb_table *table; | |
538 | const struct json *where; | |
539 | struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER; | |
540 | struct ovsdb_error *error; | |
541 | ||
542 | where = ovsdb_parser_member(parser, "where", OP_ARRAY); | |
543 | table = parse_table(x, parser, "table"); | |
544 | error = ovsdb_parser_get_error(parser); | |
545 | if (!error) { | |
546 | error = ovsdb_condition_from_json(table->schema, where, x->symtab, | |
547 | &condition); | |
548 | } | |
549 | if (!error) { | |
550 | struct delete_row_cbdata dr; | |
551 | ||
552 | dr.n_matches = 0; | |
553 | dr.table = table; | |
554 | dr.txn = x->txn; | |
555 | ovsdb_query(table, &condition, delete_row_cb, &dr); | |
556 | ||
557 | json_object_put(result, "count", json_integer_create(dr.n_matches)); | |
558 | } | |
559 | ||
560 | ovsdb_condition_destroy(&condition); | |
561 | ||
562 | return error; | |
563 | } | |
564 | ||
565 | struct wait_auxdata { | |
566 | struct ovsdb_row_hash *actual; | |
567 | struct ovsdb_row_hash *expected; | |
568 | bool *equal; | |
569 | }; | |
570 | ||
571 | static bool | |
572 | ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_) | |
573 | { | |
574 | struct wait_auxdata *aux = aux_; | |
575 | ||
576 | if (ovsdb_row_hash_contains(aux->expected, row)) { | |
577 | ovsdb_row_hash_insert(aux->actual, row); | |
578 | return true; | |
579 | } else { | |
580 | /* The query row isn't in the expected result set, so the actual and | |
581 | * expected results sets definitely differ and we can short-circuit the | |
582 | * rest of the query. */ | |
583 | *aux->equal = false; | |
584 | return false; | |
585 | } | |
586 | } | |
587 | ||
588 | static struct ovsdb_error * | |
589 | ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser, | |
c69ee87c | 590 | struct json *result OVS_UNUSED) |
f85f8ebb BP |
591 | { |
592 | struct ovsdb_table *table; | |
593 | const struct json *timeout, *where, *columns_json, *until, *rows; | |
594 | struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER; | |
595 | struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER; | |
596 | struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected); | |
597 | struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual); | |
598 | struct ovsdb_error *error; | |
599 | struct wait_auxdata aux; | |
600 | long long int timeout_msec = 0; | |
601 | size_t i; | |
602 | ||
603 | timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL); | |
604 | where = ovsdb_parser_member(parser, "where", OP_ARRAY); | |
605 | columns_json = ovsdb_parser_member(parser, "columns", | |
606 | OP_ARRAY | OP_OPTIONAL); | |
607 | until = ovsdb_parser_member(parser, "until", OP_STRING); | |
608 | rows = ovsdb_parser_member(parser, "rows", OP_ARRAY); | |
609 | table = parse_table(x, parser, "table"); | |
610 | error = ovsdb_parser_get_error(parser); | |
611 | if (!error) { | |
612 | error = ovsdb_condition_from_json(table->schema, where, x->symtab, | |
613 | &condition); | |
614 | } | |
615 | if (!error) { | |
1cb29ab0 BP |
616 | error = ovsdb_column_set_from_json(columns_json, table->schema, |
617 | &columns); | |
f85f8ebb BP |
618 | } |
619 | if (!error) { | |
620 | if (timeout) { | |
621 | timeout_msec = MIN(LLONG_MAX, json_real(timeout)); | |
622 | if (timeout_msec < 0) { | |
623 | error = ovsdb_syntax_error(timeout, NULL, | |
624 | "timeout must be nonnegative"); | |
625 | } else if (timeout_msec < x->timeout_msec) { | |
626 | x->timeout_msec = timeout_msec; | |
627 | } | |
628 | } else { | |
629 | timeout_msec = LLONG_MAX; | |
630 | } | |
631 | if (strcmp(json_string(until), "==") | |
632 | && strcmp(json_string(until), "!=")) { | |
633 | error = ovsdb_syntax_error(until, NULL, | |
634 | "\"until\" must be \"==\" or \"!=\""); | |
635 | } | |
636 | } | |
637 | if (!error) { | |
638 | /* Parse "rows" into 'expected'. */ | |
639 | ovsdb_row_hash_init(&expected, &columns); | |
640 | for (i = 0; i < rows->u.array.n; i++) { | |
f85f8ebb BP |
641 | struct ovsdb_row *row; |
642 | ||
643 | row = ovsdb_row_create(table); | |
644 | error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab, | |
645 | NULL); | |
646 | if (error) { | |
647 | break; | |
648 | } | |
649 | ||
650 | if (!ovsdb_row_hash_insert(&expected, row)) { | |
651 | /* XXX Perhaps we should abort with an error or log a | |
652 | * warning. */ | |
653 | ovsdb_row_destroy(row); | |
654 | } | |
655 | } | |
656 | } | |
657 | if (!error) { | |
658 | /* Execute query. */ | |
659 | bool equal = true; | |
660 | ovsdb_row_hash_init(&actual, &columns); | |
661 | aux.actual = &actual; | |
662 | aux.expected = &expected; | |
663 | aux.equal = &equal; | |
664 | ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux); | |
665 | if (equal) { | |
666 | /* We know that every row in 'actual' is also in 'expected'. We | |
667 | * also know that all of the rows in 'actual' are distinct and that | |
668 | * all of the rows in 'expected' are distinct. Therefore, if | |
669 | * 'actual' and 'expected' have the same number of rows, then they | |
670 | * have the same content. */ | |
671 | size_t n_actual = ovsdb_row_hash_count(&actual); | |
672 | size_t n_expected = ovsdb_row_hash_count(&expected); | |
673 | equal = n_actual == n_expected; | |
674 | } | |
675 | if (!strcmp(json_string(until), "==") != equal) { | |
676 | if (timeout && x->elapsed_msec >= timeout_msec) { | |
677 | if (x->elapsed_msec) { | |
678 | error = ovsdb_error("timed out", | |
679 | "\"wait\" timed out after %lld ms", | |
680 | x->elapsed_msec); | |
681 | } else { | |
682 | error = ovsdb_error("timed out", "\"wait\" timed out"); | |
683 | } | |
684 | } else { | |
685 | /* ovsdb_execute() will change this, if triggers really are | |
686 | * supported. */ | |
687 | error = ovsdb_error("not supported", "triggers not supported"); | |
688 | } | |
689 | } | |
690 | } | |
691 | ||
692 | ||
693 | ovsdb_row_hash_destroy(&expected, true); | |
694 | ovsdb_row_hash_destroy(&actual, false); | |
695 | ovsdb_column_set_destroy(&columns); | |
696 | ovsdb_condition_destroy(&condition); | |
697 | ||
698 | return error; | |
699 | } | |
2d2d6d4a | 700 | |
d171b584 BP |
701 | static struct ovsdb_error * |
702 | ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser, | |
c69ee87c | 703 | struct json *result OVS_UNUSED) |
d171b584 BP |
704 | { |
705 | const struct json *comment; | |
706 | ||
707 | comment = ovsdb_parser_member(parser, "comment", OP_STRING); | |
708 | if (!comment) { | |
709 | return NULL; | |
710 | } | |
711 | ovsdb_txn_add_comment(x->txn, json_string(comment)); | |
712 | ||
713 | return NULL; | |
714 | } | |
da897f41 BP |
715 | |
716 | static struct ovsdb_error * | |
717 | ovsdb_execute_assert(struct ovsdb_execution *x, struct ovsdb_parser *parser, | |
718 | struct json *result OVS_UNUSED) | |
719 | { | |
720 | const struct json *lock_name; | |
721 | ||
722 | lock_name = ovsdb_parser_member(parser, "lock", OP_STRING); | |
723 | if (!lock_name) { | |
724 | return NULL; | |
725 | } | |
726 | ||
727 | if (x->session) { | |
728 | const struct ovsdb_lock_waiter *waiter; | |
729 | ||
730 | waiter = ovsdb_session_get_lock_waiter(x->session, | |
731 | json_string(lock_name)); | |
732 | if (waiter && ovsdb_lock_waiter_is_owner(waiter)) { | |
733 | return NULL; | |
734 | } | |
735 | } | |
736 | ||
737 | return ovsdb_error("not owner", "Asserted lock %s not held.", | |
738 | json_string(lock_name)); | |
739 | } |