rdf_hash_list.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * RDF Hash List Interface Implementation
  3. *
  4. * $Source$
  5. * $Id$
  6. *
  7. * (C) Dave Beckett 2000 ILRT, University of Bristol
  8. * http://www.ilrt.bristol.ac.uk/people/cmdjb/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. */
  16. #include <config.h>
  17. #include <sys/types.h>
  18. #include <stdio.h>
  19. #include <rdf_config.h>
  20. #include <rdf_hash.h>
  21. #include <rdf_hash_list.h>
  22. struct rdf_hash_list_node_s
  23. {
  24. struct rdf_hash_list_node_s* next;
  25. char *key;
  26. size_t key_len;
  27. char *value;
  28. size_t value_len;
  29. };
  30. typedef struct rdf_hash_list_node_s rdf_hash_list_node;
  31. typedef struct
  32. {
  33. rdf_hash_list_node* first;
  34. rdf_hash_list_node* current_key;
  35. } rdf_hash_list_context;
  36. /* prototypes for local functions */
  37. static rdf_hash_list_node* rdf_hash_list_find_node(rdf_hash_list_node* list, char *key, size_t key_len, rdf_hash_list_node** prev);
  38. static void free_rdf_hash_list_node(rdf_hash_list_node* node);
  39. static int rdf_hash_list_open(void* context, char *identifier, void *mode, void *options);
  40. static int rdf_hash_list_close(void* context);
  41. static int rdf_hash_list_get(void* context, rdf_hash_data *key, rdf_hash_data *data, unsigned int flags);
  42. static int rdf_hash_list_put(void* context, rdf_hash_data *key, rdf_hash_data *data, unsigned int flags);
  43. static int rdf_hash_list_delete(void* context, rdf_hash_data *key);
  44. static int rdf_hash_list_get_seq(void* context, rdf_hash_data *key, unsigned int flags);
  45. static int rdf_hash_list_sync(void* context);
  46. static int rdf_hash_list_get_fd(void* context);
  47. static void rdf_hash_list_register_factory(rdf_hash_factory *factory);
  48. /* helper functions */
  49. static rdf_hash_list_node*
  50. rdf_hash_list_find_node(rdf_hash_list_node* list, char *key, size_t key_len,
  51. rdf_hash_list_node** prev)
  52. {
  53. rdf_hash_list_node* node;
  54. if(prev)
  55. *prev=list;
  56. for(node=list; node; node=node->next) {
  57. if(key_len == node->key_len && !memcmp(key, node->key, key_len))
  58. break;
  59. if(prev)
  60. *prev=node;
  61. }
  62. return node;
  63. }
  64. static void
  65. free_rdf_hash_list_node(rdf_hash_list_node* node)
  66. {
  67. if(node->key)
  68. RDF_FREE(cstring, node->key);
  69. if(node->value)
  70. RDF_FREE(cstring, node->value);
  71. RDF_FREE(rdf_hash_list_node, node);
  72. }
  73. /* functions implementing hash api */
  74. static int
  75. rdf_hash_list_open(void* context, char *identifier, void *mode, void *options)
  76. {
  77. /* nop */
  78. return 0;
  79. }
  80. static int
  81. rdf_hash_list_close(void* context)
  82. {
  83. rdf_hash_list_context* list_context=(rdf_hash_list_context*)context;
  84. rdf_hash_list_node *node, *next;
  85. for(node=list_context->first; node; node=next) {
  86. next=node->next;
  87. free_rdf_hash_list_node(node);
  88. }
  89. return 0;
  90. }
  91. static int
  92. rdf_hash_list_get(void* context, rdf_hash_data *key, rdf_hash_data *data, unsigned int flags)
  93. {
  94. rdf_hash_list_context* list_context=(rdf_hash_list_context*)context;
  95. rdf_hash_list_node* node;
  96. node=rdf_hash_list_find_node(list_context->first, (char*)key->data, key->size, NULL);
  97. if(!node) {
  98. /* not found */
  99. data->data = NULL;
  100. return 0;
  101. }
  102. data->data = RDF_MALLOC(hash_list_data, node->value_len);
  103. if(!data->data) {
  104. return 1;
  105. }
  106. memcpy(data->data, node->value, node->value_len);
  107. data->size = node->value_len;
  108. return 0;
  109. }
  110. static int
  111. rdf_hash_list_put(void* context, rdf_hash_data *key, rdf_hash_data *value, unsigned int flags)
  112. {
  113. rdf_hash_list_context* list_context=(rdf_hash_list_context*)context;
  114. rdf_hash_list_node* node;
  115. int is_new_node;
  116. char *new_key;
  117. char *new_value;
  118. node=rdf_hash_list_find_node(list_context->first, (char*)key->data, key->size, NULL);
  119. is_new_node=(node == NULL);
  120. if(is_new_node) {
  121. /* need new node */
  122. node=(rdf_hash_list_node*)RDF_CALLOC(rdf_hash_list_node, sizeof(rdf_hash_list_node), 1);
  123. if(!node)
  124. return 1;
  125. }
  126. /* allocate key only for a new node */
  127. if(is_new_node) {
  128. new_key=(char*)RDF_MALLOC(cstring, key->size);
  129. if(!new_key) {
  130. RDF_FREE(rdf_hash_list_node, node);
  131. return 1;
  132. }
  133. }
  134. new_value=(char*)RDF_MALLOC(cstring, value->size);
  135. if(!new_value) {
  136. RDF_FREE(cstring, new_key);
  137. if(is_new_node)
  138. RDF_FREE(rdf_hash_list_node, node);
  139. return 1;
  140. }
  141. /* at this point, all mallocs worked and list hasn't been touched*/
  142. if(is_new_node) {
  143. memcpy(new_key, key->data, key->size);
  144. node->key=new_key;
  145. node->key_len=key->size;
  146. } else {
  147. /* only now delete old data */
  148. RDF_FREE(cstring, node->value);
  149. }
  150. memcpy(new_value, value->data, value->size);
  151. node->value=new_value;
  152. node->value_len=value->size;
  153. if(is_new_node) {
  154. /* only now touch list */
  155. node->next=list_context->first;
  156. list_context->first=node;
  157. }
  158. return 0;
  159. }
  160. static int
  161. rdf_hash_list_delete(void* context, rdf_hash_data *key)
  162. {
  163. rdf_hash_list_context* list_context=(rdf_hash_list_context*)context;
  164. rdf_hash_list_node *node, *prev;
  165. node=rdf_hash_list_find_node(list_context->first, (char*)key->data, key->size, &prev);
  166. if(!node) {
  167. /* not found */
  168. return 1;
  169. }
  170. if(node == list_context->first)
  171. list_context->first=node->next;
  172. else
  173. prev->next=node->next;
  174. /* free node */
  175. free_rdf_hash_list_node(node);
  176. return 0;
  177. }
  178. static int
  179. rdf_hash_list_get_seq(void* context, rdf_hash_data *key, unsigned int flags)
  180. {
  181. rdf_hash_list_context* list_context=(rdf_hash_list_context*)context;
  182. rdf_hash_list_node* node;
  183. if(flags == RDF_HASH_FLAGS_FIRST) {
  184. node=list_context->first;
  185. } else if (flags == RDF_HASH_FLAGS_NEXT) {
  186. node=list_context->current_key ? list_context->current_key->next : NULL;
  187. } else { /* RDF_HASH_FLAGS_CURRENT */
  188. node=list_context->current_key;
  189. }
  190. if(!node) {
  191. key->data = NULL;
  192. } else {
  193. key->data = RDF_MALLOC(cstring, node->key_len);
  194. if(!key->data)
  195. return 1;
  196. memcpy(key->data, node->key, node->key_len);
  197. key->size = node->key_len;
  198. }
  199. /* save new current key */
  200. list_context->current_key=node;
  201. return 0;
  202. }
  203. static int
  204. rdf_hash_list_sync(void* context)
  205. {
  206. /* Not applicable */
  207. return 0;
  208. }
  209. static int
  210. rdf_hash_list_get_fd(void* context)
  211. {
  212. /* Not applicable */
  213. return 0;
  214. }
  215. /* local function to register LIST hash functions */
  216. static void
  217. rdf_hash_list_register_factory(rdf_hash_factory *factory)
  218. {
  219. factory->context_length = sizeof(rdf_hash_list_context);
  220. factory->open = rdf_hash_list_open;
  221. factory->close = rdf_hash_list_close;
  222. factory->get = rdf_hash_list_get;
  223. factory->put = rdf_hash_list_put;
  224. factory->delete_key = rdf_hash_list_delete;
  225. factory->get_seq = rdf_hash_list_get_seq;
  226. factory->sync = rdf_hash_list_sync;
  227. factory->get_fd = rdf_hash_list_get_fd;
  228. }
  229. void
  230. init_rdf_hash_list(void)
  231. {
  232. rdf_hash_register_factory("LIST", &rdf_hash_list_register_factory);
  233. }