Browse Source

Types: Pull UA_NodeId_order out from the Nodestore plugin

The method will be used in several places from now on.
Julius Pfrommer 5 years ago
parent
commit
4a372387b5
4 changed files with 79 additions and 80 deletions
  1. 15 5
      include/open62541/constants.h
  2. 6 1
      include/open62541/types.h
  3. 3 57
      plugins/ua_nodestore_default.c
  4. 55 17
      src/ua_types.c

+ 15 - 5
include/open62541/constants.h

@@ -112,11 +112,8 @@ typedef enum {
 #define UA_VALUERANK_THREE_DIMENSIONS          3
 
 /**
- * General Configuration Constants
- * ===============================
- *
- * This section defines constants that are used for the configuration of both
- * clients and servers.
+ * Internal Constants
+ * ==================
  *
  * Rule Handling
  * -------------
@@ -133,6 +130,19 @@ typedef enum {
     UA_RULEHANDLING_ACCEPT, /* Continue and disregard the broken rule */
 } UA_RuleHandling;
 
+/**
+ * Order
+ * -----
+ *
+ * The Order enum is used to establish an absolute ordering between elements.
+ */
+
+typedef enum {
+    UA_ORDER_LESS = -1,
+    UA_ORDER_EQ = 0,
+    UA_ORDER_MORE = 1
+} UA_Order;
+
 _UA_END_DECLS
 
 #endif /* UA_CONSTANTS_H_ */

+ 6 - 1
include/open62541/types.h

@@ -325,7 +325,12 @@ UA_EXPORT extern const UA_NodeId UA_NODEID_NULL;
 
 UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
 
-UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
+UA_Order UA_EXPORT UA_NodeId_order(const UA_NodeId *n1, const UA_NodeId *n2);
+
+static UA_INLINE UA_Boolean
+UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2) {
+    return (UA_NodeId_order(n1, n2) == UA_ORDER_EQ);
+}
 
 /* Returns a non-cryptographic hash for the NodeId */
 UA_UInt32 UA_EXPORT UA_NodeId_hash(const UA_NodeId *n);

+ 3 - 57
plugins/ua_nodestore_default.c

@@ -43,69 +43,15 @@ static enum ZIP_CMP
 cmpNodeId(const void *a, const void *b) {
     const NodeEntry *aa = (const NodeEntry*)a;
     const NodeEntry *bb = (const NodeEntry*)b;
+
     /* Compare hash */
     if(aa->nodeIdHash < bb->nodeIdHash)
         return ZIP_CMP_LESS;
     if(aa->nodeIdHash > bb->nodeIdHash)
         return ZIP_CMP_MORE;
 
-    if(UA_NodeId_equal(&aa->nodeId, &bb->nodeId))
-        return ZIP_CMP_EQ;
-
-    /* Compare namespaceIndex */
-    if(aa->nodeId.namespaceIndex < bb->nodeId.namespaceIndex)
-        return ZIP_CMP_LESS;
-    if(aa->nodeId.namespaceIndex > bb->nodeId.namespaceIndex)
-        return ZIP_CMP_MORE;
-
-    /* Compare identifierType */
-    if(aa->nodeId.identifierType < bb->nodeId.identifierType)
-        return ZIP_CMP_LESS;
-    if(aa->nodeId.identifierType > bb->nodeId.identifierType)
-        return ZIP_CMP_MORE;
-
-    /* Compare the identifier */
-    switch(aa->nodeId.identifierType) {
-    case UA_NODEIDTYPE_NUMERIC:
-        if(aa->nodeId.identifier.numeric < bb->nodeId.identifier.numeric)
-            return ZIP_CMP_LESS;
-        if(aa->nodeId.identifier.numeric > bb->nodeId.identifier.numeric)
-            return ZIP_CMP_MORE;
-        break;
-    case UA_NODEIDTYPE_GUID:
-        if(aa->nodeId.identifier.guid.data1 < bb->nodeId.identifier.guid.data1 ||
-           aa->nodeId.identifier.guid.data2 < bb->nodeId.identifier.guid.data2 ||
-           aa->nodeId.identifier.guid.data3 < bb->nodeId.identifier.guid.data3 ||
-           strncmp((const char*)aa->nodeId.identifier.guid.data4,
-                   (const char*)bb->nodeId.identifier.guid.data4, 8) < 0)
-            return ZIP_CMP_LESS;
-        if(aa->nodeId.identifier.guid.data1 > bb->nodeId.identifier.guid.data1 ||
-           aa->nodeId.identifier.guid.data2 > bb->nodeId.identifier.guid.data2 ||
-           aa->nodeId.identifier.guid.data3 > bb->nodeId.identifier.guid.data3 ||
-           strncmp((const char*)aa->nodeId.identifier.guid.data4,
-                   (const char*)bb->nodeId.identifier.guid.data4, 8) > 0)
-            return ZIP_CMP_MORE;
-        break;
-    case UA_NODEIDTYPE_STRING:
-    case UA_NODEIDTYPE_BYTESTRING: {
-        if(aa->nodeId.identifier.string.length < bb->nodeId.identifier.string.length)
-            return ZIP_CMP_LESS;
-        if(aa->nodeId.identifier.string.length > bb->nodeId.identifier.string.length)
-            return ZIP_CMP_MORE;
-        int cmp = strncmp((const char*)aa->nodeId.identifier.string.data,
-                          (const char*)bb->nodeId.identifier.string.data,
-                          aa->nodeId.identifier.string.length);
-        if(cmp < 0)
-            return ZIP_CMP_LESS;
-        if(cmp > 0)
-            return ZIP_CMP_MORE;
-        break;
-    }
-    default:
-        break;
-    }
-
-    return ZIP_CMP_EQ;
+    /* Compore nodes in detail */
+    return (enum ZIP_CMP)UA_NodeId_order(&aa->nodeId, &bb->nodeId);
 }
 
 ZIP_HEAD(NodeTree, NodeEntry);

