-/* Copyright (c) 2009, 2010 Nicira Networks
+/* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "ovsdb-data.h"
-#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include "dynamic-string.h"
#include "hash.h"
+#include "ovs-thread.h"
#include "ovsdb-error.h"
+#include "ovsdb-parser.h"
#include "json.h"
#include "shash.h"
+#include "smap.h"
#include "sort.h"
#include "unicode.h"
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
atom->integer = 0;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
ovsdb_atom_default(enum ovsdb_atomic_type type)
{
static union ovsdb_atom default_atoms[OVSDB_N_TYPES];
- static bool inited;
+ static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
- if (!inited) {
+ if (ovsthread_once_start(&once)) {
int i;
for (i = 0; i < OVSDB_N_TYPES; i++) {
ovsdb_atom_init_default(&default_atoms[i], i);
}
}
- inited = true;
+ ovsthread_once_done(&once);
}
- assert(ovsdb_atomic_type_is_valid(type));
+ ovs_assert(ovsdb_atomic_type_is_valid(type));
return &default_atoms[type];
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return atom->integer == 0;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
new->integer = old->integer;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return hash_int(atom->integer, basis);
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return a->integer < b->integer ? -1 : a->integer > b->integer;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
|| (name && strcmp(json->u.array.elems[0]->u.string, name))
|| json->u.array.elems[1]->type != value_type)
{
+ *value = NULL;
return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
json_type_to_string(value_type));
}
return NULL;
}
+static void
+ovsdb_symbol_referenced(struct ovsdb_symbol *symbol,
+ const struct ovsdb_base_type *base)
+{
+ ovs_assert(base->type == OVSDB_TYPE_UUID);
+
+ if (base->u.uuid.refTableName) {
+ switch (base->u.uuid.refType) {
+ case OVSDB_REF_STRONG:
+ symbol->strong_ref = true;
+ break;
+ case OVSDB_REF_WEAK:
+ symbol->weak_ref = true;
+ break;
+ }
+ }
+}
+
static struct ovsdb_error * WARN_UNUSED_RESULT
ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
- struct ovsdb_symbol_table *symtab)
+ struct ovsdb_symbol_table *symtab,
+ const struct ovsdb_base_type *base)
{
struct ovsdb_error *error0;
const struct json *value;
error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
if (!error1) {
- const char *name = json_string(value);
+ struct ovsdb_symbol *symbol;
ovsdb_error_destroy(error0);
- *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
+ if (!ovsdb_parser_is_id(json_string(value))) {
+ return ovsdb_syntax_error(json, NULL, "named-uuid string is "
+ "not a valid <id>");
+ }
+
+ symbol = ovsdb_symbol_table_insert(symtab, json_string(value));
+ *uuid = symbol->uuid;
+ ovsdb_symbol_referenced(symbol, base);
return NULL;
}
ovsdb_error_destroy(error1);
}
static struct ovsdb_error * WARN_UNUSED_RESULT
-ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
+ovsdb_atom_from_json__(union ovsdb_atom *atom,
+ const struct ovsdb_base_type *base,
const struct json *json,
struct ovsdb_symbol_table *symtab)
{
+ enum ovsdb_atomic_type type = base->type;
+
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
if (json->type == JSON_INTEGER) {
break;
case OVSDB_TYPE_UUID:
- return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab);
+ return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab, base);
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
return ovsdb_syntax_error(json, NULL, "expected %s",
*
* If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
* ovsdb/SPECS for information about this, and for the syntax that this
- * function accepts. */
+ * function accepts. If 'base' is a reference and a symbol is parsed, then the
+ * symbol's 'strong_ref' or 'weak_ref' member is set to true, as
+ * appropriate. */
struct ovsdb_error *
ovsdb_atom_from_json(union ovsdb_atom *atom,
const struct ovsdb_base_type *base,
{
struct ovsdb_error *error;
- error = ovsdb_atom_from_json__(atom, base->type, json, symtab);
+ error = ovsdb_atom_from_json__(atom, base, json, symtab);
if (error) {
return error;
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return json_integer_create(atom->integer);
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
+/* Returns strlen(json_to_string(ovsdb_atom_to_json(atom, type), 0)). */
+size_t
+ovsdb_atom_json_length(const union ovsdb_atom *atom,
+ enum ovsdb_atomic_type type)
+{
+ struct json json;
+
+ switch (type) {
+ case OVSDB_TYPE_VOID:
+ OVS_NOT_REACHED();
+
+ case OVSDB_TYPE_INTEGER:
+ json.type = JSON_INTEGER;
+ json.u.integer = atom->integer;
+ break;
+
+ case OVSDB_TYPE_REAL:
+ json.type = JSON_REAL;
+ json.u.real = atom->real;
+ break;
+
+ case OVSDB_TYPE_BOOLEAN:
+ json.type = atom->boolean ? JSON_TRUE : JSON_FALSE;
+ break;
+
+ case OVSDB_TYPE_STRING:
+ json.type = JSON_STRING;
+ json.u.string = atom->string;
+ break;
+
+ case OVSDB_TYPE_UUID:
+ return strlen("[\"uuid\",\"00000000-0000-0000-0000-000000000000\"]");
+
+ case OVSDB_N_TYPES:
+ default:
+ OVS_NOT_REACHED();
+ }
+
+ return json_serialized_length(&json);
+}
+
static char *
-ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
- const char *s, struct ovsdb_symbol_table *symtab)
+ovsdb_atom_from_string__(union ovsdb_atom *atom,
+ const struct ovsdb_base_type *base, const char *s,
+ struct ovsdb_symbol_table *symtab)
{
+ enum ovsdb_atomic_type type = base->type;
+
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER: {
long long int integer;
case OVSDB_TYPE_UUID:
if (*s == '@') {
- atom->uuid = ovsdb_symbol_table_insert(symtab, s)->uuid;
+ struct ovsdb_symbol *symbol = ovsdb_symbol_table_insert(symtab, s);
+ atom->uuid = symbol->uuid;
+ ovsdb_symbol_referenced(symbol, base);
} else if (!uuid_from_string(&atom->uuid, s)) {
return xasprintf("\"%s\" is not a valid UUID", s);
}
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
return NULL;
* then an identifier beginning with '@' is also acceptable. If the
* named identifier is already in 'symtab', then the associated UUID is
* used; otherwise, a new, random UUID is used and added to the symbol
- * table.
+ * table. If 'base' is a reference and a symbol is parsed, then the
+ * symbol's 'strong_ref' or 'weak_ref' member is set to true, as
+ * appropriate.
*
* Returns a null pointer if successful, otherwise an error message describing
* the problem. On failure, the contents of 'atom' are indeterminate. The
struct ovsdb_error *error;
char *msg;
- msg = ovsdb_atom_from_string__(atom, base->type, s, symtab);
+ msg = ovsdb_atom_from_string__(atom, base, s, symtab);
if (msg) {
return msg;
}
error = ovsdb_atom_check_constraints(atom, base);
if (error) {
+ ovsdb_atom_destroy(atom, base->type);
msg = ovsdb_error_to_string(error);
ovsdb_error_destroy(error);
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
ds_put_format(out, "%"PRId64, atom->integer);
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
+ }
+}
+
+/* Appends 'atom' (which has the given 'type') to 'out', in a bare string
+ * format that cannot be parsed uniformly back into a datum but is easier for
+ * shell scripts, etc., to deal with. */
+void
+ovsdb_atom_to_bare(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
+ struct ds *out)
+{
+ if (type == OVSDB_TYPE_STRING) {
+ ds_put_cstr(out, atom->string);
+ } else {
+ ovsdb_atom_to_string(atom, type, out);
}
}
struct ovsdb_error *error;
error = ovsdb_error("constraint violation",
- "\"%s\" is not a valid UTF-8 string: %s",
- s, msg);
+ "not a valid UTF-8 string: %s", msg);
free(msg);
return error;
}
if (n_chars < c->minLen) {
return ovsdb_error(
"constraint violation",
- "\"%s\" length %zu is less than minimum allowed "
+ "\"%s\" length %"PRIuSIZE" is less than minimum allowed "
"length %u", s, n_chars, c->minLen);
} else if (n_chars > c->maxLen) {
return ovsdb_error(
"constraint violation",
- "\"%s\" length %zu is greater than maximum allowed "
+ "\"%s\" length %"PRIuSIZE" is greater than maximum allowed "
"length %u", s, n_chars, c->maxLen);
}
switch (base->type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
if (atom->integer >= base->u.integer.min
"value %"PRId64,
atom->integer, base->u.integer.max);
}
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_REAL:
if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
DBL_DIG, atom->real,
DBL_DIG, base->u.real.max);
}
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_BOOLEAN:
return NULL;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
\f
int kt = type->key.type;
int vt = type->value.type;
- assert(ovsdb_type_is_valid(type));
+ ovs_assert(ovsdb_type_is_valid(type));
d = &default_data[kt][vt];
if (!d->n) {
d->n = 1;
- d->keys = (union ovsdb_atom *) ovsdb_atom_default(kt);
+ d->keys = CONST_CAST(union ovsdb_atom *, ovsdb_atom_default(kt));
if (vt != OVSDB_TYPE_VOID) {
- d->values = (union ovsdb_atom *) ovsdb_atom_default(vt);
+ d->values = CONST_CAST(union ovsdb_atom *,
+ ovsdb_atom_default(vt));
}
}
return d;
} else {
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
{
struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
if (error) {
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
n = inner->u.array.n;
if (n < type->n_min || n > type->n_max) {
return ovsdb_syntax_error(json, NULL, "%s must have %u to "
- "%u members but %zu are present",
+ "%u members but %"PRIuSIZE" are present",
class, type->n_min, type->n_max, n);
}
return error;
}
-/* This is the same as ovsdb_datum_from_json(), except that duplicate values
- * in a set or map are dropped instead of being treated as an error. */
-struct ovsdb_error *
-ovsdb_datum_from_json_unique(struct ovsdb_datum *datum,
- const struct ovsdb_type *type,
- const struct json *json,
- struct ovsdb_symbol_table *symtab)
-{
- struct ovsdb_error *error;
-
- error = ovsdb_datum_from_json__(datum, type, json, symtab);
- if (!error) {
- ovsdb_datum_sort_unique(datum, type->key.type, type->value.type);
- }
- return error;
-}
-
/* Converts 'datum', of the specified 'type', to JSON format, and returns the
* JSON. The caller is responsible for freeing the returned JSON.
*
ovsdb_datum_to_json(const struct ovsdb_datum *datum,
const struct ovsdb_type *type)
{
- if (datum->n == 1 && !ovsdb_type_is_map(type)) {
- return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
- } else if (type->value.type == OVSDB_TYPE_VOID) {
+ if (ovsdb_type_is_map(type)) {
struct json **elems;
size_t i;
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
- elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
+ elems[i] = json_array_create_2(
+ ovsdb_atom_to_json(&datum->keys[i], type->key.type),
+ ovsdb_atom_to_json(&datum->values[i], type->value.type));
}
- return wrap_json("set", json_array_create(elems, datum->n));
+ return wrap_json("map", json_array_create(elems, datum->n));
+ } else if (datum->n == 1) {
+ return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
} else {
struct json **elems;
size_t i;
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
- elems[i] = json_array_create_2(
- ovsdb_atom_to_json(&datum->keys[i], type->key.type),
- ovsdb_atom_to_json(&datum->values[i], type->value.type));
+ elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
}
- return wrap_json("map", json_array_create(elems, datum->n));
+ return wrap_json("set", json_array_create(elems, datum->n));
+ }
+}
+
+/* Returns strlen(json_to_string(ovsdb_datum_to_json(datum, type), 0)). */
+size_t
+ovsdb_datum_json_length(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type)
+{
+ if (ovsdb_type_is_map(type)) {
+ size_t length;
+
+ /* ["map",[...]]. */
+ length = 10;
+ if (datum->n > 0) {
+ size_t i;
+
+ /* Commas between pairs in the inner [...] */
+ length += datum->n - 1;
+
+ /* [,] in each pair. */
+ length += datum->n * 3;
+
+ /* Data. */
+ for (i = 0; i < datum->n; i++) {
+ length += ovsdb_atom_json_length(&datum->keys[i],
+ type->key.type);
+ length += ovsdb_atom_json_length(&datum->values[i],
+ type->value.type);
+ }
+ }
+ return length;
+ } else if (datum->n == 1) {
+ return ovsdb_atom_json_length(&datum->keys[0], type->key.type);
+ } else {
+ size_t length;
+ size_t i;
+
+ /* ["set",[...]]. */
+ length = 10;
+ if (datum->n > 0) {
+ /* Commas between elements in the inner [...]. */
+ length += datum->n - 1;
+
+ /* Data. */
+ for (i = 0; i < datum->n; i++) {
+ length += ovsdb_atom_json_length(&datum->keys[i],
+ type->key.type);
+ }
+ }
+ return length;
}
}
union ovsdb_atom key, value;
if (ovsdb_token_is_delim(*p)) {
+ char *type_str = ovsdb_type_to_english(type);
error = xasprintf("%s: unexpected \"%c\" parsing %s",
- s, *p, ovsdb_type_to_english(type));
+ s, *p, type_str);
+ free(type_str);
goto error;
}
}
}
+/* Appends to 'out' the 'datum' (with the given 'type') in a bare string format
+ * that cannot be parsed uniformly back into a datum but is easier for shell
+ * scripts, etc., to deal with. */
+void
+ovsdb_datum_to_bare(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type, struct ds *out)
+{
+ bool is_map = ovsdb_type_is_map(type);
+ size_t i;
+
+ for (i = 0; i < datum->n; i++) {
+ if (i > 0) {
+ ds_put_cstr(out, " ");
+ }
+
+ ovsdb_atom_to_bare(&datum->keys[i], type->key.type, out);
+ if (is_map) {
+ ds_put_char(out, '=');
+ ovsdb_atom_to_bare(&datum->values[i], type->value.type, out);
+ }
+ }
+}
+
+/* Initializes 'datum' as a string-to-string map whose contents are taken from
+ * 'smap'. Destroys 'smap'. */
+void
+ovsdb_datum_from_smap(struct ovsdb_datum *datum, struct smap *smap)
+{
+ struct smap_node *node, *next;
+ size_t i;
+
+ datum->n = smap_count(smap);
+ datum->keys = xmalloc(datum->n * sizeof *datum->keys);
+ datum->values = xmalloc(datum->n * sizeof *datum->values);
+
+ i = 0;
+ SMAP_FOR_EACH_SAFE (node, next, smap) {
+ smap_steal(smap, node,
+ &datum->keys[i].string, &datum->values[i].string);
+ i++;
+ }
+ ovs_assert(i == datum->n);
+
+ smap_destroy(smap);
+ ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
+}
+
static uint32_t
hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
unsigned int n, uint32_t basis)
{
size_t i;
+ if (a->n > b->n) {
+ return false;
+ }
for (i = 0; i < a->n; i++) {
if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
return false;
struct ovsdb_error *error;
a->n = n;
error = ovsdb_datum_sort(a, type->key.type);
- assert(!error);
+ ovs_assert(!error);
}
}
bool changed = false;
size_t i;
- assert(a_type->key.type == b_type->key.type);
- assert(a_type->value.type == b_type->value.type
- || b_type->value.type == OVSDB_TYPE_VOID);
+ ovs_assert(a_type->key.type == b_type->key.type);
+ ovs_assert(a_type->value.type == b_type->value.type
+ || b_type->value.type == OVSDB_TYPE_VOID);
/* XXX The big-O of this could easily be improved. */
for (i = 0; i < a->n; ) {
}
}
\f
-struct ovsdb_symbol_table {
- struct shash sh;
-};
-
struct ovsdb_symbol_table *
ovsdb_symbol_table_create(void)
{
struct ovsdb_symbol *
ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
- const struct uuid *uuid, bool used)
+ const struct uuid *uuid, bool created)
{
struct ovsdb_symbol *symbol;
- assert(!ovsdb_symbol_table_get(symtab, name));
+ ovs_assert(!ovsdb_symbol_table_get(symtab, name));
symbol = xmalloc(sizeof *symbol);
symbol->uuid = *uuid;
- symbol->used = used;
+ symbol->created = created;
+ symbol->strong_ref = false;
+ symbol->weak_ref = false;
shash_add(&symtab->sh, name, symbol);
return symbol;
}
}
return symbol;
}
-
-const char *
-ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *symtab)
-{
- struct shash_node *node;
-
- SHASH_FOR_EACH (node, &symtab->sh) {
- struct ovsdb_symbol *symbol = node->data;
- if (!symbol->used) {
- return node->name;
- }
- }
-
- return NULL;
-}
\f
/* Extracts a token from the beginning of 's' and returns a pointer just after
* the token. Stores the token itself into '*outp', which the caller is