X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=ovsdb%2Ftable.c;h=2ea73bf8a2150fb30e7d761f6eda55bf806207b9;hb=b2fda3effc787f265b5ad5dfa967ac00627bd075;hp=e15857be3d597bf960b9e247dc6fe3a04b527ef8;hpb=71c93bd4cc4d63be99cdc27ffee80c34f05bff75;p=sliver-openvswitch.git diff --git a/ovsdb/table.c b/ovsdb/table.c index e15857be3..2ea73bf8a 100644 --- a/ovsdb/table.c +++ b/ovsdb/table.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2009 Nicira Networks +/* Copyright (c) 2009, 2010, 2011 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ #include "table.h" #include +#include #include "json.h" #include "column.h" @@ -35,32 +36,51 @@ add_column(struct ovsdb_table_schema *ts, struct ovsdb_column *column) } struct ovsdb_table_schema * -ovsdb_table_schema_create(const char *name, const char *comment, bool mutable) +ovsdb_table_schema_create(const char *name, bool mutable, + unsigned int max_rows, bool is_root) { struct ovsdb_column *uuid, *version; struct ovsdb_table_schema *ts; ts = xzalloc(sizeof *ts); ts->name = xstrdup(name); - ts->comment = comment ? xstrdup(comment) : NULL; ts->mutable = mutable; shash_init(&ts->columns); + ts->max_rows = max_rows; + ts->is_root = is_root; - uuid = ovsdb_column_create( - "_uuid", "Unique identifier for this row.", - false, true, &ovsdb_type_uuid); + uuid = ovsdb_column_create("_uuid", false, true, &ovsdb_type_uuid); add_column(ts, uuid); assert(uuid->index == OVSDB_COL_UUID); - version = ovsdb_column_create( - "_version", "Unique identifier for this version of this row.", - false, false, &ovsdb_type_uuid); + version = ovsdb_column_create("_version", false, false, &ovsdb_type_uuid); add_column(ts, version); assert(version->index == OVSDB_COL_VERSION); return ts; } +struct ovsdb_table_schema * +ovsdb_table_schema_clone(const struct ovsdb_table_schema *old) +{ + struct ovsdb_table_schema *new; + struct shash_node *node; + + new = ovsdb_table_schema_create(old->name, old->mutable, + old->max_rows, old->is_root); + SHASH_FOR_EACH (node, &old->columns) { + const struct ovsdb_column *column = node->data; + + if (column->name[0] == '_') { + /* Added automatically by ovsdb_table_schema_create(). */ + continue; + } + + add_column(new, ovsdb_column_clone(column)); + } + return new; +} + void ovsdb_table_schema_destroy(struct ovsdb_table_schema *ts) { @@ -70,7 +90,6 @@ ovsdb_table_schema_destroy(struct ovsdb_table_schema *ts) ovsdb_column_destroy(node->data); } shash_destroy(&ts->columns); - free(ts->comment); free(ts->name); free(ts); } @@ -80,31 +99,45 @@ ovsdb_table_schema_from_json(const struct json *json, const char *name, struct ovsdb_table_schema **tsp) { struct ovsdb_table_schema *ts; - const struct json *comment, *columns, *mutable; + const struct json *columns, *mutable, *max_rows, *is_root; struct shash_node *node; struct ovsdb_parser parser; struct ovsdb_error *error; + long long int n_max_rows; *tsp = NULL; ovsdb_parser_init(&parser, json, "table schema for table %s", name); - comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL); columns = ovsdb_parser_member(&parser, "columns", OP_OBJECT); mutable = ovsdb_parser_member(&parser, "mutable", OP_TRUE | OP_FALSE | OP_OPTIONAL); + max_rows = ovsdb_parser_member(&parser, "maxRows", + OP_INTEGER | OP_OPTIONAL); + is_root = ovsdb_parser_member(&parser, "isRoot", OP_BOOLEAN | OP_OPTIONAL); error = ovsdb_parser_finish(&parser); if (error) { return error; } + if (max_rows) { + if (json_integer(max_rows) <= 0) { + return ovsdb_syntax_error(json, NULL, + "maxRows must be at least 1"); + } + n_max_rows = max_rows->u.integer; + } else { + n_max_rows = UINT_MAX; + } + if (shash_is_empty(json_object(columns))) { return ovsdb_syntax_error(json, NULL, "table must have at least one column"); } ts = ovsdb_table_schema_create(name, - comment ? json_string(comment) : NULL, - mutable ? json_boolean(mutable) : true); + mutable ? json_boolean(mutable) : true, + MIN(n_max_rows, UINT_MAX), + is_root ? json_boolean(is_root) : false); SHASH_FOR_EACH (node, json_object(columns)) { struct ovsdb_column *column; @@ -124,33 +157,47 @@ ovsdb_table_schema_from_json(const struct json *json, const char *name, add_column(ts, column); } *tsp = ts; - return 0; + return NULL; } +/* Returns table schema 'ts' serialized into JSON. + * + * The "isRoot" member is included in the JSON only if its value would differ + * from 'default_is_root'. Ordinarily 'default_is_root' should be false, + * because ordinarily a table would be not be part of the root set if its + * "isRoot" member is omitted. However, garbage collection was not orginally + * included in OVSDB, so in older schemas that do not include any "isRoot" + * members, every table is implicitly part of the root set. To serialize such + * a schema in a way that can be read by older OVSDB tools, specify + * 'default_is_root' as true. */ struct json * -ovsdb_table_schema_to_json(const struct ovsdb_table_schema *ts) +ovsdb_table_schema_to_json(const struct ovsdb_table_schema *ts, + bool default_is_root) { struct json *json, *columns; struct shash_node *node; json = json_object_create(); - if (ts->comment) { - json_object_put_string(json, "comment", ts->comment); - } if (!ts->mutable) { json_object_put(json, "mutable", json_boolean_create(false)); } + if (default_is_root != ts->is_root) { + json_object_put(json, "isRoot", json_boolean_create(ts->is_root)); + } columns = json_object_create(); SHASH_FOR_EACH (node, &ts->columns) { - struct ovsdb_column *column = node->data; + const struct ovsdb_column *column = node->data; if (node->name[0] != '_') { json_object_put(columns, column->name, ovsdb_column_to_json(column)); } } json_object_put(json, "columns", columns); + if (ts->max_rows != UINT_MAX) { + json_object_put(json, "maxRows", json_integer_create(ts->max_rows)); + } return json; } @@ -181,8 +228,7 @@ ovsdb_table_destroy(struct ovsdb_table *table) if (table) { struct ovsdb_row *row, *next; - HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_row, hmap_node, - &table->rows) { + HMAP_FOR_EACH_SAFE (row, next, hmap_node, &table->rows) { ovsdb_row_destroy(row); } hmap_destroy(&table->rows); @@ -192,14 +238,12 @@ ovsdb_table_destroy(struct ovsdb_table *table) } } -static const struct ovsdb_row * -ovsdb_table_get_row__(const struct ovsdb_table *table, const struct uuid *uuid, - size_t hash) +const struct ovsdb_row * +ovsdb_table_get_row(const struct ovsdb_table *table, const struct uuid *uuid) { struct ovsdb_row *row; - HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_row, hmap_node, hash, - &table->rows) { + HMAP_FOR_EACH_WITH_HASH (row, hmap_node, uuid_hash(uuid), &table->rows) { if (uuid_equals(ovsdb_row_get_uuid(row), uuid)) { return row; } @@ -208,22 +252,14 @@ ovsdb_table_get_row__(const struct ovsdb_table *table, const struct uuid *uuid, return NULL; } -const struct ovsdb_row * -ovsdb_table_get_row(const struct ovsdb_table *table, const struct uuid *uuid) -{ - return ovsdb_table_get_row__(table, uuid, uuid_hash(uuid)); -} - /* This is probably not the function you want. Use ovsdb_txn_row_modify() * instead. */ bool ovsdb_table_put_row(struct ovsdb_table *table, struct ovsdb_row *row) { const struct uuid *uuid = ovsdb_row_get_uuid(row); - size_t hash = uuid_hash(uuid); - - if (!ovsdb_table_get_row__(table, uuid, hash)) { - hmap_insert(&table->rows, &row->hmap_node, hash); + if (!ovsdb_table_get_row(table, uuid)) { + hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid)); return true; } else { return false;