repl5_replica_hash.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. /* repl5_replica_hash.c */
  39. #include "repl5.h"
  40. #include "plhash.h"
  41. /* global data */
  42. static PLHashTable *s_hash;
  43. static PRRWLock *s_lock;
  44. struct repl_enum_data
  45. {
  46. FNEnumReplica fn;
  47. void *arg;
  48. };
  49. /* Forward declarations */
  50. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg);
  51. static PRIntn replica_enumerate (PLHashEntry *he, PRIntn index, void *hash_data);
  52. int replica_init_name_hash ()
  53. {
  54. /* allocate table */
  55. s_hash = PL_NewHashTable(0, PL_HashString, PL_CompareStrings,
  56. PL_CompareValues, NULL, NULL);
  57. if (s_hash == NULL)
  58. {
  59. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_name_hash: "
  60. "failed to allocate hash table; NSPR error - %d\n",
  61. PR_GetError ());
  62. return -1;
  63. }
  64. /* create lock */
  65. s_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "replica_hash_lock");
  66. if (s_lock == NULL)
  67. {
  68. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_name_hash: "
  69. "failed to create lock; NSPR error - %d\n",
  70. PR_GetError ());
  71. replica_destroy_name_hash ();
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. void replica_destroy_name_hash ()
  77. {
  78. /* destroy the content */
  79. PL_HashTableEnumerateEntries(s_hash, replica_destroy_hash_entry, NULL);
  80. if (s_hash)
  81. PL_HashTableDestroy(s_hash);
  82. if (s_lock)
  83. PR_DestroyRWLock (s_lock);
  84. }
  85. int replica_add_by_name (const char *name, Object *replica)
  86. {
  87. if (name == NULL || replica == NULL)
  88. {
  89. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: NULL argument\n");
  90. return -1;
  91. }
  92. if (s_hash == NULL || s_lock == NULL)
  93. {
  94. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  95. "replica hash is not initialized\n");
  96. return -1;
  97. }
  98. PR_RWLock_Wlock (s_lock);
  99. /* make sure that the name is unique */
  100. if (PL_HashTableLookup(s_hash, name) != NULL)
  101. {
  102. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  103. "replica with name (%s) already in the hash\n", name);
  104. PR_RWLock_Unlock (s_lock);
  105. return -1 ;
  106. }
  107. /* acquire replica object */
  108. object_acquire (replica);
  109. /* add replica */
  110. if (PL_HashTableAdd(s_hash, name, replica) == NULL)
  111. {
  112. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  113. "failed to add replica with name (%s); NSPR error - %d\n",
  114. name, PR_GetError ());
  115. object_release (replica);
  116. PR_RWLock_Unlock (s_lock);
  117. return -1;
  118. }
  119. PR_RWLock_Unlock (s_lock);
  120. return 0;
  121. }
  122. int replica_delete_by_name (const char *name)
  123. {
  124. Object *replica;
  125. if (name == NULL)
  126. {
  127. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  128. "NULL argument\n");
  129. return -1;
  130. }
  131. if (s_hash == NULL || s_lock == NULL)
  132. {
  133. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  134. "replica hash is not initialized\n");
  135. return -1;
  136. }
  137. PR_RWLock_Wlock (s_lock);
  138. /* locate object */
  139. replica = (Object*)PL_HashTableLookup(s_hash, name);
  140. if (replica == NULL)
  141. {
  142. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  143. "replica with name (%s) is not in the hash.\n", name);
  144. PR_RWLock_Unlock (s_lock);
  145. return -1;
  146. }
  147. /* remove from hash */
  148. PL_HashTableRemove(s_hash, name);
  149. /* release replica */
  150. object_release (replica);
  151. PR_RWLock_Unlock (s_lock);
  152. return 0;
  153. }
  154. Object* replica_get_by_name (const char *name)
  155. {
  156. Object *replica;
  157. if (name == NULL)
  158. {
  159. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_get_by_name: "
  160. "NULL argument\n");
  161. return NULL;
  162. }
  163. if (s_hash == NULL || s_lock == NULL)
  164. {
  165. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_get_by_name: "
  166. "replica hash is not initialized\n");
  167. return NULL;
  168. }
  169. PR_RWLock_Rlock (s_lock);
  170. /* locate object */
  171. replica = (Object*)PL_HashTableLookup(s_hash, name);
  172. if (replica == NULL)
  173. {
  174. PR_RWLock_Unlock (s_lock);
  175. return NULL;
  176. }
  177. object_acquire (replica);
  178. PR_RWLock_Unlock (s_lock);
  179. return replica;
  180. }
  181. void replica_enumerate_replicas (FNEnumReplica fn, void *arg)
  182. {
  183. struct repl_enum_data data;
  184. PR_ASSERT (fn);
  185. data.fn = fn;
  186. data.arg = arg;
  187. PR_RWLock_Wlock (s_lock);
  188. PL_HashTableEnumerateEntries(s_hash, replica_enumerate, &data);
  189. PR_RWLock_Unlock (s_lock);
  190. }
  191. /* Helper functions */
  192. /* this function called for each hash node during hash destruction */
  193. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg)
  194. {
  195. Object *r_obj;
  196. Replica *r;
  197. if (he == NULL)
  198. return HT_ENUMERATE_NEXT;
  199. r_obj = (Object*)he->value;
  200. r = (Replica*)object_get_data (r_obj);
  201. PR_ASSERT (r);
  202. /* flash replica state to the disk */
  203. replica_flush (r);
  204. /* release replica object */
  205. object_release (r_obj);
  206. return HT_ENUMERATE_REMOVE;
  207. }
  208. static PRIntn replica_enumerate (PLHashEntry *he, PRIntn index, void *hash_data)
  209. {
  210. Object *r_obj;
  211. Replica *r;
  212. struct repl_enum_data *data = hash_data;
  213. r_obj = (Object*)he->value;
  214. PR_ASSERT (r_obj);
  215. object_acquire (r_obj);
  216. r = (Replica*)object_get_data (r_obj);
  217. PR_ASSERT (r);
  218. data->fn (r, data->arg);
  219. object_release (r_obj);
  220. return HT_ENUMERATE_NEXT;
  221. }