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 /* first time in here since startup - try to read the nexid */
102 if ( li->li_nextid == NOID ) {
103 li->li_nextid = next_id_read( be );
105 if ( li->li_nextid == NOID ) {
109 #if SLAPD_NEXTID_CHUNK > 1
110 li->li_nextid_wrote = li->li_nextid;
114 id = li->li_nextid++;
116 #if SLAPD_NEXTID_CHUNK > 1
117 if ( li->li_nextid > li->li_nextid_wrote ) {
118 li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
119 (void) next_id_write( be, li->li_nextid_wrote );
122 (void) next_id_write( be, li->li_nextid );
129 bdb2i_next_id_return( BackendDB *be, ID id )
131 #ifdef SLAPD_NEXTID_RETURN
132 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
134 if ( id != li->li_nextid - 1 ) {
140 #if !( SLAPD_NEXTID_CHUCK > 1 )
141 (void) next_id_write( be, li->li_nextid );
147 bdb2i_next_id_get( BackendDB *be )
149 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
152 /* first time in here since startup - try to read the nexid */
153 if ( li->li_nextid == NOID ) {
154 li->li_nextid = next_id_read( be );
156 if ( li->li_nextid == NOID ) {
160 #if SLAPD_NEXTID_CHUNK > 1
161 li->li_nextid_wrote = li->li_nextid;