1 /* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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.
22 #include "ovsdb-error.h"
23 #include "ovsdb-parser.h"
24 #include "ovsdb-types.h"
27 #include "transaction.h"
30 ovsdb_schema_create(const char *name, const char *version, const char *cksum)
32 struct ovsdb_schema *schema;
34 schema = xzalloc(sizeof *schema);
35 schema->name = xstrdup(name);
36 schema->version = xstrdup(version);
37 schema->cksum = xstrdup(cksum);
38 shash_init(&schema->tables);
44 ovsdb_schema_clone(const struct ovsdb_schema *old)
46 struct ovsdb_schema *new;
47 struct shash_node *node;
49 new = ovsdb_schema_create(old->name, old->version, old->cksum);
50 SHASH_FOR_EACH (node, &old->tables) {
51 const struct ovsdb_table_schema *ts = node->data;
53 shash_add(&new->tables, node->name, ovsdb_table_schema_clone(ts));
59 ovsdb_schema_destroy(struct ovsdb_schema *schema)
61 struct shash_node *node;
67 SHASH_FOR_EACH (node, &schema->tables) {
68 ovsdb_table_schema_destroy(node->data);
70 shash_destroy(&schema->tables);
72 free(schema->version);
78 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
80 struct ovsdb_schema *schema;
81 struct ovsdb_error *error;
85 json = json_from_file(file_name);
86 if (json->type == JSON_STRING) {
87 error = ovsdb_error("failed to read schema",
88 "\"%s\" could not be read as JSON (%s)",
89 file_name, json_string(json));
94 error = ovsdb_schema_from_json(json, &schema);
97 return ovsdb_wrap_error(error,
98 "failed to parse \"%s\" as ovsdb schema",
106 static struct ovsdb_error * WARN_UNUSED_RESULT
107 ovsdb_schema_check_ref_table(struct ovsdb_column *column,
108 const struct shash *tables,
109 const struct ovsdb_base_type *base,
110 const char *base_name)
112 struct ovsdb_table_schema *refTable;
114 if (base->type != OVSDB_TYPE_UUID || !base->u.uuid.refTableName) {
118 refTable = shash_find_data(tables, base->u.uuid.refTableName);
120 return ovsdb_syntax_error(NULL, NULL,
121 "column %s %s refers to undefined table %s",
122 column->name, base_name,
123 base->u.uuid.refTableName);
126 if (ovsdb_base_type_is_strong_ref(base) && !refTable->is_root) {
127 /* We cannot allow a strong reference to a non-root table to be
128 * ephemeral: if it is the only reference to a row, then replaying the
129 * database log from disk will cause the referenced row to be deleted,
130 * even though it did exist in memory. If there are references to that
131 * row later in the log (to modify it, to delete it, or just to point
132 * to it), then this will yield a transaction error. */
133 column->persistent = true;
140 is_valid_version(const char *s)
143 ignore(sscanf(s, "%*[0-9].%*[0-9].%*[0-9]%n", &n));
144 return n != -1 && s[n] == '\0';
147 /* Returns the number of tables in 'schema''s root set. */
149 root_set_size(const struct ovsdb_schema *schema)
151 struct shash_node *node;
154 SHASH_FOR_EACH (node, &schema->tables) {
155 struct ovsdb_table_schema *table = node->data;
157 n_root += table->is_root;
163 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
165 struct ovsdb_schema *schema;
166 const struct json *name, *tables, *version_json, *cksum;
167 struct ovsdb_error *error;
168 struct shash_node *node;
169 struct ovsdb_parser parser;
174 ovsdb_parser_init(&parser, json, "database schema");
175 name = ovsdb_parser_member(&parser, "name", OP_ID);
176 version_json = ovsdb_parser_member(&parser, "version",
177 OP_STRING | OP_OPTIONAL);
178 cksum = ovsdb_parser_member(&parser, "cksum", OP_STRING | OP_OPTIONAL);
179 tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
180 error = ovsdb_parser_finish(&parser);
186 version = json_string(version_json);
187 if (!is_valid_version(version)) {
188 return ovsdb_syntax_error(json, NULL, "schema version \"%s\" not "
189 "in format x.y.z", version);
192 /* Backward compatibility with old databases. */
196 schema = ovsdb_schema_create(json_string(name), version,
197 cksum ? json_string(cksum) : "");
198 SHASH_FOR_EACH (node, json_object(tables)) {
199 struct ovsdb_table_schema *table;
201 if (node->name[0] == '_') {
202 error = ovsdb_syntax_error(json, NULL, "names beginning with "
203 "\"_\" are reserved");
204 } else if (!ovsdb_parser_is_id(node->name)) {
205 error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
207 error = ovsdb_table_schema_from_json(node->data, node->name,
211 ovsdb_schema_destroy(schema);
215 shash_add(&schema->tables, table->name, table);
218 /* "isRoot" was not part of the original schema definition. Before it was
219 * added, there was no support for garbage collection. So, for backward
220 * compatibility, if the root set is empty then assume that every table is
221 * in the root set. */
222 if (root_set_size(schema) == 0) {
223 SHASH_FOR_EACH (node, &schema->tables) {
224 struct ovsdb_table_schema *table = node->data;
226 table->is_root = true;
230 /* Validate that all refTables refer to the names of tables that exist.
232 * Also force certain columns to be persistent, as explained in
233 * ovsdb_schema_check_ref_table(). This requires 'is_root' to be known, so
234 * this must follow the loop updating 'is_root' above. */
235 SHASH_FOR_EACH (node, &schema->tables) {
236 struct ovsdb_table_schema *table = node->data;
237 struct shash_node *node2;
239 SHASH_FOR_EACH (node2, &table->columns) {
240 struct ovsdb_column *column = node2->data;
242 error = ovsdb_schema_check_ref_table(column, &schema->tables,
243 &column->type.key, "key");
245 error = ovsdb_schema_check_ref_table(column, &schema->tables,
250 ovsdb_schema_destroy(schema);
261 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
263 struct json *json, *tables;
264 struct shash_node *node;
265 bool default_is_root;
267 json = json_object_create();
268 json_object_put_string(json, "name", schema->name);
269 if (schema->version[0]) {
270 json_object_put_string(json, "version", schema->version);
272 if (schema->cksum[0]) {
273 json_object_put_string(json, "cksum", schema->cksum);
276 /* "isRoot" was not part of the original schema definition. Before it was
277 * added, there was no support for garbage collection. So, for backward
278 * compatibility, if every table is in the root set then do not output
279 * "isRoot" in table schemas. */
280 default_is_root = root_set_size(schema) == shash_count(&schema->tables);
282 tables = json_object_create();
284 SHASH_FOR_EACH (node, &schema->tables) {
285 struct ovsdb_table_schema *table = node->data;
286 json_object_put(tables, table->name,
287 ovsdb_table_schema_to_json(table, default_is_root));
289 json_object_put(json, "tables", tables);
294 /* Returns true if 'a' and 'b' specify equivalent schemas, false if they
297 ovsdb_schema_equal(const struct ovsdb_schema *a,
298 const struct ovsdb_schema *b)
300 /* This implementation is simple, stupid, and slow, but I doubt that it
301 * will ever require much maintenance. */
302 struct json *ja = ovsdb_schema_to_json(a);
303 struct json *jb = ovsdb_schema_to_json(b);
304 bool equals = json_equal(ja, jb);
312 ovsdb_set_ref_table(const struct shash *tables,
313 struct ovsdb_base_type *base)
315 if (base->type == OVSDB_TYPE_UUID && base->u.uuid.refTableName) {
316 struct ovsdb_table *table;
318 table = shash_find_data(tables, base->u.uuid.refTableName);
319 base->u.uuid.refTable = table;
324 ovsdb_create(struct ovsdb_schema *schema)
326 struct shash_node *node;
329 db = xmalloc(sizeof *db);
331 list_init(&db->replicas);
332 list_init(&db->triggers);
333 db->run_triggers = false;
335 shash_init(&db->tables);
336 SHASH_FOR_EACH (node, &schema->tables) {
337 struct ovsdb_table_schema *ts = node->data;
338 shash_add(&db->tables, node->name, ovsdb_table_create(ts));
341 /* Set all the refTables. */
342 SHASH_FOR_EACH (node, &schema->tables) {
343 struct ovsdb_table_schema *table = node->data;
344 struct shash_node *node2;
346 SHASH_FOR_EACH (node2, &table->columns) {
347 struct ovsdb_column *column = node2->data;
349 ovsdb_set_ref_table(&db->tables, &column->type.key);
350 ovsdb_set_ref_table(&db->tables, &column->type.value);
358 ovsdb_destroy(struct ovsdb *db)
361 struct shash_node *node;
363 /* Remove all the replicas. */
364 while (!list_is_empty(&db->replicas)) {
365 struct ovsdb_replica *r
366 = CONTAINER_OF(list_pop_back(&db->replicas),
367 struct ovsdb_replica, node);
368 ovsdb_remove_replica(db, r);
371 /* Delete all the tables. This also deletes their schemas. */
372 SHASH_FOR_EACH (node, &db->tables) {
373 struct ovsdb_table *table = node->data;
374 ovsdb_table_destroy(table);
376 shash_destroy(&db->tables);
378 /* The schemas, but not the table that points to them, were deleted in
379 * the previous step, so we need to clear out the table. We can't
380 * destroy the table, because ovsdb_schema_destroy() will do that. */
381 shash_clear(&db->schema->tables);
383 ovsdb_schema_destroy(db->schema);
388 /* Adds some memory usage statistics for 'db' into 'usage', for use with
389 * memory_report(). */
391 ovsdb_get_memory_usage(const struct ovsdb *db, struct simap *usage)
393 const struct shash_node *node;
394 unsigned int cells = 0;
396 SHASH_FOR_EACH (node, &db->tables) {
397 const struct ovsdb_table *table = node->data;
398 unsigned int n_columns = shash_count(&table->schema->columns);
399 unsigned int n_rows = hmap_count(&table->rows);
401 cells += n_rows * n_columns;
404 simap_increase(usage, "cells", cells);
408 ovsdb_get_table(const struct ovsdb *db, const char *name)
410 return shash_find_data(&db->tables, name);
414 ovsdb_replica_init(struct ovsdb_replica *r,
415 const struct ovsdb_replica_class *class)
421 ovsdb_add_replica(struct ovsdb *db, struct ovsdb_replica *r)
423 list_push_back(&db->replicas, &r->node);
427 ovsdb_remove_replica(struct ovsdb *db OVS_UNUSED, struct ovsdb_replica *r)
429 list_remove(&r->node);
430 (r->class->destroy)(r);