Initial implementation of OVSDB.
[cascardo/ovs.git] / ovsdb / transaction.c
1 /* Copyright (c) 2009 Nicira Networks
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "transaction.h"
19
20 #include <assert.h>
21
22 #include "hash.h"
23 #include "hmap.h"
24 #include "json.h"
25 #include "ovsdb-error.h"
26 #include "ovsdb.h"
27 #include "row.h"
28 #include "table.h"
29 #include "uuid.h"
30
31 struct ovsdb_txn {
32     struct ovsdb *db;
33     struct hmap txn_tables;     /* Contains "struct ovsdb_txn_table"s. */
34 };
35
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. */
41 };
42
43 /* A row modified by the transaction:
44  *
45  *      - A row added by a transaction will have null 'old' and non-null 'new'.
46  *
47  *      - A row deleted by a transaction will have non-null 'old' and null
48  *        'new'.
49  *
50  *      - A row modified by a transaction will have non-null 'old' and 'new'.
51  *
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.
55  */
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. */
60 };
61
62 static const struct uuid *
63 ovsdb_txn_row_get_uuid(const struct ovsdb_txn_row *txn_row)
64 {
65     const struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
66     return ovsdb_row_get_uuid(row);
67 }
68
69 struct ovsdb_txn *
70 ovsdb_txn_create(struct ovsdb *db)
71 {
72     struct ovsdb_txn *txn = xmalloc(sizeof *txn);
73     txn->db = db;
74     hmap_init(&txn->txn_tables);
75     return txn;
76 }
77
78 static void
79 ovsdb_txn_destroy(struct ovsdb_txn *txn, void (*cb)(struct ovsdb_txn_row *))
80 {
81     struct ovsdb_txn_table *txn_table, *next_txn_table;
82
83     HMAP_FOR_EACH_SAFE (txn_table, next_txn_table,
84                         struct ovsdb_txn_table, hmap_node, &txn->txn_tables)
85     {
86         struct ovsdb_txn_row *txn_row, *next_txn_row;
87
88         HMAP_FOR_EACH_SAFE (txn_row, next_txn_row,
89                             struct ovsdb_txn_row, hmap_node,
90                             &txn_table->txn_rows)
91         {
92             if (txn_row->new) {
93                 txn_row->new->txn_row = NULL;
94             }
95             cb(txn_row);
96             free(txn_row);
97         }
98
99         hmap_destroy(&txn_table->txn_rows);
100         free(txn_table);
101     }
102     hmap_destroy(&txn->txn_tables);
103     free(txn);
104 }
105
106 static void
107 ovsdb_txn_row_abort(struct ovsdb_txn_row *txn_row)
108 {
109     struct ovsdb_row *old = txn_row->old;
110     struct ovsdb_row *new = txn_row->new;
111
112     if (!old) {
113         hmap_remove(&new->table->rows, &new->hmap_node);
114     } else if (!new) {
115         hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
116     } else {
117         hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
118     }
119     ovsdb_row_destroy(new);
120 }
121
122 void
123 ovsdb_txn_abort(struct ovsdb_txn *txn)
124 {
125     ovsdb_txn_destroy(txn, ovsdb_txn_row_abort);
126 }
127
128 static void
129 ovsdb_txn_row_commit(struct ovsdb_txn_row *txn_row)
130 {
131     ovsdb_row_destroy(txn_row->old);
132 }
133
134 void
135 ovsdb_txn_commit(struct ovsdb_txn *txn)
136 {
137     txn->db->run_triggers = true;
138     ovsdb_txn_destroy(txn, ovsdb_txn_row_commit);
139 }
140
141 static void
142 put_json_column(struct json *object, const struct ovsdb_row *row,
143                 const struct ovsdb_column *column)
144 {
145     json_object_put(object, column->name,
146                     ovsdb_datum_to_json(&row->fields[column->index],
147                                         &column->type));
148 }
149
150 static struct json *
151 ovsdb_txn_row_to_json(const struct ovsdb_txn_row *txn_row)
152 {
153     const struct ovsdb_row *old = txn_row->old;
154     const struct ovsdb_row *new = txn_row->new;
155     struct shash_node *node;
156     struct json *json;
157
158     if (!new) {
159         return json_null_create();
160     }
161
162     json = NULL;
163     SHASH_FOR_EACH (node, &new->table->schema->columns) {
164         struct ovsdb_column *column = node->data;
165         unsigned int index = column->index;
166
167         if (index != OVSDB_COL_UUID && column->persistent
168             && (!old || !ovsdb_datum_equals(&old->fields[index],
169                                             &new->fields[index],
170                                             &column->type)))
171         {
172             if (!json) {
173                 json = json_object_create();
174             }
175             put_json_column(json, new, column);
176         }
177     }
178     return json;
179 }
180
181 static struct json *
182 ovsdb_txn_table_to_json(const struct ovsdb_txn_table *txn_table)
183 {
184     struct ovsdb_txn_row *txn_row;
185     struct json *txn_table_json;
186
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);
191         if (txn_row_json) {
192             char uuid[UUID_LEN + 1];
193
194             if (!txn_table_json) {
195                 txn_table_json = json_object_create();
196             }
197
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);
201         }
202     }
203     return txn_table_json;
204 }
205
206 struct json *
207 ovsdb_txn_to_json(const struct ovsdb_txn *txn)
208 {
209     struct ovsdb_txn_table *txn_table;
210     struct json *txn_json;
211
212     txn_json = NULL;
213     HMAP_FOR_EACH (txn_table, struct ovsdb_txn_table, hmap_node,
214                    &txn->txn_tables) {
215         struct json *txn_table_json = ovsdb_txn_table_to_json(txn_table);
216         if (!txn_json) {
217             txn_json = json_object_create();
218         }
219         json_object_put(txn_json, txn_table->table->schema->name,
220                         txn_table_json);
221     }
222     return txn_json;
223 }
224
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)
228 {
229     const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
230     if (json->type == JSON_NULL) {
231         if (!row) {
232             return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
233                                       "row "UUID_FMT" that does not exist",
234                                       UUID_ARGS(row_uuid));
235         }
236         ovsdb_txn_row_delete(txn, row);
237         return NULL;
238     } else if (row) {
239         return ovsdb_row_from_json(ovsdb_txn_row_modify(txn, row),
240                                    json, NULL, NULL);
241     } else {
242         struct ovsdb_error *error;
243         struct ovsdb_row *new;
244
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);
248         if (error) {
249             ovsdb_row_destroy(new);
250         }
251
252         ovsdb_txn_row_insert(txn, new);
253
254         return error;
255     }
256 }
257
258 static struct ovsdb_error *
259 ovsdb_txn_table_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
260                           struct json *json)
261 {
262     struct shash_node *node;
263
264     if (json->type != JSON_OBJECT) {
265         return ovsdb_syntax_error(json, NULL, "object expected");
266     }
267
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;
273
274         if (!uuid_from_string(&row_uuid, uuid_string)) {
275             return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
276                                       uuid_string);
277         }
278
279         error = ovsdb_txn_row_from_json(txn, table, &row_uuid, txn_row_json);
280         if (error) {
281             return error;
282         }
283     }
284
285     return NULL;
286 }
287
288 struct ovsdb_error *
289 ovsdb_txn_from_json(struct ovsdb *db, const struct json *json,
290                     struct ovsdb_txn **txnp)
291 {
292     struct ovsdb_error *error;
293     struct shash_node *node;
294     struct ovsdb_txn *txn;
295
296     *txnp = NULL;
297     if (json->type != JSON_OBJECT) {
298         return ovsdb_syntax_error(json, NULL, "object expected");
299     }
300
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;
306
307         table = shash_find_data(&db->tables, table_name);
308         if (!table) {
309             error = ovsdb_syntax_error(json, "unknown table",
310                                        "No table named %s.", table_name);
311             goto error;
312         }
313
314         error = ovsdb_txn_table_from_json(txn, table, txn_table_json);
315         if (error) {
316             goto error;
317         }
318     }
319     *txnp = txn;
320     return NULL;
321
322 error:
323     ovsdb_txn_abort(txn);
324     return error;
325 }
326
327 static struct ovsdb_txn_table *
328 ovsdb_txn_get_txn_table__(struct ovsdb_txn *txn,
329                           const struct ovsdb_table *table,
330                           uint32_t hash)
331 {
332     struct ovsdb_txn_table *txn_table;
333
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) {
337             return txn_table;
338         }
339     }
340
341     return NULL;
342 }
343
344 static struct ovsdb_txn_table *
345 ovsdb_txn_get_txn_table(struct ovsdb_txn *txn, const struct ovsdb_table *table)
346 {
347     return ovsdb_txn_get_txn_table__(txn, table, hash_pointer(table, 0));
348 }
349
350 static struct ovsdb_txn_table *
351 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn,
352                            struct ovsdb_table *table)
353 {
354     uint32_t hash = hash_pointer(table, 0);
355     struct ovsdb_txn_table *txn_table;
356
357     txn_table = ovsdb_txn_get_txn_table__(txn, table, hash);
358     if (!txn_table) {
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);
363     }
364     return txn_table;
365 }
366
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)
370 {
371     uint32_t hash = ovsdb_row_hash(old ? old : new);
372     struct ovsdb_txn_row *txn_row;
373
374     txn_row = xmalloc(sizeof *txn_row);
375     txn_row->old = (struct ovsdb_row *) old;
376     txn_row->new = new;
377     hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node, hash);
378
379     return txn_row;
380 }
381
382 struct ovsdb_row *
383 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
384 {
385     struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
386
387     if (ro_row->txn_row) {
388         assert(ro_row == ro_row->txn_row->new);
389         return ro_row;
390     } else {
391         struct ovsdb_table *table = ro_row->table;
392         struct ovsdb_txn_table *txn_table;
393         struct ovsdb_row *rw_row;
394
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);
400
401         return rw_row;
402     }
403 }
404
405 void
406 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
407 {
408     uint32_t hash = ovsdb_row_hash(row);
409     struct ovsdb_table *table = row->table;
410     struct ovsdb_txn_table *txn_table;
411
412     uuid_generate(ovsdb_row_get_version_rw(row));
413
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);
417 }
418
419 /* 'row' must be assumed destroyed upon return; the caller must not reference
420  * it again. */
421 void
422 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
423 {
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;
428
429     hmap_remove(&table->rows, &row->hmap_node);
430
431     if (!txn_row) {
432         txn_table = ovsdb_txn_create_txn_table(txn, table);
433         row->txn_row = ovsdb_txn_row_create(txn_table, row, NULL);
434     } else {
435         assert(txn_row->new == row);
436         if (txn_row->old) {
437             txn_row->new = NULL;
438         } else {
439             txn_table = ovsdb_txn_get_txn_table(txn, table);
440             hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
441         }
442         ovsdb_row_destroy(row);
443     }
444 }