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"
16 /* XXX the separate handling of the NEXTID file is in contrast to TP */
17 /* the NEXTID file is beeing opened during database start-up */
19 next_id_read( BackendDB *be )
21 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
22 BDB2_TXN_HEAD *head = &li->li_txn_head;
23 FILE* fp = head->nextidFP;
27 /* set the file pointer to the beginnig of the file */
31 if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
32 Debug( LDAP_DEBUG_ANY,
33 "next_id_read: could not fgets nextid from \"%s\"\n",
34 li->li_nextid_file, 0, 0 );
41 Debug( LDAP_DEBUG_ANY,
42 "next_id_read %ld: atol(%s) return non-positive integer\n",
50 /* XXX the separate handling of the NEXTID file is in contrast to TP */
51 /* the NEXTID file is beeing opened during database start-up */
53 next_id_write( BackendDB *be, ID id )
55 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
56 BDB2_TXN_HEAD *head = &li->li_txn_head;
57 FILE* fp = head->nextidFP;
61 /* set the file pointer to the beginnig of the file */
64 /* write the nextid */
65 if ( fprintf( fp, "%ld\n", id ) == EOF ) {
66 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): cannot fprintf\n",
71 /* if forced flushing of files is in effect, do so */
72 if( li->li_dbcachewsync && ( fflush( fp ) != 0 )) {
73 Debug( LDAP_DEBUG_ANY, "next_id_write %ld: cannot fflush\n",
82 bdb2i_next_id_save( BackendDB *be )
84 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
85 ID id = bdb2i_next_id_get( be );
86 int rc = next_id_write( be, id );
89 li->li_nextid_wrote = id;
96 bdb2i_next_id( BackendDB *be )
98 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
101 /* DDD ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex ); */
103 /* first time in here since startup - try to read the nexid */
104 if ( li->li_nextid == NOID ) {
105 li->li_nextid = next_id_read( be );
107 if ( li->li_nextid == NOID ) {
111 #if SLAPD_NEXTID_CHUNK > 1
112 li->li_nextid_wrote = li->li_nextid;
116 id = li->li_nextid++;
118 #if SLAPD_NEXTID_CHUNK > 1
119 if ( li->li_nextid > li->li_nextid_wrote ) {
120 li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
121 (void) next_id_write( be, li->li_nextid_wrote );
124 (void) next_id_write( be, li->li_nextid );
127 /* DDD ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex ); */
132 bdb2i_next_id_return( BackendDB *be, ID id )
134 #ifdef SLAPD_NEXTID_RETURN
135 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
137 /* DDD ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex ); */
139 if ( id != li->li_nextid - 1 ) {
140 /* DDD ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex ); */
146 #if !( SLAPD_NEXTID_CHUCK > 1 )
147 (void) next_id_write( be, li->li_nextid );
150 /* DDD ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex ); */
155 bdb2i_next_id_get( BackendDB *be )
157 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
160 /* DDD ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex ); */
162 /* first time in here since startup - try to read the nexid */
163 if ( li->li_nextid == NOID ) {
164 li->li_nextid = next_id_read( be );
166 if ( li->li_nextid == NOID ) {
170 #if SLAPD_NEXTID_CHUNK > 1
171 li->li_nextid_wrote = li->li_nextid;
177 /* DDD ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex ); */