return NULL;
}
-static struct ovsdb_error *
-ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
- const struct ovsdb_symbol_table *symtab)
- WARN_UNUSED_RESULT;
-
-static struct ovsdb_error *
+static struct ovsdb_error * WARN_UNUSED_RESULT
ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
- const struct ovsdb_symbol_table *symtab)
+ struct ovsdb_symbol_table *symtab)
{
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);
- const struct ovsdb_symbol *symbol;
ovsdb_error_destroy(error0);
-
- symbol = ovsdb_symbol_table_get(symtab, name);
- if (symbol) {
- *uuid = symbol->uuid;
- return NULL;
- } else {
- return ovsdb_syntax_error(json, NULL,
- "unknown named-uuid \"%s\"", name);
- }
+ *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
+ 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,
const struct json *json,
- const struct ovsdb_symbol_table *symtab)
+ struct ovsdb_symbol_table *symtab)
{
switch (type) {
case OVSDB_TYPE_VOID:
ovsdb_atom_from_json(union ovsdb_atom *atom,
const struct ovsdb_base_type *base,
const struct json *json,
- const struct ovsdb_symbol_table *symtab)
+ struct ovsdb_symbol_table *symtab)
{
struct ovsdb_error *error;
static char *
ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
- const char *s)
+ const char *s, struct ovsdb_symbol_table *symtab)
{
switch (type) {
case OVSDB_TYPE_VOID:
break;
case OVSDB_TYPE_UUID:
- if (!uuid_from_string(&atom->uuid, s)) {
+ if (*s == '@') {
+ atom->uuid = ovsdb_symbol_table_insert(symtab, s)->uuid;
+ } else if (!uuid_from_string(&atom->uuid, s)) {
return xasprintf("\"%s\" is not a valid UUID", s);
}
break;
* - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
* an arbitrary string.
*
- * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format.
+ * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. If 'symtab' is nonnull,
+ * 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.
*
* Returns a null pointer if successful, otherwise an error message describing
* the problem. The caller is responsible for freeing the error.
*/
char *
ovsdb_atom_from_string(union ovsdb_atom *atom,
- const struct ovsdb_base_type *base, const char *s)
+ const struct ovsdb_base_type *base, const char *s,
+ struct ovsdb_symbol_table *symtab)
{
struct ovsdb_error *error;
char *msg;
- msg = ovsdb_atom_from_string__(atom, base->type, s);
+ msg = ovsdb_atom_from_string__(atom, base->type, s, symtab);
if (msg) {
return msg;
}
"length %u", s, n_chars, c->maxLen);
}
-#if HAVE_PCRE
- if (c->re) {
- int retval;
-
- retval = pcre_exec(c->re, NULL, s, strlen(s), 0,
- PCRE_ANCHORED | PCRE_NO_UTF8_CHECK, NULL, 0);
- if (retval == PCRE_ERROR_NOMATCH) {
- if (c->reComment) {
- return ovsdb_error("constraint violation",
- "\"%s\" is not a %s", s, c->reComment);
- } else {
- return ovsdb_error("constraint violation",
- "\"%s\" does not match regular expression "
- "/%s/", s, c->reMatch);
- }
- } else if (retval < 0) {
- /* PCRE doesn't have a function to translate an error code to a
- * description. Bizarre. See pcreapi(3) for error details. */
- return ovsdb_error("internal error", "PCRE returned error %d",
- retval);
- }
- }
-#endif /* HAVE_PCRE */
-
return NULL;
}
/* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
* (base->type must specify 'atom''s type.) Returns a null pointer if the
- * constraints are met, otherwise an error that explains the violation. */
+ * constraints are met, otherwise an error that explains the violation.
+ *
+ * Checking UUID constraints is deferred to transaction commit time, so this
+ * function does nothing for UUID constraints. */
struct ovsdb_error *
ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
const struct ovsdb_base_type *base)
{
+ if (base->enum_
+ && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {
+ struct ovsdb_error *error;
+ struct ds actual = DS_EMPTY_INITIALIZER;
+ struct ds valid = DS_EMPTY_INITIALIZER;
+
+ ovsdb_atom_to_string(atom, base->type, &actual);
+ ovsdb_datum_to_string(base->enum_,
+ ovsdb_base_type_get_enum_type(base->type),
+ &valid);
+ error = ovsdb_error("constraint violation",
+ "%s is not one of the allowed values (%s)",
+ ds_cstr(&actual), ds_cstr(&valid));
+ ds_destroy(&actual);
+ ds_destroy(&valid);
+
+ return error;
+ }
+
switch (base->type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
}
struct ovsdb_datum_sort_cbdata {
- const struct ovsdb_type *type;
+ enum ovsdb_atomic_type key_type;
struct ovsdb_datum *datum;
};
return ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
&cbdata->datum->keys[b],
- cbdata->type->key.type);
+ cbdata->key_type);
}
static void
struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
- if (cbdata->type->value.type != OVSDB_TYPE_VOID) {
+ if (cbdata->datum->values) {
ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
}
}
struct ovsdb_error *
-ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
+ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
{
if (datum->n < 2) {
return NULL;
struct ovsdb_datum_sort_cbdata cbdata;
size_t i;
- cbdata.type = type;
+ cbdata.key_type = key_type;
cbdata.datum = datum;
sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
&cbdata);
for (i = 0; i < datum->n - 1; i++) {
if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
- type->key.type)) {
- if (ovsdb_type_is_map(type)) {
+ key_type)) {
+ if (datum->values) {
return ovsdb_error(NULL, "map contains duplicate key");
} else {
return ovsdb_error(NULL, "set contains duplicate");
}
}
+void
+ovsdb_datum_sort_assert(struct ovsdb_datum *datum,
+ enum ovsdb_atomic_type key_type)
+{
+ struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
+ if (error) {
+ NOT_REACHED();
+ }
+}
+
/* Checks that each of the atoms in 'datum' conforms to the constraints
* specified by its 'type'. Returns an error if a constraint is violated,
* otherwise a null pointer.
ovsdb_datum_from_json(struct ovsdb_datum *datum,
const struct ovsdb_type *type,
const struct json *json,
- const struct ovsdb_symbol_table *symtab)
+ struct ovsdb_symbol_table *symtab)
{
struct ovsdb_error *error;
- if (ovsdb_type_is_scalar(type)) {
- datum->n = 1;
- datum->keys = xmalloc(sizeof *datum->keys);
- datum->values = NULL;
-
- error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
- json, symtab);
- if (error) {
- free(datum->keys);
- }
- return error;
- } else {
+ if (ovsdb_type_is_map(type)
+ || (json->type == JSON_ARRAY
+ && json->u.array.n > 0
+ && json->u.array.elems[0]->type == JSON_STRING
+ && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
bool is_map = ovsdb_type_is_map(type);
const char *class = is_map ? "map" : "set";
const struct json *inner;
unsigned int i;
size_t n;
- assert(is_map || ovsdb_type_is_set(type));
-
error = unwrap_json(json, class, JSON_ARRAY, &inner);
if (error) {
return error;
datum->n++;
}
- error = ovsdb_datum_sort(datum, type);
+ error = ovsdb_datum_sort(datum, type->key.type);
if (error) {
goto error;
}
error:
ovsdb_datum_destroy(datum, type);
return error;
+ } else {
+ datum->n = 1;
+ datum->keys = xmalloc(sizeof *datum->keys);
+ datum->values = NULL;
+
+ error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
+ json, symtab);
+ if (error) {
+ free(datum->keys);
+ }
+ return error;
}
}
{
/* These tests somewhat tolerate a 'datum' that does not exactly match
* 'type', in particular a datum with 'n' not in the allowed range. */
- if (datum->n == 1 && ovsdb_type_is_scalar(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) {
struct json **elems;
static char *
parse_atom_token(const char **s, const struct ovsdb_base_type *base,
- union ovsdb_atom *atom)
+ union ovsdb_atom *atom, struct ovsdb_symbol_table *symtab)
{
char *token, *error;
error = ovsdb_token_parse(s, &token);
if (!error) {
- error = ovsdb_atom_from_string(atom, base, token);
+ error = ovsdb_atom_from_string(atom, base, token, symtab);
free(token);
}
return error;
static char *
parse_key_value(const char **s, const struct ovsdb_type *type,
- union ovsdb_atom *key, union ovsdb_atom *value)
+ union ovsdb_atom *key, union ovsdb_atom *value,
+ struct ovsdb_symbol_table *symtab)
{
const char *start = *s;
char *error;
- error = parse_atom_token(s, &type->key, key);
+ error = parse_atom_token(s, &type->key, key, symtab);
if (!error && type->value.type != OVSDB_TYPE_VOID) {
*s = skip_spaces(*s);
if (**s == '=') {
(*s)++;
*s = skip_spaces(*s);
- error = parse_atom_token(s, &type->value, value);
+ error = parse_atom_token(s, &type->value, value, symtab);
} else {
error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
start, **s);
* or, for a map, '='-delimited pairs of atoms. Each atom must in a format
* acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
* in "[]" or a map in "{}"; for an empty set or map these punctuators are
- * required. */
+ * required.
+ *
+ * Optionally, a symbol table may be supplied as 'symtab'. It is passed to
+ * ovsdb_atom_to_string(). */
char *
ovsdb_datum_from_string(struct ovsdb_datum *datum,
- const struct ovsdb_type *type, const char *s)
+ const struct ovsdb_type *type, const char *s,
+ struct ovsdb_symbol_table *symtab)
{
bool is_map = ovsdb_type_is_map(type);
struct ovsdb_error *dberror;
}
/* Add to datum. */
- error = parse_key_value(&p, type, &key, &value);
+ error = parse_key_value(&p, type, &key, &value, symtab);
if (error) {
goto error;
}
goto error;
}
- dberror = ovsdb_datum_sort(datum, type);
+ dberror = ovsdb_datum_sort(datum, type->key.type);
if (dberror) {
ovsdb_error_destroy(dberror);
if (ovsdb_type_is_map(type)) {
if (n != a->n) {
struct ovsdb_error *error;
a->n = n;
- error = ovsdb_datum_sort(a, type);
+ error = ovsdb_datum_sort(a, type->key.type);
assert(!error);
}
}
}
}
if (changed) {
- struct ovsdb_error *error = ovsdb_datum_sort(a, a_type);
- assert(!error);
+ ovsdb_datum_sort_assert(a, a_type->key.type);
}
}
\f
return shash_find_data(&symtab->sh, name);
}
-void
+struct ovsdb_symbol *
ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
const struct uuid *uuid, bool used)
{
symbol->uuid = *uuid;
symbol->used = used;
shash_add(&symtab->sh, name, symbol);
+ return symbol;
+}
+
+struct ovsdb_symbol *
+ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
+ const char *name)
+{
+ struct ovsdb_symbol *symbol;
+
+ symbol = ovsdb_symbol_table_get(symtab, name);
+ if (!symbol) {
+ struct uuid uuid;
+
+ uuid_generate(&uuid);
+ symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false);
+ }
+ 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