[ovs-dev] [PATCH monitor_cond V4 02/17] ovsdb: add conditions utilities to support monitor_cond

Liran Schour lirans at il.ibm.com
Thu Feb 18 11:46:40 UTC 2016


Change ovsdb_condition to be a 3-element json array or a boolean value.
Conditions utilities will be used later for conditional monitoring.

Signed-off-by: Liran Schour <lirans at il.ibm.com>

---
v3->v4:
* Pass index_map only to ovsdb_condition_evaluate_or_datum()
* Added ovsdb_condition_is_[true|false]()
* Added ovsdb_condition_get_max_function()
* Added ovsdb_condition_diff()

v2->v3:
* Remove condition_add() and condition_remove() and all sub-functions
* Allow single bollean value XOR <condition>
---
 ovsdb/condition.c        | 210 +++++++++++++++++++++++++++++++++++++++++++++--
 ovsdb/condition.h        |  20 +++++
 tests/ovsdb-condition.at |  30 +++++++
 tests/test-ovsdb.c       |  28 ++++++-
 4 files changed, 277 insertions(+), 11 deletions(-)

diff --git a/ovsdb/condition.c b/ovsdb/condition.c
index 4baf1bb..c2210e5 100644
--- a/ovsdb/condition.c
+++ b/ovsdb/condition.c
@@ -64,6 +64,18 @@ ovsdb_clause_from_json(const struct ovsdb_table_schema *ts,
     const char *column_name;
     struct ovsdb_type type;
 
+    if (json->type == JSON_TRUE || json->type == JSON_FALSE) {
+        function_name = (json->type == JSON_TRUE) ? "true" : "false";
+        error = ovsdb_function_from_string(function_name, &clause->function);
+
+        /* column and arg fields are not being used with boolean function
+         * use dummy values */
+        clause->column = ovsdb_table_schema_get_column(ts, "_uuid");
+        clause->index = clause->column->index;
+        ovsdb_datum_init_default(&clause->arg, &clause->column->type);
+        return error;
+    }
+
     if (json->type != JSON_ARRAY
         || json->u.array.n != 3
         || json->u.array.elems[0]->type != JSON_STRING
@@ -79,6 +91,7 @@ ovsdb_clause_from_json(const struct ovsdb_table_schema *ts,
                                   "No column %s in table %s.",
                                   column_name, ts->name);
     }
+    clause->index = clause->column->index;
     type = clause->column->type;
 
     function_name = json_string(array->elems[1]);
@@ -109,7 +122,8 @@ ovsdb_clause_from_json(const struct ovsdb_table_schema *ts,
             return error;
         }
         break;
-
+    case OVSDB_F_TRUE:
+    case OVSDB_F_FALSE:
     case OVSDB_F_EQ:
     case OVSDB_F_NE:
         break;
@@ -164,6 +178,19 @@ compare_clauses_3way(const void *a_, const void *b_)
     }
 }
 
