1 /* Copyright (c) 2009 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include "transaction.h"
25 #include "ovsdb-error.h"
33 struct hmap txn_tables; /* Contains "struct ovsdb_txn_table"s. */
36 /* A table modified by a transaction. */
37 struct ovsdb_txn_table {
38 struct hmap_node hmap_node; /* Element in ovsdb_txn's txn_tables hmap. */
39 struct ovsdb_table *table;
40 struct hmap txn_rows; /* Contains "struct ovsdb_txn_row"s. */
43 /* A row modified by the transaction:
45 * - A row added by a transaction will have null 'old' and non-null 'new'.
47 * - A row deleted by a transaction will have non-null 'old' and null
50 * - A row modified by a transaction will have non-null 'old' and 'new'.
52 * - 'old' and 'new' both null is invalid. It would indicate that a row
53 * was added then deleted within a single transaction, but we instead
54 * handle that case by deleting the txn_row entirely.
56 struct ovsdb_txn_row {
57 struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
58 struct ovsdb_row *old; /* The old row. */
59 struct ovsdb_row *new; /* The new row. */
62 static const struct uuid *
63 ovsdb_txn_row_get_uuid(const struct ovsdb_txn_row *txn_row)
65 const struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
66 return ovsdb_row_get_uuid(row);
70 ovsdb_txn_create(struct ovsdb *db)
72 struct ovsdb_txn *txn = xmalloc(sizeof *txn);
74 hmap_init(&txn->txn_tables);
79 ovsdb_txn_destroy(struct ovsdb_txn *txn, void (*cb)(struct ovsdb_txn_row *))
81 struct ovsdb_txn_table *txn_table, *next_txn_table;
83 HMAP_FOR_EACH_SAFE (txn_table, next_txn_table,
84 struct ovsdb_txn_table, hmap_node, &txn->txn_tables)
86 struct ovsdb_txn_row *txn_row, *next_txn_row;
88 HMAP_FOR_EACH_SAFE (txn_row, next_txn_row,
89 struct ovsdb_txn_row, hmap_node,
93 txn_row->new->txn_row = NULL;
99 hmap_destroy(&txn_table->txn_rows);
102 hmap_destroy(&txn->txn_tables);
107 ovsdb_txn_row_abort(struct ovsdb_txn_row *txn_row)
109 struct ovsdb_row *old = txn_row->old;
110 struct ovsdb_row *new = txn_row->new;
113 hmap_remove(&new->table->rows, &new->hmap_node);
115 hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
117 hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
119 ovsdb_row_destroy(new);
123 ovsdb_txn_abort(struct ovsdb_txn *txn)
125 ovsdb_txn_destroy(txn, ovsdb_txn_row_abort);
129 ovsdb_txn_row_commit(struct ovsdb_txn_row *txn_row)
131 ovsdb_row_destroy(txn_row->old);
135 ovsdb_txn_commit(struct ovsdb_txn *txn)
137 txn->db->run_triggers = true;
138 ovsdb_txn_destroy(txn, ovsdb_txn_row_commit);
142 put_json_column(struct json *object, const struct ovsdb_row *row,
143 const struct ovsdb_column *column)
145 json_object_put(object, column->name,
146 ovsdb_datum_to_json(&row->fields[column->index],
151 ovsdb_txn_row_to_json(const struct ovsdb_txn_row *txn_row)
153 const struct ovsdb_row *old = txn_row->old;
154 const struct ovsdb_row *new = txn_row->new;
155 struct shash_node *node;
159 return json_null_create();
163 SHASH_FOR_EACH (node, &new->table->schema->columns) {
164 struct ovsdb_column *column = node->data;
165 unsigned int index = column->index;
167 if (index != OVSDB_COL_UUID && column->persistent
168 && (!old || !ovsdb_datum_equals(&old->fields[index],
173 json = json_object_create();
175 put_json_column(json, new, column);
182 ovsdb_txn_table_to_json(const struct ovsdb_txn_table *txn_table)
184 struct ovsdb_txn_row *txn_row;
185 struct json *txn_table_json;
187 txn_table_json = NULL;
188 HMAP_FOR_EACH (txn_row, struct ovsdb_txn_row, hmap_node,
189 &txn_table->txn_rows) {
190 struct json *txn_row_json = ovsdb_txn_row_to_json(txn_row);
192 char uuid[UUID_LEN + 1];
194 if (!txn_table_json) {
195 txn_table_json = json_object_create();
198 snprintf(uuid, sizeof uuid,
199 UUID_FMT, UUID_ARGS(ovsdb_txn_row_get_uuid(txn_row)));
200 json_object_put(txn_table_json, uuid, txn_row_json);
203 return txn_table_json;
207 ovsdb_txn_to_json(const struct ovsdb_txn *txn)
209 struct ovsdb_txn_table *txn_table;
210 struct json *txn_json;
213 HMAP_FOR_EACH (txn_table, struct ovsdb_txn_table, hmap_node,
215 struct json *txn_table_json = ovsdb_txn_table_to_json(txn_table);
217 txn_json = json_object_create();
219 json_object_put(txn_json, txn_table->table->schema->name,
225 static struct ovsdb_error *
226 ovsdb_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
227 const struct uuid *row_uuid, struct json *json)
229 const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
230 if (json->type == JSON_NULL) {
232 return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
233 "row "UUID_FMT" that does not exist",
234 UUID_ARGS(row_uuid));
236 ovsdb_txn_row_delete(txn, row);
239 return ovsdb_row_from_json(ovsdb_txn_row_modify(txn, row),
242 struct ovsdb_error *error;
243 struct ovsdb_row *new;
245 new = ovsdb_row_create(table);
246 *ovsdb_row_get_uuid_rw(new) = *row_uuid;
247 error = ovsdb_row_from_json(new, json, NULL, NULL);
249 ovsdb_row_destroy(new);
252 ovsdb_txn_row_insert(txn, new);
258 static struct ovsdb_error *
259 ovsdb_txn_table_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
262 struct shash_node *node;
264 if (json->type != JSON_OBJECT) {
265 return ovsdb_syntax_error(json, NULL, "object expected");
268 SHASH_FOR_EACH (node, json->u.object) {
269 const char *uuid_string = node->name;
270 struct json *txn_row_json = node->data;
271 struct ovsdb_error *error;
272 struct uuid row_uuid;
274 if (!uuid_from_string(&row_uuid, uuid_string)) {
275 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
279 error = ovsdb_txn_row_from_json(txn, table, &row_uuid, txn_row_json);
289 ovsdb_txn_from_json(struct ovsdb *db, const struct json *json,
290 struct ovsdb_txn **txnp)
292 struct ovsdb_error *error;
293 struct shash_node *node;
294 struct ovsdb_txn *txn;
297 if (json->type != JSON_OBJECT) {
298 return ovsdb_syntax_error(json, NULL, "object expected");
301 txn = ovsdb_txn_create(db);
302 SHASH_FOR_EACH (node, json->u.object) {
303 const char *table_name = node->name;
304 struct json *txn_table_json = node->data;
305 struct ovsdb_table *table;
307 table = shash_find_data(&db->tables, table_name);
309 error = ovsdb_syntax_error(json, "unknown table",
310 "No table named %s.", table_name);
314 error = ovsdb_txn_table_from_json(txn, table, txn_table_json);
323 ovsdb_txn_abort(txn);
327 static struct ovsdb_txn_table *
328 ovsdb_txn_get_txn_table__(struct ovsdb_txn *txn,
329 const struct ovsdb_table *table,
332 struct ovsdb_txn_table *txn_table;
334 HMAP_FOR_EACH_IN_BUCKET (txn_table, struct ovsdb_txn_table, hmap_node,
335 hash, &txn->txn_tables) {
336 if (txn_table->table == table) {
344 static struct ovsdb_txn_table *
345 ovsdb_txn_get_txn_table(struct ovsdb_txn *txn, const struct ovsdb_table *table)
347 return ovsdb_txn_get_txn_table__(txn, table, hash_pointer(table, 0));
350 static struct ovsdb_txn_table *
351 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn,
352 struct ovsdb_table *table)
354 uint32_t hash = hash_pointer(table, 0);
355 struct ovsdb_txn_table *txn_table;
357 txn_table = ovsdb_txn_get_txn_table__(txn, table, hash);
359 txn_table = xmalloc(sizeof *txn_table);
360 txn_table->table = table;
361 hmap_init(&txn_table->txn_rows);
362 hmap_insert(&txn->txn_tables, &txn_table->hmap_node, hash);
367 static struct ovsdb_txn_row *
368 ovsdb_txn_row_create(struct ovsdb_txn_table *txn_table,
369 const struct ovsdb_row *old, struct ovsdb_row *new)
371 uint32_t hash = ovsdb_row_hash(old ? old : new);
372 struct ovsdb_txn_row *txn_row;
374 txn_row = xmalloc(sizeof *txn_row);
375 txn_row->old = (struct ovsdb_row *) old;
377 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node, hash);
383 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
385 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
387 if (ro_row->txn_row) {
388 assert(ro_row == ro_row->txn_row->new);
391 struct ovsdb_table *table = ro_row->table;
392 struct ovsdb_txn_table *txn_table;
393 struct ovsdb_row *rw_row;
395 txn_table = ovsdb_txn_create_txn_table(txn, table);
396 rw_row = ovsdb_row_clone(ro_row);
397 uuid_generate(ovsdb_row_get_version_rw(rw_row));
398 rw_row->txn_row = ovsdb_txn_row_create(txn_table, ro_row, rw_row);
399 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
406 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
408 uint32_t hash = ovsdb_row_hash(row);
409 struct ovsdb_table *table = row->table;
410 struct ovsdb_txn_table *txn_table;
412 uuid_generate(ovsdb_row_get_version_rw(row));
414 txn_table = ovsdb_txn_create_txn_table(txn, table);
415 row->txn_row = ovsdb_txn_row_create(txn_table, NULL, row);
416 hmap_insert(&table->rows, &row->hmap_node, hash);
419 /* 'row' must be assumed destroyed upon return; the caller must not reference
422 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
424 struct ovsdb_row *row = (struct ovsdb_row *) row_;
425 struct ovsdb_table *table = row->table;
426 struct ovsdb_txn_row *txn_row = row->txn_row;
427 struct ovsdb_txn_table *txn_table;
429 hmap_remove(&table->rows, &row->hmap_node);
432 txn_table = ovsdb_txn_create_txn_table(txn, table);
433 row->txn_row = ovsdb_txn_row_create(txn_table, row, NULL);
435 assert(txn_row->new == row);
439 txn_table = ovsdb_txn_get_txn_table(txn, table);
440 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
442 ovsdb_row_destroy(row);