ua_nodestore.c 9.3 KB

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