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.
18 #include "ovsdb/column.h"
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
30 ovsdb_column_create(const char *name,
31 bool mutable, bool persistent,
32 const struct ovsdb_type *type)
34 /* Doesn't set the new column's 'index': the caller must do that. */
35 struct ovsdb_column *column;
37 column = xzalloc(sizeof *column);
38 column->name = xstrdup(name);
39 column->mutable = mutable;
40 column->persistent = persistent;
41 ovsdb_type_clone(&column->type, type);
47 ovsdb_column_clone(const struct ovsdb_column *old)
49 /* Doesn't copy the column's 'index': the caller must do that. */
50 return ovsdb_column_create(old->name,
51 old->mutable, old->persistent,
56 ovsdb_column_destroy(struct ovsdb_column *column)
58 ovsdb_type_destroy(&column->type);
64 ovsdb_column_from_json(const struct json *json, const char *name,
65 struct ovsdb_column **columnp)
67 const struct json *mutable, *ephemeral, *type_json;
68 struct ovsdb_error *error;
69 struct ovsdb_type type;
70 struct ovsdb_parser parser;
75 ovsdb_parser_init(&parser, json, "schema for column %s", name);
76 mutable = ovsdb_parser_member(&parser, "mutable",
77 OP_TRUE | OP_FALSE | OP_OPTIONAL);
78 ephemeral = ovsdb_parser_member(&parser, "ephemeral",
79 OP_TRUE | OP_FALSE | OP_OPTIONAL);
80 type_json = ovsdb_parser_member(&parser, "type", OP_STRING | OP_OBJECT);
81 error = ovsdb_parser_finish(&parser);
86 error = ovsdb_type_from_json(&type, type_json);
91 persistent = ephemeral ? !json_boolean(ephemeral) : true;
92 *columnp = ovsdb_column_create(name,
93 mutable ? json_boolean(mutable) : true,
96 ovsdb_type_destroy(&type);
102 ovsdb_column_to_json(const struct ovsdb_column *column)
104 struct json *json = json_object_create();
105 if (!column->mutable) {
106 json_object_put(json, "mutable", json_boolean_create(false));
108 if (!column->persistent) {
109 json_object_put(json, "ephemeral", json_boolean_create(true));
111 json_object_put(json, "type", ovsdb_type_to_json(&column->type));
116 ovsdb_column_set_init(struct ovsdb_column_set *set)
119 set->n_columns = set->allocated_columns = 0;
123 ovsdb_column_set_destroy(struct ovsdb_column_set *set)
129 ovsdb_column_set_clone(struct ovsdb_column_set *new,
130 const struct ovsdb_column_set *old)
132 new->columns = xmemdup(old->columns,
133 old->n_columns * sizeof *old->columns);
134 new->n_columns = new->allocated_columns = old->n_columns;
138 ovsdb_column_set_from_json(const struct json *json,
139 const struct ovsdb_table *table,
140 struct ovsdb_column_set *set)
142 ovsdb_column_set_init(set);
144 struct shash_node *node;
146 SHASH_FOR_EACH (node, &table->schema->columns) {
147 const struct ovsdb_column *column = node->data;
148 ovsdb_column_set_add(set, column);
153 struct ovsdb_error *error = NULL;
156 if (json->type != JSON_ARRAY) {
160 /* XXX this is O(n**2) */
161 for (i = 0; i < json->u.array.n; i++) {
162 const struct ovsdb_column *column;
165 if (json->u.array.elems[i]->type != JSON_STRING) {
169 s = json->u.array.elems[i]->u.string;
170 column = shash_find_data(&table->schema->columns, s);
172 error = ovsdb_syntax_error(json, NULL, "%s is not a valid "
175 } else if (ovsdb_column_set_contains(set, column->index)) {
178 ovsdb_column_set_add(set, column);
183 ovsdb_column_set_destroy(set);
184 ovsdb_column_set_init(set);
186 error = ovsdb_syntax_error(json, NULL, "array of distinct column "
194 ovsdb_column_set_to_json(const struct ovsdb_column_set *set)
199 json = json_array_create_empty();
200 for (i = 0; i < set->n_columns; i++) {
201 json_array_add(json, json_string_create(set->columns[i]->name));
207 ovsdb_column_set_add(struct ovsdb_column_set *set,
208 const struct ovsdb_column *column)
210 if (set->n_columns >= set->allocated_columns) {
211 set->columns = x2nrealloc(set->columns, &set->allocated_columns,
212 sizeof *set->columns);
214 set->columns[set->n_columns++] = column;
218 ovsdb_column_set_add_all(struct ovsdb_column_set *set,
219 const struct ovsdb_table *table)
221 struct shash_node *node;
223 SHASH_FOR_EACH (node, &table->schema->columns) {
224 const struct ovsdb_column *column = node->data;
225 ovsdb_column_set_add(set, column);
230 ovsdb_column_set_contains(const struct ovsdb_column_set *set,
231 unsigned int column_index)
235 for (i = 0; i < set->n_columns; i++) {
236 if (set->columns[i]->index == column_index) {
243 /* This comparison is sensitive to ordering of columns within a set, but that's
244 * good: the only existing caller wants to make sure that hash values are
245 * comparable, which is only true if column ordering is the same. */
247 ovsdb_column_set_equals(const struct ovsdb_column_set *a,
248 const struct ovsdb_column_set *b)
252 if (a->n_columns != b->n_columns) {
255 for (i = 0; i < a->n_columns; i++) {
256 if (a->columns[i] != b->columns[i]) {