ldbm_usn.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. int rc = 0;
  63. Slapi_Backend *be = NULL;
  64. PRUint64 last_usn = 0;
  65. /* if USN is not enabled, return immediately */
  66. if (!plugin_enabled("USN", li->li_identity)) {
  67. goto bail;
  68. }
  69. /* Search each namingContext in turn */
  70. for ( sdn = slapi_get_first_suffix( &node, 0 ); sdn != NULL;
  71. sdn = slapi_get_next_suffix( &node, 0 )) {
  72. be = slapi_mapping_tree_find_backend_for_sdn(sdn);
  73. slapi_log_error(SLAPI_LOG_TRACE, "ldbm_usn_init",
  74. "backend: %s\n", be->be_name);
  75. rc = usn_get_last_usn(be, &last_usn);
  76. if (0 == rc) { /* only when the last usn is available */
  77. be->be_usn_counter = slapi_counter_new();
  78. slapi_counter_set_value(be->be_usn_counter, last_usn);
  79. slapi_counter_increment(be->be_usn_counter); /* stores next usn */
  80. }
  81. }
  82. bail:
  83. return;
  84. }
  85. /*
  86. * usn_ge_last_usn: get the last USN from the entryusn equality index
  87. */
  88. static int
  89. usn_get_last_usn(Slapi_Backend *be, PRUint64 *last_usn)
  90. {
  91. struct attrinfo *ai = NULL;
  92. int rc = -1;
  93. DB *db = NULL;
  94. DBC *dbc = NULL;
  95. DBT key; /* For the last usn */
  96. DBT value;
  97. if (NULL == last_usn) {
  98. return rc;
  99. }
  100. memset(&key, 0, sizeof(key));
  101. memset(&value, 0, sizeof(key));
  102. *last_usn = -1; /* to start from 0 */
  103. /* Open the entryusn index */
  104. ainfo_get(be, SLAPI_ATTR_ENTRYUSN, &ai);
  105. /* Open the entryusn index file */
  106. rc = dblayer_get_index_file(be, ai, &db, DBOPEN_CREATE);
  107. if (0 != rc) {
  108. /* entryusn.db# is missing; it would be the first time. */
  109. slapi_log_error(SLAPI_LOG_FATAL, "usn_get_last_usn",
  110. "failed to open the entryusn index: %d", rc);
  111. goto bail;
  112. }
  113. /* Get a cursor */
  114. rc = db->cursor(db, NULL, &dbc, 0);
  115. if (0 != rc) {
  116. slapi_log_error(SLAPI_LOG_FATAL, "usn_get_last_usn",
  117. "failed to create a cursor: %d", rc);
  118. goto bail;
  119. }
  120. key.flags = DB_DBT_MALLOC;
  121. value.flags = DB_DBT_MALLOC;
  122. rc = dbc->c_get(dbc, &key, &value, DB_LAST);
  123. if ((0 == rc) && key.data) {
  124. char *p = (char *)key.data;
  125. while ((0 == rc) && ('=' != *p)) { /* get the last elem of equality */
  126. slapi_ch_free(&(key.data));
  127. slapi_ch_free(&(value.data));
  128. rc = dbc->c_get(dbc, &key, &value, DB_PREV);
  129. p = (char *)key.data;
  130. }
  131. if (0 == rc) {
  132. *last_usn = strtoll(++p, (char **)NULL, 0); /* key.data: =num */
  133. }
  134. } else if (DB_NOTFOUND == rc) {
  135. /* if empty, it's okay. This is just a beginning. */
  136. rc = 0;
  137. }
  138. slapi_ch_free(&(key.data));
  139. slapi_ch_free(&(value.data));
  140. bail:
  141. if (dbc) {
  142. dbc->c_close(dbc);
  143. }
  144. if (db) {
  145. dblayer_release_index_file(be, ai, db);
  146. }
  147. return rc;
  148. }
  149. /*
  150. * Whether USN is enabled or not is checked with be_usn_counter.
  151. */
  152. int
  153. ldbm_usn_enabled(Slapi_Backend *be)
  154. {
  155. return (NULL != be->be_usn_counter);
  156. }
  157. /*
  158. * set last usn to the USN slapi_counter in backend
  159. */
  160. int
  161. ldbm_set_last_usn(Slapi_Backend *be)
  162. {
  163. PRUint64 last_usn = 0;
  164. int rc = usn_get_last_usn(be, &last_usn);
  165. if (0 == rc) { /* only when the last usn is available */
  166. /* destroy old counter, if any */
  167. slapi_counter_destroy(&(be->be_usn_counter));
  168. be->be_usn_counter = slapi_counter_new();
  169. slapi_counter_set_value(be->be_usn_counter, last_usn);
  170. slapi_counter_increment(be->be_usn_counter); /* stores next usn */
  171. }
  172. return rc;
  173. }