Merge "master" branch into "db".
[cascardo/ovs.git] / ovsdb / table.c
1 /* Copyright (c) 2009 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 "table.h"
19
20 #include <assert.h>
21
22 #include "json.h"
23 #include "column.h"
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
26 #include "ovsdb-types.h"
27 #include "row.h"
28
29 static void
30 add_column(struct ovsdb_table_schema *ts, struct ovsdb_column *column)
31 {
32     assert(!shash_find(&ts->columns, column->name));
33     column->index = shash_count(&ts->columns);
34     shash_add(&ts->columns, column->name, column);
35 }
36
37 struct ovsdb_table_schema *
38 ovsdb_table_schema_create(const char *name, const char *comment, bool mutable)
39 {
40     struct ovsdb_column *uuid, *version;
41     struct ovsdb_table_schema *ts;
42
43     ts = xzalloc(sizeof *ts);
44     ts->name = xstrdup(name);
45     ts->comment = comment ? xstrdup(comment) : NULL;
46     ts->mutable = mutable;
47     shash_init(&ts->columns);
48
49     uuid = ovsdb_column_create(
50         "_uuid", "Unique identifier for this row.",
51         false, true, &ovsdb_type_uuid);
52     add_column(ts, uuid);
53     assert(uuid->index == OVSDB_COL_UUID);
54
55     version = ovsdb_column_create(
56         "_version", "Unique identifier for this version of this row.",
57         false, false, &ovsdb_type_uuid);
58     add_column(ts, version);
59     assert(version->index == OVSDB_COL_VERSION);
60
61     return ts;
62 }
63
64 void
65 ovsdb_table_schema_destroy(struct ovsdb_table_schema *ts)
66 {
67     struct shash_node *node;
68
69     SHASH_FOR_EACH (node, &ts->columns) {
70         ovsdb_column_destroy(node->data);
71     }
72     shash_destroy(&ts->columns);
73     free(ts->comment);
74     free(ts->name);
75     free(ts);
76 }
77
78 struct ovsdb_error *
79 ovsdb_table_schema_from_json(const struct json *json, const char *name,
80                              struct ovsdb_table_schema **tsp)
81 {
82     struct ovsdb_table_schema *ts;
83     const struct json *comment, *columns, *mutable;
84     struct shash_node *node;
85     struct ovsdb_parser parser;
86     struct ovsdb_error *error;
87
88     *tsp = NULL;
89
90     ovsdb_parser_init(&parser, json, "table schema for table %s", name);
91     comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL);
92     columns = ovsdb_parser_member(&parser, "columns", OP_OBJECT);
93     mutable = ovsdb_parser_member(&parser, "mutable",
94                                   OP_TRUE | OP_FALSE | OP_OPTIONAL);
95     error = ovsdb_parser_finish(&parser);
96     if (error) {
97         return error;
98     }
99
100     if (shash_is_empty(json_object(columns))) {
101         return ovsdb_syntax_error(json, NULL,
102                                   "table must have at least one column");
103     }
104
105     ts = ovsdb_table_schema_create(name,
106                                    comment ? json_string(comment) : NULL,
107                                    mutable ? json_boolean(mutable) : true);
108     SHASH_FOR_EACH (node, json_object(columns)) {
109         struct ovsdb_column *column;
110
111         if (node->name[0] == '_') {
112             error = ovsdb_syntax_error(json, NULL, "names beginning with "
113                                        "\"_\" are reserved");
114         } else if (!ovsdb_parser_is_id(node->name)) {
115             error = ovsdb_syntax_error(json, NULL, "name must be a valid id");
116         } else {
117             error = ovsdb_column_from_json(node->data, node->name, &column);
118         }
119         if (error) {
120             ovsdb_table_schema_destroy(ts);
121             return error;
122         }
123
124         add_column(ts, column);
125     }
126     *tsp = ts;
127     return 0;
128 }
129
130 struct json *
131 ovsdb_table_schema_to_json(const struct ovsdb_table_schema *ts)
132 {
133     struct json *json, *columns;
134     struct shash_node *node;
135
136     json = json_object_create();
137     if (ts->comment) {
138         json_object_put_string(json, "comment", ts->comment);
139     }
140     if (!ts->mutable) {
141         json_object_put(json, "mutable", json_boolean_create(false));
142     }
143
144     columns = json_object_create();
145
146     SHASH_FOR_EACH (node, &ts->columns) {
147         struct ovsdb_column *column = node->data;
148         if (node->name[0] != '_') {
149             json_object_put(columns, column->name,
150                             ovsdb_column_to_json(column));
151         }
152     }
153     json_object_put(json, "columns", columns);
154
155     return json;
156 }
157
158 const struct ovsdb_column *
159 ovsdb_table_schema_get_column(const struct ovsdb_table_schema *ts,
160                               const char *name)
161 {
162     return shash_find_data(&ts->columns, name);
163 }
164 \f
165 struct ovsdb_table *
166 ovsdb_table_create(struct ovsdb_table_schema *ts)
167 {
168     struct ovsdb_table *table;
169
170     table = xmalloc(sizeof *table);
171     table->schema = ts;
172     hmap_init(&table->rows);
173
174     return table;
175 }
176
177 void
178 ovsdb_table_destroy(struct ovsdb_table *table)
179 {
180     if (table) {
181         struct ovsdb_row *row, *next;
182
183         HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_row, hmap_node,
184                             &table->rows) {
185             ovsdb_row_destroy(row);
186         }
187         hmap_destroy(&table->rows);
188
189         ovsdb_table_schema_destroy(table->schema);
190         free(table);
191     }
192 }
193
194 static const struct ovsdb_row *
195 ovsdb_table_get_row__(const struct ovsdb_table *table, const struct uuid *uuid,
196                       size_t hash)
197 {
198     struct ovsdb_row *row;
199
200     HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_row, hmap_node, hash,
201                              &table->rows) {
202         if (uuid_equals(ovsdb_row_get_uuid(row), uuid)) {
203             return row;
204         }
205     }
206
207     return NULL;
208 }
209
210 const struct ovsdb_row *
211 ovsdb_table_get_row(const struct ovsdb_table *table, const struct uuid *uuid)
212 {
213     return ovsdb_table_get_row__(table, uuid, uuid_hash(uuid));
214 }
215
216 /* This is probably not the function you want.  Use ovsdb_txn_row_modify()
217  * instead. */
218 bool
219 ovsdb_table_put_row(struct ovsdb_table *table, struct ovsdb_row *row)
220 {
221     const struct uuid *uuid = ovsdb_row_get_uuid(row);
222     size_t hash = uuid_hash(uuid);
223
224     if (!ovsdb_table_get_row__(table, uuid, hash)) {
225         hmap_insert(&table->rows, &row->hmap_node, hash);
226         return true;
227     } else {
228         return false;
229     }
230 }