ua_nodestore.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 (UA_Node*)&entry->node;
  188. }
  189. void
  190. UA_NodeStore_deleteNode(UA_Node *node) {
  191. deleteEntry(container_of(node, UA_NodeStoreEntry, node));
  192. }
  193. UA_StatusCode
  194. UA_NodeStore_insert(UA_NodeStore *ns, UA_Node *node) {
  195. if(ns->size * 3 <= ns->count * 4) {
  196. if(expand(ns) != UA_STATUSCODE_GOOD)
  197. return UA_STATUSCODE_BADINTERNALERROR;
  198. }
  199. UA_NodeId tempNodeid;
  200. tempNodeid = node->nodeId;
  201. tempNodeid.namespaceIndex = 0;
  202. UA_NodeStoreEntry **entry;
  203. if(UA_NodeId_isNull(&tempNodeid)) {
  204. /* create a random nodeid */
  205. if(node->nodeId.namespaceIndex == 0)
  206. node->nodeId.namespaceIndex = 1;
  207. UA_UInt32 identifier = ns->count+1; // start value
  208. UA_UInt32 size = ns->size;
  209. hash_t increase = mod2(identifier, size);
  210. while(true) {
  211. node->nodeId.identifier.numeric = identifier;
  212. entry = findSlot(ns, &node->nodeId);
  213. if(entry)
  214. break;
  215. identifier += increase;
  216. if(identifier >= size)
  217. identifier -= size;
  218. }
  219. } else {
  220. entry = findSlot(ns, &node->nodeId);
  221. if(!entry) {
  222. deleteEntry(container_of(node, UA_NodeStoreEntry, node));
  223. return UA_STATUSCODE_BADNODEIDEXISTS;
  224. }
  225. }
  226. *entry = container_of(node, UA_NodeStoreEntry, node);
  227. ns->count++;
  228. return UA_STATUSCODE_GOOD;
  229. }
  230. UA_StatusCode
  231. UA_NodeStore_replace(UA_NodeStore *ns, UA_Node *node) {
  232. UA_NodeStoreEntry **entry = findNode(ns, &node->nodeId);
  233. if(!entry)
  234. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  235. UA_NodeStoreEntry *newEntry = container_of(node, UA_NodeStoreEntry, node);
  236. if(*entry != newEntry->orig) {
  237. // the node was replaced since the copy was made
  238. deleteEntry(newEntry);
  239. return UA_STATUSCODE_BADINTERNALERROR;
  240. }
  241. deleteEntry(*entry);
  242. *entry = newEntry;
  243. return UA_STATUSCODE_GOOD;
  244. }
  245. const UA_Node *
  246. UA_NodeStore_get(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  247. UA_NodeStoreEntry **entry = findNode(ns, nodeid);
  248. if(!entry)
  249. return NULL;
  250. return (const UA_Node*)&(*entry)->node;
  251. }
  252. UA_Node *
  253. UA_NodeStore_getCopy(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  254. UA_NodeStoreEntry **slot = findNode(ns, nodeid);
  255. if(!slot)
  256. return NULL;
  257. UA_NodeStoreEntry *entry = *slot;
  258. UA_NodeStoreEntry *new = instantiateEntry(entry->node.nodeClass);
  259. if(!new)
  260. return NULL;
  261. if(UA_Node_copyAnyNodeClass(&entry->node, &new->node) != UA_STATUSCODE_GOOD) {
  262. deleteEntry(new);
  263. return NULL;
  264. }
  265. new->orig = entry; // store the pointer to the original
  266. return &new->node;
  267. }
  268. UA_StatusCode
  269. UA_NodeStore_remove(UA_NodeStore *ns, const UA_NodeId *nodeid) {
  270. UA_NodeStoreEntry **slot = findNode(ns, nodeid);
  271. if(!slot)
  272. return UA_STATUSCODE_BADNODEIDUNKNOWN;
  273. deleteEntry(*slot);
  274. *slot = UA_NODESTORE_TOMBSTONE;
  275. ns->count--;
  276. /* Downsize the hashmap if it is very empty */
  277. if(ns->count * 8 < ns->size && ns->size > 32)
  278. expand(ns); // this can fail. we just continue with the bigger hashmap.
  279. return UA_STATUSCODE_GOOD;
  280. }
  281. void
  282. UA_NodeStore_iterate(UA_NodeStore *ns, UA_NodeStore_nodeVisitor visitor) {
  283. for(UA_UInt32 i = 0; i < ns->size; i++) {
  284. if(ns->entries[i] > UA_NODESTORE_TOMBSTONE)
  285. visitor((UA_Node*)&ns->entries[i]->node);
  286. }
  287. }
  288. #endif /* UA_ENABLE_MULTITHREADING */