1 /* Copyright (c) 2009, 2010 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"
22 #include "dynamic-string.h"
27 #include "ovsdb-error.h"
35 struct list txn_tables; /* Contains "struct ovsdb_txn_table"s. */
39 /* A table modified by a transaction. */
40 struct ovsdb_txn_table {
41 struct list node; /* Element in ovsdb_txn's txn_tables list. */
42 struct ovsdb_table *table;
43 struct hmap txn_rows; /* Contains "struct ovsdb_txn_row"s. */
46 /* A row modified by the transaction:
48 * - A row added by a transaction will have null 'old' and non-null 'new'.
50 * - A row deleted by a transaction will have non-null 'old' and null
53 * - A row modified by a transaction will have non-null 'old' and 'new'.
55 * - 'old' and 'new' both null is invalid. It would indicate that a row
56 * was added then deleted within a single transaction, but we instead
57 * handle that case by deleting the txn_row entirely.
59 struct ovsdb_txn_row {
60 struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
61 struct ovsdb_row *old; /* The old row. */
62 struct ovsdb_row *new; /* The new row. */
66 ovsdb_txn_create(struct ovsdb *db)
68 struct ovsdb_txn *txn = xmalloc(sizeof *txn);
70 list_init(&txn->txn_tables);
71 ds_init(&txn->comment);
76 ovsdb_txn_destroy(struct ovsdb_txn *txn, void (*cb)(struct ovsdb_txn_row *))
78 struct ovsdb_txn_table *txn_table, *next_txn_table;
80 LIST_FOR_EACH_SAFE (txn_table, next_txn_table,
81 struct ovsdb_txn_table, node, &txn->txn_tables) {
82 struct ovsdb_txn_row *txn_row, *next_txn_row;
84 HMAP_FOR_EACH_SAFE (txn_row, next_txn_row,
85 struct ovsdb_txn_row, hmap_node,
89 txn_row->old->txn_row = NULL;
92 txn_row->new->txn_row = NULL;
98 txn_table->table->txn_table = NULL;
99 hmap_destroy(&txn_table->txn_rows);
102 ds_destroy(&txn->comment);
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, bool durable)
137 struct ovsdb_replica *replica;
138 struct ovsdb_error *error;
140 LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
141 error = (replica->class->commit)(replica, txn, durable);
143 /* We don't support two-phase commit so only the first replica is
144 * allowed to report an error. */
145 assert(&replica->node == txn->db->replicas.next);
147 ovsdb_txn_abort(txn);
152 txn->db->run_triggers = true;
153 ovsdb_txn_destroy(txn, ovsdb_txn_row_commit);
158 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
159 ovsdb_txn_row_cb_func *cb, void *aux)
161 struct ovsdb_txn_table *t;
162 struct ovsdb_txn_row *r;
164 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
165 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
166 if (!cb(r->old, r->new, aux)) {
173 static struct ovsdb_txn_table *
174 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
176 if (!table->txn_table) {
177 struct ovsdb_txn_table *txn_table;
179 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
180 txn_table->table = table;
181 hmap_init(&txn_table->txn_rows);
182 list_push_back(&txn->txn_tables, &txn_table->node);
184 return table->txn_table;
187 static struct ovsdb_txn_row *
188 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
189 const struct ovsdb_row *old, struct ovsdb_row *new)
191 struct ovsdb_txn_table *txn_table;
192 struct ovsdb_txn_row *txn_row;
194 txn_row = xmalloc(sizeof *txn_row);
195 txn_row->old = (struct ovsdb_row *) old;
198 txn_table = ovsdb_txn_create_txn_table(txn, table);
199 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
200 ovsdb_row_hash(old ? old : new));
206 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
208 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
210 if (ro_row->txn_row) {
211 assert(ro_row == ro_row->txn_row->new);
214 struct ovsdb_table *table = ro_row->table;
215 struct ovsdb_row *rw_row;
217 rw_row = ovsdb_row_clone(ro_row);
218 uuid_generate(ovsdb_row_get_version_rw(rw_row));
219 rw_row->txn_row = ovsdb_txn_row_create(txn, table, ro_row, rw_row);
220 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
227 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
229 uint32_t hash = ovsdb_row_hash(row);
230 struct ovsdb_table *table = row->table;
232 uuid_generate(ovsdb_row_get_version_rw(row));
234 row->txn_row = ovsdb_txn_row_create(txn, table, NULL, row);
235 hmap_insert(&table->rows, &row->hmap_node, hash);
238 /* 'row' must be assumed destroyed upon return; the caller must not reference
241 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
243 struct ovsdb_row *row = (struct ovsdb_row *) row_;
244 struct ovsdb_table *table = row->table;
245 struct ovsdb_txn_row *txn_row = row->txn_row;
247 hmap_remove(&table->rows, &row->hmap_node);
250 row->txn_row = ovsdb_txn_row_create(txn, table, row, NULL);
252 assert(txn_row->new == row);
256 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
259 ovsdb_row_destroy(row);
264 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
266 if (txn->comment.length) {
267 ds_put_char(&txn->comment, '\n');
269 ds_put_cstr(&txn->comment, s);
273 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
275 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;