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.
25 #include "ovsdb-error.h"
26 #include "ovsdb-parser.h"
27 #include "ovsdb-types.h"
31 add_column(struct ovsdb_table_schema *ts, struct ovsdb_column *column)
33 assert(!shash_find(&ts->columns, column->name));
34 column->index = shash_count(&ts->columns);
35 shash_add(&ts->columns, column->name, column);
38 struct ovsdb_table_schema *
39 ovsdb_table_schema_create(const char *name, bool mutable,
40 unsigned int max_rows)
42 struct ovsdb_column *uuid, *version;
43 struct ovsdb_table_schema *ts;
45 ts = xzalloc(sizeof *ts);
46 ts->name = xstrdup(name);
47 ts->mutable = mutable;
48 shash_init(&ts->columns);
49 ts->max_rows = max_rows;
51 uuid = ovsdb_column_create("_uuid", false, true, &ovsdb_type_uuid);
53 assert(uuid->index == OVSDB_COL_UUID);
55 version = ovsdb_column_create("_version", false, false, &ovsdb_type_uuid);
56 add_column(ts, version);
57 assert(version->index == OVSDB_COL_VERSION);
62 struct ovsdb_table_schema *
63 ovsdb_table_schema_clone(const struct ovsdb_table_schema *old)
65 struct ovsdb_table_schema *new;
66 struct shash_node *node;
68 new = ovsdb_table_schema_create(old->name, old->mutable, old->max_rows);
69 SHASH_FOR_EACH (node, &old->columns) {
70 const struct ovsdb_column *column = node->data;
72 if (column->name[0] == '_') {
73 /* Added automatically by ovsdb_table_schema_create(). */
77 add_column(new, ovsdb_column_clone(column));
83 ovsdb_table_schema_destroy(struct ovsdb_table_schema *ts)
85 struct shash_node *node;
87 SHASH_FOR_EACH (node, &ts->columns) {
88 ovsdb_column_destroy(node->data);
90 shash_destroy(&ts->columns);
96 ovsdb_table_schema_from_json(const struct json *json, const char *name,
97 struct ovsdb_table_schema **tsp)
99 struct ovsdb_table_schema *ts;
100 const struct json *columns, *mutable, *max_rows;
101 struct shash_node *node;
102 struct ovsdb_parser parser;
103 struct ovsdb_error *error;
104 long long int n_max_rows;
108 ovsdb_parser_init(&parser, json, "table schema for table %s", name);
109 columns = ovsdb_parser_member(&parser, "columns", OP_OBJECT);
110 mutable = ovsdb_parser_member(&parser, "mutable",
111 OP_TRUE | OP_FALSE | OP_OPTIONAL);
112 max_rows = ovsdb_parser_member(&parser, "maxRows",
113 OP_INTEGER | OP_OPTIONAL);
114 error = ovsdb_parser_finish(&parser);
120 if (json_integer(max_rows) <= 0) {
121 return ovsdb_syntax_error(json, NULL,
122 "maxRows must be at least 1");
124 n_max_rows = max_rows->u.integer;
126 n_max_rows = UINT_MAX;
129 if (shash_is_empty(json_object(columns))) {
130 return ovsdb_syntax_error(json, NULL,
131 "table must have at least one column");
134 ts = ovsdb_table_schema_create(name,
135 mutable ? json_boolean(mutable) : true,
136 MIN(n_max_rows, UINT_MAX));
137 SHASH_FOR_EACH (node, json_object(columns)) {
138 struct ovsdb_column *column;
140 if (node->name[0] == '_') {
141 error = ovsdb_syntax_error(json, NULL, "names beginning with "
142 "\"_\" are reserved");
143 } else if (!ovsdb_parser_is_id(node->name)) {
144 error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
146 error = ovsdb_column_from_json(node->data, node->name, &column);
149 ovsdb_table_schema_destroy(ts);
153 add_column(ts, column);
160 ovsdb_table_schema_to_json(const struct ovsdb_table_schema *ts)
162 struct json *json, *columns;
163 struct shash_node *node;
165 json = json_object_create();
167 json_object_put(json, "mutable", json_boolean_create(false));
170 columns = json_object_create();
172 SHASH_FOR_EACH (node, &ts->columns) {
173 const struct ovsdb_column *column = node->data;
174 if (node->name[0] != '_') {
175 json_object_put(columns, column->name,
176 ovsdb_column_to_json(column));
179 json_object_put(json, "columns", columns);
180 if (ts->max_rows != UINT_MAX) {
181 json_object_put(json, "maxRows", json_integer_create(ts->max_rows));
187 const struct ovsdb_column *
188 ovsdb_table_schema_get_column(const struct ovsdb_table_schema *ts,
191 return shash_find_data(&ts->columns, name);
195 ovsdb_table_create(struct ovsdb_table_schema *ts)
197 struct ovsdb_table *table;
199 table = xmalloc(sizeof *table);
201 table->txn_table = NULL;
202 hmap_init(&table->rows);
208 ovsdb_table_destroy(struct ovsdb_table *table)
211 struct ovsdb_row *row, *next;
213 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_row, hmap_node,
215 ovsdb_row_destroy(row);
217 hmap_destroy(&table->rows);
219 ovsdb_table_schema_destroy(table->schema);
224 const struct ovsdb_row *
225 ovsdb_table_get_row(const struct ovsdb_table *table, const struct uuid *uuid)
227 struct ovsdb_row *row;
229 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_row, hmap_node, uuid_hash(uuid),
231 if (uuid_equals(ovsdb_row_get_uuid(row), uuid)) {
239 /* This is probably not the function you want. Use ovsdb_txn_row_modify()
242 ovsdb_table_put_row(struct ovsdb_table *table, struct ovsdb_row *row)
244 const struct uuid *uuid = ovsdb_row_get_uuid(row);
245 if (!ovsdb_table_get_row(table, uuid)) {
246 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));