Merge "master" into "next".
[cascardo/ovs.git] / ovsdb / column.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb/column.h"
19
20 #include <stdlib.h>
21
22 #include "column.h"
23 #include "json.h"
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
26 #include "table.h"
27 #include "util.h"
28
29 struct ovsdb_column *
30 ovsdb_column_create(const char *name, const char *comment,
31                     bool mutable, bool persistent,
32                     const struct ovsdb_type *type)
33 {
34     /* Doesn't set the new column's 'index': the caller must do that. */
35     struct ovsdb_column *column;
36
37     column = xzalloc(sizeof *column);
38     column->name = xstrdup(name);
39     column->comment = comment ? xstrdup(comment) : NULL;
40     column->mutable = mutable;
41     column->persistent = persistent;
42     ovsdb_type_clone(&column->type, type);
43
44     return column;
45 }
46
47 struct ovsdb_column *
48 ovsdb_column_clone(const struct ovsdb_column *old)
49 {
50     /* Doesn't copy the column's 'index': the caller must do that. */
51     return ovsdb_column_create(old->name, old->comment,
52                                old->mutable, old->persistent,
53                                &old->type);
54 }
55
56 void
57 ovsdb_column_destroy(struct ovsdb_column *column)
58 {
59     ovsdb_type_destroy(&column->type);
60     free(column->name);
61     free(column->comment);
62     free(column);
63 }
64
65 struct ovsdb_error *
66 ovsdb_column_from_json(const struct json *json, const char *name,
67                        struct ovsdb_column **columnp)
68 {
69     const struct json *comment, *mutable, *ephemeral, *type_json;
70     struct ovsdb_error *error;
71     struct ovsdb_type type;
72     struct ovsdb_parser parser;
73     bool persistent;
74
75     *columnp = NULL;
76
77     ovsdb_parser_init(&parser, json, "schema for column %s", name);
78     comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL);
79     mutable = ovsdb_parser_member(&parser, "mutable",
80                                 OP_TRUE | OP_FALSE | OP_OPTIONAL);
81     ephemeral = ovsdb_parser_member(&parser, "ephemeral",
82                                     OP_TRUE | OP_FALSE | OP_OPTIONAL);
83     type_json = ovsdb_parser_member(&parser, "type", OP_STRING | OP_OBJECT);
84     error = ovsdb_parser_finish(&parser);
85     if (error) {
86         return error;
87     }
88
89     error = ovsdb_type_from_json(&type, type_json);
90     if (error) {
91         return error;
92     }
93
94     persistent = ephemeral ? !json_boolean(ephemeral) : true;
95     *columnp = ovsdb_column_create(name,
96                                    comment ? json_string(comment) : NULL,
97                                    mutable ? json_boolean(mutable) : true,
98                                    persistent, &type);
99
100     ovsdb_type_destroy(&type);
101
102     return NULL;
103 }
104
105 struct json *
106 ovsdb_column_to_json(const struct ovsdb_column *column)
107 {
108     struct json *json = json_object_create();
109     if (column->comment) {
110         json_object_put_string(json, "comment", column->comment);
111     }
112     if (!column->mutable) {
113         json_object_put(json, "mutable", json_boolean_create(false));
114     }
115     if (!column->persistent) {
116         json_object_put(json, "ephemeral", json_boolean_create(true));
117     }
118     json_object_put(json, "type", ovsdb_type_to_json(&column->type));
119     return json;
120 }
121 \f
122 void
123 ovsdb_column_set_init(struct ovsdb_column_set *set)
124 {
125     set->columns = NULL;
126     set->n_columns = set->allocated_columns = 0;
127 }
128
129 void
130 ovsdb_column_set_destroy(struct ovsdb_column_set *set)
131 {
132     free(set->columns);
133 }
134
135 void
136 ovsdb_column_set_clone(struct ovsdb_column_set *new,
137                        const struct ovsdb_column_set *old)
138 {
139     new->columns = xmemdup(old->columns,
140                            old->n_columns * sizeof *old->columns);
141     new->n_columns = new->allocated_columns = old->n_columns;
142 }
143
144 struct ovsdb_error *
145 ovsdb_column_set_from_json(const struct json *json,
146                            const struct ovsdb_table *table,
147                            struct ovsdb_column_set *set)
148 {
149     ovsdb_column_set_init(set);
150     if (!json) {
151         struct shash_node *node;
152
153         SHASH_FOR_EACH (node, &table->schema->columns) {
154             const struct ovsdb_column *column = node->data;
155             ovsdb_column_set_add(set, column);
156         }
157
158         return NULL;
159     } else {
160         struct ovsdb_error *error = NULL;
161         size_t i;
162
163         if (json->type != JSON_ARRAY) {
164             goto error;
165         }
166
167         /* XXX this is O(n**2) */
168         for (i = 0; i < json->u.array.n; i++) {
169             const struct ovsdb_column *column;
170             const char *s;
171
172             if (json->u.array.elems[i]->type != JSON_STRING) {
173                 goto error;
174             }
175
176             s = json->u.array.elems[i]->u.string;
177             column = shash_find_data(&table->schema->columns, s);
178             if (!column) {
179                 error = ovsdb_syntax_error(json, NULL, "%s is not a valid "
180                                            "column name", s);
181                 goto error;
182             } else if (ovsdb_column_set_contains(set, column->index)) {
183                 goto error;
184             }
185             ovsdb_column_set_add(set, column);
186         }
187         return NULL;
188
189     error:
190         ovsdb_column_set_destroy(set);
191         ovsdb_column_set_init(set);
192         if (!error) {
193             error = ovsdb_syntax_error(json, NULL, "array of distinct column "
194                                        "names expected");
195         }
196         return error;
197     }
198 }
199
200 struct json *
201 ovsdb_column_set_to_json(const struct ovsdb_column_set *set)
202 {
203     struct json *json;
204     size_t i;
205
206     json = json_array_create_empty();
207     for (i = 0; i < set->n_columns; i++) {
208         json_array_add(json, json_string_create(set->columns[i]->name));
209     }
210     return json;
211 }
212
213 void
214 ovsdb_column_set_add(struct ovsdb_column_set *set,
215                      const struct ovsdb_column *column)
216 {
217     if (set->n_columns >= set->allocated_columns) {
218         set->columns = x2nrealloc(set->columns, &set->allocated_columns,
219                                   sizeof *set->columns);
220     }
221     set->columns[set->n_columns++] = column;
222 }
223
224 void
225 ovsdb_column_set_add_all(struct ovsdb_column_set *set,
226                          const struct ovsdb_table *table)
227 {
228     struct shash_node *node;
229
230     SHASH_FOR_EACH (node, &table->schema->columns) {
231         const struct ovsdb_column *column = node->data;
232         ovsdb_column_set_add(set, column);
233     }
234 }
235
236 bool
237 ovsdb_column_set_contains(const struct ovsdb_column_set *set,
238                           unsigned int column_index)
239 {
240     size_t i;
241
242     for (i = 0; i < set->n_columns; i++) {
243         if (set->columns[i]->index == column_index) {
244             return true;
245         }
246     }
247     return false;
248 }
249
250 /* This comparison is sensitive to ordering of columns within a set, but that's
251  * good: the only existing caller wants to make sure that hash values are
252  * comparable, which is only true if column ordering is the same. */
253 bool
254 ovsdb_column_set_equals(const struct ovsdb_column_set *a,
255                         const struct ovsdb_column_set *b)
256 {
257     size_t i;
258
259     if (a->n_columns != b->n_columns) {
260         return false;
261     }
262     for (i = 0; i < a->n_columns; i++) {
263         if (a->columns[i] != b->columns[i]) {
264             return false;
265         }
266     }
267     return true;
268 }