1 /* Copyright (c) 2009, 2010, 2011, 2012, 2014 Nicira, Inc.
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"
25 #include "dynamic-string.h"
27 #include "ovs-thread.h"
28 #include "ovsdb-error.h"
29 #include "ovsdb-parser.h"
37 wrap_json(const char *name, struct json *wrapped)
39 return json_array_create_2(json_string_create(name), wrapped);
42 /* Initializes 'atom' with the default value of the given 'type'.
44 * The default value for an atom is as defined in RFC 7047:
46 * - "integer" or "real": 0
50 * - "string": "" (the empty string)
52 * - "uuid": 00000000-0000-0000-0000-000000000000
54 * The caller must eventually arrange for 'atom' to be destroyed (with
55 * ovsdb_atom_destroy()). */
57 ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
63 case OVSDB_TYPE_INTEGER:
71 case OVSDB_TYPE_BOOLEAN:
72 atom->boolean = false;
75 case OVSDB_TYPE_STRING:
76 atom->string = xmemdup("", 1);
80 uuid_zero(&atom->uuid);
89 /* Returns a read-only atom of the given 'type' that has the default value for
90 * 'type'. The caller must not modify or free the returned atom.
92 * See ovsdb_atom_init_default() for an explanation of the default value of an
94 const union ovsdb_atom *
95 ovsdb_atom_default(enum ovsdb_atomic_type type)
97 static union ovsdb_atom default_atoms[OVSDB_N_TYPES];
98 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
100 if (ovsthread_once_start(&once)) {
103 for (i = 0; i < OVSDB_N_TYPES; i++) {
104 if (i != OVSDB_TYPE_VOID) {
105 ovsdb_atom_init_default(&default_atoms[i], i);
108 ovsthread_once_done(&once);
111 ovs_assert(ovsdb_atomic_type_is_valid(type));
112 return &default_atoms[type];
115 /* Returns true if 'atom', which must have the given 'type', has the default
116 * value for that type.
118 * See ovsdb_atom_init_default() for an explanation of the default value of an
121 ovsdb_atom_is_default(const union ovsdb_atom *atom,
122 enum ovsdb_atomic_type type)
125 case OVSDB_TYPE_VOID:
128 case OVSDB_TYPE_INTEGER:
129 return atom->integer == 0;
131 case OVSDB_TYPE_REAL:
132 return atom->real == 0.0;
134 case OVSDB_TYPE_BOOLEAN:
135 return atom->boolean == false;
137 case OVSDB_TYPE_STRING:
138 return atom->string[0] == '\0';
140 case OVSDB_TYPE_UUID:
141 return uuid_is_zero(&atom->uuid);
149 /* Initializes 'new' as a copy of 'old', with the given 'type'.
151 * The caller must eventually arrange for 'new' to be destroyed (with
152 * ovsdb_atom_destroy()). */
154 ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
155 enum ovsdb_atomic_type type)
158 case OVSDB_TYPE_VOID:
161 case OVSDB_TYPE_INTEGER:
162 new->integer = old->integer;
165 case OVSDB_TYPE_REAL:
166 new->real = old->real;
169 case OVSDB_TYPE_BOOLEAN:
170 new->boolean = old->boolean;
173 case OVSDB_TYPE_STRING:
174 new->string = xstrdup(old->string);
177 case OVSDB_TYPE_UUID:
178 new->uuid = old->uuid;
187 /* Swaps the contents of 'a' and 'b', which need not have the same type. */
189 ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b)
191 union ovsdb_atom tmp = *a;
196 /* Returns a hash value for 'atom', which has the specified 'type', folding
197 * 'basis' into the calculation. */
199 ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
203 case OVSDB_TYPE_VOID:
206 case OVSDB_TYPE_INTEGER:
207 return hash_int(atom->integer, basis);
209 case OVSDB_TYPE_REAL:
210 return hash_double(atom->real, basis);
212 case OVSDB_TYPE_BOOLEAN:
213 return hash_boolean(atom->boolean, basis);
215 case OVSDB_TYPE_STRING:
216 return hash_string(atom->string, basis);
218 case OVSDB_TYPE_UUID:
219 return hash_int(uuid_hash(&atom->uuid), basis);
227 /* Compares 'a' and 'b', which both have type 'type', and returns a
228 * strcmp()-like result. */
230 ovsdb_atom_compare_3way(const union ovsdb_atom *a,
231 const union ovsdb_atom *b,
232 enum ovsdb_atomic_type type)
235 case OVSDB_TYPE_VOID:
238 case OVSDB_TYPE_INTEGER:
239 return a->integer < b->integer ? -1 : a->integer > b->integer;
241 case OVSDB_TYPE_REAL:
242 return a->real < b->real ? -1 : a->real > b->real;
244 case OVSDB_TYPE_BOOLEAN:
245 return a->boolean - b->boolean;
247 case OVSDB_TYPE_STRING:
248 return strcmp(a->string, b->string);
250 case OVSDB_TYPE_UUID:
251 return uuid_compare_3way(&a->uuid, &b->uuid);
259 static struct ovsdb_error *
260 unwrap_json(const struct json *json, const char *name,
261 enum json_type value_type, const struct json **value)
263 if (json->type != JSON_ARRAY
264 || json->u.array.n != 2
265 || json->u.array.elems[0]->type != JSON_STRING
266 || (name && strcmp(json->u.array.elems[0]->u.string, name))
267 || json->u.array.elems[1]->type != value_type)
270 return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
271 json_type_to_string(value_type));
273 *value = json->u.array.elems[1];
277 static struct ovsdb_error *
278 parse_json_pair(const struct json *json,
279 const struct json **elem0, const struct json **elem1)
281 if (json->type != JSON_ARRAY || json->u.array.n != 2) {
282 return ovsdb_syntax_error(json, NULL, "expected 2-element array");
284 *elem0 = json->u.array.elems[0];
285 *elem1 = json->u.array.elems[1];
290 ovsdb_symbol_referenced(struct ovsdb_symbol *symbol,
291 const struct ovsdb_base_type *base)
293 ovs_assert(base->type == OVSDB_TYPE_UUID);
295 if (base->u.uuid.refTableName) {
296 switch (base->u.uuid.refType) {
297 case OVSDB_REF_STRONG:
298 symbol->strong_ref = true;
301 symbol->weak_ref = true;
307 static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
308 ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
309 struct ovsdb_symbol_table *symtab,
310 const struct ovsdb_base_type *base)
312 struct ovsdb_error *error0;
313 const struct json *value;
315 error0 = unwrap_json(json, "uuid", JSON_STRING, &value);
317 const char *uuid_string = json_string(value);
318 if (!uuid_from_string(uuid, uuid_string)) {
319 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
323 struct ovsdb_error *error1;
325 error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
327 struct ovsdb_symbol *symbol;
329 ovsdb_error_destroy(error0);
330 if (!ovsdb_parser_is_id(json_string(value))) {
331 return ovsdb_syntax_error(json, NULL, "named-uuid string is "
335 symbol = ovsdb_symbol_table_insert(symtab, json_string(value));
336 *uuid = symbol->uuid;
337 ovsdb_symbol_referenced(symbol, base);
340 ovsdb_error_destroy(error1);
346 static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
347 ovsdb_atom_from_json__(union ovsdb_atom *atom,
348 const struct ovsdb_base_type *base,
349 const struct json *json,
350 struct ovsdb_symbol_table *symtab)
352 enum ovsdb_atomic_type type = base->type;
355 case OVSDB_TYPE_VOID:
358 case OVSDB_TYPE_INTEGER:
359 if (json->type == JSON_INTEGER) {
360 atom->integer = json->u.integer;
365 case OVSDB_TYPE_REAL:
366 if (json->type == JSON_INTEGER) {
367 atom->real = json->u.integer;
369 } else if (json->type == JSON_REAL) {
370 atom->real = json->u.real;
375 case OVSDB_TYPE_BOOLEAN:
376 if (json->type == JSON_TRUE) {
377 atom->boolean = true;
379 } else if (json->type == JSON_FALSE) {
380 atom->boolean = false;
385 case OVSDB_TYPE_STRING:
386 if (json->type == JSON_STRING) {
387 atom->string = xstrdup(json->u.string);
392 case OVSDB_TYPE_UUID:
393 return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab, base);
400 return ovsdb_syntax_error(json, NULL, "expected %s",
401 ovsdb_atomic_type_to_string(type));
404 /* Parses 'json' as an atom of the type described by 'base'. If successful,
405 * returns NULL and initializes 'atom' with the parsed atom. On failure,
406 * returns an error and the contents of 'atom' are indeterminate. The caller
407 * is responsible for freeing the error or the atom that is returned.
409 * Violations of constraints expressed by 'base' are treated as errors.
411 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
412 * RFC 7047 for information about this, and for the syntax that this function
413 * accepts. If 'base' is a reference and a symbol is parsed, then the symbol's
414 * 'strong_ref' or 'weak_ref' member is set to true, as appropriate. */
416 ovsdb_atom_from_json(union ovsdb_atom *atom,
417 const struct ovsdb_base_type *base,
418 const struct json *json,
419 struct ovsdb_symbol_table *symtab)
421 struct ovsdb_error *error;
423 error = ovsdb_atom_from_json__(atom, base, json, symtab);
428 error = ovsdb_atom_check_constraints(atom, base);
430 ovsdb_atom_destroy(atom, base->type);
435 /* Converts 'atom', of the specified 'type', to JSON format, and returns the
436 * JSON. The caller is responsible for freeing the returned JSON.
438 * Refer to RFC 7047 for the format of the JSON that this function produces. */
440 ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
443 case OVSDB_TYPE_VOID:
446 case OVSDB_TYPE_INTEGER:
447 return json_integer_create(atom->integer);
449 case OVSDB_TYPE_REAL:
450 return json_real_create(atom->real);
452 case OVSDB_TYPE_BOOLEAN:
453 return json_boolean_create(atom->boolean);
455 case OVSDB_TYPE_STRING:
456 return json_string_create(atom->string);
458 case OVSDB_TYPE_UUID:
459 return wrap_json("uuid", json_string_create_nocopy(
460 xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid))));
469 ovsdb_atom_from_string__(union ovsdb_atom *atom,
470 const struct ovsdb_base_type *base, const char *s,
471 struct ovsdb_symbol_table *symtab)
473 enum ovsdb_atomic_type type = base->type;
476 case OVSDB_TYPE_VOID:
479 case OVSDB_TYPE_INTEGER: {
480 long long int integer;
481 if (!str_to_llong(s, 10, &integer)) {
482 return xasprintf("\"%s\" is not a valid integer", s);
484 atom->integer = integer;
488 case OVSDB_TYPE_REAL:
489 if (!str_to_double(s, &atom->real)) {
490 return xasprintf("\"%s\" is not a valid real number", s);
492 /* Our JSON input routines map negative zero to zero, so do that here
493 * too for consistency. */
494 if (atom->real == 0.0) {
499 case OVSDB_TYPE_BOOLEAN:
500 if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on")
501 || !strcmp(s, "1")) {
502 atom->boolean = true;
503 } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off")
504 || !strcmp(s, "0")) {
505 atom->boolean = false;
507 return xasprintf("\"%s\" is not a valid boolean "
508 "(use \"true\" or \"false\")", s);
512 case OVSDB_TYPE_STRING:
514 return xstrdup("An empty string is not valid as input; "
515 "use \"\" to represent the empty string");
516 } else if (*s == '"') {
517 size_t s_len = strlen(s);
519 if (s_len < 2 || s[s_len - 1] != '"') {
520 return xasprintf("%s: missing quote at end of "
522 } else if (!json_string_unescape(s + 1, s_len - 2,
524 char *error = xasprintf("%s: %s", s, atom->string);
529 atom->string = xstrdup(s);
533 case OVSDB_TYPE_UUID:
535 struct ovsdb_symbol *symbol = ovsdb_symbol_table_insert(symtab, s);
536 atom->uuid = symbol->uuid;
537 ovsdb_symbol_referenced(symbol, base);
538 } else if (!uuid_from_string(&atom->uuid, s)) {
539 return xasprintf("\"%s\" is not a valid UUID", s);
551 /* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
552 * one of the following forms:
554 * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
556 * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
559 * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
560 * "no", "off", or "0" for false.
562 * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
563 * an arbitrary string.
565 * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. If 'symtab' is nonnull,
566 * then an identifier beginning with '@' is also acceptable. If the
567 * named identifier is already in 'symtab', then the associated UUID is
568 * used; otherwise, a new, random UUID is used and added to the symbol
569 * table. If 'base' is a reference and a symbol is parsed, then the
570 * symbol's 'strong_ref' or 'weak_ref' member is set to true, as
573 * Returns a null pointer if successful, otherwise an error message describing
574 * the problem. On failure, the contents of 'atom' are indeterminate. The
575 * caller is responsible for freeing the atom or the error.
578 ovsdb_atom_from_string(union ovsdb_atom *atom,
579 const struct ovsdb_base_type *base, const char *s,
580 struct ovsdb_symbol_table *symtab)
582 struct ovsdb_error *error;
585 msg = ovsdb_atom_from_string__(atom, base, s, symtab);
590 error = ovsdb_atom_check_constraints(atom, base);
592 ovsdb_atom_destroy(atom, base->type);
593 msg = ovsdb_error_to_string(error);
594 ovsdb_error_destroy(error);
600 string_needs_quotes(const char *s)
606 if (!isalpha(c) && c != '_') {
610 while ((c = *p++) != '\0') {
611 if (!isalpha(c) && c != '_' && c != '-' && c != '.') {
616 if (!strcmp(s, "true") || !strcmp(s, "false")) {
623 /* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
624 * to ovsdb_atom_from_string(). */
626 ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
630 case OVSDB_TYPE_VOID:
633 case OVSDB_TYPE_INTEGER:
634 ds_put_format(out, "%"PRId64, atom->integer);
637 case OVSDB_TYPE_REAL:
638 ds_put_format(out, "%.*g", DBL_DIG, atom->real);
641 case OVSDB_TYPE_BOOLEAN:
642 ds_put_cstr(out, atom->boolean ? "true" : "false");
645 case OVSDB_TYPE_STRING:
646 if (string_needs_quotes(atom->string)) {
649 json.type = JSON_STRING;
650 json.u.string = atom->string;
651 json_to_ds(&json, 0, out);
653 ds_put_cstr(out, atom->string);
657 case OVSDB_TYPE_UUID:
658 ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid));
667 /* Appends 'atom' (which has the given 'type') to 'out', in a bare string
668 * format that cannot be parsed uniformly back into a datum but is easier for
669 * shell scripts, etc., to deal with. */
671 ovsdb_atom_to_bare(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
674 if (type == OVSDB_TYPE_STRING) {
675 ds_put_cstr(out, atom->string);
677 ovsdb_atom_to_string(atom, type, out);
681 static struct ovsdb_error *
682 check_string_constraints(const char *s,
683 const struct ovsdb_string_constraints *c)
688 msg = utf8_validate(s, &n_chars);
690 struct ovsdb_error *error;
692 error = ovsdb_error("constraint violation",
693 "not a valid UTF-8 string: %s", msg);
698 if (n_chars < c->minLen) {
700 "constraint violation",
701 "\"%s\" length %"PRIuSIZE" is less than minimum allowed "
702 "length %u", s, n_chars, c->minLen);
703 } else if (n_chars > c->maxLen) {
705 "constraint violation",
706 "\"%s\" length %"PRIuSIZE" is greater than maximum allowed "
707 "length %u", s, n_chars, c->maxLen);
713 /* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
714 * (base->type must specify 'atom''s type.) Returns a null pointer if the
715 * constraints are met, otherwise an error that explains the violation.
717 * Checking UUID constraints is deferred to transaction commit time, so this
718 * function does nothing for UUID constraints. */
720 ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
721 const struct ovsdb_base_type *base)
724 && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {
725 struct ovsdb_error *error;
726 struct ds actual = DS_EMPTY_INITIALIZER;
727 struct ds valid = DS_EMPTY_INITIALIZER;
729 ovsdb_atom_to_string(atom, base->type, &actual);
730 ovsdb_datum_to_string(base->enum_,
731 ovsdb_base_type_get_enum_type(base->type),
733 error = ovsdb_error("constraint violation",
734 "%s is not one of the allowed values (%s)",
735 ds_cstr(&actual), ds_cstr(&valid));
742 switch (base->type) {
743 case OVSDB_TYPE_VOID:
746 case OVSDB_TYPE_INTEGER:
747 if (atom->integer >= base->u.integer.min
748 && atom->integer <= base->u.integer.max) {
750 } else if (base->u.integer.min != INT64_MIN) {
751 if (base->u.integer.max != INT64_MAX) {
752 return ovsdb_error("constraint violation",
753 "%"PRId64" is not in the valid range "
754 "%"PRId64" to %"PRId64" (inclusive)",
756 base->u.integer.min, base->u.integer.max);
758 return ovsdb_error("constraint violation",
759 "%"PRId64" is less than minimum allowed "
761 atom->integer, base->u.integer.min);
764 return ovsdb_error("constraint violation",
765 "%"PRId64" is greater than maximum allowed "
767 atom->integer, base->u.integer.max);
771 case OVSDB_TYPE_REAL:
772 if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
774 } else if (base->u.real.min != -DBL_MAX) {
775 if (base->u.real.max != DBL_MAX) {
776 return ovsdb_error("constraint violation",
777 "%.*g is not in the valid range "
778 "%.*g to %.*g (inclusive)",
780 DBL_DIG, base->u.real.min,
781 DBL_DIG, base->u.real.max);
783 return ovsdb_error("constraint violation",
784 "%.*g is less than minimum allowed "
787 DBL_DIG, base->u.real.min);
790 return ovsdb_error("constraint violation",
791 "%.*g is greater than maximum allowed "
794 DBL_DIG, base->u.real.max);
798 case OVSDB_TYPE_BOOLEAN:
801 case OVSDB_TYPE_STRING:
802 return check_string_constraints(atom->string, &base->u.string);
804 case OVSDB_TYPE_UUID:
813 static union ovsdb_atom *
814 alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
816 if (type != OVSDB_TYPE_VOID && n) {
817 union ovsdb_atom *atoms;
820 atoms = xmalloc(n * sizeof *atoms);
821 for (i = 0; i < n; i++) {
822 ovsdb_atom_init_default(&atoms[i], type);
826 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
827 * treated as xmalloc(1). */
832 /* Initializes 'datum' as an empty datum. (An empty datum can be treated as
835 ovsdb_datum_init_empty(struct ovsdb_datum *datum)
839 datum->values = NULL;
842 /* Initializes 'datum' as a datum that has the default value for 'type'.
844 * The default value for a particular type is as defined in RFC 7047:
846 * - If n_min is 0, then the default value is the empty set (or map).
848 * - If n_min is 1, the default value is a single value or a single
849 * key-value pair, whose key and value are the defaults for their
850 * atomic types. (See ovsdb_atom_init_default() for details.)
852 * - n_min > 1 is invalid. See ovsdb_type_is_valid().
855 ovsdb_datum_init_default(struct ovsdb_datum *datum,
856 const struct ovsdb_type *type)
858 datum->n = type->n_min;
859 datum->keys = alloc_default_atoms(type->key.type, datum->n);
860 datum->values = alloc_default_atoms(type->value.type, datum->n);
863 /* Returns a read-only datum of the given 'type' that has the default value for
864 * 'type'. The caller must not modify or free the returned datum.
866 * See ovsdb_datum_init_default() for an explanation of the default value of a
868 const struct ovsdb_datum *
869 ovsdb_datum_default(const struct ovsdb_type *type)
871 if (type->n_min == 0) {
872 static const struct ovsdb_datum empty;
874 } else if (type->n_min == 1) {
875 static struct ovsdb_datum default_data[OVSDB_N_TYPES][OVSDB_N_TYPES];
876 struct ovsdb_datum *d;
877 int kt = type->key.type;
878 int vt = type->value.type;
880 ovs_assert(ovsdb_type_is_valid(type));
882 d = &default_data[kt][vt];
885 d->keys = CONST_CAST(union ovsdb_atom *, ovsdb_atom_default(kt));
886 if (vt != OVSDB_TYPE_VOID) {
887 d->values = CONST_CAST(union ovsdb_atom *,
888 ovsdb_atom_default(vt));
897 /* Returns true if 'datum', which must have the given 'type', has the default
898 * value for that type.
900 * See ovsdb_datum_init_default() for an explanation of the default value of a
903 ovsdb_datum_is_default(const struct ovsdb_datum *datum,
904 const struct ovsdb_type *type)
908 if (datum->n != type->n_min) {
911 for (i = 0; i < datum->n; i++) {
912 if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) {
915 if (type->value.type != OVSDB_TYPE_VOID
916 && !ovsdb_atom_is_default(&datum->values[i], type->value.type)) {
924 static union ovsdb_atom *
925 clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
927 if (type != OVSDB_TYPE_VOID && n) {
928 union ovsdb_atom *new;
931 new = xmalloc(n * sizeof *new);
932 for (i = 0; i < n; i++) {
933 ovsdb_atom_clone(&new[i], &old[i], type);
937 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
938 * treated as xmalloc(1). */
943 /* Initializes 'new' as a copy of 'old', with the given 'type'.
945 * The caller must eventually arrange for 'new' to be destroyed (with
946 * ovsdb_datum_destroy()). */
948 ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old,
949 const struct ovsdb_type *type)
951 unsigned int n = old->n;
953 new->keys = clone_atoms(old->keys, type->key.type, n);
954 new->values = clone_atoms(old->values, type->value.type, n);
958 free_data(enum ovsdb_atomic_type type,
959 union ovsdb_atom *atoms, size_t n_atoms)
961 if (ovsdb_atom_needs_destruction(type)) {
963 for (i = 0; i < n_atoms; i++) {
964 ovsdb_atom_destroy(&atoms[i], type);
970 /* Frees the data owned by 'datum', which must have the given 'type'.
972 * This does not actually call free(datum). If necessary, the caller must be
973 * responsible for that. */
975 ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type)
977 free_data(type->key.type, datum->keys, datum->n);
978 free_data(type->value.type, datum->values, datum->n);
981 /* Swaps the contents of 'a' and 'b', which need not have the same type. */
983 ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
985 struct ovsdb_datum tmp = *a;
990 struct ovsdb_datum_sort_cbdata {
991 enum ovsdb_atomic_type key_type;
992 enum ovsdb_atomic_type value_type;
993 struct ovsdb_datum *datum;
997 ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
999 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
1002 retval = ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
1003 &cbdata->datum->keys[b],
1005 if (retval || cbdata->value_type == OVSDB_TYPE_VOID) {
1009 return ovsdb_atom_compare_3way(&cbdata->datum->values[a],
1010 &cbdata->datum->values[b],
1011 cbdata->value_type);
1015 ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
1017 struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
1019 ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
1020 if (cbdata->datum->values) {
1021 ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
1026 ovsdb_datum_sort__(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type,
1027 enum ovsdb_atomic_type value_type)
1029 struct ovsdb_datum_sort_cbdata cbdata;
1031 cbdata.key_type = key_type;
1032 cbdata.value_type = value_type;
1033 cbdata.datum = datum;
1034 sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
1038 /* The keys in an ovsdb_datum must be unique and in sorted order. Most
1039 * functions that modify an ovsdb_datum maintain these invariants. For those
1040 * that don't, this function checks and restores these invariants for 'datum',
1041 * whose keys are of type 'key_type'.
1043 * This function returns NULL if successful, otherwise an error message. The
1044 * caller must free the returned error when it is no longer needed. On error,
1045 * 'datum' is sorted but not unique. */
1046 struct ovsdb_error *
1047 ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
1055 ovsdb_datum_sort__(datum, key_type, OVSDB_TYPE_VOID);
1057 for (i = 0; i < datum->n - 1; i++) {
1058 if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
1060 if (datum->values) {
1061 return ovsdb_error(NULL, "map contains duplicate key");
1063 return ovsdb_error(NULL, "set contains duplicate");
1070 /* This function is the same as ovsdb_datum_sort(), except that the caller
1071 * knows that 'datum' is unique. The operation therefore "cannot fail", so
1072 * this function assert-fails if it actually does. */
1074 ovsdb_datum_sort_assert(struct ovsdb_datum *datum,
1075 enum ovsdb_atomic_type key_type)
1077 struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
1083 /* This is similar to ovsdb_datum_sort(), except that it drops duplicate keys
1084 * instead of reporting an error. In a map type, the smallest value among a
1085 * group of duplicate pairs is retained and the others are dropped.
1087 * Returns the number of keys (or pairs) that were dropped. */
1089 ovsdb_datum_sort_unique(struct ovsdb_datum *datum,
1090 enum ovsdb_atomic_type key_type,
1091 enum ovsdb_atomic_type value_type)
1099 ovsdb_datum_sort__(datum, key_type, value_type);
1102 for (src = 1; src < datum->n; src++) {
1103 if (ovsdb_atom_equals(&datum->keys[src], &datum->keys[dst - 1],
1105 ovsdb_atom_destroy(&datum->keys[src], key_type);
1106 if (value_type != OVSDB_TYPE_VOID) {
1107 ovsdb_atom_destroy(&datum->values[src], value_type);
1111 datum->keys[dst] = datum->keys[src];
1112 if (value_type != OVSDB_TYPE_VOID) {
1113 datum->values[dst] = datum->values[src];
1120 return datum->n - src;
1123 /* Checks that each of the atoms in 'datum' conforms to the constraints
1124 * specified by its 'type'. Returns an error if a constraint is violated,
1125 * otherwise a null pointer.
1127 * This function is not commonly useful because the most ordinary way to obtain
1128 * a datum is ultimately via ovsdb_atom_from_string() or
1129 * ovsdb_atom_from_json(), which check constraints themselves. */
1130 struct ovsdb_error *
1131 ovsdb_datum_check_constraints(const struct ovsdb_datum *datum,
1132 const struct ovsdb_type *type)
1134 struct ovsdb_error *error;
1137 for (i = 0; i < datum->n; i++) {
1138 error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key);
1144 if (type->value.type != OVSDB_TYPE_VOID) {
1145 for (i = 0; i < datum->n; i++) {
1146 error = ovsdb_atom_check_constraints(&datum->values[i],
1157 static struct ovsdb_error *
1158 ovsdb_datum_from_json__(struct ovsdb_datum *datum,
1159 const struct ovsdb_type *type,
1160 const struct json *json,
1161 struct ovsdb_symbol_table *symtab)
1163 struct ovsdb_error *error;
1165 if (ovsdb_type_is_map(type)
1166 || (json->type == JSON_ARRAY
1167 && json->u.array.n > 0
1168 && json->u.array.elems[0]->type == JSON_STRING
1169 && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
1170 bool is_map = ovsdb_type_is_map(type);
1171 const char *class = is_map ? "map" : "set";
1172 const struct json *inner;
1176 error = unwrap_json(json, class, JSON_ARRAY, &inner);
1181 n = inner->u.array.n;
1182 if (n < type->n_min || n > type->n_max) {
1183 return ovsdb_syntax_error(json, NULL, "%s must have %u to "
1184 "%u members but %"PRIuSIZE" are present",
1185 class, type->n_min, type->n_max, n);
1189 datum->keys = xmalloc(n * sizeof *datum->keys);
1190 datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL;
1191 for (i = 0; i < n; i++) {
1192 const struct json *element = inner->u.array.elems[i];
1193 const struct json *key = NULL;
1194 const struct json *value = NULL;
1199 error = parse_json_pair(element, &key, &value);
1205 error = ovsdb_atom_from_json(&datum->keys[i], &type->key,
1212 error = ovsdb_atom_from_json(&datum->values[i],
1213 &type->value, value, symtab);
1215 ovsdb_atom_destroy(&datum->keys[i], type->key.type);
1225 ovsdb_datum_destroy(datum, type);
1229 datum->keys = xmalloc(sizeof *datum->keys);
1230 datum->values = NULL;
1232 error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
1241 /* Parses 'json' as a datum of the type described by 'type'. If successful,
1242 * returns NULL and initializes 'datum' with the parsed datum. On failure,
1243 * returns an error and the contents of 'datum' are indeterminate. The caller
1244 * is responsible for freeing the error or the datum that is returned.
1246 * Violations of constraints expressed by 'type' are treated as errors.
1248 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
1249 * RFC 7047 for information about this, and for the syntax that this function
1251 struct ovsdb_error *
1252 ovsdb_datum_from_json(struct ovsdb_datum *datum,
1253 const struct ovsdb_type *type,
1254 const struct json *json,
1255 struct ovsdb_symbol_table *symtab)
1257 struct ovsdb_error *error;
1259 error = ovsdb_datum_from_json__(datum, type, json, symtab);
1264 error = ovsdb_datum_sort(datum, type->key.type);
1266 ovsdb_datum_destroy(datum, type);
1271 /* Parses 'json' as a datum of the type described by 'type' for internal
1272 * use. This function is similar to 'ovsdb_datum_from_json', except the
1273 * member size of set or map is not checked.
1275 * The datum generated should be used then discard. It is not suitable
1276 * for storing into IDL because of the possible member size violation. */
1277 struct ovsdb_error *
1278 ovsdb_transient_datum_from_json(struct ovsdb_datum *datum,
1279 const struct ovsdb_type *type,
1280 const struct json *json)
1282 struct ovsdb_type relaxed_type = *type;
1284 relaxed_type.n_min = 0;
1285 relaxed_type.n_max = UINT_MAX;
1287 return ovsdb_datum_from_json(datum, &relaxed_type, json, NULL);
1290 /* Converts 'datum', of the specified 'type', to JSON format, and returns the
1291 * JSON. The caller is responsible for freeing the returned JSON.
1293 * 'type' constraints on datum->n are ignored.
1295 * Refer to RFC 7047 for the format of the JSON that this function produces. */
1297 ovsdb_datum_to_json(const struct ovsdb_datum *datum,
1298 const struct ovsdb_type *type)
1300 if (ovsdb_type_is_map(type)) {
1301 struct json **elems;
1304 elems = xmalloc(datum->n * sizeof *elems);
1305 for (i = 0; i < datum->n; i++) {
1306 elems[i] = json_array_create_2(
1307 ovsdb_atom_to_json(&datum->keys[i], type->key.type),
1308 ovsdb_atom_to_json(&datum->values[i], type->value.type));
1311 return wrap_json("map", json_array_create(elems, datum->n));
1312 } else if (datum->n == 1) {
1313 return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
1315 struct json **elems;
1318 elems = xmalloc(datum->n * sizeof *elems);
1319 for (i = 0; i < datum->n; i++) {
1320 elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
1323 return wrap_json("set", json_array_create(elems, datum->n));
1328 skip_spaces(const char *p)
1330 while (isspace((unsigned char) *p)) {
1337 parse_atom_token(const char **s, const struct ovsdb_base_type *base,
1338 union ovsdb_atom *atom, struct ovsdb_symbol_table *symtab)
1340 char *token, *error;
1342 error = ovsdb_token_parse(s, &token);
1344 error = ovsdb_atom_from_string(atom, base, token, symtab);
1351 parse_key_value(const char **s, const struct ovsdb_type *type,
1352 union ovsdb_atom *key, union ovsdb_atom *value,
1353 struct ovsdb_symbol_table *symtab)
1355 const char *start = *s;
1358 error = parse_atom_token(s, &type->key, key, symtab);
1359 if (!error && type->value.type != OVSDB_TYPE_VOID) {
1360 *s = skip_spaces(*s);
1363 *s = skip_spaces(*s);
1364 error = parse_atom_token(s, &type->value, value, symtab);
1366 error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
1370 ovsdb_atom_destroy(key, type->key.type);
1377 free_key_value(const struct ovsdb_type *type,
1378 union ovsdb_atom *key, union ovsdb_atom *value)
1380 ovsdb_atom_destroy(key, type->key.type);
1381 if (type->value.type != OVSDB_TYPE_VOID) {
1382 ovsdb_atom_destroy(value, type->value.type);
1386 /* Initializes 'datum' as a datum of the given 'type', parsing its contents
1387 * from 's'. The format of 's' is a series of space or comma separated atoms
1388 * or, for a map, '='-delimited pairs of atoms. Each atom must in a format
1389 * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
1390 * in "[]" or a map in "{}"; for an empty set or map these punctuators are
1393 * Optionally, a symbol table may be supplied as 'symtab'. It is passed to
1394 * ovsdb_atom_to_string(). */
1396 ovsdb_datum_from_string(struct ovsdb_datum *datum,
1397 const struct ovsdb_type *type, const char *s,
1398 struct ovsdb_symbol_table *symtab)
1400 bool is_map = ovsdb_type_is_map(type);
1401 struct ovsdb_error *dberror;
1406 ovsdb_datum_init_empty(datum);
1408 /* Swallow a leading delimiter if there is one. */
1410 if (*p == (is_map ? '{' : '[')) {
1411 end_delim = is_map ? '}' : ']';
1412 p = skip_spaces(p + 1);
1415 return xstrdup("use \"{}\" to specify the empty map");
1417 return xstrdup("use \"[]\" to specify the empty set");
1423 while (*p && *p != end_delim) {
1424 union ovsdb_atom key, value;
1426 if (ovsdb_token_is_delim(*p)) {
1427 char *type_str = ovsdb_type_to_english(type);
1428 error = xasprintf("%s: unexpected \"%c\" parsing %s",
1435 error = parse_key_value(&p, type, &key, &value, symtab);
1439 ovsdb_datum_add_unsafe(datum, &key, &value, type);
1440 free_key_value(type, &key, &value);
1442 /* Skip optional white space and comma. */
1445 p = skip_spaces(p + 1);
1449 if (*p != end_delim) {
1450 error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim);
1454 p = skip_spaces(p + 1);
1456 error = xasprintf("%s: trailing garbage after \"%c\"",
1462 if (datum->n < type->n_min) {
1463 error = xasprintf("%s: %u %s specified but the minimum number is %u",
1464 s, datum->n, is_map ? "pair(s)" : "value(s)",
1467 } else if (datum->n > type->n_max) {
1468 error = xasprintf("%s: %u %s specified but the maximum number is %u",
1469 s, datum->n, is_map ? "pair(s)" : "value(s)",
1474 dberror = ovsdb_datum_sort(datum, type->key.type);
1476 ovsdb_error_destroy(dberror);
1477 if (ovsdb_type_is_map(type)) {
1478 error = xasprintf("%s: map contains duplicate key", s);
1480 error = xasprintf("%s: set contains duplicate value", s);
1488 ovsdb_datum_destroy(datum, type);
1489 ovsdb_datum_init_empty(datum);
1493 /* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
1494 * to ovsdb_datum_from_string(). */
1496 ovsdb_datum_to_string(const struct ovsdb_datum *datum,
1497 const struct ovsdb_type *type, struct ds *out)
1499 bool is_map = ovsdb_type_is_map(type);
1502 if (type->n_max > 1 || !datum->n) {
1503 ds_put_char(out, is_map ? '{' : '[');
1505 for (i = 0; i < datum->n; i++) {
1507 ds_put_cstr(out, ", ");
1510 ovsdb_atom_to_string(&datum->keys[i], type->key.type, out);
1512 ds_put_char(out, '=');
1513 ovsdb_atom_to_string(&datum->values[i], type->value.type, out);
1516 if (type->n_max > 1 || !datum->n) {
1517 ds_put_char(out, is_map ? '}' : ']');
1521 /* Appends to 'out' the 'datum' (with the given 'type') in a bare string format
1522 * that cannot be parsed uniformly back into a datum but is easier for shell
1523 * scripts, etc., to deal with. */
1525 ovsdb_datum_to_bare(const struct ovsdb_datum *datum,
1526 const struct ovsdb_type *type, struct ds *out)
1528 bool is_map = ovsdb_type_is_map(type);
1531 for (i = 0; i < datum->n; i++) {
1533 ds_put_cstr(out, " ");
1536 ovsdb_atom_to_bare(&datum->keys[i], type->key.type, out);
1538 ds_put_char(out, '=');
1539 ovsdb_atom_to_bare(&datum->values[i], type->value.type, out);
1544 /* Initializes 'datum' as a string-to-string map whose contents are taken from
1545 * 'smap'. Destroys 'smap'. */
1547 ovsdb_datum_from_smap(struct ovsdb_datum *datum, struct smap *smap)
1549 struct smap_node *node, *next;
1552 datum->n = smap_count(smap);
1553 datum->keys = xmalloc(datum->n * sizeof *datum->keys);
1554 datum->values = xmalloc(datum->n * sizeof *datum->values);
1557 SMAP_FOR_EACH_SAFE (node, next, smap) {
1558 smap_steal(smap, node,
1559 &datum->keys[i].string, &datum->values[i].string);
1562 ovs_assert(i == datum->n);
1565 ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
1569 hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
1570 unsigned int n, uint32_t basis)
1572 if (type != OVSDB_TYPE_VOID) {
1575 for (i = 0; i < n; i++) {
1576 basis = ovsdb_atom_hash(&atoms[i], type, basis);
1583 ovsdb_datum_hash(const struct ovsdb_datum *datum,
1584 const struct ovsdb_type *type, uint32_t basis)
1586 basis = hash_atoms(type->key.type, datum->keys, datum->n, basis);
1587 basis ^= (type->key.type << 24) | (type->value.type << 16) | datum->n;
1588 basis = hash_atoms(type->value.type, datum->values, datum->n, basis);
1593 atom_arrays_compare_3way(const union ovsdb_atom *a,
1594 const union ovsdb_atom *b,
1595 enum ovsdb_atomic_type type,
1600 for (i = 0; i < n; i++) {
1601 int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type);
1611 ovsdb_datum_equals(const struct ovsdb_datum *a,
1612 const struct ovsdb_datum *b,
1613 const struct ovsdb_type *type)
1615 return !ovsdb_datum_compare_3way(a, b, type);
1619 ovsdb_datum_compare_3way(const struct ovsdb_datum *a,
1620 const struct ovsdb_datum *b,
1621 const struct ovsdb_type *type)
1626 return a->n < b->n ? -1 : 1;
1629 cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key.type, a->n);
1634 return (type->value.type == OVSDB_TYPE_VOID ? 0
1635 : atom_arrays_compare_3way(a->values, b->values, type->value.type,
1639 /* If 'key' is one of the keys in 'datum', returns its index within 'datum',
1640 * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the
1641 * 'keys' array in 'datum'.
1644 ovsdb_datum_find_key(const struct ovsdb_datum *datum,
1645 const union ovsdb_atom *key,
1646 enum ovsdb_atomic_type key_type)
1648 unsigned int low = 0;
1649 unsigned int high = datum->n;
1650 while (low < high) {
1651 unsigned int idx = (low + high) / 2;
1652 int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type);
1655 } else if (cmp > 0) {
1664 /* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
1665 * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of
1666 * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the
1667 * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
1670 ovsdb_datum_find_key_value(const struct ovsdb_datum *datum,
1671 const union ovsdb_atom *key,
1672 enum ovsdb_atomic_type key_type,
1673 const union ovsdb_atom *value,
1674 enum ovsdb_atomic_type value_type)
1676 unsigned int idx = ovsdb_datum_find_key(datum, key, key_type);
1678 && value_type != OVSDB_TYPE_VOID
1679 && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) {
1685 /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
1686 * UINT_MAX. 'type' must be the type of 'a' and 'b', except that
1687 * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not
1690 ovsdb_datum_find(const struct ovsdb_datum *a, int i,
1691 const struct ovsdb_datum *b,
1692 const struct ovsdb_type *type)
1694 return ovsdb_datum_find_key_value(b,
1695 &a->keys[i], type->key.type,
1696 a->values ? &a->values[i] : NULL,
1700 /* Returns true if every element in 'a' is also in 'b', false otherwise. */
1702 ovsdb_datum_includes_all(const struct ovsdb_datum *a,
1703 const struct ovsdb_datum *b,
1704 const struct ovsdb_type *type)
1711 for (i = 0; i < a->n; i++) {
1712 if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
1719 /* Returns true if no element in 'a' is also in 'b', false otherwise. */
1721 ovsdb_datum_excludes_all(const struct ovsdb_datum *a,
1722 const struct ovsdb_datum *b,
1723 const struct ovsdb_type *type)
1727 for (i = 0; i < a->n; i++) {
1728 if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) {
1736 ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type,
1737 unsigned int capacity)
1739 a->keys = xrealloc(a->keys, capacity * sizeof *a->keys);
1740 if (type->value.type != OVSDB_TYPE_VOID) {
1741 a->values = xrealloc(a->values, capacity * sizeof *a->values);
1745 /* Removes the element with index 'idx' from 'datum', which has type 'type'.
1746 * If 'idx' is not the last element in 'datum', then the removed element is
1747 * replaced by the (former) last element.
1749 * This function does not maintain ovsdb_datum invariants. Use
1750 * ovsdb_datum_sort() to check and restore these invariants. */
1752 ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx,
1753 const struct ovsdb_type *type)
1755 ovsdb_atom_destroy(&datum->keys[idx], type->key.type);
1756 datum->keys[idx] = datum->keys[datum->n - 1];
1757 if (type->value.type != OVSDB_TYPE_VOID) {
1758 ovsdb_atom_destroy(&datum->values[idx], type->value.type);
1759 datum->values[idx] = datum->values[datum->n - 1];
1764 /* Adds the element with the given 'key' and 'value' to 'datum', which must
1765 * have the specified 'type'.
1767 * This function always allocates memory, so it is not an efficient way to add
1768 * a number of elements to a datum.
1770 * This function does not maintain ovsdb_datum invariants. Use
1771 * ovsdb_datum_sort() to check and restore these invariants. (But a datum with
1772 * 0 or 1 elements cannot violate the invariants anyhow.) */
1774 ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
1775 const union ovsdb_atom *key,
1776 const union ovsdb_atom *value,
1777 const struct ovsdb_type *type)
1779 size_t idx = datum->n++;
1780 datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
1781 ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
1782 if (type->value.type != OVSDB_TYPE_VOID) {
1783 datum->values = xrealloc(datum->values,
1784 datum->n * sizeof *datum->values);
1785 ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
1790 ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
1791 const struct ovsdb_type *type, bool replace)
1797 for (bi = 0; bi < b->n; bi++) {
1800 ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type);
1801 if (ai == UINT_MAX) {
1803 ovsdb_datum_reallocate(a, type, a->n + (b->n - bi));
1805 ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type);
1806 if (type->value.type != OVSDB_TYPE_VOID) {
1807 ovsdb_atom_clone(&a->values[n], &b->values[bi],
1811 } else if (replace && type->value.type != OVSDB_TYPE_VOID) {
1812 ovsdb_atom_destroy(&a->values[ai], type->value.type);
1813 ovsdb_atom_clone(&a->values[ai], &b->values[bi],
1818 struct ovsdb_error *error;
1820 error = ovsdb_datum_sort(a, type->key.type);
1826 ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
1827 const struct ovsdb_datum *b,
1828 const struct ovsdb_type *b_type)
1830 bool changed = false;
1833 ovs_assert(a_type->key.type == b_type->key.type);
1834 ovs_assert(a_type->value.type == b_type->value.type
1835 || b_type->value.type == OVSDB_TYPE_VOID);
1837 /* XXX The big-O of this could easily be improved. */
1838 for (i = 0; i < a->n; ) {
1839 unsigned int idx = ovsdb_datum_find(a, i, b, b_type);
1840 if (idx != UINT_MAX) {
1842 ovsdb_datum_remove_unsafe(a, i, a_type);
1848 ovsdb_datum_sort_assert(a, a_type->key.type);
1852 struct ovsdb_symbol_table *
1853 ovsdb_symbol_table_create(void)
1855 struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab);
1856 shash_init(&symtab->sh);
1861 ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab)
1864 shash_destroy_free_data(&symtab->sh);
1869 struct ovsdb_symbol *
1870 ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab,
1873 return shash_find_data(&symtab->sh, name);
1876 struct ovsdb_symbol *
1877 ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
1878 const struct uuid *uuid, bool created)
1880 struct ovsdb_symbol *symbol;
1882 ovs_assert(!ovsdb_symbol_table_get(symtab, name));
1883 symbol = xmalloc(sizeof *symbol);
1884 symbol->uuid = *uuid;
1885 symbol->created = created;
1886 symbol->strong_ref = false;
1887 symbol->weak_ref = false;
1888 shash_add(&symtab->sh, name, symbol);
1892 struct ovsdb_symbol *
1893 ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
1896 struct ovsdb_symbol *symbol;
1898 symbol = ovsdb_symbol_table_get(symtab, name);
1902 uuid_generate(&uuid);
1903 symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false);
1908 /* APIs for Generating and apply diffs. */
1910 /* Generate a difference ovsdb_dataum between 'old' and 'new'.
1911 * 'new' can be regenerated by applying the difference to the 'old'.
1913 * The diff operation is reversible. Given 'old',
1914 * 'new' can be recreated by applying diff to 'old'.
1917 * Let d = 'old' diff 'new'
1918 * then 'new' = 'old' diff d
1920 * The 'diff' datum is always safe; the orders of keys are maintained
1921 * since they are added in order. */
1923 ovsdb_datum_diff(struct ovsdb_datum *diff,
1924 const struct ovsdb_datum *old,
1925 const struct ovsdb_datum *new,
1926 const struct ovsdb_type *type)
1930 ovsdb_datum_init_empty(diff);
1931 if (!ovsdb_type_is_composite(type)) {
1932 ovsdb_datum_clone(diff, new, type);
1936 /* Generate the diff in O(n) time. */
1937 for (oi = ni = 0; oi < old->n && ni < new->n; ) {
1938 int c = ovsdb_atom_compare_3way(&old->keys[oi], &new->keys[ni],
1941 ovsdb_datum_add_unsafe(diff, &old->keys[oi], &old->values[oi],
1945 ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni],
1949 if (type->value.type != OVSDB_TYPE_VOID &&
1950 ovsdb_atom_compare_3way(&old->values[oi], &new->values[ni],
1951 type->value.type)) {
1952 ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni],
1959 for (; oi < old->n; oi++) {
1960 ovsdb_datum_add_unsafe(diff, &old->keys[oi], &old->values[oi], type);
1963 for (; ni < new->n; ni++) {
1964 ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni], type);
1968 /* Apply 'diff' to 'old' to regenerate 'new'.
1970 * Return NULL if the 'new' is successfully generated, otherwise, return
1971 * ovsdb_error and the stat of 'new' is indeterministic. */
1972 struct ovsdb_error *
1973 ovsdb_datum_apply_diff(struct ovsdb_datum *new,
1974 const struct ovsdb_datum *old,
1975 const struct ovsdb_datum *diff,
1976 const struct ovsdb_type *type)
1978 ovsdb_datum_init_empty(new);
1979 ovsdb_datum_diff(new, old, diff, type);
1981 /* Make sure member size of 'new' conforms to type. */
1982 if (new->n < type->n_min || new->n > type->n_max) {
1983 ovsdb_datum_destroy(new, type);
1984 return ovsdb_error(NULL, "Datum crated by diff has size error");
1991 /* Extracts a token from the beginning of 's' and returns a pointer just after
1992 * the token. Stores the token itself into '*outp', which the caller is
1993 * responsible for freeing (with free()).
1995 * If 's[0]' is a delimiter, the returned token is the empty string.
1997 * A token extends from 's' to the first delimiter, as defined by
1998 * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be
1999 * escaped with a backslash, in which case the backslash does not appear in the
2000 * output. Double quotes also cause delimiters to be ignored, but the double
2001 * quotes are retained in the output. (Backslashes inside double quotes are
2002 * not removed, either.)
2005 ovsdb_token_parse(const char **s, char **outp)
2014 for (p = *s; *p != '\0'; ) {
2018 ds_put_char(&out, '\\');
2021 error = xasprintf("%s: backslash at end of argument", *s);
2024 ds_put_char(&out, *p++);
2025 } else if (!in_quotes && ovsdb_token_is_delim(c)) {
2029 ds_put_char(&out, c);
2031 in_quotes = !in_quotes;
2036 error = xasprintf("%s: quoted string extends past end of argument",
2040 *outp = ds_cstr(&out);
2050 /* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
2052 ovsdb_token_is_delim(unsigned char c)
2054 return strchr(":=, []{}!<>", c) != NULL;