diff mbox series

[ovs-dev,1/2] ovsdb-types: Add functions to compare types for equality.

Message ID 20230103174736.1390853-2-i.maximets@ovn.org
State Accepted
Commit e0e4266a90a28c2313cde6951d32d70680af327d
Headers show
Series ovsdb: Don't convert unchanged columns. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Ilya Maximets Jan. 3, 2023, 5:47 p.m. UTC
Will be used in the next commit to optimize database conversion.

Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
---
 lib/ovsdb-types.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/ovsdb-types.h |  3 +++
 2 files changed, 64 insertions(+)
diff mbox series

Patch

diff --git a/lib/ovsdb-types.c b/lib/ovsdb-types.c
index 61efe59cf..197cee1c6 100644
--- a/lib/ovsdb-types.c
+++ b/lib/ovsdb-types.c
@@ -275,6 +275,58 @@  ovsdb_base_type_is_valid(const struct ovsdb_base_type *base)
     }
 }
 
+bool
+ovsdb_base_type_equals(const struct ovsdb_base_type *a,
+                       const struct ovsdb_base_type *b)
+{
+    if (a == b) {
+        return true;
+    }
+
+    if (a->type != b->type) {
+        return false;
+    }
+
+    if ((a->enum_ && !b->enum_) || (!a->enum_ && b->enum_)) {
+        return false;
+    } else if (a->enum_ &&
+               !ovsdb_datum_equals(a->enum_, b->enum_,
+                                   ovsdb_base_type_get_enum_type(a->type))) {
+        return false;
+    }
+
+    switch (a->type) {
+    case OVSDB_TYPE_VOID:
+        return true;
+
+    case OVSDB_TYPE_INTEGER:
+        return a->integer.min == b->integer.min
+               && a->integer.max == b->integer.max;
+
+    case OVSDB_TYPE_REAL:
+        return a->real.min == b->real.min && a->real.max == b->real.max;
+
+    case OVSDB_TYPE_BOOLEAN:
+        return true;
+
+    case OVSDB_TYPE_STRING:
+        return a->string.minLen == b->string.minLen
+               && a->string.maxLen == b->string.maxLen;
+
+    case OVSDB_TYPE_UUID:
+        /* Not comparing the table pointer here, only the table name, as this
+         * function can be used to compare types from different databases, so
+         * pointers will be different. */
+        return a->uuid.refType == b->uuid.refType
+               && nullable_string_is_equal(a->uuid.refTableName,
+                                           b->uuid.refTableName);
+
+    case OVSDB_N_TYPES:
+    default:
+        OVS_NOT_REACHED();
+    }
+}
+
 bool
 ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
 {
@@ -568,6 +620,15 @@  ovsdb_type_is_valid(const struct ovsdb_type *type)
             && type->n_max >= 1);
 }
 
+bool
+ovsdb_type_equals(const struct ovsdb_type *a, const struct ovsdb_type *b)
+{
+    return ovsdb_base_type_equals(&a->key, &b->key)
+           && ovsdb_base_type_equals(&a->value, &b->value)
+           && a->n_min == b->n_min
+           && a->n_max == b->n_max;
+}
+
 static struct ovsdb_error *
 n_from_json(const struct json *json, unsigned int *n)
 {
diff --git a/lib/ovsdb-types.h b/lib/ovsdb-types.h
index b9eb0928d..9777efea3 100644
--- a/lib/ovsdb-types.h
+++ b/lib/ovsdb-types.h
@@ -107,6 +107,8 @@  void ovsdb_base_type_clone(struct ovsdb_base_type *,
 void ovsdb_base_type_destroy(struct ovsdb_base_type *);
 
 bool ovsdb_base_type_is_valid(const struct ovsdb_base_type *);
+bool ovsdb_base_type_equals(const struct ovsdb_base_type *,
+                            const struct ovsdb_base_type *);
 bool ovsdb_base_type_has_constraints(const struct ovsdb_base_type *);
 void ovsdb_base_type_clear_constraints(struct ovsdb_base_type *);
 const struct ovsdb_type *ovsdb_base_type_get_enum_type(enum ovsdb_atomic_type);
@@ -157,6 +159,7 @@  void ovsdb_type_clone(struct ovsdb_type *, const struct ovsdb_type *);
 void ovsdb_type_destroy(struct ovsdb_type *);
 
 bool ovsdb_type_is_valid(const struct ovsdb_type *);
+bool ovsdb_type_equals(const struct ovsdb_type *, const struct ovsdb_type *);
 
 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *);
 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *);