ua_nodestore.c 9.5 KB

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