ua_nodestore_ziptree.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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_default.h>
  9. #include "ziptree.h"
  10. /* container_of */
  11. #define container_of(ptr, type, member) \
  12. (type *)((uintptr_t)ptr - offsetof(type,member))
  13. struct NodeEntry;
  14. typedef struct NodeEntry NodeEntry;
  15. struct NodeEntry {
  16. ZIP_ENTRY(NodeEntry) zipfields;
  17. UA_UInt32 nodeIdHash;
  18. UA_UInt16 refCount; /* How many consumers have a reference to the node? */
  19. UA_Boolean deleted; /* Node was marked as deleted and can be deleted when refCount == 0 */
  20. NodeEntry *orig; /* If a copy is made to replace a node, track that we
  21. * replace only the node from which the copy was made.
  22. * Important for concurrent operations. */
  23. UA_NodeId nodeId; /* This is actually a UA_Node that also starts with a NodeId */
  24. };
  25. /* Absolute ordering for NodeIds */
  26. static enum ZIP_CMP
  27. cmpNodeId(const void *a, const void *b) {
  28. const NodeEntry *aa = (const NodeEntry*)a;
  29. const NodeEntry *bb = (const NodeEntry*)b;
  30. /* Compare hash */
  31. if(aa->nodeIdHash < bb->nodeIdHash)
  32. return ZIP_CMP_LESS;
  33. if(aa->nodeIdHash > bb->nodeIdHash)
  34. return ZIP_CMP_MORE;
  35. /* Compore nodes in detail */
  36. return (enum ZIP_CMP)UA_NodeId_order(&aa->nodeId, &bb->nodeId);
  37. }
  38. ZIP_HEAD(NodeTree, NodeEntry);
  39. typedef struct NodeTree NodeTree;
  40. typedef struct {
  41. NodeTree root;
  42. } ZipContext;
  43. ZIP_PROTTYPE(NodeTree, NodeEntry, NodeEntry)
  44. ZIP_IMPL(NodeTree, NodeEntry, zipfields, NodeEntry, zipfields, cmpNodeId)
  45. static NodeEntry *
  46. newEntry(UA_NodeClass nodeClass) {
  47. size_t size = sizeof(NodeEntry) - sizeof(UA_NodeId);
  48. switch(nodeClass) {
  49. case UA_NODECLASS_OBJECT:
  50. size += sizeof(UA_ObjectNode);
  51. break;
  52. case UA_NODECLASS_VARIABLE:
  53. size += sizeof(UA_VariableNode);
  54. break;
  55. case UA_NODECLASS_METHOD:
  56. size += sizeof(UA_MethodNode);
  57. break;
  58. case UA_NODECLASS_OBJECTTYPE:
  59. size += sizeof(UA_ObjectTypeNode);
  60. break;
  61. case UA_NODECLASS_VARIABLETYPE:
  62. size += sizeof(UA_VariableTypeNode);
  63. break;
  64. case UA_NODECLASS_REFERENCETYPE:
  65. size += sizeof(UA_ReferenceTypeNode);
  66. break;
  67. case UA_NODECLASS_DATATYPE:
  68. size += sizeof(UA_DataTypeNode);
  69. break;
  70. case UA_NODECLASS_VIEW:
  71. size += sizeof(UA_ViewNode);
  72. break;
  73. default:
  74. return NULL;
  75. }
  76. NodeEntry *entry = (NodeEntry*)UA_calloc(1, size);
  77. if(!entry)
  78. return NULL;
  79. UA_Node *node = (UA_Node*)&entry->nodeId;
  80. node->nodeClass = nodeClass;
  81. return entry;
  82. }
  83. static void
  84. deleteEntry(NodeEntry *entry) {
  85. UA_Node_clear((UA_Node*)&entry->nodeId);
  86. UA_free(entry);
  87. }
  88. static void
  89. cleanupEntry(NodeEntry *entry) {
  90. if(entry->deleted && entry->refCount == 0)
  91. deleteEntry(entry);
  92. }
  93. /***********************/
  94. /* Interface functions */
  95. /***********************/
  96. /* Not yet inserted into the ZipContext */
  97. static UA_Node *
  98. zipNsNewNode(void *nsCtx, UA_NodeClass nodeClass) {
  99. NodeEntry *entry = newEntry(nodeClass);
  100. if(!entry)
  101. return NULL;
  102. return (UA_Node*)&entry->nodeId;
  103. }
  104. /* Not yet inserted into the ZipContext */
  105. static void
  106. zipNsDeleteNode(void *nsCtx, UA_Node *node) {
  107. deleteEntry(container_of(node, NodeEntry, nodeId));
  108. }
  109. static const UA_Node *
  110. zipNsGetNode(void *nsCtx, const UA_NodeId *nodeId) {
  111. ZipContext *ns = (ZipContext*)nsCtx;
  112. NodeEntry dummy;
  113. dummy.nodeIdHash = UA_NodeId_hash(nodeId);
  114. dummy.nodeId = *nodeId;
  115. NodeEntry *entry = ZIP_FIND(NodeTree, &ns->root, &dummy);
  116. if(!entry)
  117. return NULL;
  118. ++entry->refCount;
  119. return (const UA_Node*)&entry->nodeId;
  120. }
  121. static void
  122. zipNsReleaseNode(void *nsCtx, const UA_Node *node) {
  123. if(!node)
  124. return;
  125. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  126. UA_assert(entry->refCount > 0);
  127. --entry->refCount;
  128. cleanupEntry(entry);
  129. }
  130. static UA_StatusCode
  131. zipNsGetNodeCopy(void *nsCtx, const UA_NodeId *nodeId,
  132. UA_Node **outNode) {
  133. /* Find the node */
  134. const UA_Node *node = zipNsGetNode(nsCtx, nodeId);
  135. if(!node)
  136. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  137. /* Create the new entry */
  138. NodeEntry *ne = newEntry(node->nodeClass);
  139. if(!ne) {
  140. zipNsReleaseNode(nsCtx, node);
  141. return UA_STATUSCODE_BADOUTOFMEMORY;
  142. }
  143. /* Copy the node content */
  144. UA_Node *nnode = (UA_Node*)&ne->nodeId;
  145. UA_StatusCode retval = UA_Node_copy(node, nnode);
  146. zipNsReleaseNode(nsCtx, node);
  147. if(retval != UA_STATUSCODE_GOOD) {
  148. deleteEntry(ne);
  149. return retval;
  150. }
  151. ne->orig = container_of(node, NodeEntry, nodeId);
  152. *outNode = nnode;
  153. return UA_STATUSCODE_GOOD;
  154. }
  155. static UA_StatusCode
  156. zipNsInsertNode(void *nsCtx, UA_Node *node, UA_NodeId *addedNodeId) {
  157. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  158. ZipContext *ns = (ZipContext*)nsCtx;
  159. /* Ensure that the NodeId is unique */
  160. NodeEntry dummy;
  161. dummy.nodeId = node->nodeId;
  162. if(node->nodeId.identifierType == UA_NODEIDTYPE_NUMERIC &&
  163. node->nodeId.identifier.numeric == 0) {
  164. do { /* Create a random nodeid until we find an unoccupied id */
  165. node->nodeId.identifier.numeric = UA_UInt32_random();
  166. dummy.nodeId.identifier.numeric = node->nodeId.identifier.numeric;
  167. dummy.nodeIdHash = UA_NodeId_hash(&node->nodeId);
  168. } while(ZIP_FIND(NodeTree, &ns->root, &dummy));
  169. } else {
  170. dummy.nodeIdHash = UA_NodeId_hash(&node->nodeId);
  171. if(ZIP_FIND(NodeTree, &ns->root, &dummy)) { /* The nodeid exists */
  172. deleteEntry(entry);
  173. return UA_STATUSCODE_BADNODEIDEXISTS;
  174. }
  175. }
  176. /* Copy the NodeId */
  177. if(addedNodeId) {
  178. UA_StatusCode retval = UA_NodeId_copy(&node->nodeId, addedNodeId);
  179. if(retval != UA_STATUSCODE_GOOD) {
  180. deleteEntry(entry);
  181. return retval;
  182. }
  183. }
  184. /* Insert the node */
  185. entry->nodeIdHash = dummy.nodeIdHash;
  186. ZIP_INSERT(NodeTree, &ns->root, entry, ZIP_FFS32(UA_UInt32_random()));
  187. return UA_STATUSCODE_GOOD;
  188. }
  189. static UA_StatusCode
  190. zipNsReplaceNode(void *nsCtx, UA_Node *node) {
  191. /* Find the node */
  192. const UA_Node *oldNode = zipNsGetNode(nsCtx, &node->nodeId);
  193. if(!oldNode) {
  194. deleteEntry(container_of(node, NodeEntry, nodeId));
  195. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  196. }
  197. /* Test if the copy is current */
  198. NodeEntry *entry = container_of(node, NodeEntry, nodeId);
  199. NodeEntry *oldEntry = container_of(oldNode, NodeEntry, nodeId);
  200. if(oldEntry != entry->orig) {
  201. /* The node was already updated since the copy was made */
  202. deleteEntry(entry);
  203. zipNsReleaseNode(nsCtx, oldNode);
  204. return UA_STATUSCODE_BADINTERNALERROR;
  205. }
  206. /* Replace */
  207. ZipContext *ns = (ZipContext*)nsCtx;
  208. ZIP_REMOVE(NodeTree, &ns->root, oldEntry);
  209. entry->nodeIdHash = oldEntry->nodeIdHash;
  210. ZIP_INSERT(NodeTree, &ns->root, entry, ZIP_RANK(entry, zipfields));
  211. oldEntry->deleted = true;
  212. zipNsReleaseNode(nsCtx, oldNode);
  213. return UA_STATUSCODE_GOOD;
  214. }
  215. static UA_StatusCode
  216. zipNsRemoveNode(void *nsCtx, const UA_NodeId *nodeId) {
  217. ZipContext *ns = (ZipContext*)nsCtx;
  218. NodeEntry dummy;
  219. dummy.nodeIdHash = UA_NodeId_hash(nodeId);
  220. dummy.nodeId = *nodeId;
  221. NodeEntry *entry = ZIP_FIND(NodeTree, &ns->root, &dummy);
  222. if(!entry)
  223. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  224. ZIP_REMOVE(NodeTree, &ns->root, entry);
  225. entry->deleted = true;
  226. cleanupEntry(entry);
  227. return UA_STATUSCODE_GOOD;
  228. }
  229. struct VisitorData {
  230. UA_NodestoreVisitor visitor;
  231. void *visitorContext;
  232. };
  233. static void
  234. nodeVisitor(NodeEntry *entry, void *data) {
  235. struct VisitorData *d = (struct VisitorData*)data;
  236. d->visitor(d->visitorContext, (UA_Node*)&entry->nodeId);
  237. }
  238. static void
  239. zipNsIterate(void *nsCtx, UA_NodestoreVisitor visitor,
  240. void *visitorCtx) {
  241. struct VisitorData d;
  242. d.visitor = visitor;
  243. d.visitorContext = visitorCtx;
  244. ZipContext *ns = (ZipContext*)nsCtx;
  245. ZIP_ITER(NodeTree, &ns->root, nodeVisitor, &d);
  246. }
  247. static void
  248. deleteNodeVisitor(NodeEntry *entry, void *data) {
  249. deleteEntry(entry);
  250. }
  251. /***********************/
  252. /* Nodestore Lifecycle */
  253. /***********************/
  254. static void
  255. zipNsClear(void *nsCtx) {
  256. if (!nsCtx)
  257. return;
  258. ZipContext *ns = (ZipContext*)nsCtx;
  259. ZIP_ITER(NodeTree, &ns->root, deleteNodeVisitor, NULL);
  260. UA_free(ns);
  261. }
  262. UA_StatusCode
  263. UA_Nodestore_ZipTree(UA_Nodestore *ns) {
  264. /* Allocate and initialize the context */
  265. ZipContext *ctx = (ZipContext*)UA_malloc(sizeof(ZipContext));
  266. if(!ctx)
  267. return UA_STATUSCODE_BADOUTOFMEMORY;
  268. ZIP_INIT(&ctx->root);
  269. /* Populate the nodestore */
  270. ns->context = (void*)ctx;
  271. ns->clear = zipNsClear;
  272. ns->newNode = zipNsNewNode;
  273. ns->deleteNode = zipNsDeleteNode;
  274. ns->getNode = zipNsGetNode;
  275. ns->releaseNode = zipNsReleaseNode;
  276. ns->getNodeCopy = zipNsGetNodeCopy;
  277. ns->insertNode = zipNsInsertNode;
  278. ns->replaceNode = zipNsReplaceNode;
  279. ns->removeNode = zipNsRemoveNode;
  280. ns->iterate = zipNsIterate;
  281. return UA_STATUSCODE_GOOD;
  282. }