1 /* id.c - keep track of the next id to be given out */
9 #ifdef HAVE_SYS_PARAM_H
10 #include <sys/param.h>
14 #include "back-bdb2.h"
17 next_id_read( Backend *be )
19 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
22 char* file = li->li_nextid_file;
25 if ( (fp = fopen( file, "r" )) == NULL ) {
26 Debug( LDAP_DEBUG_ANY,
27 "next_id_read: could not open \"%s\"\n",
32 if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
33 Debug( LDAP_DEBUG_ANY,
34 "next_id_read: could not fgets nextid from \"%s\"\n",
44 Debug( LDAP_DEBUG_ANY,
45 "next_id_read %lu: atol(%s) return non-positive integer\n",
54 next_id_write( Backend *be, ID id )
56 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
58 char* file = li->li_nextid_file;
62 if ( (fp = fopen( file, "w" )) == NULL ) {
63 Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): could not open \"%s\"\n",
70 if ( fprintf( fp, "%ld\n", id ) == EOF ) {
71 Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): cannot fprintf\n",
76 if( fclose( fp ) != 0 ) {
77 Debug( LDAP_DEBUG_ANY, "next_id_write %lu: cannot fclose\n",
86 bdb2i_next_id_save( Backend *be )
88 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
89 ID id = next_id_get( be );
90 int rc = next_id_write( be, id );
93 li->li_nextid_wrote = id;
100 bdb2i_next_id( Backend *be )
102 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
105 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
107 /* first time in here since startup - try to read the nexid */
108 if ( li->li_nextid == NOID ) {
109 li->li_nextid = next_id_read( be );
111 if ( li->li_nextid == NOID ) {
115 #if SLAPD_NEXTID_CHUNK > 1
116 li->li_nextid_wrote = li->li_nextid;
120 id = li->li_nextid++;
122 #if SLAPD_NEXTID_CHUNK > 1
123 if ( li->li_nextid > li->li_nextid_wrote ) {
124 li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
125 (void) next_id_write( be, li->li_nextid_wrote );
128 (void) next_id_write( be, li->li_nextid );
131 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
136 bdb2i_next_id_return( Backend *be, ID id )
138 #ifdef SLAPD_NEXTID_RETURN
139 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
141 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
143 if ( id != li->li_nextid - 1 ) {
144 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
150 #if !( SLAPD_NEXTID_CHUCK > 1 )
151 (void) next_id_write( be, li->li_nextid );
154 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
159 bdb2i_next_id_get( Backend *be )
161 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
164 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
166 /* first time in here since startup - try to read the nexid */
167 if ( li->li_nextid == NOID ) {
168 li->li_nextid = next_id_read( be );
170 if ( li->li_nextid == NOID ) {
174 #if SLAPD_NEXTID_CHUNK > 1
175 li->li_nextid_wrote = li->li_nextid;
181 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );