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 * WARN_UNUSED_RESULT
233 ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
234 struct ovsdb_symbol_table *symtab)
236 struct ovsdb_error *error0;
237 const struct json *value;
239 error0 = unwrap_json(json, "uuid", JSON_STRING, &value);
241 const char *uuid_string = json_string(value);
242 if (!uuid_from_string(uuid, uuid_string)) {
243 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
247 struct ovsdb_error *error1;
249 error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
251 const char *name = json_string(value);
253 ovsdb_error_destroy(error0);
254 *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
257 ovsdb_error_destroy(error1);
263 static struct ovsdb_error * WARN_UNUSED_RESULT
264 ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
265 const struct json *json,
266 struct ovsdb_symbol_table *symtab)
269 case OVSDB_TYPE_VOID:
272 case OVSDB_TYPE_INTEGER:
273 if (json->type == JSON_INTEGER) {
274 atom->integer = json->u.integer;
279 case OVSDB_TYPE_REAL:
280 if (json->type == JSON_INTEGER) {
281 atom->real = json->u.integer;
283 } else if (json->type == JSON_REAL) {
284 atom->real = json->u.real;
289 case OVSDB_TYPE_BOOLEAN:
290 if (json->type == JSON_TRUE) {
291 atom->boolean = true;
293 } else if (json->type == JSON_FALSE) {
294 atom->boolean = false;
299 case OVSDB_TYPE_STRING:
300 if (json->type == JSON_STRING) {
301 atom->string = xstrdup(json->u.string);
306 case OVSDB_TYPE_UUID:
307 return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab);
314 return ovsdb_syntax_error(json, NULL, "expected %s",
315 ovsdb_atomic_type_to_string(type));
319 ovsdb_atom_from_json(union ovsdb_atom *atom,
320 const struct ovsdb_base_type *base,
321 const struct json *json,
322 struct ovsdb_symbol_table *symtab)
324 struct ovsdb_error *error;
326 error = ovsdb_atom_from_json__(atom, base->type, json, symtab);
331 error = ovsdb_atom_check_constraints(atom, base);
333 ovsdb_atom_destroy(atom, base->type);
339 ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
342 case OVSDB_TYPE_VOID:
345 case OVSDB_TYPE_INTEGER:
346 return json_integer_create(atom->integer);
348 case OVSDB_TYPE_REAL:
349 return json_real_create(atom->real);
351 case OVSDB_TYPE_BOOLEAN:
352 return json_boolean_create(atom->boolean);
354 case OVSDB_TYPE_STRING:
355 return json_string_create(atom->string);
357 case OVSDB_TYPE_UUID:
358 return wrap_json("uuid", json_string_create_nocopy(
359 xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid))));
368 ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
372 case OVSDB_TYPE_VOID:
375 case OVSDB_TYPE_INTEGER: {
376 long long int integer;
377 if (!str_to_llong(s, 10, &integer)) {
378 return xasprintf("\"%s\" is not a valid integer", s);
380 atom->integer = integer;
384 case OVSDB_TYPE_REAL:
385 if (!str_to_double(s, &atom->real)) {
386 return xasprintf("\"%s\" is not a valid real number", s);
388 /* Our JSON input routines map negative zero to zero, so do that here
389 * too for consistency. */
390 if (atom->real == 0.0) {
395 case OVSDB_TYPE_BOOLEAN:
396 if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on")
397 || !strcmp(s, "1")) {
398 atom->boolean = true;
399 } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off")
400 || !strcmp(s, "0")) {
401 atom->boolean = false;
403 return xasprintf("\"%s\" is not a valid boolean "
404 "(use \"true\" or \"false\")", s);
408 case OVSDB_TYPE_STRING:
410 return xstrdup("An empty string is not valid as input; "
411 "use \"\" to represent the empty string");
412 } else if (*s == '"') {
413 size_t s_len = strlen(s);
415 if (s_len < 2 || s[s_len - 1] != '"') {
416 return xasprintf("%s: missing quote at end of "
418 } else if (!json_string_unescape(s + 1, s_len - 2,
420 char *error = xasprintf("%s: %s", s, atom->string);
425 atom->string = xstrdup(s);
429 case OVSDB_TYPE_UUID:
430 if (!uuid_from_string(&atom->uuid, s)) {
431 return xasprintf("\"%s\" is not a valid UUID", s);
443 /* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
444 * one of the following forms:
446 * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
448 * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
451 * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
452 * "no", "off", or "0" for false.
454 * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
455 * an arbitrary string.
457 * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format.
459 * Returns a null pointer if successful, otherwise an error message describing
460 * the problem. The caller is responsible for freeing the error.
463 ovsdb_atom_from_string(union ovsdb_atom *atom,
464 const struct ovsdb_base_type *base, const char *s)
466 struct ovsdb_error *error;
469 msg = ovsdb_atom_from_string__(atom, base->type, s);
474 error = ovsdb_atom_check_constraints(atom, base);
476 msg = ovsdb_error_to_string(error);
477 ovsdb_error_destroy(error);
483 string_needs_quotes(const char *s)
489 if (!isalpha(c) && c != '_') {
493 while ((c = *p++) != '\0') {
494 if (!isalpha(c) && c != '_' && c != '-' && c != '.') {
499 if (!strcmp(s, "true") || !strcmp(s, "false")) {
506 /* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
507 * to ovsdb_atom_from_string(). */
509 ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
513 case OVSDB_TYPE_VOID:
516 case OVSDB_TYPE_INTEGER:
517 ds_put_format(out, "%"PRId64, atom->integer);
520 case OVSDB_TYPE_REAL:
521 ds_put_format(out, "%.*g", DBL_DIG, atom->real);
524 case OVSDB_TYPE_BOOLEAN:
525 ds_put_cstr(out, atom->boolean ? "true" : "false");
528 case OVSDB_TYPE_STRING:
529 if (string_needs_quotes(atom->string)) {
532 json.type = JSON_STRING;
533 json.u.string = atom->string;
534 json_to_ds(&json, 0, out);
536 ds_put_cstr(out, atom->string);
540 case OVSDB_TYPE_UUID:
541 ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid));
550 static struct ovsdb_error *
551 check_string_constraints(const char *s,
552 const struct ovsdb_string_constraints *c)
557 msg = utf8_validate(s, &n_chars);
559 struct ovsdb_error *error;
561 error = ovsdb_error("constraint violation",
562 "\"%s\" is not a valid UTF-8 string: %s",
568 if (n_chars < c->minLen) {
570 "constraint violation",
571 "\"%s\" length %zu is less than minimum allowed "
572 "length %u", s, n_chars, c->minLen);
573 } else if (n_chars > c->maxLen) {
575 "constraint violation",
576 "\"%s\" length %zu is greater than maximum allowed "
577 "length %u", s, n_chars, c->maxLen);
583 /* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
584 * (base->type must specify 'atom''s type.) Returns a null pointer if the
585 * constraints are met, otherwise an error that explains the violation.
587 * Checking UUID constraints is deferred to transaction commit time, so this
588 * function does nothing for UUID constraints. */
590 ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
591 const struct ovsdb_base_type *base)
593 switch (base->type) {
594 case OVSDB_TYPE_VOID:
597 case OVSDB_TYPE_INTEGER:
598 if (atom->integer >= base->u.integer.min
599 && atom->integer <= base->u.integer.max) {
601 } else if (base->u.integer.min != INT64_MIN) {
602 if (base->u.integer.max != INT64_MAX) {
603 return ovsdb_error("constraint violation",
604 "%"PRId64" is not in the valid range "
605 "%"PRId64" to %"PRId64" (inclusive)",
607 base->u.integer.min, base->u.integer.max);
609 return ovsdb_error("constraint violation",
610 "%"PRId64" is less than minimum allowed "
612 atom->integer, base->u.integer.min);
615 return ovsdb_error("constraint violation",
616 "%"PRId64" is greater than maximum allowed "
618 atom->integer, base->u.integer.max);
622 case OVSDB_TYPE_REAL:
623 if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
625 } else if (base->u.real.min != -DBL_MAX) {
626 if (base->u.real.max != DBL_MAX) {
627 return ovsdb_error("constraint violation",
628 "%.*g is not in the valid range "
629 "%.*g to %.*g (inclusive)",
631 DBL_DIG, base->u.real.min,
632 DBL_DIG, base->u.real.max);
634 return ovsdb_error("constraint violation",
635 "%.*g is less than minimum allowed "
638 DBL_DIG, base->u.real.min);
641 return ovsdb_error("constraint violation",
642 "%.*g is greater than maximum allowed "
645 DBL_DIG, base->u.real.max);
649 case OVSDB_TYPE_BOOLEAN:
652 case OVSDB_TYPE_STRING:
653 return check_string_constraints(atom->string, &base->u.string);
655 case OVSDB_TYPE_UUID:
664 static union ovsdb_atom *
665 alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
667 if (type != OVSDB_TYPE_VOID && n) {
668 union ovsdb_atom *atoms;
671 atoms = xmalloc(n * sizeof *atoms);
672 for (i = 0; i < n; i++) {
673 ovsdb_atom_init_default(&atoms[i], type);
677 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
678 * treated as xmalloc(1). */
684 ovsdb_datum_init_empty(struct ovsdb_datum *datum)
688 datum->values = NULL;
692 ovsdb_datum_init_default(struct ovsdb_datum *datum,
693 const struct ovsdb_type *type)
695 datum->n = type->n_min;
696 datum->keys = alloc_default_atoms(type->key.type, datum->n);
697 datum->values = alloc_default_atoms(type->value.type, datum->n);
701 ovsdb_datum_is_default(const struct ovsdb_datum *datum,
702 const struct ovsdb_type *type)
706 if (datum->n != type->n_min) {
709 for (i = 0; i < datum->n; i++) {
710 if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) {
713 if (type->value.type != OVSDB_TYPE_VOID
714 && !ovsdb_atom_is_default(&datum->values[i], type->value.type)) {
722 static union ovsdb_atom *
723 clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
725 if (type != OVSDB_TYPE_VOID && n) {
726 union ovsdb_atom *new;
729 new = xmalloc(n * sizeof *new);
730 for (i = 0; i < n; i++) {
731 ovsdb_atom_clone(&new[i], &old[i], type);
735 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
736 * treated as xmalloc(1). */
742 ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old,
743 const struct ovsdb_type *type)
745 unsigned int n = old->n;
747 new->keys = clone_atoms(old->keys, type->key.type, n);
748 new->values = clone_atoms(old->values, type->value.type, n);
752 free_data(enum ovsdb_atomic_type type,
753 union ovsdb_atom *atoms, size_t n_atoms)
755 if (ovsdb_atom_needs_destruction(type)) {
757 for (i = 0; i < n_atoms; i++) {
758 ovsdb_atom_destroy(&atoms[i], type);
765 ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type)
767 free_data(type->key.type, datum->keys, datum->n);
768 free_data(type->value.type, datum->values, datum->n);
772 ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
774 struct ovsdb_datum tmp = *a;
779 struct ovsdb_datum_sort_cbdata {
780 const struct ovsdb_type *type;
781 struct ovsdb_datum *datum;
785 ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
787 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
789 return ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
790 &cbdata->datum->keys[b],
791 cbdata->type->key.type);
795 ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
797 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
799 ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
800 if (cbdata->type->value.type != OVSDB_TYPE_VOID) {
801 ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
806 ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
811 struct ovsdb_datum_sort_cbdata cbdata;
815 cbdata.datum = datum;
816 sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
819 for (i = 0; i < datum->n - 1; i++) {
820 if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
822 if (ovsdb_type_is_map(type)) {
823 return ovsdb_error(NULL, "map contains duplicate key");
825 return ovsdb_error(NULL, "set contains duplicate");
834 /* Checks that each of the atoms in 'datum' conforms to the constraints
835 * specified by its 'type'. Returns an error if a constraint is violated,
836 * otherwise a null pointer.
838 * This function is not commonly useful because the most ordinary way to obtain
839 * a datum is ultimately via ovsdb_atom_from_string() or
840 * ovsdb_atom_from_json(), which check constraints themselves. */
842 ovsdb_datum_check_constraints(const struct ovsdb_datum *datum,
843 const struct ovsdb_type *type)
845 struct ovsdb_error *error;
848 for (i = 0; i < datum->n; i++) {
849 error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key);
855 if (type->value.type != OVSDB_TYPE_VOID) {
856 for (i = 0; i < datum->n; i++) {
857 error = ovsdb_atom_check_constraints(&datum->values[i],
869 ovsdb_datum_from_json(struct ovsdb_datum *datum,
870 const struct ovsdb_type *type,
871 const struct json *json,
872 struct ovsdb_symbol_table *symtab)
874 struct ovsdb_error *error;
876 if (ovsdb_type_is_map(type)
877 || (json->type == JSON_ARRAY
878 && json->u.array.n > 0
879 && json->u.array.elems[0]->type == JSON_STRING
880 && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
881 bool is_map = ovsdb_type_is_map(type);
882 const char *class = is_map ? "map" : "set";
883 const struct json *inner;
887 error = unwrap_json(json, class, JSON_ARRAY, &inner);
892 n = inner->u.array.n;
893 if (n < type->n_min || n > type->n_max) {
894 return ovsdb_syntax_error(json, NULL, "%s must have %u to "
895 "%u members but %zu are present",
896 class, type->n_min, type->n_max, n);
900 datum->keys = xmalloc(n * sizeof *datum->keys);
901 datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL;
902 for (i = 0; i < n; i++) {
903 const struct json *element = inner->u.array.elems[i];
904 const struct json *key = NULL;
905 const struct json *value = NULL;
910 error = parse_json_pair(element, &key, &value);
916 error = ovsdb_atom_from_json(&datum->keys[i], &type->key,
923 error = ovsdb_atom_from_json(&datum->values[i],
924 &type->value, value, symtab);
926 ovsdb_atom_destroy(&datum->keys[i], type->key.type);
934 error = ovsdb_datum_sort(datum, type);
942 ovsdb_datum_destroy(datum, type);
946 datum->keys = xmalloc(sizeof *datum->keys);
947 datum->values = NULL;
949 error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
959 ovsdb_datum_to_json(const struct ovsdb_datum *datum,
960 const struct ovsdb_type *type)
962 /* These tests somewhat tolerate a 'datum' that does not exactly match
963 * 'type', in particular a datum with 'n' not in the allowed range. */
964 if (datum->n == 1 && !ovsdb_type_is_map(type)) {
965 return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
966 } else if (type->value.type == OVSDB_TYPE_VOID) {
970 elems = xmalloc(datum->n * sizeof *elems);
971 for (i = 0; i < datum->n; i++) {
972 elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
975 return wrap_json("set", json_array_create(elems, datum->n));
980 elems = xmalloc(datum->n * sizeof *elems);
981 for (i = 0; i < datum->n; i++) {
982 elems[i] = json_array_create_2(
983 ovsdb_atom_to_json(&datum->keys[i], type->key.type),
984 ovsdb_atom_to_json(&datum->values[i], type->value.type));
987 return wrap_json("map", json_array_create(elems, datum->n));
992 skip_spaces(const char *p)
994 while (isspace((unsigned char) *p)) {
1001 parse_atom_token(const char **s, const struct ovsdb_base_type *base,
1002 union ovsdb_atom *atom)
1004 char *token, *error;
1006 error = ovsdb_token_parse(s, &token);
1008 error = ovsdb_atom_from_string(atom, base, token);
1015 parse_key_value(const char **s, const struct ovsdb_type *type,
1016 union ovsdb_atom *key, union ovsdb_atom *value)
1018 const char *start = *s;
1021 error = parse_atom_token(s, &type->key, key);
1022 if (!error && type->value.type != OVSDB_TYPE_VOID) {
1023 *s = skip_spaces(*s);
1026 *s = skip_spaces(*s);
1027 error = parse_atom_token(s, &type->value, value);
1029 error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
1033 ovsdb_atom_destroy(key, type->key.type);
1040 free_key_value(const struct ovsdb_type *type,
1041 union ovsdb_atom *key, union ovsdb_atom *value)
1043 ovsdb_atom_destroy(key, type->key.type);
1044 if (type->value.type != OVSDB_TYPE_VOID) {
1045 ovsdb_atom_destroy(value, type->value.type);
1049 /* Initializes 'datum' as a datum of the given 'type', parsing its contents
1050 * from 's'. The format of 's' is a series of space or comma separated atoms
1051 * or, for a map, '='-delimited pairs of atoms. Each atom must in a format
1052 * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
1053 * in "[]" or a map in "{}"; for an empty set or map these punctuators are
1056 ovsdb_datum_from_string(struct ovsdb_datum *datum,
1057 const struct ovsdb_type *type, const char *s)
1059 bool is_map = ovsdb_type_is_map(type);
1060 struct ovsdb_error *dberror;
1065 ovsdb_datum_init_empty(datum);
1067 /* Swallow a leading delimiter if there is one. */
1069 if (*p == (is_map ? '{' : '[')) {
1070 end_delim = is_map ? '}' : ']';
1071 p = skip_spaces(p + 1);
1074 return xstrdup("use \"{}\" to specify the empty map");
1076 return xstrdup("use \"[]\" to specify the empty set");
1082 while (*p && *p != end_delim) {
1083 union ovsdb_atom key, value;
1085 if (ovsdb_token_is_delim(*p)) {
1086 error = xasprintf("%s: unexpected \"%c\" parsing %s",
1087 s, *p, ovsdb_type_to_english(type));
1092 error = parse_key_value(&p, type, &key, &value);
1096 ovsdb_datum_add_unsafe(datum, &key, &value, type);
1097 free_key_value(type, &key, &value);
1099 /* Skip optional white space and comma. */
1102 p = skip_spaces(p + 1);
1106 if (*p != end_delim) {
1107 error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim);
1111 p = skip_spaces(p + 1);
1113 error = xasprintf("%s: trailing garbage after \"%c\"",
1119 if (datum->n < type->n_min) {
1120 error = xasprintf("%s: %u %s specified but the minimum number is %u",
1121 s, datum->n, is_map ? "pair(s)" : "value(s)",
1124 } else if (datum->n > type->n_max) {
1125 error = xasprintf("%s: %u %s specified but the maximum number is %u",
1126 s, datum->n, is_map ? "pair(s)" : "value(s)",
1131 dberror = ovsdb_datum_sort(datum, type);
1133 ovsdb_error_destroy(dberror);
1134 if (ovsdb_type_is_map(type)) {
1135 error = xasprintf("%s: map contains duplicate key", s);
1137 error = xasprintf("%s: set contains duplicate value", s);
1145 ovsdb_datum_destroy(datum, type);
1146 ovsdb_datum_init_empty(datum);
1150 /* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
1151 * to ovsdb_datum_from_string(). */
1153 ovsdb_datum_to_string(const struct ovsdb_datum *datum,
1154 const struct ovsdb_type *type, struct ds *out)
1156 bool is_map = ovsdb_type_is_map(type);
1159 if (type->n_max > 1 || !datum->n) {
1160 ds_put_char(out, is_map ? '{' : '[');
1162 for (i = 0; i < datum->n; i++) {
1164 ds_put_cstr(out, ", ");
1167 ovsdb_atom_to_string(&datum->keys[i], type->key.type, out);
1169 ds_put_char(out, '=');
1170 ovsdb_atom_to_string(&datum->values[i], type->value.type, out);
1173 if (type->n_max > 1 || !datum->n) {
1174 ds_put_char(out, is_map ? '}' : ']');
1179 hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
1180 unsigned int n, uint32_t basis)
1182 if (type != OVSDB_TYPE_VOID) {
1185 for (i = 0; i < n; i++) {
1186 basis = ovsdb_atom_hash(&atoms[i], type, basis);
1193 ovsdb_datum_hash(const struct ovsdb_datum *datum,
1194 const struct ovsdb_type *type, uint32_t basis)
1196 basis = hash_atoms(type->key.type, datum->keys, datum->n, basis);
1197 basis ^= (type->key.type << 24) | (type->value.type << 16) | datum->n;
1198 basis = hash_atoms(type->value.type, datum->values, datum->n, basis);
1203 atom_arrays_compare_3way(const union ovsdb_atom *a,
1204 const union ovsdb_atom *b,
1205 enum ovsdb_atomic_type type,
1210 for (i = 0; i < n; i++) {
1211 int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type);
1221 ovsdb_datum_equals(const struct ovsdb_datum *a,
1222 const struct ovsdb_datum *b,
1223 const struct ovsdb_type *type)
1225 return !ovsdb_datum_compare_3way(a, b, type);
1229 ovsdb_datum_compare_3way(const struct ovsdb_datum *a,
1230 const struct ovsdb_datum *b,
1231 const struct ovsdb_type *type)
1236 return a->n < b->n ? -1 : 1;
1239 cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key.type, a->n);
1244 return (type->value.type == OVSDB_TYPE_VOID ? 0
1245 : atom_arrays_compare_3way(a->values, b->values, type->value.type,
1249 /* If 'key' is one of the keys in 'datum', returns its index within 'datum',
1250 * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the
1251 * 'keys' array in 'datum'.
1254 ovsdb_datum_find_key(const struct ovsdb_datum *datum,
1255 const union ovsdb_atom *key,
1256 enum ovsdb_atomic_type key_type)
1258 unsigned int low = 0;
1259 unsigned int high = datum->n;
1260 while (low < high) {
1261 unsigned int idx = (low + high) / 2;
1262 int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type);
1265 } else if (cmp > 0) {
1274 /* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
1275 * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of
1276 * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the
1277 * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
1280 ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,
1281 const union ovsdb_atom *key,
1282 enum ovsdb_atomic_type key_type,
1283 const union ovsdb_atom *value,
1284 enum ovsdb_atomic_type value_type)
1286 unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);
1288 && value_type != OVSDB_TYPE_VOID
1289 && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {
1295 /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
1296 * UINT_MAX. 'type' must be the type of 'a' and 'b', except that
1297 * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not
1300 ovsdb_datum_find(const struct ovsdb_datum *a, int i,
1301 const struct ovsdb_datum *b,
1302 const struct ovsdb_type *type)
1304 return ovsdb_datum_find_key_value(b,
1305 &a->keys[i], type->key.type,
1306 a->values ? &a->values[i] : NULL,
1310 /* Returns true if every element in 'a' is also in 'b', false otherwise. */
1312 ovsdb_datum_includes_all(const struct ovsdb_datum *a,
1313 const struct ovsdb_datum *b,
1314 const struct ovsdb_type *type)
1318 for (i = 0; i < a->n; i++) {
1319 if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
1326 /* Returns true if no element in 'a' is also in 'b', false otherwise. */
1328 ovsdb_datum_excludes_all(const struct ovsdb_datum *a,
1329 const struct ovsdb_datum *b,
1330 const struct ovsdb_type *type)
1334 for (i = 0; i < a->n; i++) {
1335 if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) {
1343 ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type,
1344 unsigned int capacity)
1346 a->keys = xrealloc(a->keys, capacity * sizeof *a->keys);
1347 if (type->value.type != OVSDB_TYPE_VOID) {
1348 a->values = xrealloc(a->values, capacity * sizeof *a->values);
1352 /* Removes the element with index 'idx' from 'datum', which has type 'type'.
1353 * If 'idx' is not the last element in 'datum', then the removed element is
1354 * replaced by the (former) last element.
1356 * This function does not maintain ovsdb_datum invariants. Use
1357 * ovsdb_datum_sort() to check and restore these invariants. */
1359 ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx,
1360 const struct ovsdb_type *type)
1362 ovsdb_atom_destroy(&datum->keys[idx], type->key.type);
1363 datum->keys[idx] = datum->keys[datum->n - 1];
1364 if (type->value.type != OVSDB_TYPE_VOID) {
1365 ovsdb_atom_destroy(&datum->values[idx], type->value.type);
1366 datum->values[idx] = datum->values[datum->n - 1];
1371 /* Adds the element with the given 'key' and 'value' to 'datum', which must
1372 * have the specified 'type'.
1374 * This function always allocates memory, so it is not an efficient way to add
1375 * a number of elements to a datum.
1377 * This function does not maintain ovsdb_datum invariants. Use
1378 * ovsdb_datum_sort() to check and restore these invariants. (But a datum with
1379 * 0 or 1 elements cannot violate the invariants anyhow.) */
1381 ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
1382 const union ovsdb_atom *key,
1383 const union ovsdb_atom *value,
1384 const struct ovsdb_type *type)
1386 size_t idx = datum->n++;
1387 datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
1388 ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
1389 if (type->value.type != OVSDB_TYPE_VOID) {
1390 datum->values = xrealloc(datum->values,
1391 datum->n * sizeof *datum->values);
1392 ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
1397 ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
1398 const struct ovsdb_type *type, bool replace)
1404 for (bi = 0; bi < b->n; bi++) {
1407 ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type);
1408 if (ai == UINT_MAX) {
1410 ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));
1412 ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type);
1413 if (type->value.type != OVSDB_TYPE_VOID) {
1414 ovsdb_atom_clone(&a->values[n], &b->values[bi],
1418 } else if (replace && type->value.type != OVSDB_TYPE_VOID) {
1419 ovsdb_atom_destroy(&a->values[ai], type->value.type);
1420 ovsdb_atom_clone(&a->values[ai], &b->values[bi],
1425 struct ovsdb_error *error;
1427 error = ovsdb_datum_sort(a, type);
1433 ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
1434 const struct ovsdb_datum *b,
1435 const struct ovsdb_type *b_type)
1437 bool changed = false;
1440 assert(a_type->key.type == b_type->key.type);
1441 assert(a_type->value.type == b_type->value.type
1442 || b_type->value.type == OVSDB_TYPE_VOID);
1444 /* XXX The big-O of this could easily be improved. */
1445 for (i = 0; i < a->n; ) {
1446 unsigned int idx = ovsdb_datum_find(a, i, b, b_type);
1447 if (idx != UINT_MAX) {
1449 ovsdb_datum_remove_unsafe(a, i, a_type);
1455 struct ovsdb_error *error = ovsdb_datum_sort(a, a_type);
1460 struct ovsdb_symbol_table {
1464 struct ovsdb_symbol_table *
1465 ovsdb_symbol_table_create(void)
1467 struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab);
1468 shash_init(&symtab->sh);
1473 ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab)
1476 struct shash_node *node, *next;
1478 SHASH_FOR_EACH_SAFE (node, next, &symtab->sh) {
1479 struct ovsdb_symbol *symbol = node->data;
1481 shash_delete(&symtab->sh, node);
1483 shash_destroy(&symtab->sh);
1488 struct ovsdb_symbol *
1489 ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab,
1492 return shash_find_data(&symtab->sh, name);
1495 struct ovsdb_symbol *
1496 ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
1497 const struct uuid *uuid, bool used)
1499 struct ovsdb_symbol *symbol;
1501 assert(!ovsdb_symbol_table_get(symtab, name));
1502 symbol = xmalloc(sizeof *symbol);
1503 symbol->uuid = *uuid;
1504 symbol->used = used;
1505 shash_add(&symtab->sh, name, symbol);
1509 struct ovsdb_symbol *
1510 ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
1513 struct ovsdb_symbol *symbol;
1515 symbol = ovsdb_symbol_table_get(symtab, name);
1519 uuid_generate(&uuid);
1520 symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false);
1525 /* Extracts a token from the beginning of 's' and returns a pointer just after
1526 * the token. Stores the token itself into '*outp', which the caller is
1527 * responsible for freeing (with free()).
1529 * If 's[0]' is a delimiter, the returned token is the empty string.
1531 * A token extends from 's' to the first delimiter, as defined by
1532 * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be
1533 * escaped with a backslash, in which case the backslash does not appear in the
1534 * output. Double quotes also cause delimiters to be ignored, but the double
1535 * quotes are retained in the output. (Backslashes inside double quotes are
1536 * not removed, either.)
1539 ovsdb_token_parse(const char **s, char **outp)
1548 for (p = *s; *p != '\0'; ) {
1552 ds_put_char(&out, '\\');
1555 error = xasprintf("%s: backslash at end of argument", *s);
1558 ds_put_char(&out, *p++);
1559 } else if (!in_quotes && ovsdb_token_is_delim(c)) {
1563 ds_put_char(&out, c);
1565 in_quotes = !in_quotes;
1570 error = xasprintf("%s: quoted string extends past end of argument",
1574 *outp = ds_cstr(&out);
1584 /* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
1586 ovsdb_token_is_delim(unsigned char c)
1588 return strchr(":=, []{}", c) != NULL;