-/* Copyright (c) 2009 Nicira Networks
+/* Copyright (c) 2009, 2010 Nicira Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <assert.h>
+#include "dynamic-string.h"
#include "hash.h"
#include "hmap.h"
#include "json.h"
+#include "list.h"
#include "ovsdb-error.h"
#include "ovsdb.h"
#include "row.h"
struct ovsdb_txn {
struct ovsdb *db;
- struct hmap txn_tables; /* Contains "struct ovsdb_txn_table"s. */
+ struct list txn_tables; /* Contains "struct ovsdb_txn_table"s. */
+ struct ds comment;
};
/* A table modified by a transaction. */
struct ovsdb_txn_table {
- struct hmap_node hmap_node; /* Element in ovsdb_txn's txn_tables hmap. */
+ struct list node; /* Element in ovsdb_txn's txn_tables list. */
struct ovsdb_table *table;
struct hmap txn_rows; /* Contains "struct ovsdb_txn_row"s. */
};
struct ovsdb_row *new; /* The new row. */
};
-static const struct uuid *
-ovsdb_txn_row_get_uuid(const struct ovsdb_txn_row *txn_row)
-{
- const struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
- return ovsdb_row_get_uuid(row);
-}
-
struct ovsdb_txn *
ovsdb_txn_create(struct ovsdb *db)
{
struct ovsdb_txn *txn = xmalloc(sizeof *txn);
txn->db = db;
- hmap_init(&txn->txn_tables);
+ list_init(&txn->txn_tables);
+ ds_init(&txn->comment);
return txn;
}
{
struct ovsdb_txn_table *txn_table, *next_txn_table;
- HMAP_FOR_EACH_SAFE (txn_table, next_txn_table,
- struct ovsdb_txn_table, hmap_node, &txn->txn_tables)
- {
+ LIST_FOR_EACH_SAFE (txn_table, next_txn_table,
+ struct ovsdb_txn_table, node, &txn->txn_tables) {
struct ovsdb_txn_row *txn_row, *next_txn_row;
HMAP_FOR_EACH_SAFE (txn_row, next_txn_row,
struct ovsdb_txn_row, hmap_node,
&txn_table->txn_rows)
{
+ if (txn_row->old) {
+ txn_row->old->txn_row = NULL;
+ }
if (txn_row->new) {
txn_row->new->txn_row = NULL;
}
free(txn_row);
}
+ txn_table->table->txn_table = NULL;
hmap_destroy(&txn_table->txn_rows);
free(txn_table);
}
- hmap_destroy(&txn->txn_tables);
+ ds_destroy(&txn->comment);
free(txn);
}
ovsdb_row_destroy(txn_row->old);
}
-void
-ovsdb_txn_commit(struct ovsdb_txn *txn)
-{
- txn->db->run_triggers = true;
- ovsdb_txn_destroy(txn, ovsdb_txn_row_commit);
-}
-
-static void
-put_json_column(struct json *object, const struct ovsdb_row *row,
- const struct ovsdb_column *column)
-{
- json_object_put(object, column->name,
- ovsdb_datum_to_json(&row->fields[column->index],
- &column->type));
-}
-
-static struct json *
-ovsdb_txn_row_to_json(const struct ovsdb_txn_row *txn_row)
+static struct ovsdb_txn_row *
+find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
{
- const struct ovsdb_row *old = txn_row->old;
- const struct ovsdb_row *new = txn_row->new;
- struct shash_node *node;
- struct json *json;
+ struct ovsdb_txn_row *txn_row;
- if (!new) {
- return json_null_create();
+ if (!table->txn_table) {
+ return NULL;
}
- json = NULL;
- SHASH_FOR_EACH (node, &new->table->schema->columns) {
- struct ovsdb_column *column = node->data;
- unsigned int index = column->index;
+ HMAP_FOR_EACH_WITH_HASH (txn_row, struct ovsdb_txn_row, hmap_node,
+ uuid_hash(uuid), &table->txn_table->txn_rows) {
+ const struct ovsdb_row *row;
- if (index != OVSDB_COL_UUID && column->persistent
- && (!old || !ovsdb_datum_equals(&old->fields[index],
- &new->fields[index],
- &column->type)))
- {
- if (!json) {
- json = json_object_create();
- }
- put_json_column(json, new, column);
+ row = txn_row->old ? txn_row->old : txn_row->new;
+ if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
+ return txn_row;
}
}
- return json;
+
+ return NULL;
}
-static struct json *
-ovsdb_txn_table_to_json(const struct ovsdb_txn_table *txn_table)
+static void
+ovsdb_txn_adjust_atom_refs(const union ovsdb_atom *atoms, unsigned int n,
+ const struct ovsdb_table *table,
+ int delta, struct ovsdb_error **errorp)
{
- struct ovsdb_txn_row *txn_row;
- struct json *txn_table_json;
-
- txn_table_json = NULL;
- HMAP_FOR_EACH (txn_row, struct ovsdb_txn_row, hmap_node,
- &txn_table->txn_rows) {
- struct json *txn_row_json = ovsdb_txn_row_to_json(txn_row);
- if (txn_row_json) {
- char uuid[UUID_LEN + 1];
-
- if (!txn_table_json) {
- txn_table_json = json_object_create();
+ unsigned int i;
+
+ for (i = 0; i < n; i++) {
+ const struct uuid *uuid = &atoms[i].uuid;
+ struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
+ if (txn_row) {
+ if (txn_row->old) {
+ txn_row->old->n_refs += delta;
+ }
+ if (txn_row->new) {
+ txn_row->new->n_refs += delta;
+ }
+ } else {
+ const struct ovsdb_row *row_ = ovsdb_table_get_row(table, uuid);
+ if (row_) {
+ struct ovsdb_row *row = (struct ovsdb_row *) row_;
+ row->n_refs += delta;
+ } else if (errorp) {
+ if (!*errorp) {
+ *errorp = ovsdb_error("referential integrity violation",
+ "reference to nonexistent row "
+ UUID_FMT, UUID_ARGS(uuid));
+ }
+ } else {
+ NOT_REACHED();
}
-
- snprintf(uuid, sizeof uuid,
- UUID_FMT, UUID_ARGS(ovsdb_txn_row_get_uuid(txn_row)));
- json_object_put(txn_table_json, uuid, txn_row_json);
}
}
- return txn_table_json;
}
-struct json *
-ovsdb_txn_to_json(const struct ovsdb_txn *txn)
+static void
+ovsdb_txn_adjust_row_refs(const struct ovsdb_row *r,
+ const struct ovsdb_column *column, int delta,
+ struct ovsdb_error **errorp)
{
- struct ovsdb_txn_table *txn_table;
- struct json *txn_json;
-
- txn_json = NULL;
- HMAP_FOR_EACH (txn_table, struct ovsdb_txn_table, hmap_node,
- &txn->txn_tables) {
- struct json *txn_table_json = ovsdb_txn_table_to_json(txn_table);
- if (!txn_json) {
- txn_json = json_object_create();
- }
- json_object_put(txn_json, txn_table->table->schema->name,
- txn_table_json);
+ const struct ovsdb_datum *field = &r->fields[column->index];
+ const struct ovsdb_type *type = &column->type;
+
+ if (type->key.type == OVSDB_TYPE_UUID && type->key.u.uuid.refTable) {
+ ovsdb_txn_adjust_atom_refs(field->keys, field->n,
+ type->key.u.uuid.refTable, delta, errorp);
+ }
+ if (type->value.type == OVSDB_TYPE_UUID && type->value.u.uuid.refTable) {
+ ovsdb_txn_adjust_atom_refs(field->values, field->n,
+ type->value.u.uuid.refTable, delta, errorp);
}
- return txn_json;
}
-static struct ovsdb_error *
-ovsdb_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
- const struct uuid *row_uuid, struct json *json)
+static struct ovsdb_error * WARN_UNUSED_RESULT
+ovsdb_txn_adjust_ref_counts__(struct ovsdb_txn *txn, int delta)
{
- const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
- if (json->type == JSON_NULL) {
- if (!row) {
- return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
- "row "UUID_FMT" that does not exist",
- UUID_ARGS(row_uuid));
- }
- ovsdb_txn_row_delete(txn, row);
- return NULL;
- } else if (row) {
- return ovsdb_row_from_json(ovsdb_txn_row_modify(txn, row),
- json, NULL, NULL);
- } else {
- struct ovsdb_error *error;
- struct ovsdb_row *new;
+ struct ovsdb_txn_table *t;
+ struct ovsdb_error *error;
- new = ovsdb_row_create(table);
- *ovsdb_row_get_uuid_rw(new) = *row_uuid;
- error = ovsdb_row_from_json(new, json, NULL, NULL);
- if (error) {
- ovsdb_row_destroy(new);
- }
+ error = NULL;
+ LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
+ struct ovsdb_table *table = t->table;
+ struct ovsdb_txn_row *r;
- ovsdb_txn_row_insert(txn, new);
+ HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
+ struct shash_node *node;
- return error;
+ SHASH_FOR_EACH (node, &table->schema->columns) {
+ const struct ovsdb_column *column = node->data;
+
+ if (r->old) {
+ ovsdb_txn_adjust_row_refs(r->old, column, -delta, NULL);
+ }
+ if (r->new) {
+ ovsdb_txn_adjust_row_refs(r->new, column, delta, &error);
+ }
+ }
+ }
}
+ return error;
}
-static struct ovsdb_error *
-ovsdb_txn_table_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
- struct json *json)
+static void
+ovsdb_txn_rollback_counts(struct ovsdb_txn *txn)
{
- struct shash_node *node;
+ ovsdb_error_destroy(ovsdb_txn_adjust_ref_counts__(txn, -1));
+}
- if (json->type != JSON_OBJECT) {
- return ovsdb_syntax_error(json, NULL, "object expected");
+static struct ovsdb_error * WARN_UNUSED_RESULT
+ovsdb_txn_commit_ref_counts(struct ovsdb_txn *txn)
+{
+ struct ovsdb_error *error = ovsdb_txn_adjust_ref_counts__(txn, 1);
+ if (error) {
+ ovsdb_txn_rollback_counts(txn);
}
+ return error;
+}
- SHASH_FOR_EACH (node, json->u.object) {
- const char *uuid_string = node->name;
- struct json *txn_row_json = node->data;
- struct ovsdb_error *error;
- struct uuid row_uuid;
+static struct ovsdb_error * WARN_UNUSED_RESULT
+update_ref_counts(struct ovsdb_txn *txn)
+{
+ struct ovsdb_error *error;
+ struct ovsdb_txn_table *t;
- if (!uuid_from_string(&row_uuid, uuid_string)) {
- return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
- uuid_string);
- }
+ error = ovsdb_txn_commit_ref_counts(txn);
+ if (error) {
+ return error;
+ }
- error = ovsdb_txn_row_from_json(txn, table, &row_uuid, txn_row_json);
- if (error) {
- return error;
+ LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
+ struct ovsdb_txn_row *r;
+
+ HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
+ if (!r->new && r->old->n_refs) {
+ error = ovsdb_error("referential integrity violation",
+ "cannot delete %s row "UUID_FMT" because "
+ "of %zu remaining reference(s)",
+ t->table->schema->name,
+ UUID_ARGS(ovsdb_row_get_uuid(r->old)),
+ r->old->n_refs);
+ ovsdb_txn_rollback_counts(txn);
+ return error;
+ }
}
}
}
struct ovsdb_error *
-ovsdb_txn_from_json(struct ovsdb *db, const struct json *json,
- struct ovsdb_txn **txnp)
+ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
{
+ struct ovsdb_replica *replica;
struct ovsdb_error *error;
- struct shash_node *node;
- struct ovsdb_txn *txn;
- *txnp = NULL;
- if (json->type != JSON_OBJECT) {
- return ovsdb_syntax_error(json, NULL, "object expected");
+ error = update_ref_counts(txn);
+ if (error) {
+ ovsdb_txn_abort(txn);
+ return error;
}
- txn = ovsdb_txn_create(db);
- SHASH_FOR_EACH (node, json->u.object) {
- const char *table_name = node->name;
- struct json *txn_table_json = node->data;
- struct ovsdb_table *table;
-
- table = shash_find_data(&db->tables, table_name);
- if (!table) {
- error = ovsdb_syntax_error(json, "unknown table",
- "No table named %s.", table_name);
- goto error;
- }
-
- error = ovsdb_txn_table_from_json(txn, table, txn_table_json);
+ LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
+ error = (replica->class->commit)(replica, txn, durable);
if (error) {
- goto error;
- }
- }
- *txnp = txn;
- return NULL;
-
-error:
- ovsdb_txn_abort(txn);
- return error;
-}
+ /* We don't support two-phase commit so only the first replica is
+ * allowed to report an error. */
+ assert(&replica->node == txn->db->replicas.next);
-static struct ovsdb_txn_table *
-ovsdb_txn_get_txn_table__(struct ovsdb_txn *txn,
- const struct ovsdb_table *table,
- uint32_t hash)
-{
- struct ovsdb_txn_table *txn_table;
-
- HMAP_FOR_EACH_IN_BUCKET (txn_table, struct ovsdb_txn_table, hmap_node,
- hash, &txn->txn_tables) {
- if (txn_table->table == table) {
- return txn_table;
+ ovsdb_txn_abort(txn);
+ return error;
}
}
+ txn->db->run_triggers = true;
+ ovsdb_txn_destroy(txn, ovsdb_txn_row_commit);
return NULL;
}
-static struct ovsdb_txn_table *
-ovsdb_txn_get_txn_table(struct ovsdb_txn *txn, const struct ovsdb_table *table)
+void
+ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
+ ovsdb_txn_row_cb_func *cb, void *aux)
{
- return ovsdb_txn_get_txn_table__(txn, table, hash_pointer(table, 0));
+ struct ovsdb_txn_table *t;
+ struct ovsdb_txn_row *r;
+
+ LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
+ HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
+ if (!cb(r->old, r->new, aux)) {
+ break;
+ }
+ }
+ }
}
static struct ovsdb_txn_table *
-ovsdb_txn_create_txn_table(struct ovsdb_txn *txn,
- struct ovsdb_table *table)
+ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
{
- uint32_t hash = hash_pointer(table, 0);
- struct ovsdb_txn_table *txn_table;
+ if (!table->txn_table) {
+ struct ovsdb_txn_table *txn_table;
- txn_table = ovsdb_txn_get_txn_table__(txn, table, hash);
- if (!txn_table) {
- txn_table = xmalloc(sizeof *txn_table);
+ table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
txn_table->table = table;
hmap_init(&txn_table->txn_rows);
- hmap_insert(&txn->txn_tables, &txn_table->hmap_node, hash);
+ list_push_back(&txn->txn_tables, &txn_table->node);
}
- return txn_table;
+ return table->txn_table;
}
static struct ovsdb_txn_row *
-ovsdb_txn_row_create(struct ovsdb_txn_table *txn_table,
+ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
const struct ovsdb_row *old, struct ovsdb_row *new)
{
- uint32_t hash = ovsdb_row_hash(old ? old : new);
+ struct ovsdb_txn_table *txn_table;
struct ovsdb_txn_row *txn_row;
txn_row = xmalloc(sizeof *txn_row);
txn_row->old = (struct ovsdb_row *) old;
txn_row->new = new;
- hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node, hash);
+
+ txn_table = ovsdb_txn_create_txn_table(txn, table);
+ hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
+ ovsdb_row_hash(old ? old : new));
return txn_row;
}
return ro_row;
} else {
struct ovsdb_table *table = ro_row->table;
- struct ovsdb_txn_table *txn_table;
struct ovsdb_row *rw_row;
- txn_table = ovsdb_txn_create_txn_table(txn, table);
rw_row = ovsdb_row_clone(ro_row);
+ rw_row->n_refs = ro_row->n_refs;
uuid_generate(ovsdb_row_get_version_rw(rw_row));
- rw_row->txn_row = ovsdb_txn_row_create(txn_table, ro_row, rw_row);
+ rw_row->txn_row = ovsdb_txn_row_create(txn, table, ro_row, rw_row);
hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
return rw_row;
{
uint32_t hash = ovsdb_row_hash(row);
struct ovsdb_table *table = row->table;
- struct ovsdb_txn_table *txn_table;
uuid_generate(ovsdb_row_get_version_rw(row));
- txn_table = ovsdb_txn_create_txn_table(txn, table);
- row->txn_row = ovsdb_txn_row_create(txn_table, NULL, row);
+ row->txn_row = ovsdb_txn_row_create(txn, table, NULL, row);
hmap_insert(&table->rows, &row->hmap_node, hash);
}
struct ovsdb_row *row = (struct ovsdb_row *) row_;
struct ovsdb_table *table = row->table;
struct ovsdb_txn_row *txn_row = row->txn_row;
- struct ovsdb_txn_table *txn_table;
hmap_remove(&table->rows, &row->hmap_node);
if (!txn_row) {
- txn_table = ovsdb_txn_create_txn_table(txn, table);
- row->txn_row = ovsdb_txn_row_create(txn_table, row, NULL);
+ row->txn_row = ovsdb_txn_row_create(txn, table, row, NULL);
} else {
assert(txn_row->new == row);
if (txn_row->old) {
txn_row->new = NULL;
} else {
- txn_table = ovsdb_txn_get_txn_table(txn, table);
- hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
+ hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
+ free(txn_row);
}
ovsdb_row_destroy(row);
}
}
+
+void
+ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
+{
+ if (txn->comment.length) {
+ ds_put_char(&txn->comment, '\n');
+ }
+ ds_put_cstr(&txn->comment, s);
+}
+
+const char *
+ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
+{
+ return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
+}