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.
24 #include "ovsdb-error.h"
29 static struct ovsdb_row *
30 allocate_row(const struct ovsdb_table *table)
32 size_t n_fields = shash_count(&table->schema->columns);
33 size_t row_size = (offsetof(struct ovsdb_row, fields)
34 + sizeof(struct ovsdb_datum) * n_fields);
35 struct ovsdb_row *row = xmalloc(row_size);
36 row->table = (struct ovsdb_table *) table;
38 list_init(&row->src_refs);
39 list_init(&row->dst_refs);
45 ovsdb_row_create(const struct ovsdb_table *table)
47 struct shash_node *node;
48 struct ovsdb_row *row;
50 row = allocate_row(table);
51 SHASH_FOR_EACH (node, &table->schema->columns) {
52 const struct ovsdb_column *column = node->data;
53 ovsdb_datum_init_default(&row->fields[column->index], &column->type);
59 ovsdb_row_clone(const struct ovsdb_row *old)
61 const struct ovsdb_table *table = old->table;
62 const struct shash_node *node;
63 struct ovsdb_row *new;
65 new = allocate_row(table);
66 SHASH_FOR_EACH (node, &table->schema->columns) {
67 const struct ovsdb_column *column = node->data;
68 ovsdb_datum_clone(&new->fields[column->index],
69 &old->fields[column->index],
75 /* The caller is responsible for ensuring that 'row' has been removed from its
76 * table and that it is not participating in a transaction. */
78 ovsdb_row_destroy(struct ovsdb_row *row)
81 const struct ovsdb_table *table = row->table;
82 struct ovsdb_weak_ref *weak, *next;
83 const struct shash_node *node;
85 LIST_FOR_EACH_SAFE (weak, next, struct ovsdb_weak_ref, dst_node,
87 list_remove(&weak->src_node);
88 list_remove(&weak->dst_node);
92 LIST_FOR_EACH_SAFE (weak, next, struct ovsdb_weak_ref, src_node,
94 list_remove(&weak->src_node);
95 list_remove(&weak->dst_node);
99 SHASH_FOR_EACH (node, &table->schema->columns) {
100 const struct ovsdb_column *column = node->data;
101 ovsdb_datum_destroy(&row->fields[column->index], &column->type);
108 ovsdb_row_hash_columns(const struct ovsdb_row *row,
109 const struct ovsdb_column_set *columns,
114 for (i = 0; i < columns->n_columns; i++) {
115 const struct ovsdb_column *column = columns->columns[i];
116 basis = ovsdb_datum_hash(&row->fields[column->index], &column->type,
124 ovsdb_row_compare_columns_3way(const struct ovsdb_row *a,
125 const struct ovsdb_row *b,
126 const struct ovsdb_column_set *columns)
130 for (i = 0; i < columns->n_columns; i++) {
131 const struct ovsdb_column *column = columns->columns[i];
132 int cmp = ovsdb_datum_compare_3way(&a->fields[column->index],
133 &b->fields[column->index],
144 ovsdb_row_equal_columns(const struct ovsdb_row *a,
145 const struct ovsdb_row *b,
146 const struct ovsdb_column_set *columns)
150 for (i = 0; i < columns->n_columns; i++) {
151 const struct ovsdb_column *column = columns->columns[i];
152 if (!ovsdb_datum_equals(&a->fields[column->index],
153 &b->fields[column->index],
163 ovsdb_row_update_columns(struct ovsdb_row *dst,
164 const struct ovsdb_row *src,
165 const struct ovsdb_column_set *columns)
169 for (i = 0; i < columns->n_columns; i++) {
170 const struct ovsdb_column *column = columns->columns[i];
171 ovsdb_datum_destroy(&dst->fields[column->index], &column->type);
172 ovsdb_datum_clone(&dst->fields[column->index],
173 &src->fields[column->index],
179 ovsdb_row_from_json(struct ovsdb_row *row, const struct json *json,
180 struct ovsdb_symbol_table *symtab,
181 struct ovsdb_column_set *included)
183 struct ovsdb_table_schema *schema = row->table->schema;
184 struct ovsdb_error *error;
185 struct shash_node *node;
187 if (json->type != JSON_OBJECT) {
188 return ovsdb_syntax_error(json, NULL, "row must be JSON object");
191 SHASH_FOR_EACH (node, json_object(json)) {
192 const char *column_name = node->name;
193 const struct ovsdb_column *column;
194 struct ovsdb_datum datum;
196 column = ovsdb_table_schema_get_column(schema, column_name);
198 return ovsdb_syntax_error(json, "unknown column",
199 "No column %s in table %s.",
200 column_name, schema->name);
203 error = ovsdb_datum_from_json(&datum, &column->type, node->data,
208 ovsdb_datum_swap(&row->fields[column->index], &datum);
209 ovsdb_datum_destroy(&datum, &column->type);
211 ovsdb_column_set_add(included, column);
219 put_json_column(struct json *object, const struct ovsdb_row *row,
220 const struct ovsdb_column *column)
222 json_object_put(object, column->name,
223 ovsdb_datum_to_json(&row->fields[column->index],
228 ovsdb_row_to_json(const struct ovsdb_row *row,
229 const struct ovsdb_column_set *columns)
234 json = json_object_create();
235 for (i = 0; i < columns->n_columns; i++) {
236 put_json_column(json, row, columns->columns[i]);
242 ovsdb_row_set_init(struct ovsdb_row_set *set)
245 set->n_rows = set->allocated_rows = 0;
249 ovsdb_row_set_destroy(struct ovsdb_row_set *set)
255 ovsdb_row_set_add_row(struct ovsdb_row_set *set, const struct ovsdb_row *row)
257 if (set->n_rows >= set->allocated_rows) {
258 set->rows = x2nrealloc(set->rows, &set->allocated_rows,
261 set->rows[set->n_rows++] = row;
265 ovsdb_row_set_to_json(const struct ovsdb_row_set *rows,
266 const struct ovsdb_column_set *columns)
268 struct json **json_rows;
271 json_rows = xmalloc(rows->n_rows * sizeof *json_rows);
272 for (i = 0; i < rows->n_rows; i++) {
273 json_rows[i] = ovsdb_row_to_json(rows->rows[i], columns);
275 return json_array_create(json_rows, rows->n_rows);
278 struct ovsdb_row_set_sort_cbdata {
279 struct ovsdb_row_set *set;
280 const struct ovsdb_column_set *columns;
284 ovsdb_row_set_sort_compare_cb(size_t a, size_t b, void *cbdata_)
286 struct ovsdb_row_set_sort_cbdata *cbdata = cbdata_;
287 return ovsdb_row_compare_columns_3way(cbdata->set->rows[a],
288 cbdata->set->rows[b],
293 ovsdb_row_set_sort_swap_cb(size_t a, size_t b, void *cbdata_)
295 struct ovsdb_row_set_sort_cbdata *cbdata = cbdata_;
296 const struct ovsdb_row *tmp = cbdata->set->rows[a];
297 cbdata->set->rows[a] = cbdata->set->rows[b];
298 cbdata->set->rows[b] = tmp;
302 ovsdb_row_set_sort(struct ovsdb_row_set *set,
303 const struct ovsdb_column_set *columns)
305 if (columns && columns->n_columns && set->n_rows > 1) {
306 struct ovsdb_row_set_sort_cbdata cbdata;
308 cbdata.columns = columns;
310 ovsdb_row_set_sort_compare_cb,
311 ovsdb_row_set_sort_swap_cb,
317 ovsdb_row_hash_init(struct ovsdb_row_hash *rh,
318 const struct ovsdb_column_set *columns)
320 hmap_init(&rh->rows);
321 ovsdb_column_set_clone(&rh->columns, columns);
325 ovsdb_row_hash_destroy(struct ovsdb_row_hash *rh, bool destroy_rows)
327 struct ovsdb_row_hash_node *node, *next;
329 HMAP_FOR_EACH_SAFE (node, next, struct ovsdb_row_hash_node, hmap_node,
331 hmap_remove(&rh->rows, &node->hmap_node);
333 ovsdb_row_destroy((struct ovsdb_row *) node->row);
337 hmap_destroy(&rh->rows);
338 ovsdb_column_set_destroy(&rh->columns);
342 ovsdb_row_hash_count(const struct ovsdb_row_hash *rh)
344 return hmap_count(&rh->rows);
348 ovsdb_row_hash_contains(const struct ovsdb_row_hash *rh,
349 const struct ovsdb_row *row)
351 size_t hash = ovsdb_row_hash_columns(row, &rh->columns, 0);
352 return ovsdb_row_hash_contains__(rh, row, hash);
355 /* Returns true if every row in 'b' has an equal row in 'a'. */
357 ovsdb_row_hash_contains_all(const struct ovsdb_row_hash *a,
358 const struct ovsdb_row_hash *b)
360 struct ovsdb_row_hash_node *node;
362 assert(ovsdb_column_set_equals(&a->columns, &b->columns));
363 HMAP_FOR_EACH (node, struct ovsdb_row_hash_node, hmap_node, &b->rows) {
364 if (!ovsdb_row_hash_contains__(a, node->row, node->hmap_node.hash)) {
372 ovsdb_row_hash_insert(struct ovsdb_row_hash *rh, const struct ovsdb_row *row)
374 size_t hash = ovsdb_row_hash_columns(row, &rh->columns, 0);
375 return ovsdb_row_hash_insert__(rh, row, hash);
379 ovsdb_row_hash_contains__(const struct ovsdb_row_hash *rh,
380 const struct ovsdb_row *row, size_t hash)
382 struct ovsdb_row_hash_node *node;
383 HMAP_FOR_EACH_WITH_HASH (node, struct ovsdb_row_hash_node, hmap_node,
385 if (ovsdb_row_equal_columns(row, node->row, &rh->columns)) {
393 ovsdb_row_hash_insert__(struct ovsdb_row_hash *rh, const struct ovsdb_row *row,
396 if (!ovsdb_row_hash_contains__(rh, row, hash)) {
397 struct ovsdb_row_hash_node *node = xmalloc(sizeof *node);
399 hmap_insert(&rh->rows, &node->hmap_node, hash);