+static int
+compare_clauses_3way_with_data(const void *a_, const void *b_)
+{
+    const struct ovsdb_clause *a = a_;
+    const struct ovsdb_clause *b = b_;
+    int res;
+
+    res = compare_clauses_3way(a, b);
+    return res ? res : ovsdb_datum_compare_3way(&a->arg,
+                                                &b->arg,
+                                                &a->column->type);
+ }
+
 struct ovsdb_error *
 ovsdb_condition_from_json(const struct ovsdb_table_schema *ts,
                           const struct json *json,
@@ -198,10 +225,15 @@ ovsdb_condition_from_json(const struct ovsdb_table_schema *ts,
 static struct json *
 ovsdb_clause_to_json(const struct ovsdb_clause *clause)
 {
-    return json_array_create_3(
-        json_string_create(clause->column->name),
-        json_string_create(ovsdb_function_to_string(clause->function)),
-        ovsdb_datum_to_json(&clause->arg, &clause->column->type));
+    if (clause->function > OVSDB_F_TRUE) {
+        return json_array_create_3(
+                json_string_create(clause->column->name),
+                json_string_create(ovsdb_function_to_string(clause->function)),
+                ovsdb_datum_to_json(&clause->arg, &clause->column->type));
+    }
+
+    return json_boolean_create(clause->function == OVSDB_F_TRUE ?
+                               true : false);
 }
 
 struct json *
@@ -218,13 +250,19 @@ ovsdb_condition_to_json(const struct ovsdb_condition *cnd)
 }
 
 static bool
-ovsdb_clause_evaluate(const struct ovsdb_row *row,
-                      const struct ovsdb_clause *c)
+ovsdb_clause_evaluate(const struct ovsdb_datum *fields,
+                      const struct ovsdb_clause *c,
+                      unsigned int index_map[])
 {
-    const struct ovsdb_datum *field = &row->fields[c->column->index];
+    const struct ovsdb_datum *field = &fields[index_map ?
+                                              index_map[c->column->index] :
+                                              c->column->index];
     const struct ovsdb_datum *arg = &c->arg;
     const struct ovsdb_type *type = &c->column->type;
 
+    if (c->function <= OVSDB_F_TRUE) {
+        return c->function == OVSDB_F_TRUE ? true : false;
+    }
     if (ovsdb_type_is_optional_scalar(type) && field->n == 0) {
         switch (c->function) {
             case OVSDB_F_LT:
@@ -237,6 +275,9 @@ ovsdb_clause_evaluate(const struct ovsdb_row *row,
             case OVSDB_F_NE:
             case OVSDB_F_EXCLUDES:
                 return true;
+            case OVSDB_F_TRUE:
+            case OVSDB_F_FALSE:
+                OVS_NOT_REACHED();
         }
     } else if (ovsdb_type_is_scalar(type)
                || ovsdb_type_is_optional_scalar(type)) {
@@ -257,6 +298,9 @@ ovsdb_clause_evaluate(const struct ovsdb_row *row,
             return cmp >= 0;
         case OVSDB_F_GT:
             return cmp > 0;
+        case OVSDB_F_TRUE:
+        case OVSDB_F_FALSE:
+            OVS_NOT_REACHED();
         }
     } else {
         switch (c->function) {
@@ -272,6 +316,8 @@ ovsdb_clause_evaluate(const struct ovsdb_row *row,
         case OVSDB_F_LE:
         case OVSDB_F_GE:
         case OVSDB_F_GT:
+        case OVSDB_F_TRUE:
+        case OVSDB_F_FALSE:
             OVS_NOT_REACHED();
         }
     }
@@ -279,6 +325,28 @@ ovsdb_clause_evaluate(const struct ovsdb_row *row,
     OVS_NOT_REACHED();
 }
 
+static void
+ovsdb_clause_clone(struct ovsdb_clause *new, struct ovsdb_clause *old)
+{
+    new->function = old->function;
+    new->column = old->column;
+    ovsdb_datum_clone(&new->arg,
+                      &old->arg,
+                      &old->column->type);
+}
+
+static void
+ovsdb_clause_add(struct ovsdb_condition *cond, struct ovsdb_clause *c,
+                 size_t *allocated_clauses)
+{
+    if (cond->n_clauses >= *allocated_clauses) {
+        cond->clauses = x2nrealloc(cond->clauses, allocated_clauses,
+                                   sizeof *cond->clauses);
+    }
+    ovsdb_clause_clone(&cond->clauses[cond->n_clauses], c);
+    cond->n_clauses++;
+}
+
 bool
 ovsdb_condition_evaluate(const struct ovsdb_row *row,
                          const struct ovsdb_condition *cnd)
@@ -286,7 +354,7 @@ ovsdb_condition_evaluate(const struct ovsdb_row *row,
     size_t i;
 
     for (i = 0; i < cnd->n_clauses; i++) {
-        if (!ovsdb_clause_evaluate(row, &cnd->clauses[i])) {
+        if (!ovsdb_clause_evaluate(row->fields, &cnd->clauses[i], NULL)) {
             return false;
         }
     }
@@ -294,6 +362,26 @@ ovsdb_condition_evaluate(const struct ovsdb_row *row,
     return true;
 }
 
+bool
+ovsdb_condition_evaluate_or_datum(const struct ovsdb_datum *row_datum,
+                                  const struct ovsdb_condition *cnd,
+                                  unsigned int index_map[])
+{
+    size_t i;
+
+    if (cnd->n_clauses == 0) {
+        return true;
+    }
+
+    for (i = 0; i < cnd->n_clauses; i++) {
+        if (ovsdb_clause_evaluate(row_datum, &cnd->clauses[i], index_map)) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
 void
 ovsdb_condition_destroy(struct ovsdb_condition *cnd)
 {
@@ -303,4 +391,108 @@ ovsdb_condition_destroy(struct ovsdb_condition *cnd)
         ovsdb_clause_free(&cnd->clauses[i]);
     }
     free(cnd->clauses);
+    cnd->n_clauses = 0;
+}
+
+void ovsdb_condition_init(struct ovsdb_condition *cnd)
+{
+    cnd->clauses = NULL;
+    cnd->n_clauses = 0;
+}
+
+bool ovsdb_condition_empty(const struct ovsdb_condition *cnd)
+{
+    return cnd->n_clauses == 0;
+}
+
+int ovsdb_condition_cmp(const struct ovsdb_condition *a,
+                        const struct ovsdb_condition *b)
+{
+    size_t i;
+    int res;
+
+    if (a->n_clauses != b->n_clauses) {
+        return a->n_clauses - b->n_clauses;
+    }
+
+    /* We assume clauses are sorted */
+    for (i = 0; i < a->n_clauses; i++) {
+        res = (compare_clauses_3way_with_data(&a->clauses[i], &b->clauses[i]));
+        if (res != 0) {
+            return res;
+        }
+    }
+
+    return 0;
+}
+
+void
+ovsdb_condition_clone(struct ovsdb_condition *to,
+                      const struct ovsdb_condition *from)
+{
+    size_t i;
+
+    to->clauses = xzalloc(from->n_clauses * sizeof *to->clauses);
+
+    for (i = 0; i < from->n_clauses; i++) {
+        ovsdb_clause_clone(&to->clauses[i], &from->clauses[i]);
+    }
+    to->n_clauses = from->n_clauses;
+}
+
+bool
+ovsdb_condition_is_true(const struct ovsdb_condition *cond)
+{
+    return (!cond->n_clauses ||
+         (cond->n_clauses >= 1 && (cond->clauses[0].function == OVSDB_F_TRUE)) ||
+         (cond->n_clauses >= 2 && (cond->clauses[1].function == OVSDB_F_TRUE)));
+}
+
+bool
+ovsdb_condition_is_false(const struct ovsdb_condition *cond)
+{
+    return ((cond->n_clauses == 1) &&
+            (cond->clauses[0].function == OVSDB_F_FALSE));
+ }
+
+enum ovsdb_function
+ovsdb_condition_max_function(const struct ovsdb_condition *cond)
+{
+    return cond->n_clauses > 0 ?
+        cond->clauses[cond->n_clauses - 1].function : OVSDB_F_TRUE;
+}
+
+/* Assume a and b are sorted, added and removed intitialized. return added
+ * contains clauses that are in b and not in a and removed contains clauses
+ * that are in a and not in b */
+void
+ovsdb_conditon_diff(const struct ovsdb_condition *a,
+                    const struct ovsdb_condition *b,
+                    struct ovsdb_condition *added,
+                    struct ovsdb_condition *removed)
+{
+    int i,j;
+    size_t added_allocated = 0, removed_allocated = 0;
+    ovs_assert(!added->n_clauses && !removed->n_clauses);
+
+    for (i = j = 0; i < a->n_clauses && j < b->n_clauses;) {
+        int res = compare_clauses_3way_with_data(&a->clauses[i], &b->clauses[j]);
+
+        if (!res) {
+            i++;
+            j++;
+        } else if (res < 0) {
+            ovsdb_clause_add(removed, &a->clauses[i], &removed_allocated);
+            i++;
+        } else {
+            ovsdb_clause_add(added, &b->clauses[j], &added_allocated);
+            j++;
+        }
+    }
+    for (;i < a->n_clauses; i++) {
+        ovsdb_clause_add(removed, &a->clauses[i], &removed_allocated);
+    }
+    for(;j < b->n_clauses; j++) {
+        ovsdb_clause_add(added, &b->clauses[j], &added_allocated);
+    }
 }
diff --git a/ovsdb/condition.h b/ovsdb/condition.h
index 620757f..ebfaf2a 100644
--- a/ovsdb/condition.h
+++ b/ovsdb/condition.h
@@ -27,6 +27,8 @@ struct ovsdb_row;
 /* These list is ordered in ascending order of the fraction of tables row that
  * they are (heuristically) expected to leave in query results. */
 #define OVSDB_FUNCTIONS                         \
+    OVSDB_FUNCTION(OVSDB_F_FALSE, "false")            \
+    OVSDB_FUNCTION(OVSDB_F_TRUE, "true")              \
     OVSDB_FUNCTION(OVSDB_F_EQ, "==")                  \
     OVSDB_FUNCTION(OVSDB_F_INCLUDES, "includes")      \
     OVSDB_FUNCTION(OVSDB_F_LE, "<=")                  \
@@ -50,6 +52,7 @@ const char *ovsdb_function_to_string(enum ovsdb_function);
 struct ovsdb_clause {
     enum ovsdb_function function;
     const struct ovsdb_column *column;
+    unsigned int index;
     struct ovsdb_datum arg;
 };
 
@@ -60,6 +63,8 @@ struct ovsdb_condition {
 
 #define OVSDB_CONDITION_INITIALIZER { NULL, 0 }
 
+void ovsdb_condition_init(struct ovsdb_condition *);
+bool ovsdb_condition_empty(const struct ovsdb_condition *);
 struct ovsdb_error *ovsdb_condition_from_json(
     const struct ovsdb_table_schema *,
     const struct json *, struct ovsdb_symbol_table *,
@@ -68,5 +73,20 @@ struct json *ovsdb_condition_to_json(const struct ovsdb_condition *);
 void ovsdb_condition_destroy(struct ovsdb_condition *);
 bool ovsdb_condition_evaluate(const struct ovsdb_row *,
                               const struct ovsdb_condition *);
+bool ovsdb_condition_evaluate_or_datum(const struct ovsdb_datum *,
+                                       const struct ovsdb_condition *,
+                                       unsigned int index_map[]);
+int ovsdb_condition_cmp(const struct ovsdb_condition *a,
+                        const struct ovsdb_condition *b);
+void ovsdb_condition_clone(struct ovsdb_condition *to,
+                           const struct ovsdb_condition *from);
+bool ovsdb_condition_is_true(const struct ovsdb_condition *cond);
+bool ovsdb_condition_is_false(const struct ovsdb_condition *cond);
+enum ovsdb_function ovsdb_condition_max_function(
+                                  const struct ovsdb_condition *cond);
+void ovsdb_conditon_diff(const struct ovsdb_condition *a,
+                         const struct ovsdb_condition *b,
+                         struct ovsdb_condition *added,
+                         struct ovsdb_condition *removed);
 
 #endif /* ovsdb/condition.h */
diff --git a/tests/ovsdb-condition.at b/tests/ovsdb-condition.at
index ab54b1c..96fa6fd 100644
--- a/tests/ovsdb-condition.at
+++ b/tests/ovsdb-condition.at
@@ -184,6 +184,18 @@ OVSDB_CHECK_POSITIVE([condition sorting],
       ["_uuid", "==", ["uuid", "d50e85c6-8ae7-4b16-b69e-4395928bd9be"]]]']],
   [[[["_uuid","==",["uuid","d50e85c6-8ae7-4b16-b69e-4395928bd9be"]],["i","==",1],["i","includes",2],["i","<=",3],["i","<",4],["i",">=",5],["i",">",6],["i","excludes",7],["i","!=",8]]]])
 
+OVSDB_CHECK_POSITIVE([boolean condition],
+  [[parse-conditions \
+    '{"columns": {"name": {"type": "string"}}}' \
+    '[true]']],
+  [[[true]]])
+
+OVSDB_CHECK_POSITIVE([boolean condition],
+  [[parse-conditions \
+    '{"columns": {"name": {"type": "string"}}}' \
+    '[false]']],
+  [[[false]]])
+
 OVSDB_CHECK_POSITIVE([evaluating null condition],
   [[evaluate-conditions \
     '{"columns": {"i": {"type": "integer"}}}' \
@@ -657,3 +669,21 @@ condition  5: --T-
 condition  6: -T--
 condition  7: T-TT
 condition  8: -T-T], [condition])
+
+OVSDB_CHECK_POSITIVE([evaluating false boolean condition],
+  [[evaluate-conditions-or \
+    '{"columns": {"i": {"type": "integer"}}}' \
+    '[[false,["i","==",1]]]' \
+    '[{"i": 0},
+      {"i": 1},
+      {"i": 2}']]],
+  [condition  0: -T-])
+
+OVSDB_CHECK_POSITIVE([evaluating true boolean condition],
+  [[evaluate-conditions-or \
+    '{"columns": {"i": {"type": "integer"}}}' \
+    '[[true,["i","==",1]]]' \
+    '[{"i": 0},
+      {"i": 1},
+      {"i": 2}']]],
+  [condition  0: TTT])
diff --git a/tests/test-ovsdb.c b/tests/test-ovsdb.c
index 670a141..290ef7a 100644
--- a/tests/test-ovsdb.c
+++ b/tests/test-ovsdb.c
@@ -851,8 +851,11 @@ do_parse_conditions(struct ovs_cmdl_context *ctx)
     exit(exit_code);
 }
 
+#define OVSDB_CONDITION_AND 0
+#define OVSDB_CONDITION_OR 1
+
 static void
-do_evaluate_conditions(struct ovs_cmdl_context *ctx)
+do_evaluate_condition__(struct ovs_cmdl_context *ctx, int mode)
 {
     struct ovsdb_table_schema *ts;
     struct ovsdb_table *table;
@@ -900,7 +903,15 @@ do_evaluate_conditions(struct ovs_cmdl_context *ctx)
     for (i = 0; i < n_conditions; i++) {
         printf("condition %2"PRIuSIZE":", i);
         for (j = 0; j < n_rows; j++) {
-            bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
+            bool result;
+            if (mode == OVSDB_CONDITION_AND) {
+                result = ovsdb_condition_evaluate(rows[j],
+                                                  &conditions[i]);
+            } else {
+                result = ovsdb_condition_evaluate_or_datum(rows[j]->fields,
+                                                           &conditions[i],
+                                                           false);
+            }
             if (j % 5 == 0) {
                 putchar(' ');
             }
@@ -921,6 +932,18 @@ do_evaluate_conditions(struct ovs_cmdl_context *ctx)
 }
 
 static void
+do_evaluate_conditions(struct ovs_cmdl_context *ctx)
+{
+    do_evaluate_condition__(ctx, OVSDB_CONDITION_AND);
+}
+
+static void
+do_evaluate_conditions_or(struct ovs_cmdl_context *ctx)
+{
+    do_evaluate_condition__(ctx, OVSDB_CONDITION_OR);
+}
+
+static void
 do_parse_mutations(struct ovs_cmdl_context *ctx)
 {
     struct ovsdb_table_schema *ts;
@@ -2197,6 +2220,7 @@ static struct ovs_cmdl_command all_commands[] = {
     { "compare-rows", NULL, 2, INT_MAX, do_compare_rows },
     { "parse-conditions", NULL, 2, INT_MAX, do_parse_conditions },
     { "evaluate-conditions", NULL, 3, 3, do_evaluate_conditions },
+    { "evaluate-conditions-or", NULL, 3, 3, do_evaluate_conditions_or },
     { "parse-mutations", NULL, 2, INT_MAX, do_parse_mutations },
     { "execute-mutations", NULL, 3, 3, do_execute_mutations },
     { "query", NULL, 3, 3, do_query },
-- 
2.1.4





More information about the dev mailing list