ua_nodestore_default.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  3. *
  4. * Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  5. * Copyright 2017 (c) Julian Grothoff
  6. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  7. */
  8. #include <open62541/plugin/nodestore.h>
  9. #include "ziptree.h"
  10. #ifndef UA_ENABLE_CUSTOM_NODESTORE
  11. #ifdef UA_ENABLE_MULTITHREADING
  12. #include <pthread.h>
  13. #define BEGIN_CRITSECT(NODEMAP) pthread_mutex_lock(&(NODEMAP)->mutex)
  14. #define END_CRITSECT(NODEMAP) pthread_mutex_unlock(&(NODEMAP)->mutex)
  15. #else
  16. #define BEGIN_CRITSECT(NODEMAP) do {} while(0)
  17. #define END_CRITSECT(NODEMAP) do {} while(0)
  18. #endif
  19. /* container_of */
  20. #define container_of(ptr, type, member) \
  21. (type *)((uintptr_t)ptr - offsetof(type,member))
  22. struct NodeEntry;
  23. typedef struct NodeEntry NodeEntry;
  24. struct NodeEntry {
  25. ZIP_ENTRY(NodeEntry) zipfields;
  26. UA_UInt32 nodeIdHash;
  27. UA_UInt16 refCount; /* How many consumers have a reference to the node? */
  28. UA_Boolean deleted; /* Node was marked as deleted and can be deleted when refCount == 0 */
  29. NodeEntry *orig; /* If a copy is made to replace a node, track that we
  30. * replace only the node from which the copy was made.
  31. * Important for concurrent operations. */
  32. UA_NodeId nodeId; /* This is actually a UA_Node that also starts with a NodeId */
  33. };
  34. /* Absolute ordering for NodeIds */
  35. static enum ZIP_CMP
  36. cmpNodeId(const void *a, const void *b) {
  37. const NodeEntry *aa = (const NodeEntry*)a;
  38. const NodeEntry *bb = (const NodeEntry*)b;
  39. /* Compare hash */
  40. if(aa->nodeIdHash < bb->nodeIdHash)
  41. return ZIP_CMP_LESS;
  42. if(aa->nodeIdHash > bb->nodeIdHash)
  43. return ZIP_CMP_MORE;
  44. if(UA_NodeId_equal(&aa->nodeId, &bb->nodeId))
  45. return ZIP_CMP_EQ;
  46. /* Compare namespaceIndex */
  47. if(aa->nodeId.namespaceIndex < bb->nodeId.namespaceIndex)
  48. return ZIP_CMP_LESS;
  49. if(aa->nodeId.namespaceIndex > bb->nodeId.namespaceIndex)
  50. return ZIP_CMP_MORE;
  51. /* Compare identifierType */
  52. if(aa->nodeId.identifierType < bb->nodeId.identifierType)
  53. return ZIP_CMP_LESS;
  54. if(aa->nodeId.identifierType > bb->nodeId.identifierType)
  55. return ZIP_CMP_MORE;
  56. /* Compare the identifier */
  57. switch(aa->nodeId.identifierType) {
  58. case UA_NODEIDTYPE_NUMERIC:
  59. if(aa->nodeId.identifier.numeric < bb->nodeId.identifier.numeric)
  60. return ZIP_CMP_LESS;
  61. if(aa->nodeId.identifier.numeric > bb->nodeId.identifier.numeric)
  62. return ZIP_CMP_MORE;
  63. break;
  64. case UA_NODEIDTYPE_GUID:
  65. if(aa->nodeId.identifier.guid.data1 < bb->nodeId.identifier.guid.data1 ||
  66. aa->nodeId.identifier.guid.data2 < bb->nodeId.identifier.guid.data2 ||
  67. aa->nodeId.identifier.guid.data3 < bb->nodeId.identifier.guid.data3 ||
  68. strncmp((const char*)aa->nodeId.identifier.guid.data4,
  69. (const char*)bb->nodeId.identifier.guid.data4, 8) < 0)
  70. return ZIP_CMP_LESS;
  71. if(aa->nodeId.identifier.guid.data1 > bb->nodeId.identifier.guid.data1 ||
  72. aa->nodeId.identifier.guid.data2 > bb->nodeId.identifier.guid.data2 ||
  73. aa->nodeId.identifier.guid.data3 > bb->nodeId.identifier.guid.data3 ||
  74. strncmp((const char*)aa->nodeId.identifier.guid.data4,
  75. (const char*)bb->nodeId.identifier.guid.data4, 8) > 0)
  76. return ZIP_CMP_MORE;
  77. break;
  78. case UA_NODEIDTYPE_STRING:
  79. case UA_NODEIDTYPE_BYTESTRING: {
  80. if(aa->nodeId.identifier.string.length < bb->nodeId.identifier.string.length)
  81. return ZIP_CMP_LESS;
  82. if(aa->nodeId.identifier.string.length > bb->nodeId.identifier.string.length)
  83. return ZIP_CMP_MORE;
  84. int cmp = strncmp((const char*)aa->nodeId.identifier.string.data,
  85. (const char*)bb->nodeId.identifier.string.data,
  86. aa->nodeId.identifier.string.length);
  87. if(cmp < 0)
  88. return ZIP_CMP_LESS;
  89. if(cmp > 0)
  90. return ZIP_CMP_MORE;
  91. break;
  92. }
  93. default:
  94. break;
  95. }
  96. return ZIP_CMP_EQ;
  97. }
  98. ZIP_HEAD(NodeTree, NodeEntry);
  99. typedef struct NodeTree NodeTree;
  100. typedef struct {
  101. NodeTree root;
  102. #ifdef UA_ENABLE_MULTITHREADING
  103. pthread_mutex_t mutex; /* Protect access */
  104. #endif
  105. } NodeMap;
  106. ZIP_PROTTYPE(NodeTree, NodeEntry, NodeEntry)
  107. ZIP_IMPL(NodeTree, NodeEntry, zipfields, NodeEntry, zipfields, cmpNodeId)
  108. static NodeEntry *
  109. newEntry(UA_NodeClass nodeClass) {
  110. size_t size = sizeof(NodeEntry) - sizeof(UA_NodeId);
  111. switch(nodeClass) {
  112. case UA_NODECLASS_OBJECT:
  113. size += sizeof(UA_ObjectNode);
  114. break;
  115. case UA_NODECLASS_VARIABLE:
  116. size += sizeof(UA_VariableNode);
  117. break;
  118. case UA_NODECLASS_METHOD:
  119. size += sizeof(UA_MethodNode);
  120. break;
  121. case UA_NODECLASS_OBJECTTYPE:
  122. size += sizeof(UA_ObjectTypeNode);
  123. break;
  124. case UA_NODECLASS_VARIABLETYPE:
  125. size += sizeof(UA_VariableTypeNode);
  126. break;
  127. case UA_NODECLASS_REFERENCETYPE:
  128. size += sizeof(UA_ReferenceTypeNode);
  129. break;
  130. case UA_NODECLASS_DATATYPE:
  131. size += sizeof(UA_DataTypeNode);
  132. break;
  133. case UA_NODECLASS_VIEW:
  134. size += sizeof(UA_ViewNode);
  135. break;
  136. default:
  137. return NULL;
  138. }
  139. NodeEntry *entry = (NodeEntry*)UA_calloc(1, size);
  140. if(!entry)
  141. return NULL;
  142. UA_Node *node = (UA_Node*)&entry->nodeId;
  143. node->nodeClass = nodeClass;
  144. return entry;
  145. }
  146. static void
  147. deleteEntry(NodeEntry *entry) {
  148. UA_Node_deleteMembers((UA_Node*)&entry->nodeId);
  149. UA_free(entry);
  150. }
  151. static void
  152. cleanupEntry(NodeEntry *entry) {
  153. if(entry->deleted && entry->refCount == 0)
  154. deleteEntry(entry);
  155. }
  156. /***********************/
  157. /* Interface functions */
  158. /***********************/
  159. /* Not yet inserted into the NodeMap */
  160. UA_Node *
  161. UA_Nodestore_newNode(void *nsCtx, UA_NodeClass nodeClass) {
  162. NodeEntry *entry = newEntry(nodeClass);
  163. if(!entry)
  164. return NULL;
  165. return (UA_Node*)&entry->nodeId;
  166. }
  167. /* Not yet inserted into the NodeMap */
  168. void
  169. UA_Nodestore_deleteNode(void *nsCtx, UA_Node *node) {
  170. deleteEntry(container_of(node, NodeEntry, nodeId));
  171. }
  172. const UA_Node *
  173. UA_Nodestore_getNode(void *nsCtx, const UA_NodeId *nodeId) {
  174. NodeMap *ns = (NodeMap*)nsCtx;
  175. BEGIN_CRITSECT(ns);
  176. NodeEntry dummy;
  177. dummy.nodeIdHash = UA_NodeId_hash(nodeId);
  178. dummy.nodeId = *nodeId;
  179. NodeEntry *entry = ZIP_FIND(NodeTree, &ns->root, &dummy);
  180. if(!entry) {
  181. END_CRITSECT(ns);
  182. return NULL;
  183. }
  184. ++entry->refCount;
  185. END_CRITSECT(ns);
  186. return (const UA_Node*)&entry->nodeId;
  187. }
  188. void
  189. UA_Nodestore_releaseNode(void *nsCtx, const UA_Node *node) {
  190. if(!node)
  191. return;
  192. #ifdef UA_ENABLE_MULTITHREADING
  193. NodeMap *ns = (NodeMap*)nsCtx;
  194. #endif
  195. BEGIN_CRITSECT(ns);
  196. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  197. UA_assert(entry->refCount > 0);
  198. --entry->refCount;
  199. cleanupEntry(entry);
  200. END_CRITSECT(ns);
  201. }
  202. UA_StatusCode
  203. UA_Nodestore_getNodeCopy(void *nsCtx, const UA_NodeId *nodeId,
  204. UA_Node **outNode) {
  205. /* Find the node */
  206. const UA_Node *node = UA_Nodestore_getNode(nsCtx, nodeId);
  207. if(!node)
  208. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  209. /* Create the new entry */
  210. NodeEntry *ne = newEntry(node->nodeClass);
  211. if(!ne) {
  212. UA_Nodestore_releaseNode(nsCtx, node);
  213. return UA_STATUSCODE_BADOUTOFMEMORY;
  214. }
  215. /* Copy the node content */
  216. UA_Node *nnode = (UA_Node*)&ne->nodeId;
  217. UA_StatusCode retval = UA_Node_copy(node, nnode);
  218. UA_Nodestore_releaseNode(nsCtx, node);
  219. if(retval != UA_STATUSCODE_GOOD) {
  220. deleteEntry(ne);
  221. return retval;
  222. }
  223. ne->orig = container_of(node, NodeEntry, nodeId);
  224. *outNode = nnode;
  225. return UA_STATUSCODE_GOOD;
  226. }
  227. UA_StatusCode
  228. UA_Nodestore_insertNode(void *nsCtx, UA_Node *node, UA_NodeId *addedNodeId) {
  229. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  230. NodeMap *ns = (NodeMap*)nsCtx;
  231. BEGIN_CRITSECT(ns);
  232. /* Ensure that the NodeId is unique */
  233. NodeEntry dummy;
  234. dummy.nodeId = node->nodeId;
  235. if(node->nodeId.identifierType == UA_NODEIDTYPE_NUMERIC &&
  236. node->nodeId.identifier.numeric == 0) {
  237. do { /* Create a random nodeid until we find an unoccupied id */
  238. node->nodeId.identifier.numeric = UA_UInt32_random();
  239. dummy.nodeId.identifier.numeric = node->nodeId.identifier.numeric;
  240. dummy.nodeIdHash = UA_NodeId_hash(&node->nodeId);
  241. } while(ZIP_FIND(NodeTree, &ns->root, &dummy));
  242. } else {
  243. dummy.nodeIdHash = UA_NodeId_hash(&node->nodeId);
  244. if(ZIP_FIND(NodeTree, &ns->root, &dummy)) { /* The nodeid exists */
  245. deleteEntry(entry);
  246. END_CRITSECT(ns);
  247. return UA_STATUSCODE_BADNODEIDEXISTS;
  248. }
  249. }
  250. /* Copy the NodeId */
  251. if(addedNodeId) {
  252. UA_StatusCode retval = UA_NodeId_copy(&node->nodeId, addedNodeId);
  253. if(retval != UA_STATUSCODE_GOOD) {
  254. deleteEntry(entry);
  255. END_CRITSECT(ns);
  256. return retval;
  257. }
  258. }
  259. /* Insert the node */
  260. entry->nodeIdHash = dummy.nodeIdHash;
  261. ZIP_INSERT(NodeTree, &ns->root, entry, ZIP_FFS32(UA_UInt32_random()));
  262. END_CRITSECT(ns);
  263. return UA_STATUSCODE_GOOD;
  264. }
  265. UA_StatusCode
  266. UA_Nodestore_replaceNode(void *nsCtx, UA_Node *node) {
  267. /* Find the node */
  268. const UA_Node *oldNode = UA_Nodestore_getNode(nsCtx, &node->nodeId);
  269. if(!oldNode)
  270. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  271. /* Test if the copy is current */
  272. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  273. NodeEntry *oldEntry = container_of(oldNode, NodeEntry, nodeId);
  274. if(oldEntry != entry->orig) {
  275. /* The node was already updated since the copy was made */
  276. deleteEntry(entry);
  277. UA_Nodestore_releaseNode(nsCtx, oldNode);
  278. return UA_STATUSCODE_BADINTERNALERROR;
  279. }
  280. /* Replace */
  281. NodeMap *ns = (NodeMap*)nsCtx;
  282. BEGIN_CRITSECT(ns);
  283. ZIP_REMOVE(NodeTree, &ns->root, oldEntry);
  284. entry->nodeIdHash = oldEntry->nodeIdHash;
  285. ZIP_INSERT(NodeTree, &ns->root, entry, ZIP_RANK(entry, zipfields));
  286. oldEntry->deleted = true;
  287. END_CRITSECT(ns);
  288. UA_Nodestore_releaseNode(nsCtx, oldNode);
  289. return UA_STATUSCODE_GOOD;
  290. }
  291. UA_StatusCode
  292. UA_Nodestore_removeNode(void *nsCtx, const UA_NodeId *nodeId) {
  293. NodeMap *ns = (NodeMap*)nsCtx;
  294. BEGIN_CRITSECT(ns);
  295. NodeEntry dummy;
  296. dummy.nodeIdHash = UA_NodeId_hash(nodeId);
  297. dummy.nodeId = *nodeId;
  298. NodeEntry *entry = ZIP_FIND(NodeTree, &ns->root, &dummy);
  299. if(!entry) {
  300. END_CRITSECT(ns);
  301. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  302. }
  303. ZIP_REMOVE(NodeTree, &ns->root, entry);
  304. entry->deleted = true;
  305. cleanupEntry(entry);
  306. END_CRITSECT(ns);
  307. return UA_STATUSCODE_GOOD;
  308. }
  309. struct VisitorData {
  310. UA_NodestoreVisitor visitor;
  311. void *visitorContext;
  312. };
  313. static void
  314. nodeVisitor(NodeEntry *entry, void *data) {
  315. struct VisitorData *d = (struct VisitorData*)data;
  316. d->visitor(d->visitorContext, (UA_Node*)&entry->nodeId);
  317. }
  318. void
  319. UA_Nodestore_iterate(void *nsCtx, UA_NodestoreVisitor visitor,
  320. void *visitorCtx) {
  321. struct VisitorData d;
  322. d.visitor = visitor;
  323. d.visitorContext = visitorCtx;
  324. NodeMap *ns = (NodeMap*)nsCtx;
  325. BEGIN_CRITSECT(ns);
  326. ZIP_ITER(NodeTree, &ns->root, nodeVisitor, &d);
  327. END_CRITSECT(ns);
  328. }
  329. static void
  330. deleteNodeVisitor(NodeEntry *entry, void *data) {
  331. deleteEntry(entry);
  332. }
  333. /***********************/
  334. /* Nodestore Lifecycle */
  335. /***********************/
  336. const UA_Boolean inPlaceEditAllowed = true;
  337. UA_StatusCode
  338. UA_Nodestore_new(void **nsCtx) {
  339. /* Allocate and initialize the nodemap */
  340. NodeMap *nodemap = (NodeMap*)UA_malloc(sizeof(NodeMap));
  341. if(!nodemap)
  342. return UA_STATUSCODE_BADOUTOFMEMORY;
  343. #ifdef UA_ENABLE_MULTITHREADING
  344. pthread_mutex_init(&nodemap->mutex, NULL);
  345. #endif
  346. ZIP_INIT(&nodemap->root);
  347. /* Populate the nodestore */
  348. *nsCtx = (void*)nodemap;
  349. return UA_STATUSCODE_GOOD;
  350. }
  351. void
  352. UA_Nodestore_delete(void *nsCtx) {
  353. if (!nsCtx)
  354. return;
  355. NodeMap *ns = (NodeMap*)nsCtx;
  356. #ifdef UA_ENABLE_MULTITHREADING
  357. pthread_mutex_destroy(&ns->mutex);
  358. #endif
  359. ZIP_ITER(NodeTree, &ns->root, deleteNodeVisitor, NULL);
  360. UA_free(ns);
  361. }
  362. #endif /* UA_ENABLE_CUSTOM_NODESTORE */