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.
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 struct ovsdb_schema *schema;
47 struct ovsdb_error *error;
48 struct ovsdb_log *log;
52 error = ovsdb_log_open(file_name, read_only ? O_RDONLY : O_RDWR, &log);
57 error = ovsdb_log_read(log, &json);
61 return ovsdb_io_error(EOF, "%s: database file contains no schema",
65 error = ovsdb_schema_from_json(json, &schema);
68 return ovsdb_wrap_error(error,
69 "failed to parse \"%s\" as ovsdb schema",
74 db = ovsdb_create(schema);
75 while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
76 struct ovsdb_txn *txn;
78 error = ovsdb_file_txn_from_json(db, json, &txn);
84 ovsdb_txn_commit(txn, false);
87 char *msg = ovsdb_error_to_string(error);
91 ovsdb_error_destroy(error);
95 ovsdb_file_replica_create(db, log);
104 static struct ovsdb_error *
105 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
106 const struct uuid *row_uuid, struct json *json)
108 const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
109 if (json->type == JSON_NULL) {
111 return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
112 "row "UUID_FMT" that does not exist",
113 UUID_ARGS(row_uuid));
115 ovsdb_txn_row_delete(txn, row);
118 return ovsdb_row_from_json(ovsdb_txn_row_modify(txn, row),
121 struct ovsdb_error *error;
122 struct ovsdb_row *new;
124 new = ovsdb_row_create(table);
125 *ovsdb_row_get_uuid_rw(new) = *row_uuid;
126 error = ovsdb_row_from_json(new, json, NULL, NULL);
128 ovsdb_row_destroy(new);
131 ovsdb_txn_row_insert(txn, new);
137 static struct ovsdb_error *
138 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
139 struct ovsdb_table *table, struct json *json)
141 struct shash_node *node;
143 if (json->type != JSON_OBJECT) {
144 return ovsdb_syntax_error(json, NULL, "object expected");
147 SHASH_FOR_EACH (node, json->u.object) {
148 const char *uuid_string = node->name;
149 struct json *txn_row_json = node->data;
150 struct ovsdb_error *error;
151 struct uuid row_uuid;
153 if (!uuid_from_string(&row_uuid, uuid_string)) {
154 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
158 error = ovsdb_file_txn_row_from_json(txn, table, &row_uuid,
168 static struct ovsdb_error *
169 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
170 struct ovsdb_txn **txnp)
172 struct ovsdb_error *error;
173 struct shash_node *node;
174 struct ovsdb_txn *txn;
177 if (json->type != JSON_OBJECT) {
178 return ovsdb_syntax_error(json, NULL, "object expected");
181 txn = ovsdb_txn_create(db);
182 SHASH_FOR_EACH (node, json->u.object) {
183 const char *table_name = node->name;
184 struct json *txn_table_json = node->data;
185 struct ovsdb_table *table;
187 table = shash_find_data(&db->tables, table_name);
189 if (!strcmp(table_name, "_date")
190 || !strcmp(table_name, "_comment")) {
194 error = ovsdb_syntax_error(json, "unknown table",
195 "No table named %s.", table_name);
199 error = ovsdb_file_txn_table_from_json(txn, table, txn_table_json);
208 ovsdb_txn_abort(txn);
212 /* Replica implementation. */
214 struct ovsdb_file_replica {
215 struct ovsdb_replica replica;
216 struct ovsdb_log *log;
219 static const struct ovsdb_replica_class ovsdb_file_replica_class;
222 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
224 struct ovsdb_file_replica *r = xmalloc(sizeof *r);
225 ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
227 ovsdb_add_replica(db, &r->replica);
231 static struct ovsdb_file_replica *
232 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
234 assert(replica->class == &ovsdb_file_replica_class);
235 return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
238 struct ovsdb_file_replica_aux {
239 struct json *json; /* JSON for the whole transaction. */
240 struct json *table_json; /* JSON for 'table''s transaction. */
241 struct ovsdb_table *table; /* Table described in 'table_json'. */
245 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
246 const struct ovsdb_row *new,
249 struct ovsdb_file_replica_aux *aux = aux_;
253 row = json_null_create();
255 struct shash_node *node;
258 SHASH_FOR_EACH (node, &new->table->schema->columns) {
259 const struct ovsdb_column *column = node->data;
260 const struct ovsdb_type *type = &column->type;
261 unsigned int idx = column->index;
263 if (idx != OVSDB_COL_UUID && column->persistent
264 && (!old || !ovsdb_datum_equals(&old->fields[idx],
265 &new->fields[idx], type)))
268 row = json_object_create();
270 json_object_put(row, column->name,
271 ovsdb_datum_to_json(&new->fields[idx], type));
277 struct ovsdb_table *table = new ? new->table : old->table;
278 char uuid[UUID_LEN + 1];
280 if (table != aux->table) {
281 /* Create JSON object for transaction overall. */
283 aux->json = json_object_create();
286 /* Create JSON object for transaction on this table. */
287 aux->table_json = json_object_create();
289 json_object_put(aux->json, table->schema->name, aux->table_json);
292 /* Add row to transaction for this table. */
293 snprintf(uuid, sizeof uuid,
294 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
295 json_object_put(aux->table_json, uuid, row);
301 static struct ovsdb_error *
302 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
303 const struct ovsdb_txn *txn, bool durable)
305 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
306 struct ovsdb_file_replica_aux aux;
307 struct ovsdb_error *error;
311 aux.table_json = NULL;
313 ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &aux);
316 /* Nothing to commit. */
320 comment = ovsdb_txn_get_comment(txn);
322 json_object_put_string(aux.json, "_comment", comment);
325 json_object_put(aux.json, "_date", json_integer_create(time_now()));
327 error = ovsdb_log_write(r->log, aux.json);
328 json_destroy(aux.json);
330 return ovsdb_wrap_error(error, "writing transaction failed");
334 error = ovsdb_log_commit(r->log);
336 return ovsdb_wrap_error(error, "committing transaction failed");
344 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
346 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
348 ovsdb_log_close(r->log);
352 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
353 ovsdb_file_replica_commit,
354 ovsdb_file_replica_destroy