]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/nextid.c
Code clean-up.
[openldap] / servers / slapd / back-bdb2 / nextid.c
1 /* id.c - keep track of the next id to be given out */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8
9 #ifdef HAVE_SYS_PARAM_H
10 #include <sys/param.h>
11 #endif
12
13 #include "slap.h"
14 #include "back-bdb2.h"
15
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  */
18 static ID
19 next_id_read( BackendDB *be )
20 {
21         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
22         BDB2_TXN_HEAD   *head = &li->li_txn_head;
23         FILE*   fp = head->nextidFP;
24         ID      id;
25         char    buf[20];
26
27         /*  set the file pointer to the beginnig of the file  */
28         rewind( fp );
29
30         /*  read the nextid  */
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 );
35                 return NOID;
36         }
37
38         id = atol( buf );
39
40         if(id < 1) {
41                 Debug( LDAP_DEBUG_ANY,
42                         "next_id_read %ld: atol(%s) return non-positive integer\n",
43                         id, buf, 0 );
44                 return NOID;
45         }
46
47         return id;
48 }
49
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  */
52 static int
53 next_id_write( BackendDB *be, ID id )
54 {
55         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
56         BDB2_TXN_HEAD   *head = &li->li_txn_head;
57         FILE*   fp = head->nextidFP;
58         char    buf[20];
59         int             rc = 0;
60
61         /*  set the file pointer to the beginnig of the file  */
62         rewind( fp );
63
64         /*  write the nextid  */
65         if ( fprintf( fp, "%ld\n", id ) == EOF ) {
66                 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): cannot fprintf\n",
67                     id, 0, 0 );
68                 rc = -1;
69         }
70
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",
74                     id, 0, 0 );
75                 rc = -1;
76         }
77
78         return rc;
79 }
80
81 int
82 bdb2i_next_id_save( BackendDB *be )
83 {
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 );
87
88         if (rc == 0) {
89                 li->li_nextid_wrote = id;
90         }
91
92         return rc;
93 }
94
95 ID
96 bdb2i_next_id( BackendDB *be )
97 {
98         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
99         ID              id;
100
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 );
104
105                 if ( li->li_nextid == NOID ) {
106                         li->li_nextid = 1;
107                 }
108
109 #if SLAPD_NEXTID_CHUNK > 1
110                 li->li_nextid_wrote = li->li_nextid;
111 #endif
112         }
113
114         id = li->li_nextid++;
115
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 );
120         }
121 #else
122         (void) next_id_write( be, li->li_nextid );
123 #endif
124
125         return( id );
126 }
127
128 void
129 bdb2i_next_id_return( BackendDB *be, ID id )
130 {
131 #ifdef SLAPD_NEXTID_RETURN
132         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
133
134         if ( id != li->li_nextid - 1 ) {
135                 return;
136         }
137
138         li->li_nextid--;
139
140 #if !( SLAPD_NEXTID_CHUCK > 1 )
141         (void) next_id_write( be, li->li_nextid );
142 #endif
143 #endif
144 }
145
146 ID
147 bdb2i_next_id_get( BackendDB *be )
148 {
149         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
150         ID              id;
151
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 );
155
156                 if ( li->li_nextid == NOID ) {
157                         li->li_nextid = 1;
158                 }
159
160 #if SLAPD_NEXTID_CHUNK > 1
161                 li->li_nextid_wrote = li->li_nextid;
162 #endif
163         }
164
165         id = li->li_nextid;
166
167         return( id );
168 }