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 static struct ovsdb_error *ovsdb_file_txn_from_json(struct ovsdb *,
41 static void ovsdb_file_replica_create(struct ovsdb *, struct ovsdb_log *);
44 ovsdb_file_open(const char *file_name, bool read_only, struct ovsdb **dbp)
46 enum ovsdb_log_open_mode open_mode;
47 struct ovsdb_schema *schema;
48 struct ovsdb_error *error;
49 struct ovsdb_log *log;
53 open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
54 error = ovsdb_log_open(file_name, open_mode, -1, &log);
59 error = ovsdb_log_read(log, &json);
63 return ovsdb_io_error(EOF, "%s: database file contains no schema",
67 error = ovsdb_schema_from_json(json, &schema);
70 return ovsdb_wrap_error(error,
71 "failed to parse \"%s\" as ovsdb schema",
76 db = ovsdb_create(schema);
77 while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
78 struct ovsdb_txn *txn;
80 error = ovsdb_file_txn_from_json(db, json, &txn);
86 ovsdb_txn_commit(txn, false);
89 char *msg = ovsdb_error_to_string(error);
93 ovsdb_error_destroy(error);
97 ovsdb_file_replica_create(db, log);
106 static struct ovsdb_error *
107 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
108 const struct uuid *row_uuid, struct json *json)
110 const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
111 if (json->type == JSON_NULL) {
113 return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
114 "row "UUID_FMT" that does not exist",
115 UUID_ARGS(row_uuid));
117 ovsdb_txn_row_delete(txn, row);
120 return ovsdb_row_from_json(ovsdb_txn_row_modify(txn, row),
123 struct ovsdb_error *error;
124 struct ovsdb_row *new;
126 new = ovsdb_row_create(table);
127 *ovsdb_row_get_uuid_rw(new) = *row_uuid;
128 error = ovsdb_row_from_json(new, json, NULL, NULL);
130 ovsdb_row_destroy(new);
133 ovsdb_txn_row_insert(txn, new);
139 static struct ovsdb_error *
140 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
141 struct ovsdb_table *table, struct json *json)
143 struct shash_node *node;
145 if (json->type != JSON_OBJECT) {
146 return ovsdb_syntax_error(json, NULL, "object expected");
149 SHASH_FOR_EACH (node, json->u.object) {
150 const char *uuid_string = node->name;
151 struct json *txn_row_json = node->data;
152 struct ovsdb_error *error;
153 struct uuid row_uuid;
155 if (!uuid_from_string(&row_uuid, uuid_string)) {
156 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
160 error = ovsdb_file_txn_row_from_json(txn, table, &row_uuid,
170 static struct ovsdb_error *
171 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
172 struct ovsdb_txn **txnp)
174 struct ovsdb_error *error;
175 struct shash_node *node;
176 struct ovsdb_txn *txn;
179 if (json->type != JSON_OBJECT) {
180 return ovsdb_syntax_error(json, NULL, "object expected");
183 txn = ovsdb_txn_create(db);
184 SHASH_FOR_EACH (node, json->u.object) {
185 const char *table_name = node->name;
186 struct json *txn_table_json = node->data;
187 struct ovsdb_table *table;
189 table = shash_find_data(&db->tables, table_name);
191 if (!strcmp(table_name, "_date")
192 || !strcmp(table_name, "_comment")) {
196 error = ovsdb_syntax_error(json, "unknown table",
197 "No table named %s.", table_name);
201 error = ovsdb_file_txn_table_from_json(txn, table, txn_table_json);
210 ovsdb_txn_abort(txn);
214 /* Replica implementation. */
216 struct ovsdb_file_replica {
217 struct ovsdb_replica replica;
218 struct ovsdb_log *log;
221 static const struct ovsdb_replica_class ovsdb_file_replica_class;
224 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
226 struct ovsdb_file_replica *r = xmalloc(sizeof *r);
227 ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
229 ovsdb_add_replica(db, &r->replica);
233 static struct ovsdb_file_replica *
234 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
236 assert(replica->class == &ovsdb_file_replica_class);
237 return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
240 struct ovsdb_file_replica_aux {
241 struct json *json; /* JSON for the whole transaction. */
242 struct json *table_json; /* JSON for 'table''s transaction. */
243 struct ovsdb_table *table; /* Table described in 'table_json'. */
247 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
248 const struct ovsdb_row *new,
251 struct ovsdb_file_replica_aux *aux = aux_;
255 row = json_null_create();
257 struct shash_node *node;
259 row = old ? NULL : json_object_create();
260 SHASH_FOR_EACH (node, &new->table->schema->columns) {
261 const struct ovsdb_column *column = node->data;
262 const struct ovsdb_type *type = &column->type;
263 unsigned int idx = column->index;
265 if (idx != OVSDB_COL_UUID && column->persistent
267 ? !ovsdb_datum_equals(&old->fields[idx], &new->fields[idx],
269 : !ovsdb_datum_is_default(&new->fields[idx], type)))
272 row = json_object_create();
274 json_object_put(row, column->name,
275 ovsdb_datum_to_json(&new->fields[idx], type));
281 struct ovsdb_table *table = new ? new->table : old->table;
282 char uuid[UUID_LEN + 1];
284 if (table != aux->table) {
285 /* Create JSON object for transaction overall. */
287 aux->json = json_object_create();
290 /* Create JSON object for transaction on this table. */
291 aux->table_json = json_object_create();
293 json_object_put(aux->json, table->schema->name, aux->table_json);
296 /* Add row to transaction for this table. */
297 snprintf(uuid, sizeof uuid,
298 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
299 json_object_put(aux->table_json, uuid, row);
305 static struct ovsdb_error *
306 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
307 const struct ovsdb_txn *txn, bool durable)
309 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
310 struct ovsdb_file_replica_aux aux;
311 struct ovsdb_error *error;
315 aux.table_json = NULL;
317 ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &aux);
320 /* Nothing to commit. */
324 comment = ovsdb_txn_get_comment(txn);
326 json_object_put_string(aux.json, "_comment", comment);
329 json_object_put(aux.json, "_date", json_integer_create(time_now()));
331 error = ovsdb_log_write(r->log, aux.json);
332 json_destroy(aux.json);
334 return ovsdb_wrap_error(error, "writing transaction failed");
338 error = ovsdb_log_commit(r->log);
340 return ovsdb_wrap_error(error, "committing transaction failed");
348 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
350 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
352 ovsdb_log_close(r->log);
356 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
357 ovsdb_file_replica_commit,
358 ovsdb_file_replica_destroy