ldbm_usn.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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) 2009 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. #include "back-ldbm.h"
  41. static int usn_get_last_usn(Slapi_Backend *be, PRUint64 *last_usn);
  42. /*
  43. * USN counter part in the backend
  44. * - If usn is enabled,
  45. * - For each backend,
  46. * - Get the last USN index key
  47. * - Initialize the slapi counter with the next USN (last USN + 1)
  48. *
  49. * dn: cn=entryusn,cn=default indexes,cn=config,cn=ldbm database,cn=plugins,cn=
  50. * config
  51. * objectClass: top
  52. * objectClass: nsIndex
  53. * cn: entryusn
  54. * nsSystemIndex: true
  55. * nsIndexType: eq
  56. */
  57. void
  58. ldbm_usn_init(struct ldbminfo *li)
  59. {
  60. Slapi_DN *sdn = NULL;
  61. void *node = NULL;
  62. const char *base = NULL;
  63. int rc = 0;
  64. Slapi_Backend *be = NULL;
  65. PRUint64 last_usn = 0;
  66. /* if USN is not enabled, return immediately */
  67. if (!plugin_enabled("USN", li->li_identity)) {
  68. goto bail;
  69. }
  70. /* Search each namingContext in turn */
  71. for ( sdn = slapi_get_first_suffix( &node, 0 ); sdn != NULL;
  72. sdn = slapi_get_next_suffix( &node, 0 )) {
  73. base = slapi_sdn_get_dn( sdn );
  74. be = slapi_mapping_tree_find_backend_for_sdn(sdn);
  75. slapi_log_error(SLAPI_LOG_TRACE, "ldbm_usn_init",
  76. "backend: %s\n", be->be_name);
  77. rc = usn_get_last_usn(be, &last_usn);
  78. if (0 == rc) { /* only when the last usn is available */
  79. be->be_usn_counter = slapi_counter_new();
  80. slapi_counter_set_value(be->be_usn_counter, last_usn);
  81. slapi_counter_increment(be->be_usn_counter); /* stores next usn */
  82. }
  83. }
  84. bail:
  85. return;
  86. }
  87. /*
  88. * usn_ge_last_usn: get the last USN from the entryusn equality index
  89. */
  90. static int
  91. usn_get_last_usn(Slapi_Backend *be, PRUint64 *last_usn)
  92. {
  93. struct attrinfo *ai = NULL;
  94. struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
  95. int rc = -1;
  96. DB *db = NULL;
  97. DBC *dbc = NULL;
  98. DBT key; /* For the last usn */
  99. DBT value;
  100. if (NULL == last_usn) {
  101. return rc;
  102. }
  103. memset(&key, 0, sizeof(key));
  104. memset(&value, 0, sizeof(key));
  105. *last_usn = -1; /* to start from 0 */
  106. /* Open the entryusn index */
  107. ainfo_get(be, "entryusn", &ai);
  108. /* Open the entryusn index file */
  109. rc = dblayer_get_index_file(be, ai, &db, DBOPEN_CREATE);
  110. if (0 != rc) {
  111. /* entryusn.db# is missing; it would be the first time. */
  112. slapi_log_error(SLAPI_LOG_FATAL, "usn_get_last_usn",
  113. "failed to open the entryusn index: %d", rc);
  114. goto bail;
  115. }
  116. /* Get a cursor */
  117. rc = db->cursor(db, NULL, &dbc, 0);
  118. if (0 != rc) {
  119. slapi_log_error(SLAPI_LOG_FATAL, "usn_get_last_usn",
  120. "failed to create a cursor: %d", rc);
  121. goto bail;
  122. }
  123. key.flags = DB_DBT_MALLOC;
  124. value.flags = DB_DBT_MALLOC;
  125. rc = dbc->c_get(dbc, &key, &value, DB_LAST);
  126. if ((0 == rc) && key.data) {
  127. char *p = (char *)key.data;
  128. while ((0 == rc) && ('=' != *p)) { /* get the last elem of equality */
  129. slapi_ch_free(&(key.data));
  130. slapi_ch_free(&(value.data));
  131. rc = dbc->c_get(dbc, &key, &value, DB_PREV);
  132. p = (char *)key.data;
  133. }
  134. if (0 == rc) {
  135. *last_usn = strtoll(++p, (char **)NULL, 0); /* key.data: =num */
  136. }
  137. } else if (DB_NOTFOUND == rc) {
  138. /* if empty, it's okay. This is just a beginning. */
  139. rc = 0;
  140. }
  141. slapi_ch_free(&(key.data));
  142. slapi_ch_free(&(value.data));
  143. bail:
  144. if (dbc) {
  145. dbc->c_close(dbc);
  146. }
  147. if (db) {
  148. dblayer_release_index_file(be, ai, db);
  149. }
  150. return rc;
  151. }
  152. /*
  153. * Whether USN is enabled or not is checked with be_usn_counter.
  154. */
  155. int
  156. ldbm_usn_enabled(Slapi_Backend *be)
  157. {
  158. return (NULL != be->be_usn_counter);
  159. }
  160. /*
  161. * set last usn to the USN slapi_counter in backend
  162. */
  163. int
  164. ldbm_set_last_usn(Slapi_Backend *be)
  165. {
  166. PRUint64 last_usn = 0;
  167. int rc = usn_get_last_usn(be, &last_usn);
  168. if (0 == rc) { /* only when the last usn is available */
  169. /* destroy old counter, if any */
  170. slapi_counter_destroy(&(be->be_usn_counter));
  171. be->be_usn_counter = slapi_counter_new();
  172. slapi_counter_set_value(be->be_usn_counter, last_usn);
  173. slapi_counter_increment(be->be_usn_counter); /* stores next usn */
  174. }
  175. return rc;
  176. }