+ }
+ }
+
+ /* Use UUID columns as tie-breakers. */
+ for (x = 0; x < aux->n_columns; x++) {
+ if (aux->columns[x]->type.key.type == OVSDB_TYPE_UUID) {
+ int cmp = compare_data(a_y, b_y, x, aux);
+ if (cmp) {
+ return cmp;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static void
+swap_rows(size_t a_y, size_t b_y, void *aux_)
+{
+ struct dump_table_aux *aux = aux_;
+ struct ovsdb_datum *tmp = aux->data[a_y];
+ aux->data[a_y] = aux->data[b_y];
+ aux->data[b_y] = tmp;
+}
+
+static int
+compare_columns(const void *a_, const void *b_)
+{
+ const struct ovsdb_column *const *ap = a_;
+ const struct ovsdb_column *const *bp = b_;
+ const struct ovsdb_column *a = *ap;
+ const struct ovsdb_column *b = *bp;
+
+ return strcmp(a->name, b->name);
+}
+
+static void
+dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
+{
+ const struct ovsdb_column **columns;
+ size_t n_columns;
+
+ struct ovsdb_datum **data;
+
+ struct dump_table_aux aux;
+ struct shash_node *node;
+ struct table t;
+ size_t x, y;
+
+ /* Sort columns by name, for reproducibility. */
+ columns = xmalloc(shash_count(&ts->columns) * sizeof *columns);
+ n_columns = 0;
+ SHASH_FOR_EACH (node, &ts->columns) {
+ struct ovsdb_column *column = node->data;
+ if (strcmp(column->name, "_version")) {
+ columns[n_columns++] = column;
+ }
+ }
+ qsort(columns, n_columns, sizeof *columns, compare_columns);
+
+ /* Extract data from table. */
+ data = xmalloc(rows->n * sizeof *data);
+ for (y = 0; y < rows->n; y++) {
+ struct shash *row;
+
+ if (rows->elems[y]->type != JSON_OBJECT) {
+ ovs_fatal(0, "row %"PRIuSIZE" in table %s response is not a JSON object: "
+ "%s", y, ts->name, json_to_string(rows->elems[y], 0));
+ }
+ row = json_object(rows->elems[y]);
+
+ data[y] = xmalloc(n_columns * sizeof **data);
+ for (x = 0; x < n_columns; x++) {
+ const struct json *json = shash_find_data(row, columns[x]->name);
+ if (!json) {
+ ovs_fatal(0, "row %"PRIuSIZE" in table %s response lacks %s column",
+ y, ts->name, columns[x]->name);
+ }
+
+ check_ovsdb_error(ovsdb_datum_from_json(&data[y][x],
+ &columns[x]->type,
+ json, NULL));
+ }
+ }
+
+ /* Sort rows by column values, for reproducibility. */
+ aux.data = data;
+ aux.columns = columns;
+ aux.n_columns = n_columns;
+ sort(rows->n, compare_rows, swap_rows, &aux);
+
+ /* Add column headings. */
+ table_init(&t);
+ table_set_caption(&t, xasprintf("%s table", ts->name));
+ for (x = 0; x < n_columns; x++) {
+ table_add_column(&t, "%s", columns[x]->name);
+ }
+
+ /* Print rows. */
+ for (y = 0; y < rows->n; y++) {
+ table_add_row(&t);
+ for (x = 0; x < n_columns; x++) {
+ struct cell *cell = table_add_cell(&t);
+ cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
+ cell->type = &columns[x]->type;
+ ovsdb_datum_destroy(&data[y][x], &columns[x]->type);
+ }
+ free(data[y]);
+ }
+ table_print(&t, &table_style);
+ table_destroy(&t);
+
+ free(data);
+ free(columns);
+}
+
+static void
+do_dump(struct jsonrpc *rpc, const char *database,
+ int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ struct jsonrpc_msg *request, *reply;
+ struct ovsdb_schema *schema;
+ struct json *transaction;
+
+ const struct shash_node **tables;
+ size_t n_tables;
+
+ size_t i;
+
+ schema = fetch_schema(rpc, database);
+ tables = shash_sort(&schema->tables);
+ n_tables = shash_count(&schema->tables);
+
+ /* Construct transaction to retrieve entire database. */
+ transaction = json_array_create_1(json_string_create(database));
+ for (i = 0; i < n_tables; i++) {
+ const struct ovsdb_table_schema *ts = tables[i]->data;
+ struct json *op, *columns;
+ struct shash_node *node;
+
+ columns = json_array_create_empty();
+ SHASH_FOR_EACH (node, &ts->columns) {
+ const struct ovsdb_column *column = node->data;
+
+ if (strcmp(column->name, "_version")) {
+ json_array_add(columns, json_string_create(column->name));