1 /* Copyright (c) 2009 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;
42 ovsdb_row_create(const struct ovsdb_table *table)
44 struct shash_node *node;
45 struct ovsdb_row *row;
47 row = allocate_row(table);
48 SHASH_FOR_EACH (node, &table->schema->columns) {
49 const struct ovsdb_column *column = node->data;
50 ovsdb_datum_init_default(&row->fields[column->index], &column->type);
56 ovsdb_row_clone(const struct ovsdb_row *old)
58 const struct ovsdb_table *table = old->table;
59 const struct shash_node *node;
60 struct ovsdb_row *new;
62 new = allocate_row(table);
63 SHASH_FOR_EACH (node, &table->schema->columns) {
64 const struct ovsdb_column *column = node->data;
65 ovsdb_datum_clone(&new->fields[column->index],
66 &old->fields[column->index],
72 /* The caller is responsible for ensuring that 'row' has been removed from its
73 * table and that it is not participating in a transaction. */
75 ovsdb_row_destroy(struct ovsdb_row *row)
78 const struct ovsdb_table *table = row->table;
79 const struct shash_node *node;
81 SHASH_FOR_EACH (node, &table->schema->columns) {
82 const struct ovsdb_column *column = node->data;
83 ovsdb_datum_destroy(&row->fields[column->index], &column->type);
90 ovsdb_row_hash_columns(const struct ovsdb_row *row,
91 const struct ovsdb_column_set *columns,
96 for (i = 0; i < columns->n_columns; i++) {
97 const struct ovsdb_column *column = columns->columns[i];
98 basis = ovsdb_datum_hash(&row->fields[column->index], &column->type,
106 ovsdb_row_compare_columns_3way(const struct ovsdb_row *a,
107 const struct ovsdb_row *b,
108 const struct ovsdb_column_set *columns)
112 for (i = 0; i < columns->n_columns; i++) {
113 const struct ovsdb_column *column = columns->columns[i];
114 int cmp = ovsdb_datum_compare_3way(&a->fields[column->index],
115 &b->fields[column->index],
126 ovsdb_row_equal_columns(const struct ovsdb_row *a,
127 const struct ovsdb_row *b,
128 const struct ovsdb_column_set *columns)
132 for (i = 0; i < columns->n_columns; i++) {
133 const struct ovsdb_column *column = columns->columns[i];
134 if (!ovsdb_datum_equals(&a->fields[column->index],
135 &b->fields[column->index],
145 ovsdb_row_update_columns(struct ovsdb_row *dst,
146 const struct ovsdb_row *src,
147 const struct ovsdb_column_set *columns)
151 for (i = 0; i < columns->n_columns; i++) {
152 const struct ovsdb_column *column = columns->columns[i];
153 ovsdb_datum_destroy(&dst->fields[column->index], &column->type);
154 ovsdb_datum_clone(&dst->fields[column->index],
155 &src->fields[column->index],
161 ovsdb_row_from_json(struct ovsdb_row *row, const struct json *json,
162 const struct ovsdb_symbol_table *symtab,
163 struct ovsdb_column_set *included)
165 struct ovsdb_table_schema *schema = row->table->schema;
166 struct ovsdb_error *error;
167 struct shash_node *node;
169 if (json->type != JSON_OBJECT) {
170 return ovsdb_syntax_error(json, NULL, "row must be JSON object");
173 SHASH_FOR_EACH (node, json_object(json)) {
174 const char *column_name = node->name;
175 const struct ovsdb_column *column;
176 struct ovsdb_datum datum;
178 column = ovsdb_table_schema_get_column(schema, column_name);
180 return ovsdb_syntax_error(json, "unknown column",
181 "No column %s in table %s.",
182 column_name, schema->name);
185 error = ovsdb_datum_from_json(&datum, &column->type, node->data,
190 ovsdb_datum_swap(&row->fields[column->index], &datum);
191 ovsdb_datum_destroy(&datum, &column->type);
193 ovsdb_column_set_add(included, column);
201 put_json_column(struct json *object, const struct ovsdb_row *row,
202 const struct ovsdb_column *column)
204 json_object_put(object, column->name,
205 ovsdb_datum_to_json(&row->fields[column->index],
210 ovsdb_row_to_json(const struct ovsdb_row *row,
211 const struct ovsdb_column_set *columns)
216 json = json_object_create();
217 for (i = 0; i < columns->n_columns; i++) {
218 put_json_column(json, row, columns->columns[i]);
224 ovsdb_row_set_init(struct ovsdb_row_set *set)
227 set->n_rows = set->allocated_rows = 0;
231 ovsdb_row_set_destroy(struct ovsdb_row_set *set)
237 ovsdb_row_set_add_row(struct ovsdb_row_set *set, const struct ovsdb_row *row)
239 if (set->n_rows >= set->allocated_rows) {
240 set->rows = x2nrealloc(set->rows, &set->allocated_rows,
243 set->rows[set->n_rows++] = row;
247 ovsdb_row_set_to_json(const struct ovsdb_row_set *rows,
248 const struct ovsdb_column_set *columns)
250 struct json **json_rows;
253 json_rows = xmalloc(rows->n_rows * sizeof *json_rows);
254 for (i = 0; i < rows->n_rows; i++) {
255 json_rows[i] = ovsdb_row_to_json(rows->rows[i], columns);
257 return json_array_create(json_rows, rows->n_rows);
260 struct ovsdb_row_set_sort_cbdata {
261 struct ovsdb_row_set *set;
262 const struct ovsdb_column_set *columns;
266 ovsdb_row_set_sort_compare_cb(size_t a, size_t b, void *cbdata_)
268 struct ovsdb_row_set_sort_cbdata *cbdata = cbdata_;
269 return ovsdb_row_compare_columns_3way(cbdata->set->rows[a],
270 cbdata->set->rows[b],
275 ovsdb_row_set_sort_swap_cb(size_t a, size_t b, void *cbdata_)
277 struct ovsdb_row_set_sort_cbdata *cbdata = cbdata_;
278 const struct ovsdb_row *tmp = cbdata->set->rows[a];
279 cbdata->set->rows[a] = cbdata->set->rows[b];
280 cbdata->set->rows[b] = tmp;
284 ovsdb_row_set_sort(struct ovsdb_row_set *set,
285 const struct ovsdb_column_set *columns)
287 if (columns && columns->n_columns && set->n_rows > 1) {
288 struct ovsdb_row_set_sort_cbdata cbdata;
290 cbdata.columns = columns;
292 ovsdb_row_set_sort_compare_cb,
293 ovsdb_row_set_sort_swap_cb,
299 ovsdb_row_hash_init(struct ovsdb_row_hash *rh,
300 const struct ovsdb_column_set *columns)
302 hmap_init(&rh->rows);
303 ovsdb_column_set_clone(&rh->columns, columns);
307 ovsdb_row_hash_destroy(struct ovsdb_row_hash *rh, bool destroy_rows)
309 struct ovsdb_row_hash_node *node, *next;
311 HMAP_FOR_EACH_SAFE (node, next, struct ovsdb_row_hash_node, hmap_node,
313 hmap_remove(&rh->rows, &node->hmap_node);
315 ovsdb_row_destroy((struct ovsdb_row *) node->row);
319 hmap_destroy(&rh->rows);
320 ovsdb_column_set_destroy(&rh->columns);
324 ovsdb_row_hash_count(const struct ovsdb_row_hash *rh)
326 return hmap_count(&rh->rows);
330 ovsdb_row_hash_contains(const struct ovsdb_row_hash *rh,
331 const struct ovsdb_row *row)
333 size_t hash = ovsdb_row_hash_columns(row, &rh->columns, 0);
334 return ovsdb_row_hash_contains__(rh, row, hash);
337 /* Returns true if every row in 'b' has an equal row in 'a'. */
339 ovsdb_row_hash_contains_all(const struct ovsdb_row_hash *a,
340 const struct ovsdb_row_hash *b)
342 struct ovsdb_row_hash_node *node;
344 assert(ovsdb_column_set_equals(&a->columns, &b->columns));
345 HMAP_FOR_EACH (node, struct ovsdb_row_hash_node, hmap_node, &b->rows) {
346 if (!ovsdb_row_hash_contains__(a, node->row, node->hmap_node.hash)) {
354 ovsdb_row_hash_insert(struct ovsdb_row_hash *rh, const struct ovsdb_row *row)
356 size_t hash = ovsdb_row_hash_columns(row, &rh->columns, 0);
357 return ovsdb_row_hash_insert__(rh, row, hash);
361 ovsdb_row_hash_contains__(const struct ovsdb_row_hash *rh,
362 const struct ovsdb_row *row, size_t hash)
364 struct ovsdb_row_hash_node *node;
365 HMAP_FOR_EACH_WITH_HASH (node, struct ovsdb_row_hash_node, hmap_node,
367 if (ovsdb_row_equal_columns(row, node->row, &rh->columns)) {
375 ovsdb_row_hash_insert__(struct ovsdb_row_hash *rh, const struct ovsdb_row *row,
378 if (!ovsdb_row_hash_contains__(rh, row, hash)) {
379 struct ovsdb_row_hash_node *node = xmalloc(sizeof *node);
381 hmap_insert(&rh->rows, &node->hmap_node, hash);