1 /* Copyright (c) 2009, 2010, 2011, 2012 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 if (!m->column->mutable) {
99 return ovsdb_syntax_error(json, "constraint violation",
100 "Cannot mutate immutable column %s in "
101 "table %s.", column_name, ts->name);
104 ovsdb_type_clone(&m->type, &m->column->type);
106 mutator_name = json_string(array->elems[1]);
107 error = ovsdb_mutator_from_string(mutator_name, &m->mutator);
112 /* Type-check and relax restrictions on 'type' if appropriate. */
113 switch (m->mutator) {
119 if ((!ovsdb_type_is_scalar(&m->type) && !ovsdb_type_is_set(&m->type))
120 || (m->type.key.type != OVSDB_TYPE_INTEGER
121 && m->type.key.type != OVSDB_TYPE_REAL)
122 || (m->mutator == OVSDB_M_MOD
123 && m->type.key.type == OVSDB_TYPE_REAL)) {
124 return type_mismatch(m, json);
126 ovsdb_base_type_clear_constraints(&m->type.key);
127 m->type.n_min = m->type.n_max = 1;
128 error = ovsdb_datum_from_json(&m->arg, &m->type, array->elems[2],
134 if (!ovsdb_type_is_set(&m->type) && !ovsdb_type_is_map(&m->type)) {
135 return type_mismatch(m, json);
138 if (m->mutator == OVSDB_M_DELETE) {
139 m->type.n_max = UINT_MAX;
141 error = ovsdb_datum_from_json(&m->arg, &m->type, array->elems[2],
143 if (error && ovsdb_type_is_map(&m->type)
144 && m->mutator == OVSDB_M_DELETE) {
145 ovsdb_error_destroy(error);
146 m->type.value.type = OVSDB_TYPE_VOID;
147 error = ovsdb_datum_from_json(&m->arg, &m->type, array->elems[2],
158 ovsdb_type_destroy(&m->type);
164 ovsdb_mutation_free(struct ovsdb_mutation *m)
166 ovsdb_datum_destroy(&m->arg, &m->type);
167 ovsdb_type_destroy(&m->type);
171 ovsdb_mutation_set_from_json(const struct ovsdb_table_schema *ts,
172 const struct json *json,
173 struct ovsdb_symbol_table *symtab,
174 struct ovsdb_mutation_set *set)
176 const struct json_array *array = json_array(json);
179 set->mutations = xmalloc(array->n * sizeof *set->mutations);
180 set->n_mutations = 0;
181 for (i = 0; i < array->n; i++) {
182 struct ovsdb_error *error;
183 error = ovsdb_mutation_from_json(ts, array->elems[i], symtab,
186 ovsdb_mutation_set_destroy(set);
187 set->mutations = NULL;
188 set->n_mutations = 0;
198 ovsdb_mutation_to_json(const struct ovsdb_mutation *m)
200 return json_array_create_3(
201 json_string_create(m->column->name),
202 json_string_create(ovsdb_mutator_to_string(m->mutator)),
203 ovsdb_datum_to_json(&m->arg, &m->type));
207 ovsdb_mutation_set_to_json(const struct ovsdb_mutation_set *set)
209 struct json **mutations;
212 mutations = xmalloc(set->n_mutations * sizeof *mutations);
213 for (i = 0; i < set->n_mutations; i++) {
214 mutations[i] = ovsdb_mutation_to_json(&set->mutations[i]);
216 return json_array_create(mutations, set->n_mutations);
220 ovsdb_mutation_set_destroy(struct ovsdb_mutation_set *set)
224 for (i = 0; i < set->n_mutations; i++) {
225 ovsdb_mutation_free(&set->mutations[i]);
227 free(set->mutations);
230 enum ovsdb_mutation_scalar_error {
236 struct ovsdb_scalar_mutation {
237 int (*mutate_integer)(int64_t *x, int64_t y);
238 int (*mutate_real)(double *x, double y);
239 enum ovsdb_mutator mutator;
242 static const struct ovsdb_scalar_mutation add_mutation;
243 static const struct ovsdb_scalar_mutation sub_mutation;
244 static const struct ovsdb_scalar_mutation mul_mutation;
245 static const struct ovsdb_scalar_mutation div_mutation;
246 static const struct ovsdb_scalar_mutation mod_mutation;
248 static struct ovsdb_error *
249 ovsdb_mutation_scalar_error(enum ovsdb_mutation_scalar_error error,
250 enum ovsdb_mutator mutator)
254 return OVSDB_BUG("unexpected success");
257 return ovsdb_error("domain error", "Division by zero.");
260 return ovsdb_error("range error",
261 "Result of \"%s\" operation is out of range.",
262 ovsdb_mutator_to_string(mutator));
265 return OVSDB_BUG("unexpected error");
270 check_real_range(double x)
272 return x >= -DBL_MAX && x <= DBL_MAX ? 0 : ME_RANGE;
275 static struct ovsdb_error *
276 mutate_scalar(const struct ovsdb_type *dst_type, struct ovsdb_datum *dst,
277 const union ovsdb_atom *arg,
278 const struct ovsdb_scalar_mutation *mutation)
280 const struct ovsdb_base_type *base = &dst_type->key;
281 struct ovsdb_error *error;
284 if (base->type == OVSDB_TYPE_INTEGER) {
285 int64_t y = arg->integer;
286 for (i = 0; i < dst->n; i++) {
287 enum ovsdb_mutation_scalar_error me;
289 me = (mutation->mutate_integer)(&dst->keys[i].integer, y);
291 return ovsdb_mutation_scalar_error(me, mutation->mutator);
294 } else if (base->type == OVSDB_TYPE_REAL) {
295 double y = arg->real;
296 for (i = 0; i < dst->n; i++) {
297 double *x = &dst->keys[i].real;
298 enum ovsdb_mutation_scalar_error me;
300 me = (mutation->mutate_real)(x, y);
302 me = check_real_range(*x);
305 return ovsdb_mutation_scalar_error(me, mutation->mutator);
312 for (i = 0; i < dst->n; i++) {
313 error = ovsdb_atom_check_constraints(&dst->keys[i], base);
319 error = ovsdb_datum_sort(dst, dst_type->key.type);
321 ovsdb_error_destroy(error);
322 return ovsdb_error("constraint violation",
323 "Result of \"%s\" operation contains duplicates.",
324 ovsdb_mutator_to_string(mutation->mutator));
329 static struct ovsdb_error *
330 ovsdb_mutation_check_count(struct ovsdb_datum *dst,
331 const struct ovsdb_type *dst_type)
333 if (!ovsdb_datum_conforms_to_type(dst, dst_type)) {
334 char *s = ovsdb_type_to_english(dst_type);
335 struct ovsdb_error *e = ovsdb_error(
336 "constraint violation",
337 "Attempted to store %u elements in %s.", dst->n, s);
345 ovsdb_mutation_set_execute(struct ovsdb_row *row,
346 const struct ovsdb_mutation_set *set)
350 for (i = 0; i < set->n_mutations; i++) {
351 const struct ovsdb_mutation *m = &set->mutations[i];
352 struct ovsdb_datum *dst = &row->fields[m->column->index];
353 const struct ovsdb_type *dst_type = &m->column->type;
354 const struct ovsdb_datum *arg = &set->mutations[i].arg;
355 const struct ovsdb_type *arg_type = &m->type;
356 struct ovsdb_error *error;
358 switch (m->mutator) {
360 error = mutate_scalar(dst_type, dst, &arg->keys[0], &add_mutation);
364 error = mutate_scalar(dst_type, dst, &arg->keys[0], &sub_mutation);
368 error = mutate_scalar(dst_type, dst, &arg->keys[0], &mul_mutation);
372 error = mutate_scalar(dst_type, dst, &arg->keys[0], &div_mutation);
376 error = mutate_scalar(dst_type, dst, &arg->keys[0], &mod_mutation);
380 ovsdb_datum_union(dst, arg, dst_type, false);
381 error = ovsdb_mutation_check_count(dst, dst_type);
385 ovsdb_datum_subtract(dst, dst_type, arg, arg_type);
386 error = ovsdb_mutation_check_count(dst, dst_type);
401 add_int(int64_t *x, int64_t y)
403 /* Check for overflow. See _Hacker's Delight_ pp. 27. */
404 int64_t z = ~(*x ^ y) & INT64_MIN;
405 if ((~(*x ^ y) & ~(((*x ^ z) + y) ^ y)) >> 63) {
414 sub_int(int64_t *x, int64_t y)
416 /* Check for overflow. See _Hacker's Delight_ pp. 27. */
417 int64_t z = (*x ^ y) & INT64_MIN;
418 if (((*x ^ y) & (((*x ^ z) - y) ^ y)) >> 63) {
427 mul_int(int64_t *x, int64_t y)
429 /* Check for overflow. See _Hacker's Delight_ pp. 30. */
432 ? *x >= INT64_MAX / y
433 : y < INT64_MIN / *x)
436 : *x != 0 && y < INT64_MAX / y)) {
445 check_int_div(int64_t x, int64_t y)
447 /* Check for overflow. See _Hacker's Delight_ pp. 32. */
450 } else if (x == INT64_MIN && y == -1) {
458 div_int(int64_t *x, int64_t y)
460 int error = check_int_div(*x, y);
468 mod_int(int64_t *x, int64_t y)
470 int error = check_int_div(*x, y);
478 add_double(double *x, double y)
485 sub_double(double *x, double y)
492 mul_double(double *x, double y)
499 div_double(double *x, double y)
509 static const struct ovsdb_scalar_mutation add_mutation = {
510 add_int, add_double, OVSDB_M_ADD
513 static const struct ovsdb_scalar_mutation sub_mutation = {
514 sub_int, sub_double, OVSDB_M_SUB
517 static const struct ovsdb_scalar_mutation mul_mutation = {
518 mul_int, mul_double, OVSDB_M_MUL
521 static const struct ovsdb_scalar_mutation div_mutation = {
522 div_int, div_double, OVSDB_M_DIV
525 static const struct ovsdb_scalar_mutation mod_mutation = {
526 mod_int, NULL, OVSDB_M_MOD