+static void
+do_parse_mutations(int argc, char *argv[])
+{
+ struct ovsdb_table_schema *ts;
+ struct json *json;
+ int exit_code = 0;
+ int i;
+
+ json = unbox_json(parse_json(argv[1]));
+ check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
+ json_destroy(json);
+
+ for (i = 2; i < argc; i++) {
+ struct ovsdb_mutation_set set;
+ struct ovsdb_error *error;
+
+ json = parse_json(argv[i]);
+ error = ovsdb_mutation_set_from_json(ts, json, NULL, &set);
+ if (!error) {
+ print_and_free_json(ovsdb_mutation_set_to_json(&set));
+ } else {
+ char *s = ovsdb_error_to_string(error);
+ ovs_error(0, "%s", s);
+ free(s);
+ ovsdb_error_destroy(error);
+ exit_code = 1;
+ }
+ json_destroy(json);
+
+ ovsdb_mutation_set_destroy(&set);
+ }
+ ovsdb_table_schema_destroy(ts);
+
+ exit(exit_code);
+}
+
+static void
+do_execute_mutations(int argc OVS_UNUSED, char *argv[])
+{
+ struct ovsdb_table_schema *ts;
+ struct ovsdb_table *table;
+ struct ovsdb_mutation_set *sets;
+ size_t n_sets;
+ struct ovsdb_row **rows;
+ size_t n_rows;
+ struct json *json;
+ size_t i, j;
+
+ /* Parse table schema, create table. */
+ json = unbox_json(parse_json(argv[1]));
+ check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts));
+ json_destroy(json);
+
+ table = ovsdb_table_create(ts);
+
+ /* Parse mutations. */
+ json = parse_json(argv[2]);
+ if (json->type != JSON_ARRAY) {
+ ovs_fatal(0, "MUTATION argument is not JSON array");
+ }
+ n_sets = json->u.array.n;
+ sets = xmalloc(n_sets * sizeof *sets);
+ for (i = 0; i < n_sets; i++) {
+ check_ovsdb_error(ovsdb_mutation_set_from_json(ts,
+ json->u.array.elems[i],
+ NULL, &sets[i]));
+ }
+ json_destroy(json);
+
+ /* Parse rows. */
+ json = parse_json(argv[3]);
+ if (json->type != JSON_ARRAY) {
+ ovs_fatal(0, "ROW argument is not JSON array");
+ }
+ n_rows = json->u.array.n;
+ rows = xmalloc(n_rows * sizeof *rows);
+ for (i = 0; i < n_rows; i++) {
+ rows[i] = ovsdb_row_create(table);
+ check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[i],
+ NULL, NULL));
+ }
+ json_destroy(json);
+
+ for (i = 0; i < n_sets; i++) {
+ printf("mutation %2zu:\n", i);
+ for (j = 0; j < n_rows; j++) {
+ struct ovsdb_error *error;
+ struct ovsdb_row *row;
+
+ row = ovsdb_row_clone(rows[j]);
+ error = ovsdb_mutation_set_execute(row, &sets[i]);
+
+ printf("row %zu: ", j);
+ if (error) {
+ print_and_free_ovsdb_error(error);
+ } else {
+ struct ovsdb_column_set columns;
+ struct shash_node *node;
+
+ ovsdb_column_set_init(&columns);
+ SHASH_FOR_EACH (node, &ts->columns) {
+ struct ovsdb_column *c = node->data;
+ if (!ovsdb_datum_equals(&row->fields[c->index],
+ &rows[j]->fields[c->index],
+ &c->type)) {
+ ovsdb_column_set_add(&columns, c);
+ }
+ }
+ if (columns.n_columns) {
+ print_and_free_json(ovsdb_row_to_json(row, &columns));
+ } else {
+ printf("no change\n");
+ }
+ ovsdb_column_set_destroy(&columns);
+ }
+ ovsdb_row_destroy(row);
+ }
+ printf("\n");
+ }
+
+ for (i = 0; i < n_sets; i++) {
+ ovsdb_mutation_set_destroy(&sets[i]);
+ }
+ free(sets);
+ for (i = 0; i < n_rows; i++) {
+ ovsdb_row_destroy(rows[i]);
+ }
+ free(rows);
+ ovsdb_table_destroy(table); /* Also destroys 'ts'. */
+}
+
+/* Inserts a row, without bothering to update metadata such as refcounts. */
+static void
+put_row(struct ovsdb_table *table, struct ovsdb_row *row)
+{
+ const struct uuid *uuid = ovsdb_row_get_uuid(row);
+ if (!ovsdb_table_get_row(table, uuid)) {
+ hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
+ }
+}
+