repl5_replica_hash.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* repl5_replica_hash.c */
  7. #include "repl5.h"
  8. #include "plhash.h"
  9. /* global data */
  10. static PLHashTable *s_hash;
  11. static PRRWLock *s_lock;
  12. struct repl_enum_data
  13. {
  14. FNEnumReplica fn;
  15. void *arg;
  16. };
  17. /* Forward declarations */
  18. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg);
  19. static PRIntn replica_enumerate (PLHashEntry *he, PRIntn index, void *hash_data);
  20. int replica_init_name_hash ()
  21. {
  22. /* allocate table */
  23. s_hash = PL_NewHashTable(0, PL_HashString, PL_CompareStrings,
  24. PL_CompareValues, NULL, NULL);
  25. if (s_hash == NULL)
  26. {
  27. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_name_hash: "
  28. "failed to allocate hash table; NSPR error - %d\n",
  29. PR_GetError ());
  30. return -1;
  31. }
  32. /* create lock */
  33. s_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "replica_hash_lock");
  34. if (s_lock == NULL)
  35. {
  36. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_init_name_hash: "
  37. "failed to create lock; NSPR error - %d\n",
  38. PR_GetError ());
  39. replica_destroy_name_hash ();
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. void replica_destroy_name_hash ()
  45. {
  46. /* destroy the content */
  47. PL_HashTableEnumerateEntries(s_hash, replica_destroy_hash_entry, NULL);
  48. if (s_hash)
  49. PL_HashTableDestroy(s_hash);
  50. if (s_lock)
  51. PR_DestroyRWLock (s_lock);
  52. }
  53. int replica_add_by_name (const char *name, Object *replica)
  54. {
  55. if (name == NULL || replica == NULL)
  56. {
  57. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: NULL argument\n");
  58. return -1;
  59. }
  60. if (s_hash == NULL || s_lock == NULL)
  61. {
  62. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  63. "replica hash is not initialized\n");
  64. return -1;
  65. }
  66. PR_RWLock_Wlock (s_lock);
  67. /* make sure that the name is unique */
  68. if (PL_HashTableLookup(s_hash, name) != NULL)
  69. {
  70. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  71. "replica with name (%s) already in the hash\n", name);
  72. PR_RWLock_Unlock (s_lock);
  73. return -1 ;
  74. }
  75. /* acquire replica object */
  76. object_acquire (replica);
  77. /* add replica */
  78. if (PL_HashTableAdd(s_hash, name, replica) == NULL)
  79. {
  80. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_add_by_name: "
  81. "failed to add replica with name (%s); NSPR error - %d\n",
  82. name, PR_GetError ());
  83. object_release (replica);
  84. PR_RWLock_Unlock (s_lock);
  85. return -1;
  86. }
  87. PR_RWLock_Unlock (s_lock);
  88. return 0;
  89. }
  90. int replica_delete_by_name (const char *name)
  91. {
  92. Object *replica;
  93. if (name == NULL)
  94. {
  95. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  96. "NULL argument\n");
  97. return -1;
  98. }
  99. if (s_hash == NULL || s_lock == NULL)
  100. {
  101. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  102. "replica hash is not initialized\n");
  103. return -1;
  104. }
  105. PR_RWLock_Wlock (s_lock);
  106. /* locate object */
  107. replica = (Object*)PL_HashTableLookup(s_hash, name);
  108. if (replica == NULL)
  109. {
  110. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_delete_by_name: "
  111. "replica with name (%s) is not in the hash.\n", name);
  112. PR_RWLock_Unlock (s_lock);
  113. return -1;
  114. }
  115. /* remove from hash */
  116. PL_HashTableRemove(s_hash, name);
  117. /* release replica */
  118. object_release (replica);
  119. PR_RWLock_Unlock (s_lock);
  120. return 0;
  121. }
  122. Object* replica_get_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_get_by_name: "
  128. "NULL argument\n");
  129. return NULL;
  130. }
  131. if (s_hash == NULL || s_lock == NULL)
  132. {
  133. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_get_by_name: "
  134. "replica hash is not initialized\n");
  135. return NULL;
  136. }
  137. PR_RWLock_Rlock (s_lock);
  138. /* locate object */
  139. replica = (Object*)PL_HashTableLookup(s_hash, name);
  140. if (replica == NULL)
  141. {
  142. PR_RWLock_Unlock (s_lock);
  143. return NULL;
  144. }
  145. object_acquire (replica);
  146. PR_RWLock_Unlock (s_lock);
  147. return replica;
  148. }
  149. void replica_enumerate_replicas (FNEnumReplica fn, void *arg)
  150. {
  151. struct repl_enum_data data;
  152. PR_ASSERT (fn);
  153. data.fn = fn;
  154. data.arg = arg;
  155. PR_RWLock_Wlock (s_lock);
  156. PL_HashTableEnumerateEntries(s_hash, replica_enumerate, &data);
  157. PR_RWLock_Unlock (s_lock);
  158. }
  159. /* Helper functions */
  160. /* this function called for each hash node during hash destruction */
  161. static PRIntn replica_destroy_hash_entry (PLHashEntry *he, PRIntn index, void *arg)
  162. {
  163. Object *r_obj;
  164. Replica *r;
  165. if (he == NULL)
  166. return HT_ENUMERATE_NEXT;
  167. r_obj = (Object*)he->value;
  168. r = (Replica*)object_get_data (r_obj);
  169. PR_ASSERT (r);
  170. /* flash replica state to the disk */
  171. replica_flush (r);
  172. /* release replica object */
  173. object_release (r_obj);
  174. return HT_ENUMERATE_REMOVE;
  175. }
  176. static PRIntn replica_enumerate (PLHashEntry *he, PRIntn index, void *hash_data)
  177. {
  178. Object *r_obj;
  179. Replica *r;
  180. struct repl_enum_data *data = hash_data;
  181. r_obj = (Object*)he->value;
  182. PR_ASSERT (r_obj);
  183. object_acquire (r_obj);
  184. r = (Replica*)object_get_data (r_obj);
  185. PR_ASSERT (r);
  186. data->fn (r, data->arg);
  187. object_release (r_obj);
  188. return HT_ENUMERATE_NEXT;
  189. }