]>
Commit | Line | Data |
---|---|---|
c532bf9d | 1 | /* Copyright (c) 2009, 2010 Nicira Networks |
f85f8ebb BP |
2 | * |
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: | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
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. | |
14 | */ | |
15 | ||
16 | #include <config.h> | |
17 | ||
18 | #include "ovsdb-data.h" | |
19 | ||
20 | #include <assert.h> | |
0194f33a BP |
21 | #include <ctype.h> |
22 | #include <float.h> | |
23 | #include <inttypes.h> | |
e9f8f936 | 24 | #include <limits.h> |
f85f8ebb | 25 | |
0194f33a | 26 | #include "dynamic-string.h" |
f85f8ebb BP |
27 | #include "hash.h" |
28 | #include "ovsdb-error.h" | |
29 | #include "json.h" | |
30 | #include "shash.h" | |
31 | #include "sort.h" | |
32 | ||
33 | static struct json * | |
34 | wrap_json(const char *name, struct json *wrapped) | |
35 | { | |
36 | return json_array_create_2(json_string_create(name), wrapped); | |
37 | } | |
38 | ||
39 | void | |
40 | ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type) | |
41 | { | |
42 | switch (type) { | |
43 | case OVSDB_TYPE_VOID: | |
44 | NOT_REACHED(); | |
45 | ||
46 | case OVSDB_TYPE_INTEGER: | |
47 | atom->integer = 0; | |
48 | break; | |
49 | ||
50 | case OVSDB_TYPE_REAL: | |
51 | atom->real = 0.0; | |
52 | break; | |
53 | ||
54 | case OVSDB_TYPE_BOOLEAN: | |
55 | atom->boolean = false; | |
56 | break; | |
57 | ||
58 | case OVSDB_TYPE_STRING: | |
59 | atom->string = xmemdup("", 1); | |
60 | break; | |
61 | ||
62 | case OVSDB_TYPE_UUID: | |
63 | uuid_zero(&atom->uuid); | |
64 | break; | |
65 | ||
66 | case OVSDB_N_TYPES: | |
67 | default: | |
68 | NOT_REACHED(); | |
69 | } | |
70 | } | |
71 | ||
c532bf9d BP |
72 | bool |
73 | ovsdb_atom_is_default(const union ovsdb_atom *atom, | |
74 | enum ovsdb_atomic_type type) | |
75 | { | |
76 | switch (type) { | |
77 | case OVSDB_TYPE_VOID: | |
78 | NOT_REACHED(); | |
79 | ||
80 | case OVSDB_TYPE_INTEGER: | |
81 | return atom->integer == 0; | |
82 | ||
83 | case OVSDB_TYPE_REAL: | |
84 | return atom->real == 0.0; | |
85 | ||
86 | case OVSDB_TYPE_BOOLEAN: | |
87 | return atom->boolean == false; | |
88 | ||
89 | case OVSDB_TYPE_STRING: | |
90 | return atom->string[0] == '\0'; | |
91 | ||
92 | case OVSDB_TYPE_UUID: | |
93 | return uuid_is_zero(&atom->uuid); | |
94 | ||
95 | case OVSDB_N_TYPES: | |
96 | default: | |
97 | NOT_REACHED(); | |
98 | } | |
99 | } | |
100 | ||
f85f8ebb BP |
101 | void |
102 | ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old, | |
103 | enum ovsdb_atomic_type type) | |
104 | { | |
105 | switch (type) { | |
106 | case OVSDB_TYPE_VOID: | |
107 | NOT_REACHED(); | |
108 | ||
109 | case OVSDB_TYPE_INTEGER: | |
110 | new->integer = old->integer; | |
111 | break; | |
112 | ||
113 | case OVSDB_TYPE_REAL: | |
114 | new->real = old->real; | |
115 | break; | |
116 | ||
117 | case OVSDB_TYPE_BOOLEAN: | |
118 | new->boolean = old->boolean; | |
119 | break; | |
120 | ||
121 | case OVSDB_TYPE_STRING: | |
122 | new->string = xstrdup(old->string); | |
123 | break; | |
124 | ||
125 | case OVSDB_TYPE_UUID: | |
126 | new->uuid = old->uuid; | |
127 | break; | |
128 | ||
129 | case OVSDB_N_TYPES: | |
130 | default: | |
131 | NOT_REACHED(); | |
132 | } | |
133 | } | |
134 | ||
135 | void | |
136 | ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b) | |
137 | { | |
138 | union ovsdb_atom tmp = *a; | |
139 | *a = *b; | |
140 | *b = tmp; | |
141 | } | |
142 | ||
143 | uint32_t | |
144 | ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, | |
145 | uint32_t basis) | |
146 | { | |
147 | switch (type) { | |
148 | case OVSDB_TYPE_VOID: | |
149 | NOT_REACHED(); | |
150 | ||
151 | case OVSDB_TYPE_INTEGER: | |
152 | return hash_int(atom->integer, basis); | |
153 | ||
154 | case OVSDB_TYPE_REAL: | |
155 | return hash_double(atom->real, basis); | |
156 | ||
157 | case OVSDB_TYPE_BOOLEAN: | |
158 | return hash_boolean(atom->boolean, basis); | |
159 | ||
160 | case OVSDB_TYPE_STRING: | |
161 | return hash_string(atom->string, basis); | |
162 | ||
163 | case OVSDB_TYPE_UUID: | |
164 | return hash_int(uuid_hash(&atom->uuid), basis); | |
165 | ||
166 | case OVSDB_N_TYPES: | |
167 | default: | |
168 | NOT_REACHED(); | |
169 | } | |
170 | } | |
171 | ||
172 | int | |
173 | ovsdb_atom_compare_3way(const union ovsdb_atom *a, | |
174 | const union ovsdb_atom *b, | |
175 | enum ovsdb_atomic_type type) | |
176 | { | |
177 | switch (type) { | |
178 | case OVSDB_TYPE_VOID: | |
179 | NOT_REACHED(); | |
180 | ||
181 | case OVSDB_TYPE_INTEGER: | |
182 | return a->integer < b->integer ? -1 : a->integer > b->integer; | |
183 | ||
184 | case OVSDB_TYPE_REAL: | |
185 | return a->real < b->real ? -1 : a->real > b->real; | |
186 | ||
187 | case OVSDB_TYPE_BOOLEAN: | |
188 | return a->boolean - b->boolean; | |
189 | ||
190 | case OVSDB_TYPE_STRING: | |
191 | return strcmp(a->string, b->string); | |
192 | ||
193 | case OVSDB_TYPE_UUID: | |
194 | return uuid_compare_3way(&a->uuid, &b->uuid); | |
195 | ||
196 | case OVSDB_N_TYPES: | |
197 | default: | |
198 | NOT_REACHED(); | |
199 | } | |
200 | } | |
201 | ||
202 | static struct ovsdb_error * | |
203 | unwrap_json(const struct json *json, const char *name, | |
204 | enum json_type value_type, const struct json **value) | |
205 | { | |
206 | if (json->type != JSON_ARRAY | |
207 | || json->u.array.n != 2 | |
208 | || json->u.array.elems[0]->type != JSON_STRING | |
209 | || (name && strcmp(json->u.array.elems[0]->u.string, name)) | |
210 | || json->u.array.elems[1]->type != value_type) | |
211 | { | |
212 | return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name, | |
213 | json_type_to_string(value_type)); | |
214 | } | |
215 | *value = json->u.array.elems[1]; | |
216 | return NULL; | |
217 | } | |
218 | ||
219 | static struct ovsdb_error * | |
220 | parse_json_pair(const struct json *json, | |
221 | const struct json **elem0, const struct json **elem1) | |
222 | { | |
223 | if (json->type != JSON_ARRAY || json->u.array.n != 2) { | |
224 | return ovsdb_syntax_error(json, NULL, "expected 2-element array"); | |
225 | } | |
226 | *elem0 = json->u.array.elems[0]; | |
227 | *elem1 = json->u.array.elems[1]; | |
228 | return NULL; | |
229 | } | |
230 | ||
231 | static struct ovsdb_error * | |
232 | ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, | |
233 | const struct ovsdb_symbol_table *symtab) | |
234 | WARN_UNUSED_RESULT; | |
235 | ||
236 | static struct ovsdb_error * | |
237 | ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, | |
238 | const struct ovsdb_symbol_table *symtab) | |
239 | { | |
240 | struct ovsdb_error *error0; | |
241 | const struct json *value; | |
242 | ||
243 | error0 = unwrap_json(json, "uuid", JSON_STRING, &value); | |
244 | if (!error0) { | |
245 | const char *uuid_string = json_string(value); | |
246 | if (!uuid_from_string(uuid, uuid_string)) { | |
247 | return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID", | |
248 | uuid_string); | |
249 | } | |
250 | } else if (symtab) { | |
251 | struct ovsdb_error *error1; | |
252 | ||
253 | error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value); | |
254 | if (!error1) { | |
255 | const char *name = json_string(value); | |
2d2d6d4a | 256 | const struct ovsdb_symbol *symbol; |
f85f8ebb BP |
257 | |
258 | ovsdb_error_destroy(error0); | |
259 | ||
2d2d6d4a BP |
260 | symbol = ovsdb_symbol_table_get(symtab, name); |
261 | if (symbol) { | |
262 | *uuid = symbol->uuid; | |
f85f8ebb BP |
263 | return NULL; |
264 | } else { | |
265 | return ovsdb_syntax_error(json, NULL, | |
266 | "unknown named-uuid \"%s\"", name); | |
267 | } | |
268 | } | |
269 | ovsdb_error_destroy(error1); | |
270 | } | |
271 | ||
272 | return error0; | |
273 | } | |
274 | ||
275 | struct ovsdb_error * | |
276 | ovsdb_atom_from_json(union ovsdb_atom *atom, enum ovsdb_atomic_type type, | |
277 | const struct json *json, | |
278 | const struct ovsdb_symbol_table *symtab) | |
279 | { | |
280 | switch (type) { | |
281 | case OVSDB_TYPE_VOID: | |
282 | NOT_REACHED(); | |
283 | ||
284 | case OVSDB_TYPE_INTEGER: | |
285 | if (json->type == JSON_INTEGER) { | |
286 | atom->integer = json->u.integer; | |
287 | return NULL; | |
288 | } | |
289 | break; | |
290 | ||
291 | case OVSDB_TYPE_REAL: | |
292 | if (json->type == JSON_INTEGER) { | |
293 | atom->real = json->u.integer; | |
294 | return NULL; | |
295 | } else if (json->type == JSON_REAL) { | |
296 | atom->real = json->u.real; | |
297 | return NULL; | |
298 | } | |
299 | break; | |
300 | ||
301 | case OVSDB_TYPE_BOOLEAN: | |
302 | if (json->type == JSON_TRUE) { | |
303 | atom->boolean = true; | |
304 | return NULL; | |
305 | } else if (json->type == JSON_FALSE) { | |
306 | atom->boolean = false; | |
307 | return NULL; | |
308 | } | |
309 | break; | |
310 | ||
311 | case OVSDB_TYPE_STRING: | |
312 | if (json->type == JSON_STRING) { | |
313 | atom->string = xstrdup(json->u.string); | |
314 | return NULL; | |
315 | } | |
316 | break; | |
317 | ||
318 | case OVSDB_TYPE_UUID: | |
319 | return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab); | |
320 | ||
321 | case OVSDB_N_TYPES: | |
322 | default: | |
323 | NOT_REACHED(); | |
324 | } | |
325 | ||
326 | return ovsdb_syntax_error(json, NULL, "expected %s", | |
327 | ovsdb_atomic_type_to_string(type)); | |
328 | } | |
329 | ||
330 | struct json * | |
331 | ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type) | |
332 | { | |
333 | switch (type) { | |
334 | case OVSDB_TYPE_VOID: | |
335 | NOT_REACHED(); | |
336 | ||
337 | case OVSDB_TYPE_INTEGER: | |
338 | return json_integer_create(atom->integer); | |
339 | ||
340 | case OVSDB_TYPE_REAL: | |
341 | return json_real_create(atom->real); | |
342 | ||
343 | case OVSDB_TYPE_BOOLEAN: | |
344 | return json_boolean_create(atom->boolean); | |
345 | ||
346 | case OVSDB_TYPE_STRING: | |
347 | return json_string_create(atom->string); | |
348 | ||
349 | case OVSDB_TYPE_UUID: | |
350 | return wrap_json("uuid", json_string_create_nocopy( | |
351 | xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid)))); | |
352 | ||
353 | case OVSDB_N_TYPES: | |
354 | default: | |
355 | NOT_REACHED(); | |
356 | } | |
357 | } | |
0194f33a BP |
358 | |
359 | /* Initializes 'atom' to a value of the given 'type' parsed from 's', which | |
360 | * takes one of the following forms: | |
361 | * | |
362 | * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign. | |
363 | * | |
364 | * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by | |
365 | * strtod(). | |
366 | * | |
367 | * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false", | |
368 | * "no", "off", or "0" for false. | |
369 | * | |
370 | * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise | |
371 | * an arbitrary string. | |
372 | * | |
373 | * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. | |
1bc6ff29 BP |
374 | * |
375 | * Returns a null pointer if successful, otherwise an error message describing | |
376 | * the problem. The caller is responsible for freeing the error. | |
0194f33a | 377 | */ |
1bc6ff29 | 378 | char * |
0194f33a BP |
379 | ovsdb_atom_from_string(union ovsdb_atom *atom, enum ovsdb_atomic_type type, |
380 | const char *s) | |
381 | { | |
382 | switch (type) { | |
383 | case OVSDB_TYPE_VOID: | |
384 | NOT_REACHED(); | |
385 | ||
386 | case OVSDB_TYPE_INTEGER: { | |
387 | long long int integer; | |
388 | if (!str_to_llong(s, 10, &integer)) { | |
1bc6ff29 | 389 | return xasprintf("\"%s\" is not a valid integer", s); |
0194f33a BP |
390 | } |
391 | atom->integer = integer; | |
392 | } | |
393 | break; | |
394 | ||
395 | case OVSDB_TYPE_REAL: | |
396 | if (!str_to_double(s, &atom->real)) { | |
1bc6ff29 | 397 | return xasprintf("\"%s\" is not a valid real number", s); |
0194f33a | 398 | } |
54a687fd BP |
399 | /* Our JSON input routines map negative zero to zero, so do that here |
400 | * too for consistency. */ | |
401 | if (atom->real == 0.0) { | |
402 | atom->real = 0.0; | |
403 | } | |
0194f33a BP |
404 | break; |
405 | ||
406 | case OVSDB_TYPE_BOOLEAN: | |
407 | if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on") | |
408 | || !strcmp(s, "1")) { | |
409 | atom->boolean = true; | |
410 | } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off") | |
411 | || !strcmp(s, "0")) { | |
412 | atom->boolean = false; | |
413 | } else { | |
1bc6ff29 BP |
414 | return xasprintf("\"%s\" is not a valid boolean " |
415 | "(use \"true\" or \"false\")", s); | |
0194f33a BP |
416 | } |
417 | break; | |
418 | ||
419 | case OVSDB_TYPE_STRING: | |
420 | if (*s == '\0') { | |
1bc6ff29 BP |
421 | return xstrdup("An empty string is not valid as input; " |
422 | "use \"\" to represent the empty string"); | |
0194f33a BP |
423 | } else if (*s == '"') { |
424 | size_t s_len = strlen(s); | |
425 | ||
426 | if (s_len < 2 || s[s_len - 1] != '"') { | |
1bc6ff29 BP |
427 | return xasprintf("%s: missing quote at end of " |
428 | "quoted string", s); | |
0194f33a BP |
429 | } else if (!json_string_unescape(s + 1, s_len - 2, |
430 | &atom->string)) { | |
1bc6ff29 BP |
431 | char *error = xasprintf("%s: %s", s, atom->string); |
432 | free(atom->string); | |
433 | return error; | |
0194f33a BP |
434 | } |
435 | } else { | |
436 | atom->string = xstrdup(s); | |
437 | } | |
438 | break; | |
439 | ||
440 | case OVSDB_TYPE_UUID: | |
441 | if (!uuid_from_string(&atom->uuid, s)) { | |
1bc6ff29 | 442 | return xasprintf("\"%s\" is not a valid UUID", s); |
0194f33a BP |
443 | } |
444 | break; | |
445 | ||
446 | case OVSDB_N_TYPES: | |
447 | default: | |
448 | NOT_REACHED(); | |
449 | } | |
1bc6ff29 BP |
450 | |
451 | return NULL; | |
0194f33a BP |
452 | } |
453 | ||
454 | static bool | |
455 | string_needs_quotes(const char *s) | |
456 | { | |
457 | const char *p = s; | |
458 | unsigned char c; | |
459 | ||
460 | c = *p++; | |
461 | if (!isalpha(c) && c != '_') { | |
462 | return true; | |
463 | } | |
464 | ||
465 | while ((c = *p++) != '\0') { | |
466 | if (!isalpha(c) && c != '_' && c != '-' && c != '.') { | |
467 | return true; | |
468 | } | |
469 | } | |
470 | ||
471 | if (!strcmp(s, "true") || !strcmp(s, "false")) { | |
472 | return true; | |
473 | } | |
474 | ||
475 | return false; | |
476 | } | |
477 | ||
478 | /* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable | |
479 | * to ovsdb_atom_from_string(). */ | |
480 | void | |
481 | ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, | |
482 | struct ds *out) | |
483 | { | |
484 | switch (type) { | |
485 | case OVSDB_TYPE_VOID: | |
486 | NOT_REACHED(); | |
487 | ||
488 | case OVSDB_TYPE_INTEGER: | |
489 | ds_put_format(out, "%"PRId64, atom->integer); | |
490 | break; | |
491 | ||
492 | case OVSDB_TYPE_REAL: | |
493 | ds_put_format(out, "%.*g", DBL_DIG, atom->real); | |
494 | break; | |
495 | ||
496 | case OVSDB_TYPE_BOOLEAN: | |
497 | ds_put_cstr(out, atom->boolean ? "true" : "false"); | |
498 | break; | |
499 | ||
500 | case OVSDB_TYPE_STRING: | |
501 | if (string_needs_quotes(atom->string)) { | |
502 | struct json json; | |
503 | ||
504 | json.type = JSON_STRING; | |
505 | json.u.string = atom->string; | |
506 | json_to_ds(&json, 0, out); | |
507 | } else { | |
508 | ds_put_cstr(out, atom->string); | |
509 | } | |
510 | break; | |
511 | ||
512 | case OVSDB_TYPE_UUID: | |
513 | ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid)); | |
514 | break; | |
515 | ||
516 | case OVSDB_N_TYPES: | |
517 | default: | |
518 | NOT_REACHED(); | |
519 | } | |
520 | } | |
f85f8ebb BP |
521 | \f |
522 | static union ovsdb_atom * | |
523 | alloc_default_atoms(enum ovsdb_atomic_type type, size_t n) | |
524 | { | |
525 | if (type != OVSDB_TYPE_VOID && n) { | |
526 | union ovsdb_atom *atoms; | |
527 | unsigned int i; | |
528 | ||
529 | atoms = xmalloc(n * sizeof *atoms); | |
530 | for (i = 0; i < n; i++) { | |
531 | ovsdb_atom_init_default(&atoms[i], type); | |
532 | } | |
533 | return atoms; | |
534 | } else { | |
535 | /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is | |
536 | * treated as xmalloc(1). */ | |
537 | return NULL; | |
538 | } | |
539 | } | |
540 | ||
2f47998b BP |
541 | void |
542 | ovsdb_datum_init_empty(struct ovsdb_datum *datum) | |
543 | { | |
544 | datum->n = 0; | |
545 | datum->keys = NULL; | |
546 | datum->values = NULL; | |
547 | } | |
548 | ||
f85f8ebb BP |
549 | void |
550 | ovsdb_datum_init_default(struct ovsdb_datum *datum, | |
551 | const struct ovsdb_type *type) | |
552 | { | |
553 | datum->n = type->n_min; | |
554 | datum->keys = alloc_default_atoms(type->key_type, datum->n); | |
555 | datum->values = alloc_default_atoms(type->value_type, datum->n); | |
556 | } | |
557 | ||
c532bf9d BP |
558 | bool |
559 | ovsdb_datum_is_default(const struct ovsdb_datum *datum, | |
560 | const struct ovsdb_type *type) | |
561 | { | |
562 | size_t i; | |
563 | ||
564 | if (datum->n != type->n_min) { | |
565 | return false; | |
566 | } | |
567 | for (i = 0; i < datum->n; i++) { | |
568 | if (!ovsdb_atom_is_default(&datum->keys[i], type->key_type)) { | |
569 | return false; | |
570 | } | |
571 | if (type->value_type != OVSDB_TYPE_VOID | |
572 | && !ovsdb_atom_is_default(&datum->values[i], type->value_type)) { | |
573 | return false; | |
574 | } | |
575 | } | |
576 | ||
577 | return true; | |
578 | } | |
579 | ||
f85f8ebb BP |
580 | static union ovsdb_atom * |
581 | clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n) | |
582 | { | |
583 | if (type != OVSDB_TYPE_VOID && n) { | |
584 | union ovsdb_atom *new; | |
585 | unsigned int i; | |
586 | ||
587 | new = xmalloc(n * sizeof *new); | |
588 | for (i = 0; i < n; i++) { | |
589 | ovsdb_atom_clone(&new[i], &old[i], type); | |
590 | } | |
591 | return new; | |
592 | } else { | |
593 | /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is | |
594 | * treated as xmalloc(1). */ | |
595 | return NULL; | |
596 | } | |
597 | } | |
598 | ||
599 | void | |
600 | ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old, | |
601 | const struct ovsdb_type *type) | |
602 | { | |
603 | unsigned int n = old->n; | |
604 | new->n = n; | |
605 | new->keys = clone_atoms(old->keys, type->key_type, n); | |
606 | new->values = clone_atoms(old->values, type->value_type, n); | |
607 | } | |
608 | ||
609 | static void | |
610 | free_data(enum ovsdb_atomic_type type, | |
611 | union ovsdb_atom *atoms, size_t n_atoms) | |
612 | { | |
613 | if (ovsdb_atom_needs_destruction(type)) { | |
614 | unsigned int i; | |
615 | for (i = 0; i < n_atoms; i++) { | |
616 | ovsdb_atom_destroy(&atoms[i], type); | |
617 | } | |
618 | } | |
619 | free(atoms); | |
620 | } | |
621 | ||
622 | void | |
623 | ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type) | |
624 | { | |
625 | free_data(type->key_type, datum->keys, datum->n); | |
626 | free_data(type->value_type, datum->values, datum->n); | |
627 | } | |
628 | ||
629 | void | |
630 | ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b) | |
631 | { | |
632 | struct ovsdb_datum tmp = *a; | |
633 | *a = *b; | |
634 | *b = tmp; | |
635 | } | |
636 | ||
637 | struct ovsdb_datum_sort_cbdata { | |
638 | const struct ovsdb_type *type; | |
639 | struct ovsdb_datum *datum; | |
640 | }; | |
641 | ||
642 | static int | |
643 | ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_) | |
644 | { | |
645 | struct ovsdb_datum_sort_cbdata *cbdata = cbdata_; | |
646 | ||
647 | return ovsdb_atom_compare_3way(&cbdata->datum->keys[a], | |
648 | &cbdata->datum->keys[b], | |
649 | cbdata->type->key_type); | |
650 | } | |
651 | ||
652 | static void | |
653 | ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_) | |
654 | { | |
655 | struct ovsdb_datum_sort_cbdata *cbdata = cbdata_; | |
656 | ||
657 | ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]); | |
658 | if (cbdata->type->value_type != OVSDB_TYPE_VOID) { | |
659 | ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]); | |
660 | } | |
661 | } | |
662 | ||
e9f8f936 | 663 | struct ovsdb_error * |
f85f8ebb BP |
664 | ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type) |
665 | { | |
666 | if (datum->n < 2) { | |
667 | return NULL; | |
668 | } else { | |
669 | struct ovsdb_datum_sort_cbdata cbdata; | |
670 | size_t i; | |
671 | ||
672 | cbdata.type = type; | |
673 | cbdata.datum = datum; | |
674 | sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb, | |
675 | &cbdata); | |
676 | ||
677 | for (i = 0; i < datum->n - 1; i++) { | |
678 | if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1], | |
679 | type->key_type)) { | |
680 | if (ovsdb_type_is_map(type)) { | |
681 | return ovsdb_error(NULL, "map contains duplicate key"); | |
682 | } else { | |
683 | return ovsdb_error(NULL, "set contains duplicate"); | |
684 | } | |
685 | } | |
686 | } | |
687 | ||
688 | return NULL; | |
689 | } | |
690 | } | |
691 | ||
692 | struct ovsdb_error * | |
693 | ovsdb_datum_from_json(struct ovsdb_datum *datum, | |
694 | const struct ovsdb_type *type, | |
695 | const struct json *json, | |
696 | const struct ovsdb_symbol_table *symtab) | |
697 | { | |
698 | struct ovsdb_error *error; | |
699 | ||
700 | if (ovsdb_type_is_scalar(type)) { | |
701 | datum->n = 1; | |
702 | datum->keys = xmalloc(sizeof *datum->keys); | |
703 | datum->values = NULL; | |
704 | ||
705 | error = ovsdb_atom_from_json(&datum->keys[0], type->key_type, | |
706 | json, symtab); | |
707 | if (error) { | |
708 | free(datum->keys); | |
709 | } | |
710 | return error; | |
711 | } else { | |
712 | bool is_map = ovsdb_type_is_map(type); | |
713 | const char *class = is_map ? "map" : "set"; | |
714 | const struct json *inner; | |
715 | unsigned int i; | |
716 | size_t n; | |
717 | ||
718 | assert(is_map || ovsdb_type_is_set(type)); | |
719 | ||
720 | error = unwrap_json(json, class, JSON_ARRAY, &inner); | |
721 | if (error) { | |
722 | return error; | |
723 | } | |
724 | ||
725 | n = inner->u.array.n; | |
726 | if (n < type->n_min || n > type->n_max) { | |
727 | return ovsdb_syntax_error(json, NULL, "%s must have %u to " | |
728 | "%u members but %zu are present", | |
729 | class, type->n_min, type->n_max, n); | |
730 | } | |
731 | ||
732 | datum->n = 0; | |
733 | datum->keys = xmalloc(n * sizeof *datum->keys); | |
734 | datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL; | |
735 | for (i = 0; i < n; i++) { | |
736 | const struct json *element = inner->u.array.elems[i]; | |
737 | const struct json *key = NULL; | |
738 | const struct json *value = NULL; | |
739 | ||
740 | if (!is_map) { | |
741 | key = element; | |
742 | } else { | |
743 | error = parse_json_pair(element, &key, &value); | |
744 | if (error) { | |
745 | goto error; | |
746 | } | |
747 | } | |
748 | ||
749 | error = ovsdb_atom_from_json(&datum->keys[i], type->key_type, | |
750 | key, symtab); | |
751 | if (error) { | |
752 | goto error; | |
753 | } | |
754 | ||
755 | if (is_map) { | |
756 | error = ovsdb_atom_from_json(&datum->values[i], | |
757 | type->value_type, value, symtab); | |
758 | if (error) { | |
759 | ovsdb_atom_destroy(&datum->keys[i], type->key_type); | |
760 | goto error; | |
761 | } | |
762 | } | |
763 | ||
764 | datum->n++; | |
765 | } | |
766 | ||
767 | error = ovsdb_datum_sort(datum, type); | |
768 | if (error) { | |
769 | goto error; | |
770 | } | |
771 | ||
772 | return NULL; | |
773 | ||
774 | error: | |
775 | ovsdb_datum_destroy(datum, type); | |
776 | return error; | |
777 | } | |
778 | } | |
779 | ||
780 | struct json * | |
781 | ovsdb_datum_to_json(const struct ovsdb_datum *datum, | |
782 | const struct ovsdb_type *type) | |
783 | { | |
784 | /* These tests somewhat tolerate a 'datum' that does not exactly match | |
785 | * 'type', in particular a datum with 'n' not in the allowed range. */ | |
786 | if (datum->n == 1 && ovsdb_type_is_scalar(type)) { | |
787 | return ovsdb_atom_to_json(&datum->keys[0], type->key_type); | |
788 | } else if (type->value_type == OVSDB_TYPE_VOID) { | |
789 | struct json **elems; | |
790 | size_t i; | |
791 | ||
792 | elems = xmalloc(datum->n * sizeof *elems); | |
793 | for (i = 0; i < datum->n; i++) { | |
794 | elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key_type); | |
795 | } | |
796 | ||
797 | return wrap_json("set", json_array_create(elems, datum->n)); | |
798 | } else { | |
799 | struct json **elems; | |
800 | size_t i; | |
801 | ||
802 | elems = xmalloc(datum->n * sizeof *elems); | |
803 | for (i = 0; i < datum->n; i++) { | |
804 | elems[i] = json_array_create_2( | |
805 | ovsdb_atom_to_json(&datum->keys[i], type->key_type), | |
806 | ovsdb_atom_to_json(&datum->values[i], type->value_type)); | |
807 | } | |
808 | ||
809 | return wrap_json("map", json_array_create(elems, datum->n)); | |
810 | } | |
811 | } | |
812 | ||
0194f33a BP |
813 | static const char * |
814 | skip_spaces(const char *p) | |
815 | { | |
cb456217 BP |
816 | while (isspace((unsigned char) *p)) { |
817 | p++; | |
818 | } | |
819 | return p; | |
0194f33a BP |
820 | } |
821 | ||
1bc6ff29 BP |
822 | static char * |
823 | parse_atom_token(const char **s, enum ovsdb_atomic_type type, | |
824 | union ovsdb_atom *atom) | |
0194f33a | 825 | { |
1bc6ff29 | 826 | char *token, *error; |
0194f33a | 827 | |
1bc6ff29 BP |
828 | error = ovsdb_token_parse(s, &token); |
829 | if (!error) { | |
830 | error = ovsdb_atom_from_string(atom, type, token); | |
831 | free(token); | |
832 | } | |
833 | return error; | |
834 | } | |
0194f33a | 835 | |
0194f33a | 836 | |
1bc6ff29 BP |
837 | static char * |
838 | parse_key_value(const char **s, const struct ovsdb_type *type, | |
839 | union ovsdb_atom *key, union ovsdb_atom *value) | |
840 | { | |
841 | const char *start = *s; | |
842 | char *error; | |
843 | ||
844 | error = parse_atom_token(s, type->key_type, key); | |
845 | if (!error && type->value_type != OVSDB_TYPE_VOID) { | |
9397a13f | 846 | *s = skip_spaces(*s); |
1bc6ff29 BP |
847 | if (**s == '=') { |
848 | (*s)++; | |
9397a13f | 849 | *s = skip_spaces(*s); |
1bc6ff29 BP |
850 | error = parse_atom_token(s, type->value_type, value); |
851 | } else { | |
852 | error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"", | |
853 | start, **s); | |
854 | } | |
855 | if (error) { | |
856 | ovsdb_atom_destroy(key, type->key_type); | |
0194f33a | 857 | } |
0194f33a | 858 | } |
1bc6ff29 | 859 | return error; |
0194f33a BP |
860 | } |
861 | ||
862 | static void | |
863 | free_key_value(const struct ovsdb_type *type, | |
864 | union ovsdb_atom *key, union ovsdb_atom *value) | |
865 | { | |
866 | ovsdb_atom_destroy(key, type->key_type); | |
867 | if (type->value_type != OVSDB_TYPE_VOID) { | |
868 | ovsdb_atom_destroy(value, type->value_type); | |
869 | } | |
870 | } | |
871 | ||
872 | /* Initializes 'datum' as a datum of the given 'type', parsing its contents | |
873 | * from 's'. The format of 's' is a series of space or comma separated atoms | |
874 | * or, for a map, '='-delimited pairs of atoms. Each atom must in a format | |
875 | * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed | |
876 | * in "[]" or a map in "{}"; for an empty set or map these punctuators are | |
877 | * required. */ | |
1bc6ff29 | 878 | char * |
0194f33a BP |
879 | ovsdb_datum_from_string(struct ovsdb_datum *datum, |
880 | const struct ovsdb_type *type, const char *s) | |
881 | { | |
882 | bool is_map = ovsdb_type_is_map(type); | |
1bc6ff29 | 883 | struct ovsdb_error *dberror; |
0194f33a BP |
884 | const char *p; |
885 | int end_delim; | |
1bc6ff29 | 886 | char *error; |
0194f33a BP |
887 | |
888 | ovsdb_datum_init_empty(datum); | |
889 | ||
890 | /* Swallow a leading delimiter if there is one. */ | |
891 | p = skip_spaces(s); | |
892 | if (*p == (is_map ? '{' : '[')) { | |
893 | end_delim = is_map ? '}' : ']'; | |
894 | p = skip_spaces(p + 1); | |
895 | } else if (!*p) { | |
896 | if (is_map) { | |
1bc6ff29 | 897 | return xstrdup("use \"{}\" to specify the empty map"); |
0194f33a | 898 | } else { |
1bc6ff29 | 899 | return xstrdup("use \"[]\" to specify the empty set"); |
0194f33a BP |
900 | } |
901 | } else { | |
902 | end_delim = 0; | |
903 | } | |
904 | ||
905 | while (*p && *p != end_delim) { | |
906 | union ovsdb_atom key, value; | |
907 | ||
908 | if (ovsdb_token_is_delim(*p)) { | |
1bc6ff29 BP |
909 | error = xasprintf("%s: unexpected \"%c\" parsing %s", |
910 | s, *p, ovsdb_type_to_english(type)); | |
911 | goto error; | |
0194f33a BP |
912 | } |
913 | ||
914 | /* Add to datum. */ | |
1bc6ff29 BP |
915 | error = parse_key_value(&p, type, &key, &value); |
916 | if (error) { | |
917 | goto error; | |
918 | } | |
0194f33a BP |
919 | ovsdb_datum_add_unsafe(datum, &key, &value, type); |
920 | free_key_value(type, &key, &value); | |
921 | ||
922 | /* Skip optional white space and comma. */ | |
923 | p = skip_spaces(p); | |
924 | if (*p == ',') { | |
925 | p = skip_spaces(p + 1); | |
926 | } | |
927 | } | |
928 | ||
929 | if (*p != end_delim) { | |
1bc6ff29 BP |
930 | error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim); |
931 | goto error; | |
0194f33a BP |
932 | } |
933 | if (end_delim) { | |
934 | p = skip_spaces(p + 1); | |
935 | if (*p) { | |
1bc6ff29 BP |
936 | error = xasprintf("%s: trailing garbage after \"%c\"", |
937 | s, end_delim); | |
938 | goto error; | |
0194f33a BP |
939 | } |
940 | } | |
941 | ||
942 | if (datum->n < type->n_min) { | |
1bc6ff29 BP |
943 | error = xasprintf("%s: %u %s specified but the minimum number is %u", |
944 | s, datum->n, is_map ? "pair(s)" : "value(s)", | |
945 | type->n_min); | |
946 | goto error; | |
0194f33a | 947 | } else if (datum->n > type->n_max) { |
1bc6ff29 BP |
948 | error = xasprintf("%s: %u %s specified but the maximum number is %u", |
949 | s, datum->n, is_map ? "pair(s)" : "value(s)", | |
950 | type->n_max); | |
951 | goto error; | |
0194f33a BP |
952 | } |
953 | ||
1bc6ff29 BP |
954 | dberror = ovsdb_datum_sort(datum, type); |
955 | if (dberror) { | |
956 | ovsdb_error_destroy(dberror); | |
0194f33a | 957 | if (ovsdb_type_is_map(type)) { |
1bc6ff29 | 958 | error = xasprintf("%s: map contains duplicate key", s); |
0194f33a | 959 | } else { |
1bc6ff29 | 960 | error = xasprintf("%s: set contains duplicate value", s); |
0194f33a | 961 | } |
1bc6ff29 | 962 | goto error; |
0194f33a | 963 | } |
1bc6ff29 BP |
964 | |
965 | return NULL; | |
966 | ||
967 | error: | |
968 | ovsdb_datum_destroy(datum, type); | |
969 | ovsdb_datum_init_empty(datum); | |
970 | return error; | |
0194f33a BP |
971 | } |
972 | ||
973 | /* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable | |
974 | * to ovsdb_datum_from_string(). */ | |
975 | void | |
976 | ovsdb_datum_to_string(const struct ovsdb_datum *datum, | |
977 | const struct ovsdb_type *type, struct ds *out) | |
978 | { | |
979 | bool is_map = ovsdb_type_is_map(type); | |
980 | size_t i; | |
981 | ||
982 | if (type->n_max > 1 || !datum->n) { | |
983 | ds_put_char(out, is_map ? '{' : '['); | |
984 | } | |
985 | for (i = 0; i < datum->n; i++) { | |
986 | if (i > 0) { | |
987 | ds_put_cstr(out, ", "); | |
988 | } | |
989 | ||
990 | ovsdb_atom_to_string(&datum->keys[i], type->key_type, out); | |
991 | if (is_map) { | |
992 | ds_put_char(out, '='); | |
993 | ovsdb_atom_to_string(&datum->values[i], type->value_type, out); | |
994 | } | |
995 | } | |
996 | if (type->n_max > 1 || !datum->n) { | |
997 | ds_put_char(out, is_map ? '}' : ']'); | |
998 | } | |
999 | } | |
1000 | ||
f85f8ebb BP |
1001 | static uint32_t |
1002 | hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms, | |
1003 | unsigned int n, uint32_t basis) | |
1004 | { | |
1005 | if (type != OVSDB_TYPE_VOID) { | |
1006 | unsigned int i; | |
1007 | ||
1008 | for (i = 0; i < n; i++) { | |
1009 | basis = ovsdb_atom_hash(&atoms[i], type, basis); | |
1010 | } | |
1011 | } | |
1012 | return basis; | |
1013 | } | |
1014 | ||
1015 | uint32_t | |
1016 | ovsdb_datum_hash(const struct ovsdb_datum *datum, | |
1017 | const struct ovsdb_type *type, uint32_t basis) | |
1018 | { | |
1019 | basis = hash_atoms(type->key_type, datum->keys, datum->n, basis); | |
1020 | basis ^= (type->key_type << 24) | (type->value_type << 16) | datum->n; | |
1021 | basis = hash_atoms(type->value_type, datum->values, datum->n, basis); | |
1022 | return basis; | |
1023 | } | |
1024 | ||
1025 | static int | |
1026 | atom_arrays_compare_3way(const union ovsdb_atom *a, | |
2f47998b BP |
1027 | const union ovsdb_atom *b, |
1028 | enum ovsdb_atomic_type type, | |
1029 | size_t n) | |
f85f8ebb BP |
1030 | { |
1031 | unsigned int i; | |
1032 | ||
1033 | for (i = 0; i < n; i++) { | |
1034 | int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type); | |
1035 | if (cmp) { | |
1036 | return cmp; | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | return 0; | |
1041 | } | |
1042 | ||
1043 | bool | |
1044 | ovsdb_datum_equals(const struct ovsdb_datum *a, | |
1045 | const struct ovsdb_datum *b, | |
1046 | const struct ovsdb_type *type) | |
1047 | { | |
1048 | return !ovsdb_datum_compare_3way(a, b, type); | |
1049 | } | |
1050 | ||
1051 | int | |
1052 | ovsdb_datum_compare_3way(const struct ovsdb_datum *a, | |
1053 | const struct ovsdb_datum *b, | |
1054 | const struct ovsdb_type *type) | |
1055 | { | |
1056 | int cmp; | |
1057 | ||
1058 | if (a->n != b->n) { | |
1059 | return a->n < b->n ? -1 : 1; | |
1060 | } | |
1061 | ||
1062 | cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key_type, a->n); | |
1063 | if (cmp) { | |
1064 | return cmp; | |
1065 | } | |
1066 | ||
1067 | return (type->value_type == OVSDB_TYPE_VOID ? 0 | |
1068 | : atom_arrays_compare_3way(a->values, b->values, type->value_type, | |
1069 | a->n)); | |
1070 | } | |
1071 | ||
2f47998b BP |
1072 | /* If 'key' is one of the keys in 'datum', returns its index within 'datum', |
1073 | * otherwise UINT_MAX. 'key_type' must be the type of the atoms stored in the | |
1074 | * 'keys' array in 'datum'. | |
1075 | */ | |
1076 | unsigned int | |
1077 | ovsdb_datum_find_key(const struct ovsdb_datum *datum, | |
1078 | const union ovsdb_atom *key, | |
1079 | enum ovsdb_atomic_type key_type) | |
1080 | { | |
1081 | unsigned int low = 0; | |
1082 | unsigned int high = datum->n; | |
1083 | while (low < high) { | |
1084 | unsigned int idx = (low + high) / 2; | |
1085 | int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type); | |
1086 | if (cmp < 0) { | |
1087 | high = idx; | |
1088 | } else if (cmp > 0) { | |
1089 | low = idx + 1; | |
1090 | } else { | |
1091 | return idx; | |
1092 | } | |
1093 | } | |
1094 | return UINT_MAX; | |
1095 | } | |
1096 | ||
1097 | /* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its | |
1098 | * index within 'datum', otherwise UINT_MAX. 'key_type' must be the type of | |
1099 | * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the | |
1100 | * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys. | |
1101 | */ | |
1102 | unsigned int | |
1103 | ovsdb_datum_find_key_value(const struct ovsdb_datum *datum, | |
1104 | const union ovsdb_atom *key, | |
1105 | enum ovsdb_atomic_type key_type, | |
1106 | const union ovsdb_atom *value, | |
1107 | enum ovsdb_atomic_type value_type) | |
1108 | { | |
1109 | unsigned int idx = ovsdb_datum_find_key(datum, key, key_type); | |
1110 | if (idx != UINT_MAX | |
1111 | && value_type != OVSDB_TYPE_VOID | |
1112 | && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) { | |
1113 | idx = UINT_MAX; | |
1114 | } | |
1115 | return idx; | |
1116 | } | |
1117 | ||
e9f8f936 BP |
1118 | /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise |
1119 | * UINT_MAX. 'type' must be the type of 'a' and 'b', except that | |
1120 | * type->value_type may be set to OVSDB_TYPE_VOID to compare keys but not | |
1121 | * values. */ | |
1122 | static unsigned int | |
1123 | ovsdb_datum_find(const struct ovsdb_datum *a, int i, | |
1124 | const struct ovsdb_datum *b, | |
1125 | const struct ovsdb_type *type) | |
f85f8ebb | 1126 | { |
2f47998b BP |
1127 | return ovsdb_datum_find_key_value(b, |
1128 | &a->keys[i], type->key_type, | |
1129 | a->values ? &a->values[i] : NULL, | |
1130 | type->value_type); | |
f85f8ebb BP |
1131 | } |
1132 | ||
1133 | /* Returns true if every element in 'a' is also in 'b', false otherwise. */ | |
1134 | bool | |
1135 | ovsdb_datum_includes_all(const struct ovsdb_datum *a, | |
1136 | const struct ovsdb_datum *b, | |
1137 | const struct ovsdb_type *type) | |
1138 | { | |
1139 | size_t i; | |
1140 | ||
1141 | for (i = 0; i < a->n; i++) { | |
e9f8f936 | 1142 | if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) { |
f85f8ebb BP |
1143 | return false; |
1144 | } | |
1145 | } | |
1146 | return true; | |
1147 | } | |
1148 | ||
1149 | /* Returns true if no element in 'a' is also in 'b', false otherwise. */ | |
1150 | bool | |
1151 | ovsdb_datum_excludes_all(const struct ovsdb_datum *a, | |
1152 | const struct ovsdb_datum *b, | |
1153 | const struct ovsdb_type *type) | |
1154 | { | |
1155 | size_t i; | |
1156 | ||
1157 | for (i = 0; i < a->n; i++) { | |
e9f8f936 | 1158 | if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) { |
f85f8ebb BP |
1159 | return false; |
1160 | } | |
1161 | } | |
1162 | return true; | |
1163 | } | |
e9f8f936 BP |
1164 | |
1165 | static void | |
1166 | ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type, | |
1167 | unsigned int capacity) | |
1168 | { | |
1169 | a->keys = xrealloc(a->keys, capacity * sizeof *a->keys); | |
1170 | if (type->value_type != OVSDB_TYPE_VOID) { | |
1171 | a->values = xrealloc(a->values, capacity * sizeof *a->values); | |
1172 | } | |
1173 | } | |
1174 | ||
2f47998b BP |
1175 | /* Removes the element with index 'idx' from 'datum', which has type 'type'. |
1176 | * If 'idx' is not the last element in 'datum', then the removed element is | |
1177 | * replaced by the (former) last element. | |
1178 | * | |
1179 | * This function does not maintain ovsdb_datum invariants. Use | |
1180 | * ovsdb_datum_sort() to check and restore these invariants. */ | |
1181 | void | |
1182 | ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx, | |
1183 | const struct ovsdb_type *type) | |
e9f8f936 | 1184 | { |
2f47998b BP |
1185 | ovsdb_atom_destroy(&datum->keys[idx], type->key_type); |
1186 | datum->keys[idx] = datum->keys[datum->n - 1]; | |
e9f8f936 | 1187 | if (type->value_type != OVSDB_TYPE_VOID) { |
2f47998b BP |
1188 | ovsdb_atom_destroy(&datum->values[idx], type->value_type); |
1189 | datum->values[idx] = datum->values[datum->n - 1]; | |
1190 | } | |
1191 | datum->n--; | |
1192 | } | |
1193 | ||
1194 | /* Adds the element with the given 'key' and 'value' to 'datum', which must | |
1195 | * have the specified 'type'. | |
1196 | * | |
1197 | * This function always allocates memory, so it is not an efficient way to add | |
1198 | * a number of elements to a datum. | |
1199 | * | |
1200 | * This function does not maintain ovsdb_datum invariants. Use | |
1201 | * ovsdb_datum_sort() to check and restore these invariants. (But a datum with | |
1202 | * 0 or 1 elements cannot violate the invariants anyhow.) */ | |
1203 | void | |
1204 | ovsdb_datum_add_unsafe(struct ovsdb_datum *datum, | |
1205 | const union ovsdb_atom *key, | |
1206 | const union ovsdb_atom *value, | |
1207 | const struct ovsdb_type *type) | |
1208 | { | |
1209 | size_t idx = datum->n++; | |
1210 | datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys); | |
1211 | ovsdb_atom_clone(&datum->keys[idx], key, type->key_type); | |
1212 | if (type->value_type != OVSDB_TYPE_VOID) { | |
1213 | datum->values = xrealloc(datum->values, | |
1214 | datum->n * sizeof *datum->values); | |
1215 | ovsdb_atom_clone(&datum->values[idx], value, type->value_type); | |
e9f8f936 | 1216 | } |
e9f8f936 BP |
1217 | } |
1218 | ||
1219 | void | |
2f47998b BP |
1220 | ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b, |
1221 | const struct ovsdb_type *type, bool replace) | |
e9f8f936 | 1222 | { |
e9f8f936 | 1223 | unsigned int n; |
2f47998b | 1224 | size_t bi; |
e9f8f936 | 1225 | |
e9f8f936 | 1226 | n = a->n; |
2f47998b BP |
1227 | for (bi = 0; bi < b->n; bi++) { |
1228 | unsigned int ai; | |
1229 | ||
1230 | ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key_type); | |
1231 | if (ai == UINT_MAX) { | |
e9f8f936 | 1232 | if (n == a->n) { |
2f47998b | 1233 | ovsdb_datum_reallocate(a, type, a->n + (b->n - bi)); |
e9f8f936 | 1234 | } |
2f47998b | 1235 | ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key_type); |
e9f8f936 | 1236 | if (type->value_type != OVSDB_TYPE_VOID) { |
2f47998b | 1237 | ovsdb_atom_clone(&a->values[n], &b->values[bi], |
e9f8f936 BP |
1238 | type->value_type); |
1239 | } | |
1240 | n++; | |
2f47998b BP |
1241 | } else if (replace && type->value_type != OVSDB_TYPE_VOID) { |
1242 | ovsdb_atom_destroy(&a->values[ai], type->value_type); | |
1243 | ovsdb_atom_clone(&a->values[ai], &b->values[bi], | |
1244 | type->value_type); | |
e9f8f936 BP |
1245 | } |
1246 | } | |
1247 | if (n != a->n) { | |
1248 | struct ovsdb_error *error; | |
1249 | a->n = n; | |
1250 | error = ovsdb_datum_sort(a, type); | |
1251 | assert(!error); | |
1252 | } | |
1253 | } | |
1254 | ||
1255 | void | |
1256 | ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type, | |
1257 | const struct ovsdb_datum *b, | |
1258 | const struct ovsdb_type *b_type) | |
1259 | { | |
1260 | bool changed = false; | |
1261 | size_t i; | |
1262 | ||
1263 | assert(a_type->key_type == b_type->key_type); | |
1264 | assert(a_type->value_type == b_type->value_type | |
1265 | || b_type->value_type == OVSDB_TYPE_VOID); | |
1266 | ||
1267 | /* XXX The big-O of this could easily be improved. */ | |
1268 | for (i = 0; i < a->n; ) { | |
1269 | unsigned int idx = ovsdb_datum_find(a, i, b, b_type); | |
1270 | if (idx != UINT_MAX) { | |
1271 | changed = true; | |
2f47998b | 1272 | ovsdb_datum_remove_unsafe(a, i, a_type); |
e9f8f936 BP |
1273 | } else { |
1274 | i++; | |
1275 | } | |
1276 | } | |
1277 | if (changed) { | |
1278 | struct ovsdb_error *error = ovsdb_datum_sort(a, a_type); | |
1279 | assert(!error); | |
1280 | } | |
1281 | } | |
f85f8ebb BP |
1282 | \f |
1283 | struct ovsdb_symbol_table { | |
1284 | struct shash sh; | |
1285 | }; | |
1286 | ||
1287 | struct ovsdb_symbol_table * | |
1288 | ovsdb_symbol_table_create(void) | |
1289 | { | |
1290 | struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab); | |
1291 | shash_init(&symtab->sh); | |
1292 | return symtab; | |
1293 | } | |
1294 | ||
1295 | void | |
1296 | ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab) | |
1297 | { | |
1298 | if (symtab) { | |
1299 | struct shash_node *node, *next; | |
1300 | ||
1301 | SHASH_FOR_EACH_SAFE (node, next, &symtab->sh) { | |
2d2d6d4a BP |
1302 | struct ovsdb_symbol *symbol = node->data; |
1303 | free(symbol); | |
f85f8ebb BP |
1304 | shash_delete(&symtab->sh, node); |
1305 | } | |
1306 | shash_destroy(&symtab->sh); | |
1307 | free(symtab); | |
1308 | } | |
1309 | } | |
1310 | ||
2d2d6d4a | 1311 | struct ovsdb_symbol * |
f85f8ebb BP |
1312 | ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab, |
1313 | const char *name) | |
1314 | { | |
1315 | return shash_find_data(&symtab->sh, name); | |
1316 | } | |
1317 | ||
1318 | void | |
1319 | ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name, | |
2d2d6d4a | 1320 | const struct uuid *uuid, bool used) |
f85f8ebb | 1321 | { |
2d2d6d4a BP |
1322 | struct ovsdb_symbol *symbol; |
1323 | ||
1324 | assert(!ovsdb_symbol_table_get(symtab, name)); | |
1325 | symbol = xmalloc(sizeof *symbol); | |
1326 | symbol->uuid = *uuid; | |
1327 | symbol->used = used; | |
1328 | shash_add(&symtab->sh, name, symbol); | |
f85f8ebb | 1329 | } |
0194f33a BP |
1330 | \f |
1331 | /* Extracts a token from the beginning of 's' and returns a pointer just after | |
1332 | * the token. Stores the token itself into '*outp', which the caller is | |
1333 | * responsible for freeing (with free()). | |
1334 | * | |
1335 | * If 's[0]' is a delimiter, the returned token is the empty string. | |
1336 | * | |
1337 | * A token extends from 's' to the first delimiter, as defined by | |
1338 | * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be | |
1339 | * escaped with a backslash, in which case the backslash does not appear in the | |
1340 | * output. Double quotes also cause delimiters to be ignored, but the double | |
1341 | * quotes are retained in the output. (Backslashes inside double quotes are | |
1342 | * not removed, either.) | |
1343 | */ | |
1bc6ff29 BP |
1344 | char * |
1345 | ovsdb_token_parse(const char **s, char **outp) | |
0194f33a BP |
1346 | { |
1347 | const char *p; | |
1348 | struct ds out; | |
1349 | bool in_quotes; | |
1bc6ff29 | 1350 | char *error; |
0194f33a BP |
1351 | |
1352 | ds_init(&out); | |
1353 | in_quotes = false; | |
1bc6ff29 | 1354 | for (p = *s; *p != '\0'; ) { |
0194f33a BP |
1355 | int c = *p++; |
1356 | if (c == '\\') { | |
1357 | if (in_quotes) { | |
1358 | ds_put_char(&out, '\\'); | |
1359 | } | |
1360 | if (!*p) { | |
1bc6ff29 BP |
1361 | error = xasprintf("%s: backslash at end of argument", *s); |
1362 | goto error; | |
0194f33a BP |
1363 | } |
1364 | ds_put_char(&out, *p++); | |
1365 | } else if (!in_quotes && ovsdb_token_is_delim(c)) { | |
1366 | p--; | |
1367 | break; | |
1368 | } else { | |
1369 | ds_put_char(&out, c); | |
1370 | if (c == '"') { | |
1371 | in_quotes = !in_quotes; | |
1372 | } | |
1373 | } | |
1374 | } | |
1375 | if (in_quotes) { | |
1bc6ff29 BP |
1376 | error = xasprintf("%s: quoted string extends past end of argument", |
1377 | *s); | |
1378 | goto error; | |
0194f33a BP |
1379 | } |
1380 | *outp = ds_cstr(&out); | |
1bc6ff29 BP |
1381 | *s = p; |
1382 | return NULL; | |
1383 | ||
1384 | error: | |
1385 | ds_destroy(&out); | |
1386 | *outp = NULL; | |
1387 | return error; | |
0194f33a BP |
1388 | } |
1389 | ||
1390 | /* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */ | |
1391 | bool | |
1392 | ovsdb_token_is_delim(unsigned char c) | |
1393 | { | |
1394 | return strchr(":=, []{}", c) != NULL; | |
1395 | } |