+ fflush(stdout);
+ }
+ }
+ jsonrpc_msg_destroy(msg);
+ }
+}
+
+struct dump_table_aux {
+ struct ovsdb_datum **data;
+ const struct ovsdb_column **columns;
+ size_t n_columns;
+};
+
+static int
+compare_data(size_t a_y, size_t b_y, size_t x,
+ const struct dump_table_aux *aux)
+{
+ return ovsdb_datum_compare_3way(&aux->data[a_y][x],
+ &aux->data[b_y][x],
+ &aux->columns[x]->type);
+}
+
+static int
+compare_rows(size_t a_y, size_t b_y, void *aux_)
+{
+ struct dump_table_aux *aux = aux_;
+ size_t x;
+
+ /* Skip UUID columns on the first pass, since their values tend to be
+ * random and make our results less reproducible. */
+ 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;
+ }
+ }
+ }
+
+ /* 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 char *
+format_data(const struct ovsdb_datum *datum, const struct ovsdb_type *type)
+{
+ if (data_format == DF_JSON) {
+ struct json *json = ovsdb_datum_to_json(datum, type);
+ char *s = json_to_string(json, JSSF_SORT);
+ json_destroy(json);
+ return s;
+ } else if (data_format == DF_STRING) {
+ struct ds s;
+
+ ds_init(&s);
+ ovsdb_datum_to_string(datum, type, &s);
+ return ds_steal_cstr(&s);
+ } else {
+ NOT_REACHED();
+ }
+}
+
+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 %zu 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 %zu in table %s response lacks %s column",
+ y, ts->name, columns[x]->name);