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
);
40 /* Initializes 'atom' with the default value of the given 'type'.
42 * The default value for an atom is as defined in ovsdb/SPECS:
44 * - "integer" or "real": 0
48 * - "string": "" (the empty string)
50 * - "uuid": 00000000-0000-0000-0000-000000000000
52 * The caller must eventually arrange for 'atom' to be destroyed (with
53 * ovsdb_atom_destroy()). */
55 ovsdb_atom_init_default(union ovsdb_atom
*atom
, enum ovsdb_atomic_type type
)
61 case OVSDB_TYPE_INTEGER
:
69 case OVSDB_TYPE_BOOLEAN
:
70 atom
->boolean
= false;
73 case OVSDB_TYPE_STRING
:
74 atom
->string
= xmemdup("", 1);
78 uuid_zero(&atom
->uuid
);
87 /* Returns a read-only atom of the given 'type' that has the default value for
88 * 'type'. The caller must not modify or free the returned atom.
90 * See ovsdb_atom_init_default() for an explanation of the default value of an
92 const union ovsdb_atom
*
93 ovsdb_atom_default(enum ovsdb_atomic_type type
)
95 static union ovsdb_atom default_atoms
[OVSDB_N_TYPES
];
101 for (i
= 0; i
< OVSDB_N_TYPES
; i
++) {
102 if (i
!= OVSDB_TYPE_VOID
) {
103 ovsdb_atom_init_default(&default_atoms
[i
], i
);
109 assert(ovsdb_atomic_type_is_valid(type
));
110 return &default_atoms
[type
];
113 /* Returns true if 'atom', which must have the given 'type', has the default
114 * value for that type.
116 * See ovsdb_atom_init_default() for an explanation of the default value of an
119 ovsdb_atom_is_default(const union ovsdb_atom
*atom
,
120 enum ovsdb_atomic_type type
)
123 case OVSDB_TYPE_VOID
:
126 case OVSDB_TYPE_INTEGER
:
127 return atom
->integer
== 0;
129 case OVSDB_TYPE_REAL
:
130 return atom
->real
== 0.0;
132 case OVSDB_TYPE_BOOLEAN
:
133 return atom
->boolean
== false;
135 case OVSDB_TYPE_STRING
:
136 return atom
->string
[0] == '\0';
138 case OVSDB_TYPE_UUID
:
139 return uuid_is_zero(&atom
->uuid
);
147 /* Initializes 'new' as a copy of 'old', with the given 'type'.
149 * The caller must eventually arrange for 'new' to be destroyed (with
150 * ovsdb_atom_destroy()). */
152 ovsdb_atom_clone(union ovsdb_atom
*new, const union ovsdb_atom
*old
,
153 enum ovsdb_atomic_type type
)
156 case OVSDB_TYPE_VOID
:
159 case OVSDB_TYPE_INTEGER
:
160 new->integer
= old
->integer
;
163 case OVSDB_TYPE_REAL
:
164 new->real
= old
->real
;
167 case OVSDB_TYPE_BOOLEAN
:
168 new->boolean
= old
->boolean
;
171 case OVSDB_TYPE_STRING
:
172 new->string
= xstrdup(old
->string
);
175 case OVSDB_TYPE_UUID
:
176 new->uuid
= old
->uuid
;
185 /* Swaps the contents of 'a' and 'b', which need not have the same type. */
187 ovsdb_atom_swap(union ovsdb_atom
*a
, union ovsdb_atom
*b
)
189 union ovsdb_atom tmp
= *a
;
194 /* Returns a hash value for 'atom', which has the specified 'type', folding
195 * 'basis' into the calculation. */
197 ovsdb_atom_hash(const union ovsdb_atom
*atom
, enum ovsdb_atomic_type type
,
201 case OVSDB_TYPE_VOID
:
204 case OVSDB_TYPE_INTEGER
:
205 return hash_int(atom
->integer
, basis
);
207 case OVSDB_TYPE_REAL
:
208 return hash_double(atom
->real
, basis
);
210 case OVSDB_TYPE_BOOLEAN
:
211 return hash_boolean(atom
->boolean
, basis
);
213 case OVSDB_TYPE_STRING
:
214 return hash_string(atom
->string
, basis
);
216 case OVSDB_TYPE_UUID
:
217 return hash_int(uuid_hash(&atom
->uuid
), basis
);
225 /* Compares 'a' and 'b', which both have type 'type', and returns a
226 * strcmp()-like result. */
228 ovsdb_atom_compare_3way(const union ovsdb_atom
*a
,
229 const union ovsdb_atom
*b
,
230 enum ovsdb_atomic_type type
)
233 case OVSDB_TYPE_VOID
:
236 case OVSDB_TYPE_INTEGER
:
237 return a
->integer
< b
->integer
? -1 : a
->integer
> b
->integer
;
239 case OVSDB_TYPE_REAL
:
240 return a
->real
< b
->real
? -1 : a
->real
> b
->real
;
242 case OVSDB_TYPE_BOOLEAN
:
243 return a
->boolean
- b
->boolean
;
245 case OVSDB_TYPE_STRING
:
246 return strcmp(a
->string
, b
->string
);
248 case OVSDB_TYPE_UUID
:
249 return uuid_compare_3way(&a
->uuid
, &b
->uuid
);
257 static struct ovsdb_error
*
258 unwrap_json(const struct json
*json
, const char *name
,
259 enum json_type value_type
, const struct json
**value
)
261 if (json
->type
!= JSON_ARRAY
262 || json
->u
.array
.n
!= 2
263 || json
->u
.array
.elems
[0]->type
!= JSON_STRING
264 || (name
&& strcmp(json
->u
.array
.elems
[0]->u
.string
, name
))
265 || json
->u
.array
.elems
[1]->type
!= value_type
)
267 return ovsdb_syntax_error(json
, NULL
, "expected [\"%s\", <%s>]", name
,
268 json_type_to_string(value_type
));
270 *value
= json
->u
.array
.elems
[1];
274 static struct ovsdb_error
*
275 parse_json_pair(const struct json
*json
,
276 const struct json
**elem0
, const struct json
**elem1
)
278 if (json
->type
!= JSON_ARRAY
|| json
->u
.array
.n
!= 2) {
279 return ovsdb_syntax_error(json
, NULL
, "expected 2-element array");
281 *elem0
= json
->u
.array
.elems
[0];
282 *elem1
= json
->u
.array
.elems
[1];
286 static struct ovsdb_error
* WARN_UNUSED_RESULT
287 ovsdb_atom_parse_uuid(struct uuid
*uuid
, const struct json
*json
,
288 struct ovsdb_symbol_table
*symtab
)
290 struct ovsdb_error
*error0
;
291 const struct json
*value
;
293 error0
= unwrap_json(json
, "uuid", JSON_STRING
, &value
);
295 const char *uuid_string
= json_string(value
);
296 if (!uuid_from_string(uuid
, uuid_string
)) {
297 return ovsdb_syntax_error(json
, NULL
, "\"%s\" is not a valid UUID",
301 struct ovsdb_error
*error1
;
303 error1
= unwrap_json(json
, "named-uuid", JSON_STRING
, &value
);
305 const char *name
= json_string(value
);
307 ovsdb_error_destroy(error0
);
308 *uuid
= ovsdb_symbol_table_insert(symtab
, name
)->uuid
;
311 ovsdb_error_destroy(error1
);
317 static struct ovsdb_error
* WARN_UNUSED_RESULT
318 ovsdb_atom_from_json__(union ovsdb_atom
*atom
, enum ovsdb_atomic_type type
,
319 const struct json
*json
,
320 struct ovsdb_symbol_table
*symtab
)
323 case OVSDB_TYPE_VOID
:
326 case OVSDB_TYPE_INTEGER
:
327 if (json
->type
== JSON_INTEGER
) {
328 atom
->integer
= json
->u
.integer
;
333 case OVSDB_TYPE_REAL
:
334 if (json
->type
== JSON_INTEGER
) {
335 atom
->real
= json
->u
.integer
;
337 } else if (json
->type
== JSON_REAL
) {
338 atom
->real
= json
->u
.real
;
343 case OVSDB_TYPE_BOOLEAN
:
344 if (json
->type
== JSON_TRUE
) {
345 atom
->boolean
= true;
347 } else if (json
->type
== JSON_FALSE
) {
348 atom
->boolean
= false;
353 case OVSDB_TYPE_STRING
:
354 if (json
->type
== JSON_STRING
) {
355 atom
->string
= xstrdup(json
->u
.string
);
360 case OVSDB_TYPE_UUID
:
361 return ovsdb_atom_parse_uuid(&atom
->uuid
, json
, symtab
);
368 return ovsdb_syntax_error(json
, NULL
, "expected %s",
369 ovsdb_atomic_type_to_string(type
));
372 /* Parses 'json' as an atom of the type described by 'base'. If successful,
373 * returns NULL and initializes 'atom' with the parsed atom. On failure,
374 * returns an error and the contents of 'atom' are indeterminate. The caller
375 * is responsible for freeing the error or the atom that is returned.
377 * Violations of constraints expressed by 'base' are treated as errors.
379 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
380 * ovsdb/SPECS for information about this, and for the syntax that this
381 * function accepts. */
383 ovsdb_atom_from_json(union ovsdb_atom
*atom
,
384 const struct ovsdb_base_type
*base
,
385 const struct json
*json
,
386 struct ovsdb_symbol_table
*symtab
)
388 struct ovsdb_error
*error
;
390 error
= ovsdb_atom_from_json__(atom
, base
->type
, json
, symtab
);
395 error
= ovsdb_atom_check_constraints(atom
, base
);
397 ovsdb_atom_destroy(atom
, base
->type
);
402 /* Converts 'atom', of the specified 'type', to JSON format, and returns the
403 * JSON. The caller is responsible for freeing the returned JSON.
405 * Refer to ovsdb/SPECS for the format of the JSON that this function
408 ovsdb_atom_to_json(const union ovsdb_atom
*atom
, enum ovsdb_atomic_type type
)
411 case OVSDB_TYPE_VOID
:
414 case OVSDB_TYPE_INTEGER
:
415 return json_integer_create(atom
->integer
);
417 case OVSDB_TYPE_REAL
:
418 return json_real_create(atom
->real
);
420 case OVSDB_TYPE_BOOLEAN
:
421 return json_boolean_create(atom
->boolean
);
423 case OVSDB_TYPE_STRING
:
424 return json_string_create(atom
->string
);
426 case OVSDB_TYPE_UUID
:
427 return wrap_json("uuid", json_string_create_nocopy(
428 xasprintf(UUID_FMT
, UUID_ARGS(&atom
->uuid
))));
437 ovsdb_atom_from_string__(union ovsdb_atom
*atom
, enum ovsdb_atomic_type type
,
438 const char *s
, struct ovsdb_symbol_table
*symtab
)
441 case OVSDB_TYPE_VOID
:
444 case OVSDB_TYPE_INTEGER
: {
445 long long int integer
;
446 if (!str_to_llong(s
, 10, &integer
)) {
447 return xasprintf("\"%s\" is not a valid integer", s
);
449 atom
->integer
= integer
;
453 case OVSDB_TYPE_REAL
:
454 if (!str_to_double(s
, &atom
->real
)) {
455 return xasprintf("\"%s\" is not a valid real number", s
);
457 /* Our JSON input routines map negative zero to zero, so do that here
458 * too for consistency. */
459 if (atom
->real
== 0.0) {
464 case OVSDB_TYPE_BOOLEAN
:
465 if (!strcmp(s
, "true") || !strcmp(s
, "yes") || !strcmp(s
, "on")
466 || !strcmp(s
, "1")) {
467 atom
->boolean
= true;
468 } else if (!strcmp(s
, "false") || !strcmp(s
, "no") || !strcmp(s
, "off")
469 || !strcmp(s
, "0")) {
470 atom
->boolean
= false;
472 return xasprintf("\"%s\" is not a valid boolean "
473 "(use \"true\" or \"false\")", s
);
477 case OVSDB_TYPE_STRING
:
479 return xstrdup("An empty string is not valid as input; "
480 "use \"\" to represent the empty string");
481 } else if (*s
== '"') {
482 size_t s_len
= strlen(s
);
484 if (s_len
< 2 || s
[s_len
- 1] != '"') {
485 return xasprintf("%s: missing quote at end of "
487 } else if (!json_string_unescape(s
+ 1, s_len
- 2,
489 char *error
= xasprintf("%s: %s", s
, atom
->string
);
494 atom
->string
= xstrdup(s
);
498 case OVSDB_TYPE_UUID
:
500 atom
->uuid
= ovsdb_symbol_table_insert(symtab
, s
)->uuid
;
501 } else if (!uuid_from_string(&atom
->uuid
, s
)) {
502 return xasprintf("\"%s\" is not a valid UUID", s
);
514 /* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
515 * one of the following forms:
517 * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
519 * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
522 * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false",
523 * "no", "off", or "0" for false.
525 * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
526 * an arbitrary string.
528 * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. If 'symtab' is nonnull,
529 * then an identifier beginning with '@' is also acceptable. If the
530 * named identifier is already in 'symtab', then the associated UUID is
531 * used; otherwise, a new, random UUID is used and added to the symbol
534 * Returns a null pointer if successful, otherwise an error message describing
535 * the problem. On failure, the contents of 'atom' are indeterminate. The
536 * caller is responsible for freeing the atom or the error.
539 ovsdb_atom_from_string(union ovsdb_atom
*atom
,
540 const struct ovsdb_base_type
*base
, const char *s
,
541 struct ovsdb_symbol_table
*symtab
)
543 struct ovsdb_error
*error
;
546 msg
= ovsdb_atom_from_string__(atom
, base
->type
, s
, symtab
);
551 error
= ovsdb_atom_check_constraints(atom
, base
);
553 msg
= ovsdb_error_to_string(error
);
554 ovsdb_error_destroy(error
);
560 string_needs_quotes(const char *s
)
566 if (!isalpha(c
) && c
!= '_') {
570 while ((c
= *p
++) != '\0') {
571 if (!isalpha(c
) && c
!= '_' && c
!= '-' && c
!= '.') {
576 if (!strcmp(s
, "true") || !strcmp(s
, "false")) {
583 /* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable
584 * to ovsdb_atom_from_string(). */
586 ovsdb_atom_to_string(const union ovsdb_atom
*atom
, enum ovsdb_atomic_type type
,
590 case OVSDB_TYPE_VOID
:
593 case OVSDB_TYPE_INTEGER
:
594 ds_put_format(out
, "%"PRId64
, atom
->integer
);
597 case OVSDB_TYPE_REAL
:
598 ds_put_format(out
, "%.*g", DBL_DIG
, atom
->real
);
601 case OVSDB_TYPE_BOOLEAN
:
602 ds_put_cstr(out
, atom
->boolean
? "true" : "false");
605 case OVSDB_TYPE_STRING
:
606 if (string_needs_quotes(atom
->string
)) {
609 json
.type
= JSON_STRING
;
610 json
.u
.string
= atom
->string
;
611 json_to_ds(&json
, 0, out
);
613 ds_put_cstr(out
, atom
->string
);
617 case OVSDB_TYPE_UUID
:
618 ds_put_format(out
, UUID_FMT
, UUID_ARGS(&atom
->uuid
));
627 static struct ovsdb_error
*
628 check_string_constraints(const char *s
,
629 const struct ovsdb_string_constraints
*c
)
634 msg
= utf8_validate(s
, &n_chars
);
636 struct ovsdb_error
*error
;
638 error
= ovsdb_error("constraint violation",
639 "\"%s\" is not a valid UTF-8 string: %s",
645 if (n_chars
< c
->minLen
) {
647 "constraint violation",
648 "\"%s\" length %zu is less than minimum allowed "
649 "length %u", s
, n_chars
, c
->minLen
);
650 } else if (n_chars
> c
->maxLen
) {
652 "constraint violation",
653 "\"%s\" length %zu is greater than maximum allowed "
654 "length %u", s
, n_chars
, c
->maxLen
);
660 /* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
661 * (base->type must specify 'atom''s type.) Returns a null pointer if the
662 * constraints are met, otherwise an error that explains the violation.
664 * Checking UUID constraints is deferred to transaction commit time, so this
665 * function does nothing for UUID constraints. */
667 ovsdb_atom_check_constraints(const union ovsdb_atom
*atom
,
668 const struct ovsdb_base_type
*base
)
671 && ovsdb_datum_find_key(base
->enum_
, atom
, base
->type
) == UINT_MAX
) {
672 struct ovsdb_error
*error
;
673 struct ds actual
= DS_EMPTY_INITIALIZER
;
674 struct ds valid
= DS_EMPTY_INITIALIZER
;
676 ovsdb_atom_to_string(atom
, base
->type
, &actual
);
677 ovsdb_datum_to_string(base
->enum_
,
678 ovsdb_base_type_get_enum_type(base
->type
),
680 error
= ovsdb_error("constraint violation",
681 "%s is not one of the allowed values (%s)",
682 ds_cstr(&actual
), ds_cstr(&valid
));
689 switch (base
->type
) {
690 case OVSDB_TYPE_VOID
:
693 case OVSDB_TYPE_INTEGER
:
694 if (atom
->integer
>= base
->u
.integer
.min
695 && atom
->integer
<= base
->u
.integer
.max
) {
697 } else if (base
->u
.integer
.min
!= INT64_MIN
) {
698 if (base
->u
.integer
.max
!= INT64_MAX
) {
699 return ovsdb_error("constraint violation",
700 "%"PRId64
" is not in the valid range "
701 "%"PRId64
" to %"PRId64
" (inclusive)",
703 base
->u
.integer
.min
, base
->u
.integer
.max
);
705 return ovsdb_error("constraint violation",
706 "%"PRId64
" is less than minimum allowed "
708 atom
->integer
, base
->u
.integer
.min
);
711 return ovsdb_error("constraint violation",
712 "%"PRId64
" is greater than maximum allowed "
714 atom
->integer
, base
->u
.integer
.max
);
718 case OVSDB_TYPE_REAL
:
719 if (atom
->real
>= base
->u
.real
.min
&& atom
->real
<= base
->u
.real
.max
) {
721 } else if (base
->u
.real
.min
!= -DBL_MAX
) {
722 if (base
->u
.real
.max
!= DBL_MAX
) {
723 return ovsdb_error("constraint violation",
724 "%.*g is not in the valid range "
725 "%.*g to %.*g (inclusive)",
727 DBL_DIG
, base
->u
.real
.min
,
728 DBL_DIG
, base
->u
.real
.max
);
730 return ovsdb_error("constraint violation",
731 "%.*g is less than minimum allowed "
734 DBL_DIG
, base
->u
.real
.min
);
737 return ovsdb_error("constraint violation",
738 "%.*g is greater than maximum allowed "
741 DBL_DIG
, base
->u
.real
.max
);
745 case OVSDB_TYPE_BOOLEAN
:
748 case OVSDB_TYPE_STRING
:
749 return check_string_constraints(atom
->string
, &base
->u
.string
);
751 case OVSDB_TYPE_UUID
:
760 static union ovsdb_atom
*
761 alloc_default_atoms(enum ovsdb_atomic_type type
, size_t n
)
763 if (type
!= OVSDB_TYPE_VOID
&& n
) {
764 union ovsdb_atom
*atoms
;
767 atoms
= xmalloc(n
* sizeof *atoms
);
768 for (i
= 0; i
< n
; i
++) {
769 ovsdb_atom_init_default(&atoms
[i
], type
);
773 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
774 * treated as xmalloc(1). */
779 /* Initializes 'datum' as an empty datum. (An empty datum can be treated as
782 ovsdb_datum_init_empty(struct ovsdb_datum
*datum
)
786 datum
->values
= NULL
;
789 /* Initializes 'datum' as a datum that has the default value for 'type'.
791 * The default value for a particular type is as defined in ovsdb/SPECS:
793 * - If n_min is 0, then the default value is the empty set (or map).
795 * - If n_min is 1, the default value is a single value or a single
796 * key-value pair, whose key and value are the defaults for their
797 * atomic types. (See ovsdb_atom_init_default() for details.)
799 * - n_min > 1 is invalid. See ovsdb_type_is_valid().
802 ovsdb_datum_init_default(struct ovsdb_datum
*datum
,
803 const struct ovsdb_type
*type
)
805 datum
->n
= type
->n_min
;
806 datum
->keys
= alloc_default_atoms(type
->key
.type
, datum
->n
);
807 datum
->values
= alloc_default_atoms(type
->value
.type
, datum
->n
);
810 /* Returns a read-only datum of the given 'type' that has the default value for
811 * 'type'. The caller must not modify or free the returned datum.
813 * See ovsdb_datum_init_default() for an explanation of the default value of a
815 const struct ovsdb_datum
*
816 ovsdb_datum_default(const struct ovsdb_type
*type
)
818 if (type
->n_min
== 0) {
819 static const struct ovsdb_datum empty
;
821 } else if (type
->n_min
== 1) {
822 static struct ovsdb_datum default_data
[OVSDB_N_TYPES
][OVSDB_N_TYPES
];
823 struct ovsdb_datum
*d
;
824 int kt
= type
->key
.type
;
825 int vt
= type
->value
.type
;
827 assert(ovsdb_type_is_valid(type
));
829 d
= &default_data
[kt
][vt
];
832 d
->keys
= (union ovsdb_atom
*) ovsdb_atom_default(kt
);
833 if (vt
!= OVSDB_TYPE_VOID
) {
834 d
->values
= (union ovsdb_atom
*) ovsdb_atom_default(vt
);
843 /* Returns true if 'datum', which must have the given 'type', has the default
844 * value for that type.
846 * See ovsdb_datum_init_default() for an explanation of the default value of a
849 ovsdb_datum_is_default(const struct ovsdb_datum
*datum
,
850 const struct ovsdb_type
*type
)
854 if (datum
->n
!= type
->n_min
) {
857 for (i
= 0; i
< datum
->n
; i
++) {
858 if (!ovsdb_atom_is_default(&datum
->keys
[i
], type
->key
.type
)) {
861 if (type
->value
.type
!= OVSDB_TYPE_VOID
862 && !ovsdb_atom_is_default(&datum
->values
[i
], type
->value
.type
)) {
870 static union ovsdb_atom
*
871 clone_atoms(const union ovsdb_atom
*old
, enum ovsdb_atomic_type type
, size_t n
)
873 if (type
!= OVSDB_TYPE_VOID
&& n
) {
874 union ovsdb_atom
*new;
877 new = xmalloc(n
* sizeof *new);
878 for (i
= 0; i
< n
; i
++) {
879 ovsdb_atom_clone(&new[i
], &old
[i
], type
);
883 /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
884 * treated as xmalloc(1). */
889 /* Initializes 'new' as a copy of 'old', with the given 'type'.
891 * The caller must eventually arrange for 'new' to be destroyed (with
892 * ovsdb_datum_destroy()). */
894 ovsdb_datum_clone(struct ovsdb_datum
*new, const struct ovsdb_datum
*old
,
895 const struct ovsdb_type
*type
)
897 unsigned int n
= old
->n
;
899 new->keys
= clone_atoms(old
->keys
, type
->key
.type
, n
);
900 new->values
= clone_atoms(old
->values
, type
->value
.type
, n
);
904 free_data(enum ovsdb_atomic_type type
,
905 union ovsdb_atom
*atoms
, size_t n_atoms
)
907 if (ovsdb_atom_needs_destruction(type
)) {
909 for (i
= 0; i
< n_atoms
; i
++) {
910 ovsdb_atom_destroy(&atoms
[i
], type
);
916 /* Frees the data owned by 'datum', which must have the given 'type'.
918 * This does not actually call free(datum). If necessary, the caller must be
919 * responsible for that. */
921 ovsdb_datum_destroy(struct ovsdb_datum
*datum
, const struct ovsdb_type
*type
)
923 free_data(type
->key
.type
, datum
->keys
, datum
->n
);
924 free_data(type
->value
.type
, datum
->values
, datum
->n
);
927 /* Swaps the contents of 'a' and 'b', which need not have the same type. */
929 ovsdb_datum_swap(struct ovsdb_datum
*a
, struct ovsdb_datum
*b
)
931 struct ovsdb_datum tmp
= *a
;
936 struct ovsdb_datum_sort_cbdata
{
937 enum ovsdb_atomic_type key_type
;
938 struct ovsdb_datum
*datum
;
942 ovsdb_datum_sort_compare_cb(size_t a
, size_t b
, void *cbdata_
)
944 struct ovsdb_datum_sort_cbdata
*cbdata
= cbdata_
;
946 return ovsdb_atom_compare_3way(&cbdata
->datum
->keys
[a
],
947 &cbdata
->datum
->keys
[b
],
952 ovsdb_datum_sort_swap_cb(size_t a
, size_t b
, void *cbdata_
)
954 struct ovsdb_datum_sort_cbdata
*cbdata
= cbdata_
;
956 ovsdb_atom_swap(&cbdata
->datum
->keys
[a
], &cbdata
->datum
->keys
[b
]);
957 if (cbdata
->datum
->values
) {
958 ovsdb_atom_swap(&cbdata
->datum
->values
[a
], &cbdata
->datum
->values
[b
]);
962 /* The keys in an ovsdb_datum must be unique and in sorted order. Most
963 * functions that modify an ovsdb_datum maintain these invariants. For those
964 * that don't, this function checks and restores these invariants for 'datum',
965 * whose keys are of type 'key_type'.
967 * This function returns NULL if successful, otherwise an error message. The
968 * caller must free the returned error when it is no longer needed. On error,
969 * 'datum' is sorted but not unique. */
971 ovsdb_datum_sort(struct ovsdb_datum
*datum
, enum ovsdb_atomic_type key_type
)
976 struct ovsdb_datum_sort_cbdata cbdata
;
979 cbdata
.key_type
= key_type
;
980 cbdata
.datum
= datum
;
981 sort(datum
->n
, ovsdb_datum_sort_compare_cb
, ovsdb_datum_sort_swap_cb
,
984 for (i
= 0; i
< datum
->n
- 1; i
++) {
985 if (ovsdb_atom_equals(&datum
->keys
[i
], &datum
->keys
[i
+ 1],
988 return ovsdb_error(NULL
, "map contains duplicate key");
990 return ovsdb_error(NULL
, "set contains duplicate");
999 /* This function is the same as ovsdb_datum_sort(), except that the caller
1000 * knows that 'datum' is unique. The operation therefore "cannot fail", so
1001 * this function assert-fails if it actually does. */
1003 ovsdb_datum_sort_assert(struct ovsdb_datum
*datum
,
1004 enum ovsdb_atomic_type key_type
)
1006 struct ovsdb_error
*error
= ovsdb_datum_sort(datum
, key_type
);
1012 /* Checks that each of the atoms in 'datum' conforms to the constraints
1013 * specified by its 'type'. Returns an error if a constraint is violated,
1014 * otherwise a null pointer.
1016 * This function is not commonly useful because the most ordinary way to obtain
1017 * a datum is ultimately via ovsdb_atom_from_string() or
1018 * ovsdb_atom_from_json(), which check constraints themselves. */
1019 struct ovsdb_error
*
1020 ovsdb_datum_check_constraints(const struct ovsdb_datum
*datum
,
1021 const struct ovsdb_type
*type
)
1023 struct ovsdb_error
*error
;
1026 for (i
= 0; i
< datum
->n
; i
++) {
1027 error
= ovsdb_atom_check_constraints(&datum
->keys
[i
], &type
->key
);
1033 if (type
->value
.type
!= OVSDB_TYPE_VOID
) {
1034 for (i
= 0; i
< datum
->n
; i
++) {
1035 error
= ovsdb_atom_check_constraints(&datum
->values
[i
],
1046 /* Parses 'json' as a datum of the type described by 'type'. If successful,
1047 * returns NULL and initializes 'datum' with the parsed datum. On failure,
1048 * returns an error and the contents of 'datum' are indeterminate. The caller
1049 * is responsible for freeing the error or the datum that is returned.
1051 * Violations of constraints expressed by 'type' are treated as errors.
1053 * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
1054 * ovsdb/SPECS for information about this, and for the syntax that this
1055 * function accepts. */
1056 struct ovsdb_error
*
1057 ovsdb_datum_from_json(struct ovsdb_datum
*datum
,
1058 const struct ovsdb_type
*type
,
1059 const struct json
*json
,
1060 struct ovsdb_symbol_table
*symtab
)
1062 struct ovsdb_error
*error
;
1064 if (ovsdb_type_is_map(type
)
1065 || (json
->type
== JSON_ARRAY
1066 && json
->u
.array
.n
> 0
1067 && json
->u
.array
.elems
[0]->type
== JSON_STRING
1068 && !strcmp(json
->u
.array
.elems
[0]->u
.string
, "set"))) {
1069 bool is_map
= ovsdb_type_is_map(type
);
1070 const char *class = is_map
? "map" : "set";
1071 const struct json
*inner
;
1075 error
= unwrap_json(json
, class, JSON_ARRAY
, &inner
);
1080 n
= inner
->u
.array
.n
;
1081 if (n
< type
->n_min
|| n
> type
->n_max
) {
1082 return ovsdb_syntax_error(json
, NULL
, "%s must have %u to "
1083 "%u members but %zu are present",
1084 class, type
->n_min
, type
->n_max
, n
);
1088 datum
->keys
= xmalloc(n
* sizeof *datum
->keys
);
1089 datum
->values
= is_map
? xmalloc(n
* sizeof *datum
->values
) : NULL
;
1090 for (i
= 0; i
< n
; i
++) {
1091 const struct json
*element
= inner
->u
.array
.elems
[i
];
1092 const struct json
*key
= NULL
;
1093 const struct json
*value
= NULL
;
1098 error
= parse_json_pair(element
, &key
, &value
);
1104 error
= ovsdb_atom_from_json(&datum
->keys
[i
], &type
->key
,
1111 error
= ovsdb_atom_from_json(&datum
->values
[i
],
1112 &type
->value
, value
, symtab
);
1114 ovsdb_atom_destroy(&datum
->keys
[i
], type
->key
.type
);
1122 error
= ovsdb_datum_sort(datum
, type
->key
.type
);
1130 ovsdb_datum_destroy(datum
, type
);
1134 datum
->keys
= xmalloc(sizeof *datum
->keys
);
1135 datum
->values
= NULL
;
1137 error
= ovsdb_atom_from_json(&datum
->keys
[0], &type
->key
,
1147 /* Converts 'datum', of the specified 'type', to JSON format, and returns the
1148 * JSON. The caller is responsible for freeing the returned JSON.
1150 * 'type' constraints on datum->n are ignored.
1152 * Refer to ovsdb/SPECS for the format of the JSON that this function
1155 ovsdb_datum_to_json(const struct ovsdb_datum
*datum
,
1156 const struct ovsdb_type
*type
)
1158 if (datum
->n
== 1 && !ovsdb_type_is_map(type
)) {
1159 return ovsdb_atom_to_json(&datum
->keys
[0], type
->key
.type
);
1160 } else if (type
->value
.type
== OVSDB_TYPE_VOID
) {
1161 struct json
**elems
;
1164 elems
= xmalloc(datum
->n
* sizeof *elems
);
1165 for (i
= 0; i
< datum
->n
; i
++) {
1166 elems
[i
] = ovsdb_atom_to_json(&datum
->keys
[i
], type
->key
.type
);
1169 return wrap_json("set", json_array_create(elems
, datum
->n
));
1171 struct json
**elems
;
1174 elems
= xmalloc(datum
->n
* sizeof *elems
);
1175 for (i
= 0; i
< datum
->n
; i
++) {
1176 elems
[i
] = json_array_create_2(
1177 ovsdb_atom_to_json(&datum
->keys
[i
], type
->key
.type
),
1178 ovsdb_atom_to_json(&datum
->values
[i
], type
->value
.type
));
1181 return wrap_json("map", json_array_create(elems
, datum
->n
));
1186 skip_spaces(const char *p
)
1188 while (isspace((unsigned char) *p
)) {
1195 parse_atom_token(const char **s
, const struct ovsdb_base_type
*base
,
1196 union ovsdb_atom
*atom
, struct ovsdb_symbol_table
*symtab
)
1198 char *token
, *error
;
1200 error
= ovsdb_token_parse(s
, &token
);
1202 error
= ovsdb_atom_from_string(atom
, base
, token
, symtab
);
1209 parse_key_value(const char **s
, const struct ovsdb_type
*type
,
1210 union ovsdb_atom
*key
, union ovsdb_atom
*value
,
1211 struct ovsdb_symbol_table
*symtab
)
1213 const char *start
= *s
;
1216 error
= parse_atom_token(s
, &type
->key
, key
, symtab
);
1217 if (!error
&& type
->value
.type
!= OVSDB_TYPE_VOID
) {
1218 *s
= skip_spaces(*s
);
1221 *s
= skip_spaces(*s
);
1222 error
= parse_atom_token(s
, &type
->value
, value
, symtab
);
1224 error
= xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
1228 ovsdb_atom_destroy(key
, type
->key
.type
);
1235 free_key_value(const struct ovsdb_type
*type
,
1236 union ovsdb_atom
*key
, union ovsdb_atom
*value
)
1238 ovsdb_atom_destroy(key
, type
->key
.type
);
1239 if (type
->value
.type
!= OVSDB_TYPE_VOID
) {
1240 ovsdb_atom_destroy(value
, type
->value
.type
);
1244 /* Initializes 'datum' as a datum of the given 'type', parsing its contents
1245 * from 's'. The format of 's' is a series of space or comma separated atoms
1246 * or, for a map, '='-delimited pairs of atoms. Each atom must in a format
1247 * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed
1248 * in "[]" or a map in "{}"; for an empty set or map these punctuators are
1251 * Optionally, a symbol table may be supplied as 'symtab'. It is passed to
1252 * ovsdb_atom_to_string(). */
1254 ovsdb_datum_from_string(struct ovsdb_datum
*datum
,
1255 const struct ovsdb_type
*type
, const char *s
,
1256 struct ovsdb_symbol_table
*symtab
)
1258 bool is_map
= ovsdb_type_is_map(type
);
1259 struct ovsdb_error
*dberror
;
1264 ovsdb_datum_init_empty(datum
);
1266 /* Swallow a leading delimiter if there is one. */
1268 if (*p
== (is_map
? '{' : '[')) {
1269 end_delim
= is_map
? '}' : ']';
1270 p
= skip_spaces(p
+ 1);
1273 return xstrdup("use \"{}\" to specify the empty map");
1275 return xstrdup("use \"[]\" to specify the empty set");
1281 while (*p
&& *p
!= end_delim
) {
1282 union ovsdb_atom key
, value
;
1284 if (ovsdb_token_is_delim(*p
)) {
1285 error
= xasprintf("%s: unexpected \"%c\" parsing %s",
1286 s
, *p
, ovsdb_type_to_english(type
));
1291 error
= parse_key_value(&p
, type
, &key
, &value
, symtab
);
1295 ovsdb_datum_add_unsafe(datum
, &key
, &value
, type
);
1296 free_key_value(type
, &key
, &value
);
1298 /* Skip optional white space and comma. */
1301 p
= skip_spaces(p
+ 1);
1305 if (*p
!= end_delim
) {
1306 error
= xasprintf("%s: missing \"%c\" at end of data", s
, end_delim
);
1310 p
= skip_spaces(p
+ 1);
1312 error
= xasprintf("%s: trailing garbage after \"%c\"",
1318 if (datum
->n
< type
->n_min
) {
1319 error
= xasprintf("%s: %u %s specified but the minimum number is %u",
1320 s
, datum
->n
, is_map
? "pair(s)" : "value(s)",
1323 } else if (datum
->n
> type
->n_max
) {
1324 error
= xasprintf("%s: %u %s specified but the maximum number is %u",
1325 s
, datum
->n
, is_map
? "pair(s)" : "value(s)",
1330 dberror
= ovsdb_datum_sort(datum
, type
->key
.type
);
1332 ovsdb_error_destroy(dberror
);
1333 if (ovsdb_type_is_map(type
)) {
1334 error
= xasprintf("%s: map contains duplicate key", s
);
1336 error
= xasprintf("%s: set contains duplicate value", s
);
1344 ovsdb_datum_destroy(datum
, type
);
1345 ovsdb_datum_init_empty(datum
);
1349 /* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable
1350 * to ovsdb_datum_from_string(). */
1352 ovsdb_datum_to_string(const struct ovsdb_datum
*datum
,
1353 const struct ovsdb_type
*type
, struct ds
*out
)
1355 bool is_map
= ovsdb_type_is_map(type
);
1358 if (type
->n_max
> 1 || !datum
->n
) {
1359 ds_put_char(out
, is_map
? '{' : '[');
1361 for (i
= 0; i
< datum
->n
; i
++) {
1363 ds_put_cstr(out
, ", ");
1366 ovsdb_atom_to_string(&datum
->keys
[i
], type
->key
.type
, out
);
1368 ds_put_char(out
, '=');
1369 ovsdb_atom_to_string(&datum
->values
[i
], type
->value
.type
, out
);
1372 if (type
->n_max
> 1 || !datum
->n
) {
1373 ds_put_char(out
, is_map
? '}' : ']');
1378 hash_atoms(enum ovsdb_atomic_type type
, const union ovsdb_atom
*atoms
,
1379 unsigned int n
, uint32_t basis
)
1381 if (type
!= OVSDB_TYPE_VOID
) {
1384 for (i
= 0; i
< n
; i
++) {
1385 basis
= ovsdb_atom_hash(&atoms
[i
], type
, basis
);
1392 ovsdb_datum_hash(const struct ovsdb_datum
*datum
,
1393 const struct ovsdb_type
*type
, uint32_t basis
)
1395 basis
= hash_atoms(type
->key
.type
, datum
->keys
, datum
->n
, basis
);
1396 basis
^= (type
->key
.type
<< 24) | (type
->value
.type
<< 16) | datum
->n
;
1397 basis
= hash_atoms(type
->value
.type
, datum
->values
, datum
->n
, basis
);
1402 atom_arrays_compare_3way(const union ovsdb_atom
*a
,
1403 const union ovsdb_atom
*b
,
1404 enum ovsdb_atomic_type type
,
1409 for (i
= 0; i
< n
; i
++) {
1410 int cmp
= ovsdb_atom_compare_3way(&a
[i
], &b
[i
], type
);
1420 ovsdb_datum_equals(const struct ovsdb_datum
*a
,
1421 const struct ovsdb_datum
*b
,
1422 const struct ovsdb_type
*type
)
1424 return !ovsdb_datum_compare_3way(a
, b
, type
);
1428 ovsdb_datum_compare_3way(const struct ovsdb_datum
*a
,
1429 const struct ovsdb_datum
*b
,
1430 const struct ovsdb_type
*type
)
1435 return a
->n
< b
->n
? -1 : 1;
1438 cmp
= atom_arrays_compare_3way(a
->keys
, b
->keys
, type
->key
.type
, a
->n
);
1443 return (type
->value
.type
== OVSDB_TYPE_VOID
? 0
1444 : atom_arrays_compare_3way(a
->values
, b
->values
, type
->value
.type
,
1448 /* If 'key' is one of the keys in 'datum', returns its index within 'datum',
1449 * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the
1450 * 'keys' array in 'datum'.
1453 ovsdb_datum_find_key(const struct ovsdb_datum
*datum
,
1454 const union ovsdb_atom
*key
,
1455 enum ovsdb_atomic_type key_type
)
1457 unsigned int low
= 0;
1458 unsigned int high
= datum
->n
;
1459 while (low
< high
) {
1460 unsigned int idx
= (low
+ high
) / 2;
1461 int cmp
= ovsdb_atom_compare_3way(key
, &datum
->keys
[idx
], key_type
);
1464 } else if (cmp
> 0) {
1473 /* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its
1474 * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of
1475 * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the
1476 * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys.
1479 ovsdb_datum_find_key_value(const struct ovsdb_datum
*datum
,
1480 const union ovsdb_atom
*key
,
1481 enum ovsdb_atomic_type key_type
,
1482 const union ovsdb_atom
*value
,
1483 enum ovsdb_atomic_type value_type
)
1485 unsigned int idx
= ovsdb_datum_find_key(datum
, key
, key_type
);
1487 && value_type
!= OVSDB_TYPE_VOID
1488 && !ovsdb_atom_equals(&datum
->values
[idx
], value
, value_type
)) {
1494 /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise
1495 * UINT_MAX. 'type' must be the type of 'a' and 'b', except that
1496 * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not
1499 ovsdb_datum_find(const struct ovsdb_datum
*a
, int i
,
1500 const struct ovsdb_datum
*b
,
1501 const struct ovsdb_type
*type
)
1503 return ovsdb_datum_find_key_value(b
,
1504 &a
->keys
[i
], type
->key
.type
,
1505 a
->values
? &a
->values
[i
] : NULL
,
1509 /* Returns true if every element in 'a' is also in 'b', false otherwise. */
1511 ovsdb_datum_includes_all(const struct ovsdb_datum
*a
,
1512 const struct ovsdb_datum
*b
,
1513 const struct ovsdb_type
*type
)
1517 for (i
= 0; i
< a
->n
; i
++) {
1518 if (ovsdb_datum_find(a
, i
, b
, type
) == UINT_MAX
) {
1525 /* Returns true if no element in 'a' is also in 'b', false otherwise. */
1527 ovsdb_datum_excludes_all(const struct ovsdb_datum
*a
,
1528 const struct ovsdb_datum
*b
,
1529 const struct ovsdb_type
*type
)
1533 for (i
= 0; i
< a
->n
; i
++) {
1534 if (ovsdb_datum_find(a
, i
, b
, type
) != UINT_MAX
) {
1542 ovsdb_datum_reallocate(struct ovsdb_datum
*a
, const struct ovsdb_type
*type
,
1543 unsigned int capacity
)
1545 a
->keys
= xrealloc(a
->keys
, capacity
* sizeof *a
->keys
);
1546 if (type
->value
.type
!= OVSDB_TYPE_VOID
) {
1547 a
->values
= xrealloc(a
->values
, capacity
* sizeof *a
->values
);
1551 /* Removes the element with index 'idx' from 'datum', which has type 'type'.
1552 * If 'idx' is not the last element in 'datum', then the removed element is
1553 * replaced by the (former) last element.
1555 * This function does not maintain ovsdb_datum invariants. Use
1556 * ovsdb_datum_sort() to check and restore these invariants. */
1558 ovsdb_datum_remove_unsafe(struct ovsdb_datum
*datum
, size_t idx
,
1559 const struct ovsdb_type
*type
)
1561 ovsdb_atom_destroy(&datum
->keys
[idx
], type
->key
.type
);
1562 datum
->keys
[idx
] = datum
->keys
[datum
->n
- 1];
1563 if (type
->value
.type
!= OVSDB_TYPE_VOID
) {
1564 ovsdb_atom_destroy(&datum
->values
[idx
], type
->value
.type
);
1565 datum
->values
[idx
] = datum
->values
[datum
->n
- 1];
1570 /* Adds the element with the given 'key' and 'value' to 'datum', which must
1571 * have the specified 'type'.
1573 * This function always allocates memory, so it is not an efficient way to add
1574 * a number of elements to a datum.
1576 * This function does not maintain ovsdb_datum invariants. Use
1577 * ovsdb_datum_sort() to check and restore these invariants. (But a datum with
1578 * 0 or 1 elements cannot violate the invariants anyhow.) */
1580 ovsdb_datum_add_unsafe(struct ovsdb_datum
*datum
,
1581 const union ovsdb_atom
*key
,
1582 const union ovsdb_atom
*value
,
1583 const struct ovsdb_type
*type
)
1585 size_t idx
= datum
->n
++;
1586 datum
->keys
= xrealloc(datum
->keys
, datum
->n
* sizeof *datum
->keys
);
1587 ovsdb_atom_clone(&datum
->keys
[idx
], key
, type
->key
.type
);
1588 if (type
->value
.type
!= OVSDB_TYPE_VOID
) {
1589 datum
->values
= xrealloc(datum
->values
,
1590 datum
->n
* sizeof *datum
->values
);
1591 ovsdb_atom_clone(&datum
->values
[idx
], value
, type
->value
.type
);
1596 ovsdb_datum_union(struct ovsdb_datum
*a
, const struct ovsdb_datum
*b
,
1597 const struct ovsdb_type
*type
, bool replace
)
1603 for (bi
= 0; bi
< b
->n
; bi
++) {
1606 ai
= ovsdb_datum_find_key(a
, &b
->keys
[bi
], type
->key
.type
);
1607 if (ai
== UINT_MAX
) {
1609 ovsdb_datum_reallocate(a
, type
, a
->n
+ (b
->n
- bi
));
1611 ovsdb_atom_clone(&a
->keys
[n
], &b
->keys
[bi
], type
->key
.type
);
1612 if (type
->value
.type
!= OVSDB_TYPE_VOID
) {
1613 ovsdb_atom_clone(&a
->values
[n
], &b
->values
[bi
],
1617 } else if (replace
&& type
->value
.type
!= OVSDB_TYPE_VOID
) {
1618 ovsdb_atom_destroy(&a
->values
[ai
], type
->value
.type
);
1619 ovsdb_atom_clone(&a
->values
[ai
], &b
->values
[bi
],
1624 struct ovsdb_error
*error
;
1626 error
= ovsdb_datum_sort(a
, type
->key
.type
);
1632 ovsdb_datum_subtract(struct ovsdb_datum
*a
, const struct ovsdb_type
*a_type
,
1633 const struct ovsdb_datum
*b
,
1634 const struct ovsdb_type
*b_type
)
1636 bool changed
= false;
1639 assert(a_type
->key
.type
== b_type
->key
.type
);
1640 assert(a_type
->value
.type
== b_type
->value
.type
1641 || b_type
->value
.type
== OVSDB_TYPE_VOID
);
1643 /* XXX The big-O of this could easily be improved. */
1644 for (i
= 0; i
< a
->n
; ) {
1645 unsigned int idx
= ovsdb_datum_find(a
, i
, b
, b_type
);
1646 if (idx
!= UINT_MAX
) {
1648 ovsdb_datum_remove_unsafe(a
, i
, a_type
);
1654 ovsdb_datum_sort_assert(a
, a_type
->key
.type
);
1658 struct ovsdb_symbol_table
{
1662 struct ovsdb_symbol_table
*
1663 ovsdb_symbol_table_create(void)
1665 struct ovsdb_symbol_table
*symtab
= xmalloc(sizeof *symtab
);
1666 shash_init(&symtab
->sh
);
1671 ovsdb_symbol_table_destroy(struct ovsdb_symbol_table
*symtab
)
1674 shash_destroy_free_data(&symtab
->sh
);
1679 struct ovsdb_symbol
*
1680 ovsdb_symbol_table_get(const struct ovsdb_symbol_table
*symtab
,
1683 return shash_find_data(&symtab
->sh
, name
);
1686 struct ovsdb_symbol
*
1687 ovsdb_symbol_table_put(struct ovsdb_symbol_table
*symtab
, const char *name
,
1688 const struct uuid
*uuid
, bool used
)
1690 struct ovsdb_symbol
*symbol
;
1692 assert(!ovsdb_symbol_table_get(symtab
, name
));
1693 symbol
= xmalloc(sizeof *symbol
);
1694 symbol
->uuid
= *uuid
;
1695 symbol
->used
= used
;
1696 shash_add(&symtab
->sh
, name
, symbol
);
1700 struct ovsdb_symbol
*
1701 ovsdb_symbol_table_insert(struct ovsdb_symbol_table
*symtab
,
1704 struct ovsdb_symbol
*symbol
;
1706 symbol
= ovsdb_symbol_table_get(symtab
, name
);
1710 uuid_generate(&uuid
);
1711 symbol
= ovsdb_symbol_table_put(symtab
, name
, &uuid
, false);
1717 ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table
*symtab
)
1719 struct shash_node
*node
;
1721 SHASH_FOR_EACH (node
, &symtab
->sh
) {
1722 struct ovsdb_symbol
*symbol
= node
->data
;
1723 if (!symbol
->used
) {
1731 /* Extracts a token from the beginning of 's' and returns a pointer just after
1732 * the token. Stores the token itself into '*outp', which the caller is
1733 * responsible for freeing (with free()).
1735 * If 's[0]' is a delimiter, the returned token is the empty string.
1737 * A token extends from 's' to the first delimiter, as defined by
1738 * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be
1739 * escaped with a backslash, in which case the backslash does not appear in the
1740 * output. Double quotes also cause delimiters to be ignored, but the double
1741 * quotes are retained in the output. (Backslashes inside double quotes are
1742 * not removed, either.)
1745 ovsdb_token_parse(const char **s
, char **outp
)
1754 for (p
= *s
; *p
!= '\0'; ) {
1758 ds_put_char(&out
, '\\');
1761 error
= xasprintf("%s: backslash at end of argument", *s
);
1764 ds_put_char(&out
, *p
++);
1765 } else if (!in_quotes
&& ovsdb_token_is_delim(c
)) {
1769 ds_put_char(&out
, c
);
1771 in_quotes
= !in_quotes
;
1776 error
= xasprintf("%s: quoted string extends past end of argument",
1780 *outp
= ds_cstr(&out
);
1790 /* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */
1792 ovsdb_token_is_delim(unsigned char c
)
1794 return strchr(":=, []{}!<>", c
) != NULL
;