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.
27 #include "ovsdb-error.h"
31 #include "transaction.h"
35 #define THIS_MODULE VLM_ovsdb_file
38 /* A transaction being converted to JSON for writing to a file. */
39 struct ovsdb_file_txn {
40 struct json *json; /* JSON for the whole transaction. */
41 struct json *table_json; /* JSON for 'table''s transaction. */
42 struct ovsdb_table *table; /* Table described in 'table_json'. */
45 static void ovsdb_file_txn_init(struct ovsdb_file_txn *);
46 static void ovsdb_file_txn_add_row(struct ovsdb_file_txn *,
47 const struct ovsdb_row *old,
48 const struct ovsdb_row *new);
49 static struct ovsdb_error *ovsdb_file_txn_commit(struct json *,
53 static struct ovsdb_error *ovsdb_file_txn_from_json(struct ovsdb *,
56 static void ovsdb_file_replica_create(struct ovsdb *, struct ovsdb_log *);
59 ovsdb_file_open(const char *file_name, bool read_only, struct ovsdb **dbp)
61 enum ovsdb_log_open_mode open_mode;
62 struct ovsdb_schema *schema;
63 struct ovsdb_error *error;
64 struct ovsdb_log *log;
68 open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
69 error = ovsdb_log_open(file_name, open_mode, -1, &log);
74 error = ovsdb_log_read(log, &json);
78 return ovsdb_io_error(EOF, "%s: database file contains no schema",
82 error = ovsdb_schema_from_json(json, &schema);
85 return ovsdb_wrap_error(error,
86 "failed to parse \"%s\" as ovsdb schema",
91 db = ovsdb_create(schema);
92 while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
93 struct ovsdb_txn *txn;
95 error = ovsdb_file_txn_from_json(db, json, &txn);
101 ovsdb_txn_commit(txn, false);
104 char *msg = ovsdb_error_to_string(error);
105 VLOG_WARN("%s", msg);
108 ovsdb_error_destroy(error);
112 ovsdb_file_replica_create(db, log);
114 ovsdb_log_close(log);
121 static struct ovsdb_error *
122 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
123 const struct uuid *row_uuid, struct json *json)
125 const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
126 if (json->type == JSON_NULL) {
128 return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
129 "row "UUID_FMT" that does not exist",
130 UUID_ARGS(row_uuid));
132 ovsdb_txn_row_delete(txn, row);
135 return ovsdb_row_from_json(ovsdb_txn_row_modify(txn, row),
138 struct ovsdb_error *error;
139 struct ovsdb_row *new;
141 new = ovsdb_row_create(table);
142 *ovsdb_row_get_uuid_rw(new) = *row_uuid;
143 error = ovsdb_row_from_json(new, json, NULL, NULL);
145 ovsdb_row_destroy(new);
148 ovsdb_txn_row_insert(txn, new);
154 static struct ovsdb_error *
155 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
156 struct ovsdb_table *table, struct json *json)
158 struct shash_node *node;
160 if (json->type != JSON_OBJECT) {
161 return ovsdb_syntax_error(json, NULL, "object expected");
164 SHASH_FOR_EACH (node, json->u.object) {
165 const char *uuid_string = node->name;
166 struct json *txn_row_json = node->data;
167 struct ovsdb_error *error;
168 struct uuid row_uuid;
170 if (!uuid_from_string(&row_uuid, uuid_string)) {
171 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
175 error = ovsdb_file_txn_row_from_json(txn, table, &row_uuid,
185 static struct ovsdb_error *
186 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
187 struct ovsdb_txn **txnp)
189 struct ovsdb_error *error;
190 struct shash_node *node;
191 struct ovsdb_txn *txn;
194 if (json->type != JSON_OBJECT) {
195 return ovsdb_syntax_error(json, NULL, "object expected");
198 txn = ovsdb_txn_create(db);
199 SHASH_FOR_EACH (node, json->u.object) {
200 const char *table_name = node->name;
201 struct json *txn_table_json = node->data;
202 struct ovsdb_table *table;
204 table = shash_find_data(&db->tables, table_name);
206 if (!strcmp(table_name, "_date")
207 || !strcmp(table_name, "_comment")) {
211 error = ovsdb_syntax_error(json, "unknown table",
212 "No table named %s.", table_name);
216 error = ovsdb_file_txn_table_from_json(txn, table, txn_table_json);
225 ovsdb_txn_abort(txn);
229 /* Replica implementation. */
231 struct ovsdb_file_replica {
232 struct ovsdb_replica replica;
233 struct ovsdb_log *log;
236 static const struct ovsdb_replica_class ovsdb_file_replica_class;
239 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
241 struct ovsdb_file_replica *r = xmalloc(sizeof *r);
242 ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
244 ovsdb_add_replica(db, &r->replica);
248 static struct ovsdb_file_replica *
249 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
251 assert(replica->class == &ovsdb_file_replica_class);
252 return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
256 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
257 const struct ovsdb_row *new,
260 struct ovsdb_file_txn *ftxn = ftxn_;
261 ovsdb_file_txn_add_row(ftxn, old, new);
265 static struct ovsdb_error *
266 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
267 const struct ovsdb_txn *txn, bool durable)
269 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
270 struct ovsdb_file_txn ftxn;
272 ovsdb_file_txn_init(&ftxn);
273 ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &ftxn);
275 /* Nothing to commit. */
279 return ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
284 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
286 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
288 ovsdb_log_close(r->log);
292 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
293 ovsdb_file_replica_commit,
294 ovsdb_file_replica_destroy
298 ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
301 ftxn->table_json = NULL;
306 ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn,
307 const struct ovsdb_row *old,
308 const struct ovsdb_row *new)
313 row = json_null_create();
315 struct shash_node *node;
317 row = old ? NULL : json_object_create();
318 SHASH_FOR_EACH (node, &new->table->schema->columns) {
319 const struct ovsdb_column *column = node->data;
320 const struct ovsdb_type *type = &column->type;
321 unsigned int idx = column->index;
323 if (idx != OVSDB_COL_UUID && column->persistent
325 ? !ovsdb_datum_equals(&old->fields[idx], &new->fields[idx],
327 : !ovsdb_datum_is_default(&new->fields[idx], type)))
330 row = json_object_create();
332 json_object_put(row, column->name,
333 ovsdb_datum_to_json(&new->fields[idx], type));
339 struct ovsdb_table *table = new ? new->table : old->table;
340 char uuid[UUID_LEN + 1];
342 if (table != ftxn->table) {
343 /* Create JSON object for transaction overall. */
345 ftxn->json = json_object_create();
348 /* Create JSON object for transaction on this table. */
349 ftxn->table_json = json_object_create();
351 json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
354 /* Add row to transaction for this table. */
355 snprintf(uuid, sizeof uuid,
356 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
357 json_object_put(ftxn->table_json, uuid, row);
361 static struct ovsdb_error *
362 ovsdb_file_txn_commit(struct json *json, const char *comment,
363 bool durable, struct ovsdb_log *log)
365 struct ovsdb_error *error;
368 json = json_object_create();
371 json_object_put_string(json, "_comment", comment);
373 json_object_put(json, "_date", json_integer_create(time_now()));
375 error = ovsdb_log_write(log, json);
378 return ovsdb_wrap_error(error, "writing transaction failed");
382 error = ovsdb_log_commit(log);
384 return ovsdb_wrap_error(error, "committing transaction failed");