1 /* Copyright (c) 2009, 2010, 2011 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.
24 #include "ovsdb-error.h"
30 ovsdb_mutator_from_string(const char *name, enum ovsdb_mutator *mutator)
32 #define OVSDB_MUTATOR(ENUM, NAME) \
33 if (!strcmp(name, NAME)) { \
40 return ovsdb_syntax_error(NULL, "unknown mutator",
41 "No mutator named %s.", name);
45 ovsdb_mutator_to_string(enum ovsdb_mutator mutator)
48 #define OVSDB_MUTATOR(ENUM, NAME) case ENUM: return NAME;
56 static WARN_UNUSED_RESULT struct ovsdb_error *
57 type_mismatch(const struct ovsdb_mutation *m, const struct json *json)
59 struct ovsdb_error *error;
62 s = ovsdb_type_to_english(&m->column->type);
63 error = ovsdb_syntax_error(
64 json, NULL, "Type mismatch: \"%s\" operator may not be "
65 "applied to column %s of type %s.",
66 ovsdb_mutator_to_string(m->mutator), m->column->name, s);
72 static WARN_UNUSED_RESULT struct ovsdb_error *
73 ovsdb_mutation_from_json(const struct ovsdb_table_schema *ts,
74 const struct json *json,
75 struct ovsdb_symbol_table *symtab,
76 struct ovsdb_mutation *m)
78 const struct json_array *array;
79 struct ovsdb_error *error;
80 const char *mutator_name;
81 const char *column_name;
83 if (json->type != JSON_ARRAY
84 || json->u.array.n != 3
85 || json->u.array.elems[0]->type != JSON_STRING
86 || json->u.array.elems[1]->type != JSON_STRING) {
87 return ovsdb_syntax_error(json, NULL, "Parse error in mutation.");
89 array = json_array(json);
91 column_name = json_string(array->elems[0]);
92 m->column = ovsdb_table_schema_get_column(ts, column_name);
94 return ovsdb_syntax_error(json, "unknown column",
95 "No column %s in table %s.",
96 column_name, ts->name);
98 ovsdb_type_clone(&m->type, &m->column->type);
100 mutator_name = json_string(array->elems[1]);
101 error = ovsdb_mutator_from_string(mutator_name, &m->mutator);
106 /* Type-check and relax restrictions on 'type' if appropriate. */
107 switch (m->mutator) {
113 if ((!ovsdb_type_is_scalar(&m->type) && !ovsdb_type_is_set(&m->type))
114 || (m->type.key.type != OVSDB_TYPE_INTEGER
115 && m->type.key.type != OVSDB_TYPE_REAL)
116 || (m->mutator == OVSDB_M_MOD
117 && m->type.key.type == OVSDB_TYPE_REAL)) {
118 return type_mismatch(m, json);
120 ovsdb_base_type_clear_constraints(&m->type.key);
121 m->type.n_min = m->type.n_max = 1;
122 error = ovsdb_datum_from_json(&m->arg, &m->type, array->elems[2],
128 if (!ovsdb_type_is_set(&m->type) && !ovsdb_type_is_map(&m->type)) {
129 return type_mismatch(m, json);
132 if (m->mutator == OVSDB_M_DELETE) {
133 m->type.n_max = UINT_MAX;
135 error = ovsdb_datum_from_json(&m->arg, &m->type, array->elems[2],
137 if (error && ovsdb_type_is_map(&m->type)
138 && m->mutator == OVSDB_M_DELETE) {
139 ovsdb_error_destroy(error);
140 m->type.value.type = OVSDB_TYPE_VOID;
141 error = ovsdb_datum_from_json(&m->arg, &m->type, array->elems[2],
152 ovsdb_type_destroy(&m->type);
158 ovsdb_mutation_free(struct ovsdb_mutation *m)
160 ovsdb_datum_destroy(&m->arg, &m->type);
161 ovsdb_type_destroy(&m->type);
165 ovsdb_mutation_set_from_json(const struct ovsdb_table_schema *ts,
166 const struct json *json,
167 struct ovsdb_symbol_table *symtab,
168 struct ovsdb_mutation_set *set)
170 const struct json_array *array = json_array(json);
173 set->mutations = xmalloc(array->n * sizeof *set->mutations);
174 set->n_mutations = 0;
175 for (i = 0; i < array->n; i++) {
176 struct ovsdb_error *error;
177 error = ovsdb_mutation_from_json(ts, array->elems[i], symtab,
180 ovsdb_mutation_set_destroy(set);
181 set->mutations = NULL;
182 set->n_mutations = 0;
192 ovsdb_mutation_to_json(const struct ovsdb_mutation *m)
194 return json_array_create_3(
195 json_string_create(m->column->name),
196 json_string_create(ovsdb_mutator_to_string(m->mutator)),
197 ovsdb_datum_to_json(&m->arg, &m->type));
201 ovsdb_mutation_set_to_json(const struct ovsdb_mutation_set *set)
203 struct json **mutations;
206 mutations = xmalloc(set->n_mutations * sizeof *mutations);
207 for (i = 0; i < set->n_mutations; i++) {
208 mutations[i] = ovsdb_mutation_to_json(&set->mutations[i]);
210 return json_array_create(mutations, set->n_mutations);
214 ovsdb_mutation_set_destroy(struct ovsdb_mutation_set *set)
218 for (i = 0; i < set->n_mutations; i++) {
219 ovsdb_mutation_free(&set->mutations[i]);
221 free(set->mutations);
224 enum ovsdb_mutation_scalar_error {
230 struct ovsdb_scalar_mutation {
231 int (*mutate_integer)(int64_t *x, int64_t y);
232 int (*mutate_real)(double *x, double y);
233 enum ovsdb_mutator mutator;
236 static const struct ovsdb_scalar_mutation add_mutation;
237 static const struct ovsdb_scalar_mutation sub_mutation;
238 static const struct ovsdb_scalar_mutation mul_mutation;
239 static const struct ovsdb_scalar_mutation div_mutation;
240 static const struct ovsdb_scalar_mutation mod_mutation;
242 static struct ovsdb_error *
243 ovsdb_mutation_scalar_error(enum ovsdb_mutation_scalar_error error,
244 enum ovsdb_mutator mutator)
248 return OVSDB_BUG("unexpected success");
251 return ovsdb_error("domain error", "Division by zero.");
254 return ovsdb_error("range error",
255 "Result of \"%s\" operation is out of range.",
256 ovsdb_mutator_to_string(mutator));
259 return OVSDB_BUG("unexpected error");
264 check_real_range(double x)
266 return x >= -DBL_MAX && x <= DBL_MAX ? 0 : ME_RANGE;
269 static struct ovsdb_error *
270 mutate_scalar(const struct ovsdb_type *dst_type, struct ovsdb_datum *dst,
271 const union ovsdb_atom *arg,
272 const struct ovsdb_scalar_mutation *mutation)
274 const struct ovsdb_base_type *base = &dst_type->key;
275 struct ovsdb_error *error;
278 if (base->type == OVSDB_TYPE_INTEGER) {
279 int64_t y = arg->integer;
280 for (i = 0; i < dst->n; i++) {
281 enum ovsdb_mutation_scalar_error me;
283 me = (mutation->mutate_integer)(&dst->keys[i].integer, y);
285 return ovsdb_mutation_scalar_error(me, mutation->mutator);
288 } else if (base->type == OVSDB_TYPE_REAL) {
289 double y = arg->real;
290 for (i = 0; i < dst->n; i++) {
291 double *x = &dst->keys[i].real;
292 enum ovsdb_mutation_scalar_error me;
294 me = (mutation->mutate_real)(x, y);
296 me = check_real_range(*x);
299 return ovsdb_mutation_scalar_error(me, mutation->mutator);
306 for (i = 0; i < dst->n; i++) {
307 error = ovsdb_atom_check_constraints(&dst->keys[i], base);
313 error = ovsdb_datum_sort(dst, dst_type->key.type);
315 ovsdb_error_destroy(error);
316 return ovsdb_error("constraint violation",
317 "Result of \"%s\" operation contains duplicates.",
318 ovsdb_mutator_to_string(mutation->mutator));
323 static struct ovsdb_error *
324 ovsdb_mutation_check_count(struct ovsdb_datum *dst,
325 const struct ovsdb_type *dst_type)
327 if (!ovsdb_datum_conforms_to_type(dst, dst_type)) {
328 char *s = ovsdb_type_to_english(dst_type);
329 struct ovsdb_error *e = ovsdb_error(
330 "constraint violation",
331 "Attempted to store %u elements in %s.", dst->n, s);
339 ovsdb_mutation_set_execute(struct ovsdb_row *row,
340 const struct ovsdb_mutation_set *set)
344 for (i = 0; i < set->n_mutations; i++) {
345 const struct ovsdb_mutation *m = &set->mutations[i];
346 struct ovsdb_datum *dst = &row->fields[m->column->index];
347 const struct ovsdb_type *dst_type = &m->column->type;
348 const struct ovsdb_datum *arg = &set->mutations[i].arg;
349 const struct ovsdb_type *arg_type = &m->type;
350 struct ovsdb_error *error;
352 switch (m->mutator) {
354 error = mutate_scalar(dst_type, dst, &arg->keys[0], &add_mutation);
358 error = mutate_scalar(dst_type, dst, &arg->keys[0], &sub_mutation);
362 error = mutate_scalar(dst_type, dst, &arg->keys[0], &mul_mutation);
366 error = mutate_scalar(dst_type, dst, &arg->keys[0], &div_mutation);
370 error = mutate_scalar(dst_type, dst, &arg->keys[0], &mod_mutation);
374 ovsdb_datum_union(dst, arg, dst_type, false);
375 error = ovsdb_mutation_check_count(dst, dst_type);
379 ovsdb_datum_subtract(dst, dst_type, arg, arg_type);
380 error = ovsdb_mutation_check_count(dst, dst_type);
395 add_int(int64_t *x, int64_t y)
397 /* Check for overflow. See _Hacker's Delight_ pp. 27. */
398 int64_t z = ~(*x ^ y) & INT64_MIN;
399 if ((~(*x ^ y) & ~(((*x ^ z) + y) ^ y)) >> 63) {
408 sub_int(int64_t *x, int64_t y)
410 /* Check for overflow. See _Hacker's Delight_ pp. 27. */
411 int64_t z = (*x ^ y) & INT64_MIN;
412 if (((*x ^ y) & (((*x ^ z) - y) ^ y)) >> 63) {
421 mul_int(int64_t *x, int64_t y)
423 /* Check for overflow. See _Hacker's Delight_ pp. 30. */
426 ? *x >= INT64_MAX / y
427 : y < INT64_MIN / *x)
430 : *x != 0 && y < INT64_MAX / y)) {
439 check_int_div(int64_t x, int64_t y)
441 /* Check for overflow. See _Hacker's Delight_ pp. 32. */
444 } else if (x == INT64_MIN && y == -1) {
452 div_int(int64_t *x, int64_t y)
454 int error = check_int_div(*x, y);
462 mod_int(int64_t *x, int64_t y)
464 int error = check_int_div(*x, y);
472 add_double(double *x, double y)
479 sub_double(double *x, double y)
486 mul_double(double *x, double y)
493 div_double(double *x, double y)
503 static const struct ovsdb_scalar_mutation add_mutation = {
504 add_int, add_double, OVSDB_M_ADD
507 static const struct ovsdb_scalar_mutation sub_mutation = {
508 sub_int, sub_double, OVSDB_M_SUB
511 static const struct ovsdb_scalar_mutation mul_mutation = {
512 mul_int, mul_double, OVSDB_M_MUL
515 static const struct ovsdb_scalar_mutation div_mutation = {
516 div_int, div_double, OVSDB_M_DIV
519 static const struct ovsdb_scalar_mutation mod_mutation = {
520 mod_int, NULL, OVSDB_M_MOD