+ free(txn_row);
+
+ return NULL;
+}
+
+static void
+add_weak_ref(struct ovsdb_txn *txn,
+ const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
+{
+ struct ovsdb_row *src = CONST_CAST(struct ovsdb_row *, src_);
+ struct ovsdb_row *dst = CONST_CAST(struct ovsdb_row *, dst_);
+ struct ovsdb_weak_ref *weak;
+
+ if (src == dst) {
+ return;
+ }
+
+ dst = ovsdb_txn_row_modify(txn, dst);
+
+ if (!list_is_empty(&dst->dst_refs)) {
+ /* Omit duplicates. */
+ weak = CONTAINER_OF(list_back(&dst->dst_refs),
+ struct ovsdb_weak_ref, dst_node);
+ if (weak->src == src) {
+ return;
+ }
+ }
+
+ weak = xmalloc(sizeof *weak);
+ weak->src = src;
+ list_push_back(&dst->dst_refs, &weak->dst_node);
+ list_push_back(&src->src_refs, &weak->src_node);
+}
+
+static struct ovsdb_error * WARN_UNUSED_RESULT
+assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
+{
+ struct ovsdb_table *table;
+ struct shash_node *node;
+
+ if (txn_row->old) {
+ /* Mark rows that have weak references to 'txn_row' as modified, so
+ * that their weak references will get reassessed. */
+ struct ovsdb_weak_ref *weak, *next;
+
+ LIST_FOR_EACH_SAFE (weak, next, dst_node, &txn_row->old->dst_refs) {
+ if (!weak->src->txn_row) {
+ ovsdb_txn_row_modify(txn, weak->src);
+ }
+ }
+ }
+
+ if (!txn_row->new) {
+ /* We don't have to do anything about references that originate at
+ * 'txn_row', because ovsdb_row_destroy() will remove those weak
+ * references. */
+ return NULL;
+ }
+
+ table = txn_row->table;
+ SHASH_FOR_EACH (node, &table->schema->columns) {
+ const struct ovsdb_column *column = node->data;
+ struct ovsdb_datum *datum = &txn_row->new->fields[column->index];
+ unsigned int orig_n, i;
+ bool zero = false;
+
+ orig_n = datum->n;
+
+ if (ovsdb_base_type_is_weak_ref(&column->type.key)) {
+ for (i = 0; i < datum->n; ) {
+ const struct ovsdb_row *row;
+
+ row = ovsdb_table_get_row(column->type.key.u.uuid.refTable,
+ &datum->keys[i].uuid);
+ if (row) {
+ add_weak_ref(txn, txn_row->new, row);
+ i++;
+ } else {
+ if (uuid_is_zero(&datum->keys[i].uuid)) {
+ zero = true;
+ }
+ ovsdb_datum_remove_unsafe(datum, i, &column->type);
+ }
+ }
+ }
+
+ if (ovsdb_base_type_is_weak_ref(&column->type.value)) {
+ for (i = 0; i < datum->n; ) {
+ const struct ovsdb_row *row;
+
+ row = ovsdb_table_get_row(column->type.value.u.uuid.refTable,
+ &datum->values[i].uuid);
+ if (row) {
+ add_weak_ref(txn, txn_row->new, row);
+ i++;
+ } else {
+ if (uuid_is_zero(&datum->values[i].uuid)) {
+ zero = true;
+ }
+ ovsdb_datum_remove_unsafe(datum, i, &column->type);
+ }
+ }
+ }
+
+ if (datum->n != orig_n) {
+ bitmap_set1(txn_row->changed, column->index);
+ ovsdb_datum_sort_assert(datum, column->type.key.type);
+ if (datum->n < column->type.n_min) {
+ const struct uuid *row_uuid = ovsdb_row_get_uuid(txn_row->new);
+ if (zero && !txn_row->old) {
+ return ovsdb_error(
+ "constraint violation",
+ "Weak reference column \"%s\" in \"%s\" row "UUID_FMT
+ " (inserted within this transaction) contained "
+ "all-zeros UUID (probably as the default value for "
+ "this column) but deleting this value caused a "
+ "constraint volation because this column is not "
+ "allowed to be empty.", column->name,
+ table->schema->name, UUID_ARGS(row_uuid));
+ } else {
+ return ovsdb_error(
+ "constraint violation",
+ "Deletion of %u weak reference(s) to deleted (or "
+ "never-existing) rows from column \"%s\" in \"%s\" "
+ "row "UUID_FMT" %scaused this column to become empty, "
+ "but constraints on this column disallow an "
+ "empty column.",
+ orig_n - datum->n, column->name, table->schema->name,
+ UUID_ARGS(row_uuid),
+ (txn_row->old
+ ? ""
+ : "(inserted within this transaction) "));
+ }
+ }
+ }
+ }
+
+ return NULL;
+}
+
+static struct ovsdb_error * WARN_UNUSED_RESULT
+determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
+{
+ struct ovsdb_table *table = txn_row->table;
+
+ if (txn_row->old && txn_row->new) {
+ struct shash_node *node;
+ bool changed = false;
+
+ SHASH_FOR_EACH (node, &table->schema->columns) {
+ const struct ovsdb_column *column = node->data;
+ const struct ovsdb_type *type = &column->type;
+ unsigned int idx = column->index;
+
+ if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
+ &txn_row->new->fields[idx],
+ type)) {
+ bitmap_set1(txn_row->changed, idx);
+ changed = true;
+ }
+ }
+
+ if (!changed) {
+ /* Nothing actually changed in this row, so drop it. */
+ ovsdb_txn_row_abort(txn, txn_row);
+ }
+ } else {
+ bitmap_set_multiple(txn_row->changed, 0,
+ shash_count(&table->schema->columns), 1);
+ }
+
+ return NULL;
+}
+
+static struct ovsdb_error * WARN_UNUSED_RESULT
+check_max_rows(struct ovsdb_txn *txn)
+{
+ struct ovsdb_txn_table *t;
+
+ LIST_FOR_EACH (t, node, &txn->txn_tables) {
+ size_t n_rows = hmap_count(&t->table->rows);
+ unsigned int max_rows = t->table->schema->max_rows;
+
+ if (n_rows > max_rows) {
+ return ovsdb_error("constraint violation",
+ "transaction causes \"%s\" table to contain "
+ "%"PRIuSIZE" rows, greater than the schema-defined "
+ "limit of %u row(s)",
+ t->table->schema->name, n_rows, max_rows);
+ }
+ }
+
+ return NULL;
+}
+
+static struct ovsdb_row *
+ovsdb_index_search(struct hmap *index, struct ovsdb_row *row, size_t i,
+ uint32_t hash)
+{
+ const struct ovsdb_table *table = row->table;
+ const struct ovsdb_column_set *columns = &table->schema->indexes[i];
+ struct hmap_node *node;
+
+ for (node = hmap_first_with_hash(index, hash); node;
+ node = hmap_next_with_hash(node)) {
+ struct ovsdb_row *irow = ovsdb_row_from_index_node(node, table, i);
+ if (ovsdb_row_equal_columns(row, irow, columns)) {
+ return irow;
+ }
+ }
+
+ return NULL;
+}
+
+static void
+duplicate_index_row__(const struct ovsdb_column_set *index,
+ const struct ovsdb_row *row,
+ const char *title,
+ struct ds *out)
+{
+ size_t n_columns = shash_count(&row->table->schema->columns);
+
+ ds_put_format(out, "%s row, with UUID "UUID_FMT", ",
+ title, UUID_ARGS(ovsdb_row_get_uuid(row)));
+ if (!row->txn_row
+ || bitmap_scan(row->txn_row->changed, 0, n_columns) == n_columns) {
+ ds_put_cstr(out, "existed in the database before this "
+ "transaction and was not modified by the transaction.");
+ } else if (!row->txn_row->old) {
+ ds_put_cstr(out, "was inserted by this transaction.");
+ } else if (ovsdb_row_equal_columns(row->txn_row->old,
+ row->txn_row->new, index)) {
+ ds_put_cstr(out, "existed in the database before this "
+ "transaction, which modified some of the row's columns "
+ "but not any columns in this index.");
+ } else {
+ ds_put_cstr(out, "had the following index values before the "
+ "transaction: ");
+ ovsdb_row_columns_to_string(row->txn_row->old, index, out);
+ ds_put_char(out, '.');
+ }
+}
+
+static struct ovsdb_error * WARN_UNUSED_RESULT
+duplicate_index_row(const struct ovsdb_column_set *index,
+ const struct ovsdb_row *a,
+ const struct ovsdb_row *b)
+{
+ struct ovsdb_column_set all_columns;
+ struct ovsdb_error *error;
+ char *index_s;
+ struct ds s;
+
+ /* Put 'a' and 'b' in a predictable order to make error messages
+ * reproducible for testing. */
+ ovsdb_column_set_init(&all_columns);
+ ovsdb_column_set_add_all(&all_columns, a->table);
+ if (ovsdb_row_compare_columns_3way(a, b, &all_columns) < 0) {
+ const struct ovsdb_row *tmp = a;
+ a = b;
+ b = tmp;
+ }
+ ovsdb_column_set_destroy(&all_columns);
+
+ index_s = ovsdb_column_set_to_string(index);
+
+ ds_init(&s);
+ ds_put_format(&s, "Transaction causes multiple rows in \"%s\" table to "
+ "have identical values (", a->table->schema->name);
+ ovsdb_row_columns_to_string(a, index, &s);
+ ds_put_format(&s, ") for index on %s. ", index_s);
+ duplicate_index_row__(index, a, "First", &s);
+ ds_put_cstr(&s, " ");
+ duplicate_index_row__(index, b, "Second", &s);
+
+ free(index_s);
+
+ error = ovsdb_error("constraint violation", "%s", ds_cstr(&s));
+ ds_destroy(&s);
+ return error;
+}
+
+static struct ovsdb_error * WARN_UNUSED_RESULT
+check_index_uniqueness(struct ovsdb_txn *txn OVS_UNUSED,
+ struct ovsdb_txn_row *txn_row)
+{
+ struct ovsdb_txn_table *txn_table = txn_row->table->txn_table;
+ struct ovsdb_table *table = txn_row->table;
+ struct ovsdb_row *row = txn_row->new;
+ size_t i;
+
+ if (!row) {
+ return NULL;
+ }
+
+ for (i = 0; i < table->schema->n_indexes; i++) {
+ const struct ovsdb_column_set *index = &table->schema->indexes[i];
+ struct ovsdb_row *irow;
+ uint32_t hash;
+
+ hash = ovsdb_row_hash_columns(row, index, 0);
+ irow = ovsdb_index_search(&txn_table->txn_indexes[i], row, i, hash);
+ if (irow) {
+ return duplicate_index_row(index, irow, row);
+ }
+
+ irow = ovsdb_index_search(&table->indexes[i], row, i, hash);
+ if (irow && !irow->txn_row) {
+ return duplicate_index_row(index, irow, row);
+ }
+
+ hmap_insert(&txn_table->txn_indexes[i],
+ ovsdb_row_get_index_node(row, i), hash);
+ }
+
+ return NULL;