+ 55 - 17
src/ua_types.c

@@ -285,27 +285,65 @@ UA_NodeId_isNull(const UA_NodeId *p) {
     return false;
 }
 
-UA_Boolean
-UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2) {
-    if(n1 == NULL || n2 == NULL)
-        return false;
-    if(n1->namespaceIndex != n2->namespaceIndex ||
-       n1->identifierType!=n2->identifierType)
-        return false;
+/* Absolute ordering for NodeIds */
+UA_Order
+UA_NodeId_order(const UA_NodeId *n1, const UA_NodeId *n2) {
+    /* Compare namespaceIndex */
+    if(n1->namespaceIndex < n2->namespaceIndex)
+        return UA_ORDER_LESS;
+    if(n1->namespaceIndex > n2->namespaceIndex)
+        return UA_ORDER_MORE;
+
+    /* Compare identifierType */
+    if(n1->identifierType < n2->identifierType)
+        return UA_ORDER_LESS;
+    if(n1->identifierType > n2->identifierType)
+        return UA_ORDER_MORE;
+
+    /* Compare the identifier */
     switch(n1->identifierType) {
     case UA_NODEIDTYPE_NUMERIC:
-        return (n1->identifier.numeric == n2->identifier.numeric);
-    case UA_NODEIDTYPE_STRING:
-        return UA_String_equal(&n1->identifier.string,
-                               &n2->identifier.string);
+        if(n1->identifier.numeric < n2->identifier.numeric)
+            return UA_ORDER_LESS;
+        if(n1->identifier.numeric > n2->identifier.numeric)
+            return UA_ORDER_MORE;
+        break;
     case UA_NODEIDTYPE_GUID:
-        return UA_Guid_equal(&n1->identifier.guid,
-                             &n2->identifier.guid);
-    case UA_NODEIDTYPE_BYTESTRING:
-        return UA_ByteString_equal(&n1->identifier.byteString,
-                                   &n2->identifier.byteString);
+        if(n1->identifier.guid.data1 < n2->identifier.guid.data1 ||
+           n1->identifier.guid.data2 < n2->identifier.guid.data2 ||
+           n1->identifier.guid.data3 < n2->identifier.guid.data3 ||
+           strncmp((const char*)n1->identifier.guid.data4,
+                   (const char*)n2->identifier.guid.data4, 8) < 0)
+            return UA_ORDER_LESS;
+        if(n1->identifier.guid.data1 > n2->identifier.guid.data1 ||
+           n1->identifier.guid.data2 > n2->identifier.guid.data2 ||
+           n1->identifier.guid.data3 > n2->identifier.guid.data3 ||
+           strncmp((const char*)n1->identifier.guid.data4,
+                   (const char*)n2->identifier.guid.data4, 8) > 0)
+            return UA_ORDER_MORE;
+        break;
+    case UA_NODEIDTYPE_STRING:
+    case UA_NODEIDTYPE_BYTESTRING: {
+        if(n1->identifier.string.length < n2->identifier.string.length)
+            return UA_ORDER_LESS;
+        if(n1->identifier.string.length > n2->identifier.string.length)
+            return UA_ORDER_MORE;
+        if(n1->identifier.string.length > 0) {
+            int cmp = strncmp((const char*)n1->identifier.string.data,
+                              (const char*)n2->identifier.string.data,
+                              n1->identifier.string.length);
+            if(cmp < 0)
+                return UA_ORDER_LESS;
+            if(cmp > 0)
+                return UA_ORDER_MORE;
+        }
+        break;
     }
-    return false;
+    default:
+        break;
+    }
+
+    return UA_ORDER_EQ;
 }
 
 UA_Boolean