ua_nodestore.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "ua_nodestore.h"
  5. #include "ua_server_internal.h"
  6. #include "ua_util.h"
  7. #ifndef UA_ENABLE_MULTITHREADING /* conditional compilation */
  8. #define UA_NODESTORE_MINSIZE 64
  9. typedef struct UA_NodeStoreEntry {
  10. struct UA_NodeStoreEntry *orig; // the version this is a copy from (or NULL)
  11. UA_Node node;
  12. } UA_NodeStoreEntry;
  13. #define UA_NODESTORE_TOMBSTONE ((UA_NodeStoreEntry*)0x01)
  14. struct UA_NodeStore {
  15. UA_NodeStoreEntry **entries;
  16. UA_UInt32 size;
  17. UA_UInt32 count;
  18. UA_UInt32 sizePrimeIndex;
  19. };
  20. /* The size of the hash-map is always a prime number. They are chosen to be
  21. * close to the next power of 2. So the size ca. doubles with each prime. */
  22. static UA_UInt32 const primes[] = {
  23. 7, 13, 31, 61, 127, 251,
  24. 509, 1021, 2039, 4093, 8191, 16381,
  25. 32749, 65521, 131071, 262139, 524287, 1048573,
  26. 2097143, 4194301, 8388593, 16777213, 33554393, 67108859,
  27. 134217689, 268435399, 536870909, 1073741789, 2147483647, 4294967291
  28. };
  29. static UA_UInt32 mod(UA_UInt32 h, UA_UInt32 size) { return h % size; }
  30. static UA_UInt32 mod2(UA_UInt32 h, UA_UInt32 size) { return 1 + (h % (size - 2)); }
  31. static UA_UInt16
  32. higher_prime_index(UA_UInt32 n) {
  33. UA_UInt16 low = 0;
  34. UA_UInt16 high = (UA_UInt16)(sizeof(primes) / sizeof(UA_UInt32));
  35. while(low != high) {
  36. UA_UInt16 mid = (UA_UInt16)(low + ((high - low) / 2));
  37. if(n > primes[mid])
  38. low = (UA_UInt16)(mid + 1);
  39. else
  40. high = mid;
  41. }
  42. return low;
  43. }
  44. static UA_NodeStoreEntry *
  45. instantiateEntry(UA_NodeClass nodeClass) {
  46. size_t size = sizeof(UA_NodeStoreEntry) - sizeof(UA_Node);
  47. switch(nodeClass) {
  48. case UA_NODECLASS_OBJECT:
  49. size += sizeof(UA_ObjectNode);
  50. break;
  51. case UA_NODECLASS_VARIABLE:
  52. size += sizeof(UA_VariableNode);
  53. break;
  54. case UA_NODECLASS_METHOD:
  55. size += sizeof(UA_MethodNode);
  56. break;
  57. case UA_NODECLASS_OBJECTTYPE:
  58. size += sizeof(UA_ObjectTypeNode);
  59. break;
  60. case UA_NODECLASS_VARIABLETYPE:
  61. size += sizeof(UA_VariableTypeNode);
  62. break;
  63. case UA_NODECLASS_REFERENCETYPE:
  64. size += sizeof(UA_ReferenceTypeNode);
  65. break;
  66. case UA_NODECLASS_DATATYPE:
  67. size += sizeof(UA_DataTypeNode);
  68. break;
  69. case UA_NODECLASS_VIEW:
  70. size += sizeof(UA_ViewNode);
  71. break;
  72. default:
  73. return NULL;
  74. }
  75. UA_NodeStoreEntry *entry = (UA_NodeStoreEntry *)UA_calloc(1, size);
  76. if(!entry)
  77. return NULL;
  78. entry->node.nodeClass = nodeClass;
  79. return entry;
  80. }
  81. static void
  82. deleteEntry(UA_NodeStoreEntry *entry) {
  83. UA_Node_deleteMembersAnyNodeClass(&entry->node);
  84. UA_free(entry);
  85. }
  86. /* returns slot of a valid node or null */
  87. static UA_NodeStoreEntry **
  88. findNode(const UA_NodeStore *ns, const UA_NodeId *nodeid) {
  89. UA_UInt32 h = UA_NodeId_hash(nodeid);
  90. UA_UInt32 size = ns->size;
  91. UA_UInt32 idx = mod(h, size);
  92. UA_UInt32 hash2 = mod2(h, size);
  93. while(true) {
  94. UA_NodeStoreEntry *e = ns->entries[idx];
  95. if(!e)
  96. return NULL;
  97. if(e > UA_NODESTORE_TOMBSTONE &&
  98. UA_NodeId_equal(&e->node.nodeId, nodeid))
  99. return &ns->entries[idx];
  100. idx += hash2;
  101. if(idx >= size)
  102. idx -= size;
  103. }
  104. /* NOTREACHED */
  105. return NULL;
  106. }
  107. /* returns an empty slot or null if the nodeid exists */
  108. static UA_NodeStoreEntry **
  109. findSlot(const UA_NodeStore *ns, const UA_NodeId *nodeid) {
  110. UA_UInt32 h = UA_NodeId_hash(nodeid);
  111. UA_UInt32 size = ns->size;
  112. UA_UInt32 idx = mod(h, size);
  113. UA_UInt32 hash2 = mod2(h, size);
  114. while(true) {
  115. UA_NodeStoreEntry *e = ns->entries[idx];
  116. if(e > UA_NODESTORE_TOMBSTONE &&
  117. UA_NodeId_equal(&e->node.nodeId, nodeid))
  118. return NULL;
  119. if(ns->entries[idx] <= UA_NODESTORE_TOMBSTONE)
  120. return &ns->entries[idx];
  121. idx += hash2;
  122. if(idx >= size)
  123. idx -= size;
  124. }
  125. /* NOTREACHED */
  126. return NULL;
  127. }
  128. /* The occupancy of the table after the call will be about 50% */
  129. static UA_StatusCode
  130. expand(UA_NodeStore *ns) {
  131. UA_UInt32 osize = ns->size;
  132. UA_UInt32 count = ns->count;
  133. /* Resize only when table after removal of unused elements is either too
  134. full or too empty */
  135. if(count * 2 < osize && (count * 8 > osize || osize <= UA_NODESTORE_MINSIZE))
  136. return UA_STATUSCODE_GOOD;
  137. UA_NodeStoreEntry **oentries = ns->entries;
  138. UA_UInt32 nindex = higher_prime_index(count * 2);
  139. UA_UInt32 nsize = primes[nindex];
  140. UA_NodeStoreEntry **nentries = (UA_NodeStoreEntry **)UA_calloc(nsize, sizeof(UA_NodeStoreEntry*));
  141. if(!nentries)
  142. return UA_STATUSCODE_BADOUTOFMEMORY;
  143. ns->entries = nentries;
  144. ns->size = nsize;
  145. ns->sizePrimeIndex = nindex;
  146. /* recompute the position of every entry and insert the pointer */
  147. for(size_t i = 0, j = 0; i < osize && j < count; ++i) {
  148. if(oentries[i] <= UA_NODESTORE_TOMBSTONE)
  149. continue;
  150. UA_NodeStoreEntry **e = findSlot(ns, &oentries[i]->node.nodeId);
  151. *e = oentries[i];
  152. ++j;
  153. }
  154. UA_free(oentries);
  155. return UA_STATUSCODE_GOOD;
  156. }
  157. /**********************/
  158. /* Exported functions */
  159. /**********************/
  160. UA_NodeStore *
  161. UA_NodeStore_new(void) {
  162. UA_NodeStore *ns = (UA_NodeStore *)UA_malloc(sizeof(UA_NodeStore));
  163. if(!ns)
  164. return NULL;
  165. ns->sizePrimeIndex = higher_prime_index(UA_NODESTORE_MINSIZE);
  166. ns->size = primes[ns->sizePrimeIndex];
  167. ns->count = 0;
  168. ns->entries = (UA_NodeStoreEntry **)UA_calloc(ns->size, sizeof(UA_NodeStoreEntry*));
  169. if(!ns->entries) {
  170. UA_free(ns);
  171. return NULL;
  172. }
  173. return ns;
  174. }
  175. void
  176. UA_NodeStore_delete(UA_NodeStore *ns) {
  177. UA_UInt32 size = ns->size;
  178. UA_NodeStoreEntry **entries = ns->entries;
  179. for(UA_UInt32 i = 0; i < size; ++i) {
  180. if(entries[i] > UA_NODESTORE_TOMBSTONE)
  181. deleteEntry(entries[i]);
  182. }
  183. UA_free(ns->entries);
  184. UA_free(ns);
  185. }
  186. UA_Node *
  187. UA_NodeStore_newNode(UA_NodeClass nodeClass) {
  188. UA_NodeStoreEntry *entry = instantiateEntry(nodeClass);
  189. if(!entry)
  190. return NULL;
  191. return &entry->node;
  192. }
  193. void
  194. UA_NodeStore_deleteNode(UA_Node *node) {
  195. UA_NodeStoreEntry *entry = container_of(node, UA_NodeStoreEntry, node);
  196. UA_assert(&entry->node == node);
  197. deleteEntry(entry);
  198. }
  199. UA_StatusCode
  200. UA_NodeStore_insert(UA_NodeStore *ns, UA_Node *node) {
  201. if(ns->size * 3 <= ns->count * 4) {
  202. if(expand(ns) != UA_STATUSCODE_GOOD)
  203. return UA_STATUSCODE_BADINTERNALERROR;
  204. }
  205. UA_NodeId tempNodeid;
  206. tempNodeid = node->nodeId;
  207. tempNodeid.namespaceIndex = 0;
  208. UA_NodeStoreEntry **entry;
  209. if(UA_NodeId_isNull(&tempNodeid)) {
  210. /* create a random nodeid */
  211. if(node->nodeId.namespaceIndex == 0)
  212. node->nodeId.namespaceIndex = 1;
  213. UA_UInt32 identifier = ns->count+1; // start value
  214. UA_UInt32 size = ns->size;
  215. UA_UInt32 increase = mod2(identifier, size);
  216. while(true) {
  217. node->nodeId.identifier.numeric = identifier;
  218. entry = findSlot(ns, &node->nodeId);
  219. if(entry)
  220. break;
  221. identifier += increase;
  222. if(identifier >= size)
  223. identifier -= size;
  224. }
  225. } else {
  226. entry = findSlot(ns, &node->nodeId);
  227. if(!entry) {
  228. deleteEntry(container_of(node, UA_NodeStoreEntry, node));
  229. return UA_STATUSCODE_BADNODEIDEXISTS;
  230. }
  231. }
  232. *entry = container_of(node, UA_NodeStoreEntry, node);
  233. ++ns->count;
  234. return UA_STATUSCODE_GOOD;
  235. }
  236. UA_StatusCode
  237. UA_NodeStore_replace(UA_NodeStore *ns, UA_Node *node) {
  238. UA_NodeStoreEntry **entry = findNode(ns, &node->nodeId);
  239. if(!entry)
  240. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  241. UA_NodeStoreEntry *newEntry = container_of(node, UA_NodeStoreEntry, node);
  242. if(*entry != newEntry->orig) {
  243. // the node was replaced since the copy was made
  244. deleteEntry(newEntry);
  245. return UA_STATUSCODE_BADINTERNALERROR;
  246. }
  247. deleteEntry(*entry);
  248. *entry = newEntry;
  249. return UA_STATUSCODE_GOOD;
  250. }
  251. const UA_Node *
  252. UA_NodeStore_get(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  253. UA_NodeStoreEntry **entry = findNode(ns, nodeid);
  254. if(!entry)
  255. return NULL;
  256. return (const UA_Node*)&(*entry)->node;
  257. }
  258. UA_Node *
  259. UA_NodeStore_getCopy(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  260. UA_NodeStoreEntry **slot = findNode(ns, nodeid);
  261. if(!slot)
  262. return NULL;
  263. UA_NodeStoreEntry *entry = *slot;
  264. UA_NodeStoreEntry *newItem = instantiateEntry(entry->node.nodeClass);
  265. if(!newItem)
  266. return NULL;
  267. if(UA_Node_copyAnyNodeClass(&entry->node, &newItem->node) != UA_STATUSCODE_GOOD) {
  268. deleteEntry(newItem);
  269. return NULL;
  270. }
  271. newItem->orig = entry; // store the pointer to the original
  272. return &newItem->node;
  273. }
  274. UA_StatusCode
  275. UA_NodeStore_remove(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  276. UA_NodeStoreEntry **slot = findNode(ns, nodeid);
  277. if(!slot)
  278. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  279. deleteEntry(*slot);
  280. *slot = UA_NODESTORE_TOMBSTONE;
  281. --ns->count;
  282. /* Downsize the hashmap if it is very empty */
  283. if(ns->count * 8 < ns->size && ns->size > 32)
  284. expand(ns); // this can fail. we just continue with the bigger hashmap.
  285. return UA_STATUSCODE_GOOD;
  286. }
  287. void
  288. UA_NodeStore_iterate(UA_NodeStore *ns, UA_NodeStore_nodeVisitor visitor) {
  289. for(UA_UInt32 i = 0; i < ns->size; ++i) {
  290. if(ns->entries[i] > UA_NODESTORE_TOMBSTONE)
  291. visitor((UA_Node*)&ns->entries[i]->node);
  292. }
  293. }
  294. #endif /* UA_ENABLE_MULTITHREADING */