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"
30 #include "transaction.h"
34 #define THIS_MODULE VLM_ovsdb_file
37 static struct ovsdb_error *ovsdb_file_txn_from_json(struct ovsdb *,
40 static void ovsdb_file_replica_create(struct ovsdb *, struct ovsdb_log *);
43 ovsdb_file_open(const char *file_name, bool read_only, struct ovsdb **dbp)
45 struct ovsdb_schema *schema;
46 struct ovsdb_error *error;
47 struct ovsdb_log *log;
51 error = ovsdb_log_open(file_name, read_only ? O_RDONLY : O_RDWR, &log);
56 error = ovsdb_log_read(log, &json);
60 return ovsdb_io_error(EOF, "%s: database file contains no schema",
64 error = ovsdb_schema_from_json(json, &schema);
67 return ovsdb_wrap_error(error,
68 "failed to parse \"%s\" as ovsdb schema",
73 db = ovsdb_create(schema);
74 while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
75 struct ovsdb_txn *txn;
77 error = ovsdb_file_txn_from_json(db, json, &txn);
83 ovsdb_txn_commit(txn, false);
86 char *msg = ovsdb_error_to_string(error);
90 ovsdb_error_destroy(error);
94 ovsdb_file_replica_create(db, log);
103 static struct ovsdb_error *
104 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
105 const struct uuid *row_uuid, struct json *json)
107 const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
108 if (json->type == JSON_NULL) {
110 return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
111 "row "UUID_FMT" that does not exist",
112 UUID_ARGS(row_uuid));
114 ovsdb_txn_row_delete(txn, row);
117 return ovsdb_row_from_json(ovsdb_txn_row_modify(txn, row),
120 struct ovsdb_error *error;
121 struct ovsdb_row *new;
123 new = ovsdb_row_create(table);
124 *ovsdb_row_get_uuid_rw(new) = *row_uuid;
125 error = ovsdb_row_from_json(new, json, NULL, NULL);
127 ovsdb_row_destroy(new);
130 ovsdb_txn_row_insert(txn, new);
136 static struct ovsdb_error *
137 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
138 struct ovsdb_table *table, struct json *json)
140 struct shash_node *node;
142 if (json->type != JSON_OBJECT) {
143 return ovsdb_syntax_error(json, NULL, "object expected");
146 SHASH_FOR_EACH (node, json->u.object) {
147 const char *uuid_string = node->name;
148 struct json *txn_row_json = node->data;
149 struct ovsdb_error *error;
150 struct uuid row_uuid;
152 if (!uuid_from_string(&row_uuid, uuid_string)) {
153 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
157 error = ovsdb_file_txn_row_from_json(txn, table, &row_uuid,
167 static struct ovsdb_error *
168 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
169 struct ovsdb_txn **txnp)
171 struct ovsdb_error *error;
172 struct shash_node *node;
173 struct ovsdb_txn *txn;
176 if (json->type != JSON_OBJECT) {
177 return ovsdb_syntax_error(json, NULL, "object expected");
180 txn = ovsdb_txn_create(db);
181 SHASH_FOR_EACH (node, json->u.object) {
182 const char *table_name = node->name;
183 struct json *txn_table_json = node->data;
184 struct ovsdb_table *table;
186 table = shash_find_data(&db->tables, table_name);
188 error = ovsdb_syntax_error(json, "unknown table",
189 "No table named %s.", table_name);
193 error = ovsdb_file_txn_table_from_json(txn, table, txn_table_json);
202 ovsdb_txn_abort(txn);
206 /* Replica implementation. */
208 struct ovsdb_file_replica {
209 struct ovsdb_replica replica;
210 struct ovsdb_log *log;
213 static const struct ovsdb_replica_class ovsdb_file_replica_class;
216 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
218 struct ovsdb_file_replica *r = xmalloc(sizeof *r);
219 ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
221 ovsdb_add_replica(db, &r->replica);
225 static struct ovsdb_file_replica *
226 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
228 assert(replica->class == &ovsdb_file_replica_class);
229 return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
232 struct ovsdb_file_replica_aux {
233 struct json *json; /* JSON for the whole transaction. */
234 struct json *table_json; /* JSON for 'table''s transaction. */
235 struct ovsdb_table *table; /* Table described in 'table_json'. */
239 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
240 const struct ovsdb_row *new,
243 struct ovsdb_file_replica_aux *aux = aux_;
247 row = json_null_create();
249 struct shash_node *node;
252 SHASH_FOR_EACH (node, &new->table->schema->columns) {
253 const struct ovsdb_column *column = node->data;
254 const struct ovsdb_type *type = &column->type;
255 unsigned int idx = column->index;
257 if (idx != OVSDB_COL_UUID && column->persistent
258 && (!old || !ovsdb_datum_equals(&old->fields[idx],
259 &new->fields[idx], type)))
262 row = json_object_create();
264 json_object_put(row, column->name,
265 ovsdb_datum_to_json(&new->fields[idx], type));
271 struct ovsdb_table *table = new ? new->table : old->table;
272 char uuid[UUID_LEN + 1];
274 if (table != aux->table) {
275 /* Create JSON object for transaction overall. */
277 aux->json = json_object_create();
280 /* Create JSON object for transaction on this table. */
281 aux->table_json = json_object_create();
283 json_object_put(aux->json, table->schema->name, aux->table_json);
286 /* Add row to transaction for this table. */
287 snprintf(uuid, sizeof uuid,
288 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
289 json_object_put(aux->table_json, uuid, row);
295 static struct ovsdb_error *
296 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
297 const struct ovsdb_txn *txn, bool durable)
299 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
300 struct ovsdb_file_replica_aux aux;
301 struct ovsdb_error *error;
304 aux.table_json = NULL;
306 ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &aux);
309 /* Nothing to commit. */
313 error = ovsdb_log_write(r->log, aux.json);
314 json_destroy(aux.json);
316 return ovsdb_wrap_error(error, "writing transaction failed");
320 error = ovsdb_log_commit(r->log);
322 return ovsdb_wrap_error(error, "committing transaction failed");
330 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
332 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
334 ovsdb_log_close(r->log);
338 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
339 ovsdb_file_replica_commit,
340 ovsdb_file_replica_destroy