/storage/object.c

https://github.com/eagafonov/jabberd2 · C · 326 lines · 204 code · 84 blank · 38 comment · 38 complexity · 5d046f22a38d90105832df4110d016b0 MD5 · raw file

  1. /*
  2. * jabberd - Jabber Open Source Server
  3. * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
  4. * Ryan Eatmon, Robert Norris
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
  19. */
  20. #include "storage.h"
  21. /** @file storage/object.c
  22. * @brief object sets
  23. * @author Robert Norris
  24. * $Date: 2005/06/02 04:48:24 $
  25. * $Revision: 1.10 $
  26. */
  27. /* union for xhash_iter_get to comply with strict-alias rules for gcc3 */
  28. union xhashv
  29. {
  30. void **val;
  31. os_field_t *osf_val;
  32. };
  33. os_t os_new(void) {
  34. pool_t p;
  35. os_t os;
  36. p = pool_new();
  37. os = (os_t) pmalloco(p, sizeof(struct os_st));
  38. os->p = p;
  39. return os;
  40. }
  41. void os_free(os_t os) {
  42. pool_free(os->p);
  43. }
  44. int os_count(os_t os) {
  45. return os->count;
  46. }
  47. int os_iter_first(os_t os) {
  48. os->iter = os->head;
  49. if(os->iter == NULL)
  50. return 0;
  51. return 1;
  52. }
  53. int os_iter_next(os_t os) {
  54. if(os->iter == NULL)
  55. return 0;
  56. os->iter = os->iter->next;
  57. if(os->iter == NULL)
  58. return 0;
  59. return 1;
  60. }
  61. os_object_t os_iter_object(os_t os) {
  62. return os->iter;
  63. }
  64. os_object_t os_object_new(os_t os) {
  65. os_object_t o;
  66. log_debug(ZONE, "creating new object");
  67. o = (os_object_t) pmalloco(os->p, sizeof(struct os_object_st));
  68. o->os = os;
  69. o->hash = xhash_new(51);
  70. /* make sure that the hash gets freed when the os pool gets freed */
  71. pool_cleanup(os->p, (pool_cleanup_t) xhash_free, (void *)(o->hash) );
  72. /* insert at the end, we have to preserve order */
  73. o->prev = os->tail;
  74. if(os->tail != NULL) os->tail->next = o;
  75. os->tail = o;
  76. if(os->head == NULL) os->head = o;
  77. os->count++;
  78. return o;
  79. }
  80. void os_object_free(os_object_t o) {
  81. log_debug(ZONE, "dropping object");
  82. if(o->prev != NULL)
  83. o->prev->next = o->next;
  84. if(o->next != NULL)
  85. o->next->prev = o->prev;
  86. if(o->os->head == o)
  87. o->os->head = o->next;
  88. if(o->os->tail == o)
  89. o->os->tail = o->prev;
  90. if(o->os->iter == o)
  91. o->os->iter = o->next;
  92. o->os->count--;
  93. }
  94. /* wrappers for os_object_put to avoid breaking strict-aliasing rules in gcc3 */
  95. void os_object_put_time(os_object_t o, const char *key, const time_t *val) {
  96. void *ptr = (void *) val;
  97. os_object_put(o, key, ptr, os_type_INTEGER);
  98. }
  99. void os_object_put(os_object_t o, const char *key, const void *val, os_type_t type) {
  100. os_field_t osf;
  101. nad_t nad;
  102. log_debug(ZONE, "adding field %s (val %x type %d) to object", key, val, type);
  103. osf = pmalloco(o->os->p, sizeof(struct os_field_st));
  104. osf->key = pstrdup(o->os->p, key);
  105. switch(type) {
  106. case os_type_BOOLEAN:
  107. case os_type_INTEGER:
  108. osf->val = (void *) (intptr_t) (* (int *) val);
  109. break;
  110. case os_type_STRING:
  111. osf->val = (void *) pstrdup(o->os->p, (char *) val);
  112. break;
  113. case os_type_NAD:
  114. nad = nad_copy((nad_t) val);
  115. /* make sure that the nad gets freed when the os pool gets freed */
  116. pool_cleanup(o->os->p, (pool_cleanup_t) nad_free, (void *) nad);
  117. osf->val = (void *) nad;
  118. break;
  119. case os_type_UNKNOWN:
  120. break;
  121. }
  122. osf->type = type;
  123. xhash_put(o->hash, osf->key, (void *) osf);
  124. }
  125. /* wrappers for os_object_get to avoid breaking strict-aliasing rules in gcc3 */
  126. int os_object_get_nad(os_t os, os_object_t o, const char *key, nad_t *val) {
  127. void *ptr = (void *) val;
  128. int ret;
  129. ret = os_object_get(os, o, key, &ptr, os_type_NAD, NULL);
  130. *val = (nad_t) ptr;
  131. return ret;
  132. }
  133. int os_object_get_str(os_t os, os_object_t o, const char *key, char **val) {
  134. void *ptr = (void *) val;
  135. int ret;
  136. ret = os_object_get(os, o, key, &ptr, os_type_STRING, NULL);
  137. *val = (char *) ptr;
  138. return ret;
  139. }
  140. int os_object_get_int(os_t os, os_object_t o, const char *key, int *val) {
  141. void *ptr = (void *) val;
  142. int ret;
  143. ret = os_object_get(os, o, key, &ptr, os_type_INTEGER, NULL);
  144. *val = (int) (long) ptr;
  145. return ret;
  146. }
  147. int os_object_get_bool(os_t os, os_object_t o, const char *key, int *val) {
  148. void *ptr = (void *) val;
  149. int ret;
  150. ret = os_object_get(os, o, key, &ptr, os_type_INTEGER, NULL);
  151. *val = (int) (long) ptr;
  152. return ret;
  153. }
  154. int os_object_get_time(os_t os, os_object_t o, const char *key, time_t *val) {
  155. void *ptr = (void *) val;
  156. int ret;
  157. ret = os_object_get(os, o, key, &ptr, os_type_INTEGER, NULL);
  158. *val = (time_t) ptr;
  159. return ret;
  160. }
  161. int os_object_get(os_t os, os_object_t o, const char *key, void **val, os_type_t type, os_type_t *ot) {
  162. os_field_t osf;
  163. nad_t nad;
  164. /* Type complexity is to deal with string/NADs. If an object contains xml, it will only be
  165. parsed and returned as a NAD if type == os_type_NAD, otherwise if type == os_type_UNKNOWN
  166. it will be returned as string, unless it's already been converted to a NAD */
  167. osf = (os_field_t) xhash_get(o->hash, key);
  168. if(osf == NULL) {
  169. *val = NULL;
  170. return 0;
  171. }
  172. if (ot != NULL)
  173. *ot = osf->type;
  174. if (type == os_type_UNKNOWN)
  175. type = osf->type;
  176. if (type == os_type_UNKNOWN)
  177. type = osf->type;
  178. switch(type) {
  179. case os_type_BOOLEAN:
  180. case os_type_INTEGER:
  181. * (int *) val = (int) (intptr_t) osf->val;
  182. break;
  183. case os_type_STRING:
  184. *val = osf->val;
  185. break;
  186. case os_type_NAD:
  187. /* check to see whether it's already a NAD */
  188. if (osf->type == os_type_NAD) {
  189. *val = osf->val;
  190. } else {
  191. /* parse the string into a NAD */
  192. nad = nad_parse(((char *) osf->val) + 3, strlen(osf->val) - 3);
  193. if(nad == NULL) {
  194. /* unparseable NAD */
  195. log_debug(ZONE, "cell returned from storage for key %s has unparseable XML content (%lu bytes)", key, strlen(osf->val)-3);
  196. *val = NULL;
  197. return 0;
  198. }
  199. /* replace the string with a NAD */
  200. osf->val = (void *) nad;
  201. pool_cleanup(os->p, (pool_cleanup_t) nad_free, (void *) nad);
  202. *val = osf->val;
  203. osf->type = os_type_NAD;
  204. }
  205. break;
  206. default:
  207. *val = NULL;
  208. }
  209. log_debug(ZONE, "got field %s (val %x type %d) to object", key, *val, type);
  210. return 1;
  211. }
  212. int os_object_iter_first(os_object_t o) {
  213. return xhash_iter_first(o->hash);
  214. }
  215. int os_object_iter_next(os_object_t o) {
  216. return xhash_iter_next(o->hash);
  217. }
  218. void os_object_iter_get(os_object_t o, char **key, void **val, os_type_t *type) {
  219. os_field_t osf;
  220. union xhashv xhv;
  221. int keylen;
  222. xhv.osf_val = &osf;
  223. xhash_iter_get(o->hash, (const char **) key, &keylen, xhv.val);
  224. if(*key == NULL) {
  225. *val = NULL;
  226. return;
  227. }
  228. *type = osf->type;
  229. switch(osf->type) {
  230. case os_type_BOOLEAN:
  231. case os_type_INTEGER:
  232. * (int *) val = (int) (intptr_t) osf->val;
  233. break;
  234. case os_type_STRING:
  235. case os_type_NAD:
  236. *val = osf->val;
  237. break;
  238. default:
  239. *val = NULL;
  240. }
  241. log_debug(ZONE, "got iter field %s (val %x type %d) to object", *key, *val, *type);
  242. }