repl5_replica_hash.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_replica_hash.c */
  42. #include "repl5.h"
  43. #include "plhash.h"
  44. /* global data */
  45. static PLHashTable *s_hash;
  46. static Slapi_RWLock *s_lock;
  47. struct repl_enum_data
  48. {
  49. FNEnumReplica fn;
  50. void *arg;
  51. };
  52. /* Forward declarations */
  53. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg);
  54. static PRIntn replica_enumerate (PLHashEntry *he, PRIntn index, void *hash_data);
  55. int replica_init_name_hash ()
  56. {
  57. /* allocate table */
  58. s_hash = PL_NewHashTable(0, PL_HashString, PL_CompareStrings,
  59. PL_CompareValues, NULL, NULL);
  60. if (s_hash == NULL)
  61. {
  62. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_name_hash: "
  63. "failed to allocate hash table; NSPR error - %d\n",
  64. PR_GetError ());
  65. return -1;
  66. }
  67. /* create lock */
  68. s_lock = slapi_new_rwlock();
  69. if (s_lock == NULL)
  70. {
  71. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_name_hash: "
  72. "failed to create lock; NSPR error - %d\n",
  73. PR_GetError ());
  74. replica_destroy_name_hash ();
  75. return -1;
  76. }
  77. return 0;
  78. }
  79. void replica_destroy_name_hash ()
  80. {
  81. /* destroy the content */
  82. PL_HashTableEnumerateEntries(s_hash, replica_destroy_hash_entry, NULL);
  83. if (s_hash)
  84. PL_HashTableDestroy(s_hash);
  85. if (s_lock)
  86. slapi_destroy_rwlock (s_lock);
  87. }
  88. int replica_add_by_name (const char *name, Object *replica)
  89. {
  90. if (name == NULL || replica == NULL)
  91. {
  92. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: NULL argument\n");
  93. return -1;
  94. }
  95. if (s_hash == NULL || s_lock == NULL)
  96. {
  97. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  98. "replica hash is not initialized\n");
  99. return -1;
  100. }
  101. slapi_rwlock_wrlock (s_lock);
  102. /* make sure that the name is unique */
  103. if (PL_HashTableLookup(s_hash, name) != NULL)
  104. {
  105. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  106. "replica with name (%s) already in the hash\n", name);
  107. slapi_rwlock_unlock (s_lock);
  108. return -1 ;
  109. }
  110. /* acquire replica object */
  111. object_acquire (replica);
  112. /* add replica */
  113. if (PL_HashTableAdd(s_hash, name, replica) == NULL)
  114. {
  115. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  116. "failed to add replica with name (%s); NSPR error - %d\n",
  117. name, PR_GetError ());
  118. object_release (replica);
  119. slapi_rwlock_unlock (s_lock);
  120. return -1;
  121. }
  122. slapi_rwlock_unlock (s_lock);
  123. return 0;
  124. }
  125. int replica_delete_by_name (const char *name)
  126. {
  127. Object *replica;
  128. if (name == NULL)
  129. {
  130. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  131. "NULL argument\n");
  132. return -1;
  133. }
  134. if (s_hash == NULL || s_lock == NULL)
  135. {
  136. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  137. "replica hash is not initialized\n");
  138. return -1;
  139. }
  140. slapi_rwlock_wrlock (s_lock);
  141. /* locate object */
  142. replica = (Object*)PL_HashTableLookup(s_hash, name);
  143. if (replica == NULL)
  144. {
  145. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  146. "replica with name (%s) is not in the hash.\n", name);
  147. slapi_rwlock_unlock (s_lock);
  148. return -1;
  149. }
  150. /* remove from hash */
  151. PL_HashTableRemove(s_hash, name);
  152. /* release replica */
  153. object_release (replica);
  154. slapi_rwlock_unlock (s_lock);
  155. return 0;
  156. }
  157. Object* replica_get_by_name (const char *name)
  158. {
  159. Object *replica;
  160. if (name == NULL)
  161. {
  162. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_get_by_name: "
  163. "NULL argument\n");
  164. return NULL;
  165. }
  166. if (s_hash == NULL || s_lock == NULL)
  167. {
  168. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_get_by_name: "
  169. "replica hash is not initialized\n");
  170. return NULL;
  171. }
  172. slapi_rwlock_rdlock (s_lock);
  173. /* locate object */
  174. replica = (Object*)PL_HashTableLookup(s_hash, name);
  175. if (replica == NULL)
  176. {
  177. slapi_rwlock_unlock (s_lock);
  178. return NULL;
  179. }
  180. object_acquire (replica);
  181. slapi_rwlock_unlock (s_lock);
  182. return replica;
  183. }
  184. void replica_enumerate_replicas (FNEnumReplica fn, void *arg)
  185. {
  186. struct repl_enum_data data;
  187. PR_ASSERT (fn);
  188. data.fn = fn;
  189. data.arg = arg;
  190. slapi_rwlock_wrlock (s_lock);
  191. PL_HashTableEnumerateEntries(s_hash, replica_enumerate, &data);
  192. slapi_rwlock_unlock (s_lock);
  193. }
  194. /* Helper functions */
  195. /* this function called for each hash node during hash destruction */
  196. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg)
  197. {
  198. Object *r_obj;
  199. Replica *r;
  200. if (he == NULL)
  201. return HT_ENUMERATE_NEXT;
  202. r_obj = (Object*)he->value;
  203. r = (Replica*)object_get_data (r_obj);
  204. PR_ASSERT (r);
  205. /* flash replica state to the disk */
  206. replica_flush (r);
  207. /* release replica object */
  208. object_release (r_obj);
  209. return HT_ENUMERATE_REMOVE;
  210. }
  211. static PRIntn replica_enumerate (PLHashEntry *he, PRIntn index, void *hash_data)
  212. {
  213. Object *r_obj;
  214. Replica *r;
  215. struct repl_enum_data *data = hash_data;
  216. r_obj = (Object*)he->value;
  217. PR_ASSERT (r_obj);
  218. object_acquire (r_obj);
  219. r = (Replica*)object_get_data (r_obj);
  220. PR_ASSERT (r);
  221. data->fn (r, data->arg);
  222. object_release (r_obj);
  223. return HT_ENUMERATE_NEXT;
  224. }