1 /* Copyright (c) 2009, 2010 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.
18 #include "ovsdb-data.h"
26 #include "dynamic-string.h"
28 #include "ovsdb-error.h"
35 wrap_json(const char *name, struct json *wrapped)
37 return json_array_create_2(json_string_create(name), wrapped);
41 ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
47 case OVSDB_TYPE_INTEGER:
55 case OVSDB_TYPE_BOOLEAN:
56 atom->boolean = false;
59 case OVSDB_TYPE_STRING:
60 atom->string = xmemdup("", 1);
64 uuid_zero(&atom->uuid);
74 ovsdb_atom_is_default(const union ovsdb_atom *atom,
75 enum ovsdb_atomic_type type)
81 case OVSDB_TYPE_INTEGER:
82 return atom->integer == 0;
85 return atom->real == 0.0;
87 case OVSDB_TYPE_BOOLEAN:
88 return atom->boolean == false;
90 case OVSDB_TYPE_STRING:
91 return atom->string[0] == '\0';
94 return uuid_is_zero(&atom->uuid);
103 ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
104 enum ovsdb_atomic_type type)
107 case OVSDB_TYPE_VOID:
110 case OVSDB_TYPE_INTEGER:
111 new->integer = old->integer;
114 case OVSDB_TYPE_REAL:
115 new->real = old->real;
118 case OVSDB_TYPE_BOOLEAN:
119 new->boolean = old->boolean;
122 case OVSDB_TYPE_STRING:
123 new->string = xstrdup(old->string);
126 case OVSDB_TYPE_UUID:
127 new->uuid = old->uuid;
137 ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b)
139 union ovsdb_atom tmp = *a;
145 ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
149 case OVSDB_TYPE_VOID:
152 case OVSDB_TYPE_INTEGER:
153 return hash_int(atom->integer, basis);
155 case OVSDB_TYPE_REAL:
156 return hash_double(atom->real, basis);
158 case OVSDB_TYPE_BOOLEAN:
159 return hash_boolean(atom->boolean, basis);
161 case OVSDB_TYPE_STRING:
162 return hash_string(atom->string, basis);
164 case OVSDB_TYPE_UUID:
165 return hash_int(uuid_hash(&atom->uuid), basis);
174 ovsdb_atom_compare_3way(const union ovsdb_atom *a,
175 const union ovsdb_atom *b,
176 enum ovsdb_atomic_type type)
179 case OVSDB_TYPE_VOID:
182 case OVSDB_TYPE_INTEGER:
183 return a->integer < b->integer ? -1 : a->integer > b->integer;
185 case OVSDB_TYPE_REAL:
186 return a->real < b->real ? -1 : a->real > b->real;
188 case OVSDB_TYPE_BOOLEAN:
189 return a->boolean - b->boolean;
191 case OVSDB_TYPE_STRING:
192 return strcmp(a->string, b->string);
194 case OVSDB_TYPE_UUID:
195 return uuid_compare_3way(&a->uuid, &b->uuid);
203 static struct ovsdb_error *
204 unwrap_json(const struct json *json, const char *name,
205 enum json_type value_type, const struct json **value)
207 if (json->type != JSON_ARRAY
208 || json->u.array.n != 2
209 || json->u.array.elems[0]->type != JSON_STRING
210 || (name && strcmp(json->u.array.elems[0]->u.string, name))
211 || json->u.array.elems[1]->type != value_type)
213 return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
214 json_type_to_string(value_type));
216 *value = json->u.array.elems[1];
220 static struct ovsdb_error *
221 parse_json_pair(const struct json *json,
222 const struct json **elem0, const struct json **elem1)
224 if (json->type != JSON_ARRAY || json->u.array.n != 2) {
225 return ovsdb_syntax_error(json, NULL, "expected 2-element array");
227 *elem0 = json->u.array.elems[0];
228 *elem1 = json->u.array.elems[1];
232 static struct ovsdb_error *
233 ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
234 const struct ovsdb_symbol_table *symtab)
237 static struct ovsdb_error *
238 ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
239 const struct ovsdb_symbol_table *symtab)
241 struct ovsdb_error *error0;
242 const struct json *value;
244 error0 = unwrap_json(json, "uuid", JSON_STRING, &value);
246 const char *uuid_string = json_string(value);
247 if (!uuid_from_string(uuid, uuid_string)) {
248 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
252 struct ovsdb_error *error1;
254 error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
256 const char *name = json_string(value);
257 const struct ovsdb_symbol *symbol;
259 ovsdb_error_destroy(error0);
261 symbol = ovsdb_symbol_table_get(symtab, name);
263 *uuid = symbol->uuid;
266 return ovsdb_syntax_error(json, NULL,
267 "unknown named-uuid \"%s\"", name);
270 ovsdb_error_destroy(error1);
276 static struct ovsdb_error * WARN_UNUSED_RESULT
277 ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
278 const struct json *json,
279 const struct ovsdb_symbol_table *symtab)
282 case OVSDB_TYPE_VOID:
285 case OVSDB_TYPE_INTEGER:
286 if (json->type == JSON_INTEGER) {
287 atom->integer = json->u.integer;
292 case OVSDB_TYPE_REAL:
293 if (json->type == JSON_INTEGER) {
294 atom->real = json->u.integer;
296 } else if (json->type == JSON_REAL) {
297 atom->real = json->u.real;
302 case OVSDB_TYPE_BOOLEAN:
303 if (json->type == JSON_TRUE) {
304 atom->boolean = true;
306 } else if (json->type == JSON_FALSE) {
307 atom->boolean = false;
312 case OVSDB_TYPE_STRING:
313 if (json->type == JSON_STRING) {
314 atom->string = xstrdup(json->u.string);
319 case OVSDB_TYPE_UUID:
320 return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab);
327 return ovsdb_syntax_error(json, NULL, "expected %s",
328 ovsdb_atomic_type_to_string(type));
332 ovsdb_atom_from_json(union ovsdb_atom *atom,
333 const struct ovsdb_base_type *base,
334 const struct json *json,
335 const struct ovsdb_symbol_table *symtab)
337 struct ovsdb_error *error;
339 error = ovsdb_atom_from_json__(atom, base->type, json, symtab);
344 error = ovsdb_atom_check_constraints(atom, base);
346 ovsdb_atom_destroy(atom, base->type);
352 ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
355 case OVSDB_TYPE_VOID:
358 case OVSDB_TYPE_INTEGER:
359 return json_integer_create(atom->integer);
361 case OVSDB_TYPE_REAL:
362 return json_real_create(atom->real);
364 case OVSDB_TYPE_BOOLEAN:
365 return json_boolean_create(atom->boolean);
367 case OVSDB_TYPE_STRING:
368 return json_string_create(atom->string);
370 case OVSDB_TYPE_UUID:
371 return wrap_json("uuid", json_string_create_nocopy(
372 xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid))));
381 ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
385 case OVSDB_TYPE_VOID:
388 case OVSDB_TYPE_INTEGER: {
389 long long int integer;
390 if (!str_to_llong(s, 10, &integer)) {
391 return xasprintf("\"%s\" is not a valid integer", s);
393 atom->integer = integer;
397 case OVSDB_TYPE_REAL:
398 if (!str_to_double(s, &atom->real)) {
399 return xasprintf("\"%s\" is not a valid real number", s);
401 /* Our JSON input routines map negative zero to zero, so do that here
402 * too for consistency. */
403 if (atom->real == 0.0) {
408 case OVSDB_TYPE_BOOLEAN:
409 if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on")
410 || !strcmp(s, "1")) {
411 atom->boolean = true;
412 } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off")
413 || !strcmp(s, "0")) {
414 atom->boolean = false;
416 return xasprintf("\"%s\" is not a valid boolean "
417 "(use \"true\" or \"false\")", s);
421 case OVSDB_TYPE_STRING:
423 return xstrdup("An empty string is not valid as input; "
424 "use \"\" to represent the empty string");
425 } else if (*s == '"') {
426 size_t s_len = strlen(s);
428 if (s_len < 2 || s[s_len - 1] != '"') {
429 return xasprintf("%s: missing quote at end of "
431 } else if (!json_string_unescape(s + 1, s_len - 2,
433 char *error = xasprintf("%s: %s", s, atom->string);
438 atom->string = xstrdup(s);
442 case OVSDB_TYPE_UUID:
443 if (!uuid_from_string(&atom->uuid, s)) {
444 return xasprintf("\"%s\" is not a valid UUID", s);
456 /* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
457 * one of the following forms:
459 * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
461 * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
464 * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
465 * "no", "off", or "0" for false.
467 * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
468 * an arbitrary string.
470 * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format.
472 * Returns a null pointer if successful, otherwise an error message describing
473 * the problem. The caller is responsible for freeing the error.
476 ovsdb_atom_from_string(union ovsdb_atom *atom,
477 const struct ovsdb_base_type *base, const char *s)
479 struct ovsdb_error *error;
482 msg = ovsdb_atom_from_string__(atom, base->type, s);
487 error = ovsdb_atom_check_constraints(atom, base);
489 msg = ovsdb_error_to_string(error);
490 ovsdb_error_destroy(error);
496 string_needs_quotes(const char *s)
502 if (!isalpha(c) && c != '_') {
506 while ((c = *p++) != '\0') {
507 if (!isalpha(c) && c != '_' && c != '-' && c != '.') {
512 if (!strcmp(s, "true") || !strcmp(s, "false")) {
519 /* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
520 * to ovsdb_atom_from_string(). */
522 ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
526 case OVSDB_TYPE_VOID:
529 case OVSDB_TYPE_INTEGER:
530 ds_put_format(out, "%"PRId64, atom->integer);
533 case OVSDB_TYPE_REAL:
534 ds_put_format(out, "%.*g", DBL_DIG, atom->real);
537 case OVSDB_TYPE_BOOLEAN:
538 ds_put_cstr(out, atom->boolean ? "true" : "false");
541 case OVSDB_TYPE_STRING:
542 if (string_needs_quotes(atom->string)) {
545 json.type = JSON_STRING;
546 json.u.string = atom->string;
547 json_to_ds(&json, 0, out);
549 ds_put_cstr(out, atom->string);
553 case OVSDB_TYPE_UUID:
554 ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid));
563 static struct ovsdb_error *
564 check_string_constraints(const char *s,
565 const struct ovsdb_string_constraints *c)
570 msg = utf8_validate(s, &n_chars);
572 struct ovsdb_error *error;
574 error = ovsdb_error("constraint violation",
575 "\"%s\" is not a valid UTF-8 string: %s",
581 if (n_chars < c->minLen) {
583 "constraint violation",
584 "\"%s\" length %zu is less than minimum allowed "
585 "length %u", s, n_chars, c->minLen);
586 } else if (n_chars > c->maxLen) {
588 "constraint violation",
589 "\"%s\" length %zu is greater than maximum allowed "
590 "length %u", s, n_chars, c->maxLen);
597 retval = pcre_exec(c->re, NULL, s, strlen(s), 0,
598 PCRE_ANCHORED | PCRE_NO_UTF8_CHECK, NULL, 0);
599 if (retval == PCRE_ERROR_NOMATCH) {
601 return ovsdb_error("constraint violation",
602 "\"%s\" is not a %s", s, c->reComment);
604 return ovsdb_error("constraint violation",
605 "\"%s\" does not match regular expression "
606 "/%s/", s, c->reMatch);
608 } else if (retval < 0) {
609 /* PCRE doesn't have a function to translate an error code to a
610 * description. Bizarre. See pcreapi(3) for error details. */
611 return ovsdb_error("internal error", "PCRE returned error %d",
615 #endif /* HAVE_PCRE */
620 /* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
621 * (base->type must specify 'atom''s type.) Returns a null pointer if the
622 * constraints are met, otherwise an error that explains the violation.
624 * Checking UUID constraints is deferred to transaction commit time, so this
625 * function does nothing for UUID constraints. */
627 ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
628 const struct ovsdb_base_type *base)
630 switch (base->type) {
631 case OVSDB_TYPE_VOID:
634 case OVSDB_TYPE_INTEGER:
635 if (atom->integer >= base->u.integer.min
636 && atom->integer <= base->u.integer.max) {
638 } else if (base->u.integer.min != INT64_MIN) {
639 if (base->u.integer.max != INT64_MAX) {
640 return ovsdb_error("constraint violation",
641 "%"PRId64" is not in the valid range "
642 "%"PRId64" to %"PRId64" (inclusive)",
644 base->u.integer.min, base->u.integer.max);
646 return ovsdb_error("constraint violation",
647 "%"PRId64" is less than minimum allowed "
649 atom->integer, base->u.integer.min);
652 return ovsdb_error("constraint violation",
653 "%"PRId64" is greater than maximum allowed "
655 atom->integer, base->u.integer.max);
659 case OVSDB_TYPE_REAL:
660 if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
662 } else if (base->u.real.min != -DBL_MAX) {
663 if (base->u.real.max != DBL_MAX) {
664 return ovsdb_error("constraint violation",
665 "%.*g is not in the valid range "
666 "%.*g to %.*g (inclusive)",
668 DBL_DIG, base->u.real.min,
669 DBL_DIG, base->u.real.max);
671 return ovsdb_error("constraint violation",
672 "%.*g is less than minimum allowed "
675 DBL_DIG, base->u.real.min);
678 return ovsdb_error("constraint violation",
679 "%.*g is greater than maximum allowed "
682 DBL_DIG, base->u.real.max);
686 case OVSDB_TYPE_BOOLEAN:
689 case OVSDB_TYPE_STRING:
690 return check_string_constraints(atom->string, &base->u.string);
692 case OVSDB_TYPE_UUID:
701 static union ovsdb_atom *
702 alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
704 if (type != OVSDB_TYPE_VOID && n) {
705 union ovsdb_atom *atoms;
708 atoms = xmalloc(n * sizeof *atoms);
709 for (i = 0; i < n; i++) {
710 ovsdb_atom_init_default(&atoms[i], type);
714 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
715 * treated as xmalloc(1). */
721 ovsdb_datum_init_empty(struct ovsdb_datum *datum)
725 datum->values = NULL;
729 ovsdb_datum_init_default(struct ovsdb_datum *datum,
730 const struct ovsdb_type *type)
732 datum->n = type->n_min;
733 datum->keys = alloc_default_atoms(type->key.type, datum->n);
734 datum->values = alloc_default_atoms(type->value.type, datum->n);
738 ovsdb_datum_is_default(const struct ovsdb_datum *datum,
739 const struct ovsdb_type *type)
743 if (datum->n != type->n_min) {
746 for (i = 0; i < datum->n; i++) {
747 if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) {
750 if (type->value.type != OVSDB_TYPE_VOID
751 && !ovsdb_atom_is_default(&datum->values[i], type->value.type)) {
759 static union ovsdb_atom *
760 clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
762 if (type != OVSDB_TYPE_VOID && n) {
763 union ovsdb_atom *new;
766 new = xmalloc(n * sizeof *new);
767 for (i = 0; i < n; i++) {
768 ovsdb_atom_clone(&new[i], &old[i], type);
772 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
773 * treated as xmalloc(1). */
779 ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old,
780 const struct ovsdb_type *type)
782 unsigned int n = old->n;
784 new->keys = clone_atoms(old->keys, type->key.type, n);
785 new->values = clone_atoms(old->values, type->value.type, n);
789 free_data(enum ovsdb_atomic_type type,
790 union ovsdb_atom *atoms, size_t n_atoms)
792 if (ovsdb_atom_needs_destruction(type)) {
794 for (i = 0; i < n_atoms; i++) {
795 ovsdb_atom_destroy(&atoms[i], type);
802 ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type)
804 free_data(type->key.type, datum->keys, datum->n);
805 free_data(type->value.type, datum->values, datum->n);
809 ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
811 struct ovsdb_datum tmp = *a;
816 struct ovsdb_datum_sort_cbdata {
817 const struct ovsdb_type *type;
818 struct ovsdb_datum *datum;
822 ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
824 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
826 return ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
827 &cbdata->datum->keys[b],
828 cbdata->type->key.type);
832 ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
834 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
836 ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
837 if (cbdata->type->value.type != OVSDB_TYPE_VOID) {
838 ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
843 ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
848 struct ovsdb_datum_sort_cbdata cbdata;
852 cbdata.datum = datum;
853 sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
856 for (i = 0; i < datum->n - 1; i++) {
857 if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
859 if (ovsdb_type_is_map(type)) {
860 return ovsdb_error(NULL, "map contains duplicate key");
862 return ovsdb_error(NULL, "set contains duplicate");
871 /* Checks that each of the atoms in 'datum' conforms to the constraints
872 * specified by its 'type'. Returns an error if a constraint is violated,
873 * otherwise a null pointer.
875 * This function is not commonly useful because the most ordinary way to obtain
876 * a datum is ultimately via ovsdb_atom_from_string() or
877 * ovsdb_atom_from_json(), which check constraints themselves. */
879 ovsdb_datum_check_constraints(const struct ovsdb_datum *datum,
880 const struct ovsdb_type *type)
882 struct ovsdb_error *error;
885 for (i = 0; i < datum->n; i++) {
886 error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key);
892 if (type->value.type != OVSDB_TYPE_VOID) {
893 for (i = 0; i < datum->n; i++) {
894 error = ovsdb_atom_check_constraints(&datum->values[i],
906 ovsdb_datum_from_json(struct ovsdb_datum *datum,
907 const struct ovsdb_type *type,
908 const struct json *json,
909 const struct ovsdb_symbol_table *symtab)
911 struct ovsdb_error *error;
913 if (ovsdb_type_is_scalar(type)) {
915 datum->keys = xmalloc(sizeof *datum->keys);
916 datum->values = NULL;
918 error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
925 bool is_map = ovsdb_type_is_map(type);
926 const char *class = is_map ? "map" : "set";
927 const struct json *inner;
931 assert(is_map || ovsdb_type_is_set(type));
933 error = unwrap_json(json, class, JSON_ARRAY, &inner);
938 n = inner->u.array.n;
939 if (n < type->n_min || n > type->n_max) {
940 return ovsdb_syntax_error(json, NULL, "%s must have %u to "
941 "%u members but %zu are present",
942 class, type->n_min, type->n_max, n);
946 datum->keys = xmalloc(n * sizeof *datum->keys);
947 datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL;
948 for (i = 0; i < n; i++) {
949 const struct json *element = inner->u.array.elems[i];
950 const struct json *key = NULL;
951 const struct json *value = NULL;
956 error = parse_json_pair(element, &key, &value);
962 error = ovsdb_atom_from_json(&datum->keys[i], &type->key,
969 error = ovsdb_atom_from_json(&datum->values[i],
970 &type->value, value, symtab);
972 ovsdb_atom_destroy(&datum->keys[i], type->key.type);
980 error = ovsdb_datum_sort(datum, type);
988 ovsdb_datum_destroy(datum, type);
994 ovsdb_datum_to_json(const struct ovsdb_datum *datum,
995 const struct ovsdb_type *type)
997 /* These tests somewhat tolerate a 'datum' that does not exactly match
998 * 'type', in particular a datum with 'n' not in the allowed range. */
999 if (datum->n == 1 && ovsdb_type_is_scalar(type)) {
1000 return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
1001 } else if (type->value.type == OVSDB_TYPE_VOID) {
1002 struct json **elems;
1005 elems = xmalloc(datum->n * sizeof *elems);
1006 for (i = 0; i < datum->n; i++) {
1007 elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
1010 return wrap_json("set", json_array_create(elems, datum->n));
1012 struct json **elems;
1015 elems = xmalloc(datum->n * sizeof *elems);
1016 for (i = 0; i < datum->n; i++) {
1017 elems[i] = json_array_create_2(
1018 ovsdb_atom_to_json(&datum->keys[i], type->key.type),
1019 ovsdb_atom_to_json(&datum->values[i], type->value.type));
1022 return wrap_json("map", json_array_create(elems, datum->n));
1027 skip_spaces(const char *p)
1029 while (isspace((unsigned char) *p)) {
1036 parse_atom_token(const char **s, const struct ovsdb_base_type *base,
1037 union ovsdb_atom *atom)
1039 char *token, *error;
1041 error = ovsdb_token_parse(s, &token);
1043 error = ovsdb_atom_from_string(atom, base, token);
1050 parse_key_value(const char **s, const struct ovsdb_type *type,
1051 union ovsdb_atom *key, union ovsdb_atom *value)
1053 const char *start = *s;
1056 error = parse_atom_token(s, &type->key, key);
1057 if (!error && type->value.type != OVSDB_TYPE_VOID) {
1058 *s = skip_spaces(*s);
1061 *s = skip_spaces(*s);
1062 error = parse_atom_token(s, &type->value, value);
1064 error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
1068 ovsdb_atom_destroy(key, type->key.type);
1075 free_key_value(const struct ovsdb_type *type,
1076 union ovsdb_atom *key, union ovsdb_atom *value)
1078 ovsdb_atom_destroy(key, type->key.type);
1079 if (type->value.type != OVSDB_TYPE_VOID) {
1080 ovsdb_atom_destroy(value, type->value.type);
1084 /* Initializes 'datum' as a datum of the given 'type', parsing its contents
1085 * from 's'. The format of 's' is a series of space or comma separated atoms
1086 * or, for a map, '='-delimited pairs of atoms. Each atom must in a format
1087 * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
1088 * in "[]" or a map in "{}"; for an empty set or map these punctuators are
1091 ovsdb_datum_from_string(struct ovsdb_datum *datum,
1092 const struct ovsdb_type *type, const char *s)
1094 bool is_map = ovsdb_type_is_map(type);
1095 struct ovsdb_error *dberror;
1100 ovsdb_datum_init_empty(datum);
1102 /* Swallow a leading delimiter if there is one. */
1104 if (*p == (is_map ? '{' : '[')) {
1105 end_delim = is_map ? '}' : ']';
1106 p = skip_spaces(p + 1);
1109 return xstrdup("use \"{}\" to specify the empty map");
1111 return xstrdup("use \"[]\" to specify the empty set");
1117 while (*p && *p != end_delim) {
1118 union ovsdb_atom key, value;
1120 if (ovsdb_token_is_delim(*p)) {
1121 error = xasprintf("%s: unexpected \"%c\" parsing %s",
1122 s, *p, ovsdb_type_to_english(type));
1127 error = parse_key_value(&p, type, &key, &value);
1131 ovsdb_datum_add_unsafe(datum, &key, &value, type);
1132 free_key_value(type, &key, &value);
1134 /* Skip optional white space and comma. */
1137 p = skip_spaces(p + 1);
1141 if (*p != end_delim) {
1142 error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim);
1146 p = skip_spaces(p + 1);
1148 error = xasprintf("%s: trailing garbage after \"%c\"",
1154 if (datum->n < type->n_min) {
1155 error = xasprintf("%s: %u %s specified but the minimum number is %u",
1156 s, datum->n, is_map ? "pair(s)" : "value(s)",
1159 } else if (datum->n > type->n_max) {
1160 error = xasprintf("%s: %u %s specified but the maximum number is %u",
1161 s, datum->n, is_map ? "pair(s)" : "value(s)",
1166 dberror = ovsdb_datum_sort(datum, type);
1168 ovsdb_error_destroy(dberror);
1169 if (ovsdb_type_is_map(type)) {
1170 error = xasprintf("%s: map contains duplicate key", s);
1172 error = xasprintf("%s: set contains duplicate value", s);
1180 ovsdb_datum_destroy(datum, type);
1181 ovsdb_datum_init_empty(datum);
1185 /* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
1186 * to ovsdb_datum_from_string(). */
1188 ovsdb_datum_to_string(const struct ovsdb_datum *datum,
1189 const struct ovsdb_type *type, struct ds *out)
1191 bool is_map = ovsdb_type_is_map(type);
1194 if (type->n_max > 1 || !datum->n) {
1195 ds_put_char(out, is_map ? '{' : '[');
1197 for (i = 0; i < datum->n; i++) {
1199 ds_put_cstr(out, ", ");
1202 ovsdb_atom_to_string(&datum->keys[i], type->key.type, out);
1204 ds_put_char(out, '=');
1205 ovsdb_atom_to_string(&datum->values[i], type->value.type, out);
1208 if (type->n_max > 1 || !datum->n) {
1209 ds_put_char(out, is_map ? '}' : ']');
1214 hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
1215 unsigned int n, uint32_t basis)
1217 if (type != OVSDB_TYPE_VOID) {
1220 for (i = 0; i < n; i++) {
1221 basis = ovsdb_atom_hash(&atoms[i], type, basis);
1228 ovsdb_datum_hash(const struct ovsdb_datum *datum,
1229 const struct ovsdb_type *type, uint32_t basis)
1231 basis = hash_atoms(type->key.type, datum->keys, datum->n, basis);
1232 basis ^= (type->key.type << 24) | (type->value.type << 16) | datum->n;
1233 basis = hash_atoms(type->value.type, datum->values, datum->n, basis);
1238 atom_arrays_compare_3way(const union ovsdb_atom *a,
1239 const union ovsdb_atom *b,
1240 enum ovsdb_atomic_type type,
1245 for (i = 0; i < n; i++) {
1246 int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type);
1256 ovsdb_datum_equals(const struct ovsdb_datum *a,
1257 const struct ovsdb_datum *b,
1258 const struct ovsdb_type *type)
1260 return !ovsdb_datum_compare_3way(a, b, type);
1264 ovsdb_datum_compare_3way(const struct ovsdb_datum *a,
1265 const struct ovsdb_datum *b,
1266 const struct ovsdb_type *type)
1271 return a->n < b->n ? -1 : 1;
1274 cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key.type, a->n);
1279 return (type->value.type == OVSDB_TYPE_VOID ? 0
1280 : atom_arrays_compare_3way(a->values, b->values, type->value.type,
1284 /* If 'key' is one of the keys in 'datum', returns its index within 'datum',
1285 * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the
1286 * 'keys' array in 'datum'.
1289 ovsdb_datum_find_key(const struct ovsdb_datum *datum,
1290 const union ovsdb_atom *key,
1291 enum ovsdb_atomic_type key_type)
1293 unsigned int low = 0;
1294 unsigned int high = datum->n;
1295 while (low < high) {
1296 unsigned int idx = (low + high) / 2;
1297 int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type);
1300 } else if (cmp > 0) {
1309 /* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
1310 * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of
1311 * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the
1312 * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
1315 ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,
1316 const union ovsdb_atom *key,
1317 enum ovsdb_atomic_type key_type,
1318 const union ovsdb_atom *value,
1319 enum ovsdb_atomic_type value_type)
1321 unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);
1323 && value_type != OVSDB_TYPE_VOID
1324 && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {
1330 /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
1331 * UINT_MAX. 'type' must be the type of 'a' and 'b', except that
1332 * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not
1335 ovsdb_datum_find(const struct ovsdb_datum *a, int i,
1336 const struct ovsdb_datum *b,
1337 const struct ovsdb_type *type)
1339 return ovsdb_datum_find_key_value(b,
1340 &a->keys[i], type->key.type,
1341 a->values ? &a->values[i] : NULL,
1345 /* Returns true if every element in 'a' is also in 'b', false otherwise. */
1347 ovsdb_datum_includes_all(const struct ovsdb_datum *a,
1348 const struct ovsdb_datum *b,
1349 const struct ovsdb_type *type)
1353 for (i = 0; i < a->n; i++) {
1354 if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
1361 /* Returns true if no element in 'a' is also in 'b', false otherwise. */
1363 ovsdb_datum_excludes_all(const struct ovsdb_datum *a,
1364 const struct ovsdb_datum *b,
1365 const struct ovsdb_type *type)
1369 for (i = 0; i < a->n; i++) {
1370 if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) {
1378 ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type,
1379 unsigned int capacity)
1381 a->keys = xrealloc(a->keys, capacity * sizeof *a->keys);
1382 if (type->value.type != OVSDB_TYPE_VOID) {
1383 a->values = xrealloc(a->values, capacity * sizeof *a->values);
1387 /* Removes the element with index 'idx' from 'datum', which has type 'type'.
1388 * If 'idx' is not the last element in 'datum', then the removed element is
1389 * replaced by the (former) last element.
1391 * This function does not maintain ovsdb_datum invariants. Use
1392 * ovsdb_datum_sort() to check and restore these invariants. */
1394 ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx,
1395 const struct ovsdb_type *type)
1397 ovsdb_atom_destroy(&datum->keys[idx], type->key.type);
1398 datum->keys[idx] = datum->keys[datum->n - 1];
1399 if (type->value.type != OVSDB_TYPE_VOID) {
1400 ovsdb_atom_destroy(&datum->values[idx], type->value.type);
1401 datum->values[idx] = datum->values[datum->n - 1];
1406 /* Adds the element with the given 'key' and 'value' to 'datum', which must
1407 * have the specified 'type'.
1409 * This function always allocates memory, so it is not an efficient way to add
1410 * a number of elements to a datum.
1412 * This function does not maintain ovsdb_datum invariants. Use
1413 * ovsdb_datum_sort() to check and restore these invariants. (But a datum with
1414 * 0 or 1 elements cannot violate the invariants anyhow.) */
1416 ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
1417 const union ovsdb_atom *key,
1418 const union ovsdb_atom *value,
1419 const struct ovsdb_type *type)
1421 size_t idx = datum->n++;
1422 datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
1423 ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
1424 if (type->value.type != OVSDB_TYPE_VOID) {
1425 datum->values = xrealloc(datum->values,
1426 datum->n * sizeof *datum->values);
1427 ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
1432 ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
1433 const struct ovsdb_type *type, bool replace)
1439 for (bi = 0; bi < b->n; bi++) {
1442 ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type);
1443 if (ai == UINT_MAX) {
1445 ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));
1447 ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type);
1448 if (type->value.type != OVSDB_TYPE_VOID) {
1449 ovsdb_atom_clone(&a->values[n], &b->values[bi],
1453 } else if (replace && type->value.type != OVSDB_TYPE_VOID) {
1454 ovsdb_atom_destroy(&a->values[ai], type->value.type);
1455 ovsdb_atom_clone(&a->values[ai], &b->values[bi],
1460 struct ovsdb_error *error;
1462 error = ovsdb_datum_sort(a, type);
1468 ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
1469 const struct ovsdb_datum *b,
1470 const struct ovsdb_type *b_type)
1472 bool changed = false;
1475 assert(a_type->key.type == b_type->key.type);
1476 assert(a_type->value.type == b_type->value.type
1477 || b_type->value.type == OVSDB_TYPE_VOID);
1479 /* XXX The big-O of this could easily be improved. */
1480 for (i = 0; i < a->n; ) {
1481 unsigned int idx = ovsdb_datum_find(a, i, b, b_type);
1482 if (idx != UINT_MAX) {
1484 ovsdb_datum_remove_unsafe(a, i, a_type);
1490 struct ovsdb_error *error = ovsdb_datum_sort(a, a_type);
1495 struct ovsdb_symbol_table {
1499 struct ovsdb_symbol_table *
1500 ovsdb_symbol_table_create(void)
1502 struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab);
1503 shash_init(&symtab->sh);
1508 ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab)
1511 struct shash_node *node, *next;
1513 SHASH_FOR_EACH_SAFE (node, next, &symtab->sh) {
1514 struct ovsdb_symbol *symbol = node->data;
1516 shash_delete(&symtab->sh, node);
1518 shash_destroy(&symtab->sh);
1523 struct ovsdb_symbol *
1524 ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab,
1527 return shash_find_data(&symtab->sh, name);
1531 ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
1532 const struct uuid *uuid, bool used)
1534 struct ovsdb_symbol *symbol;
1536 assert(!ovsdb_symbol_table_get(symtab, name));
1537 symbol = xmalloc(sizeof *symbol);
1538 symbol->uuid = *uuid;
1539 symbol->used = used;
1540 shash_add(&symtab->sh, name, symbol);
1543 /* Extracts a token from the beginning of 's' and returns a pointer just after
1544 * the token. Stores the token itself into '*outp', which the caller is
1545 * responsible for freeing (with free()).
1547 * If 's[0]' is a delimiter, the returned token is the empty string.
1549 * A token extends from 's' to the first delimiter, as defined by
1550 * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be
1551 * escaped with a backslash, in which case the backslash does not appear in the
1552 * output. Double quotes also cause delimiters to be ignored, but the double
1553 * quotes are retained in the output. (Backslashes inside double quotes are
1554 * not removed, either.)
1557 ovsdb_token_parse(const char **s, char **outp)
1566 for (p = *s; *p != '\0'; ) {
1570 ds_put_char(&out, '\\');
1573 error = xasprintf("%s: backslash at end of argument", *s);
1576 ds_put_char(&out, *p++);
1577 } else if (!in_quotes && ovsdb_token_is_delim(c)) {
1581 ds_put_char(&out, c);
1583 in_quotes = !in_quotes;
1588 error = xasprintf("%s: quoted string extends past end of argument",
1592 *outp = ds_cstr(&out);
1602 /* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
1604 ovsdb_token_is_delim(unsigned char c)
1606 return strchr(":=, []{}", c) != NULL;