X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=ovsdb%2Fcolumn.c;h=26b7a0b372e2b0d86e8ad41826baec49f8caf9aa;hb=0ef165ecb57943e17a8ee8270df68ffb8d032e29;hp=1e8a2d09d6b75cde68ab00935fac4190bb6f3996;hpb=f85f8ebbfac946c19b3c6eb0f4170f579d0a4d25;p=sliver-openvswitch.git diff --git a/ovsdb/column.c b/ovsdb/column.c index 1e8a2d09d..26b7a0b37 100644 --- a/ovsdb/column.c +++ b/ovsdb/column.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2009 Nicira Networks +/* Copyright (c) 2009, 2010, 2011 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ #include #include "column.h" +#include "dynamic-string.h" #include "json.h" #include "ovsdb-error.h" #include "ovsdb-parser.h" @@ -27,27 +28,36 @@ #include "util.h" struct ovsdb_column * -ovsdb_column_create(const char *name, const char *comment, +ovsdb_column_create(const char *name, bool mutable, bool persistent, const struct ovsdb_type *type) { - struct ovsdb_column *ts; + /* Doesn't set the new column's 'index': the caller must do that. */ + struct ovsdb_column *column; - ts = xzalloc(sizeof *ts); - ts->name = xstrdup(name); - ts->comment = comment ? xstrdup(comment) : NULL; - ts->mutable = mutable; - ts->persistent = persistent; - ts->type = *type; + column = xzalloc(sizeof *column); + column->name = xstrdup(name); + column->mutable = mutable; + column->persistent = persistent; + ovsdb_type_clone(&column->type, type); - return ts; + return column; +} + +struct ovsdb_column * +ovsdb_column_clone(const struct ovsdb_column *old) +{ + /* Doesn't copy the column's 'index': the caller must do that. */ + return ovsdb_column_create(old->name, + old->mutable, old->persistent, + &old->type); } void ovsdb_column_destroy(struct ovsdb_column *column) { + ovsdb_type_destroy(&column->type); free(column->name); - free(column->comment); free(column); } @@ -55,7 +65,7 @@ struct ovsdb_error * ovsdb_column_from_json(const struct json *json, const char *name, struct ovsdb_column **columnp) { - const struct json *comment, *mutable, *ephemeral, *type_json; + const struct json *mutable, *ephemeral, *type_json; struct ovsdb_error *error; struct ovsdb_type type; struct ovsdb_parser parser; @@ -64,7 +74,6 @@ ovsdb_column_from_json(const struct json *json, const char *name, *columnp = NULL; ovsdb_parser_init(&parser, json, "schema for column %s", name); - comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL); mutable = ovsdb_parser_member(&parser, "mutable", OP_TRUE | OP_FALSE | OP_OPTIONAL); ephemeral = ovsdb_parser_member(&parser, "ephemeral", @@ -82,9 +91,11 @@ ovsdb_column_from_json(const struct json *json, const char *name, persistent = ephemeral ? !json_boolean(ephemeral) : true; *columnp = ovsdb_column_create(name, - comment ? json_string(comment) : NULL, mutable ? json_boolean(mutable) : true, persistent, &type); + + ovsdb_type_destroy(&type); + return NULL; } @@ -92,9 +103,6 @@ struct json * ovsdb_column_to_json(const struct ovsdb_column *column) { struct json *json = json_object_create(); - if (column->comment) { - json_object_put_string(json, "comment", column->comment); - } if (!column->mutable) { json_object_put(json, "mutable", json_boolean_create(false)); } @@ -129,20 +137,21 @@ ovsdb_column_set_clone(struct ovsdb_column_set *new, struct ovsdb_error * ovsdb_column_set_from_json(const struct json *json, - const struct ovsdb_table *table, + const struct ovsdb_table_schema *schema, struct ovsdb_column_set *set) { ovsdb_column_set_init(set); if (!json) { struct shash_node *node; - SHASH_FOR_EACH (node, &table->schema->columns) { + SHASH_FOR_EACH (node, &schema->columns) { const struct ovsdb_column *column = node->data; ovsdb_column_set_add(set, column); } return NULL; } else { + struct ovsdb_error *error = NULL; size_t i; if (json->type != JSON_ARRAY) { @@ -151,27 +160,69 @@ ovsdb_column_set_from_json(const struct json *json, /* XXX this is O(n**2) */ for (i = 0; i < json->u.array.n; i++) { - struct ovsdb_column *column; + const struct ovsdb_column *column; + const char *s; if (json->u.array.elems[i]->type != JSON_STRING) { goto error; } - column = shash_find_data(&table->schema->columns, - json->u.array.elems[i]->u.string); - if (ovsdb_column_set_contains(set, column->index)) { + s = json->u.array.elems[i]->u.string; + column = shash_find_data(&schema->columns, s); + if (!column) { + error = ovsdb_syntax_error(json, NULL, "%s is not a valid " + "column name", s); + goto error; + } else if (ovsdb_column_set_contains(set, column->index)) { goto error; } ovsdb_column_set_add(set, column); } - return NULL; + + error: + ovsdb_column_set_destroy(set); + ovsdb_column_set_init(set); + if (!error) { + error = ovsdb_syntax_error(json, NULL, "array of distinct column " + "names expected"); + } + return error; + } +} + +struct json * +ovsdb_column_set_to_json(const struct ovsdb_column_set *set) +{ + struct json *json; + size_t i; + + json = json_array_create_empty(); + for (i = 0; i < set->n_columns; i++) { + json_array_add(json, json_string_create(set->columns[i]->name)); } + return json; +} -error: - ovsdb_column_set_destroy(set); - return ovsdb_syntax_error(json, NULL, - "array of distinct column names expected"); +/* Returns an English string listing the contents of 'set', e.g. "columns + * \"a\", \"b\", and \"c\"". The caller must free the string. */ +char * +ovsdb_column_set_to_string(const struct ovsdb_column_set *set) +{ + if (!set->n_columns) { + return xstrdup("no columns"); + } else { + struct ds s; + size_t i; + + ds_init(&s); + ds_put_format(&s, "column%s ", set->n_columns > 1 ? "s" : ""); + for (i = 0; i < set->n_columns; i++) { + const char *delimiter = english_list_delimiter(i, set->n_columns); + ds_put_format(&s, "%s\"%s\"", delimiter, set->columns[i]->name); + } + return ds_steal_cstr(&s); + } } void