]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
document search disable feature (spin-off of limit on unchecked entries)
[openldap] / servers / slapd / back-ldbm / nextid.c
1 /* nextid.c - keep track of the next id to be given out */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23 #include <ac/param.h>
24
25 #include "slap.h"
26 #include "back-ldbm.h"
27
28 static int
29 next_id_read( Backend *be, ID *idp )
30 {
31         Datum key, data;
32         DBCache *db;
33
34         *idp = NOID;
35
36         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
37             == NULL ) {
38 #ifdef NEW_LOGGING
39                 LDAP_LOG( BACK_LDBM, CRIT,
40                    "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
41 #else
42                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
43                         0, 0, 0 );
44 #endif
45
46                 return( -1 );
47         }
48
49         ldbm_datum_init( key );
50         key.dptr = (char *) idp;
51         key.dsize = sizeof(ID);
52
53         data = ldbm_cache_fetch( db, key );
54
55         if( data.dptr != NULL ) {
56                 AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
57                 ldbm_datum_free( db->dbc_db, data );
58
59         } else {
60                 *idp = 1;
61         }
62
63         ldbm_cache_close( be, db );
64         return( 0 );
65 }
66
67 int
68 next_id_write( Backend *be, ID id )
69 {
70         Datum key, data;
71         DBCache *db;
72         ID noid = NOID;
73         int flags, rc = 0;
74
75         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
76             == NULL ) {
77 #ifdef NEW_LOGGING
78                 LDAP_LOG( BACK_LDBM, CRIT,
79                   "next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
80 #else
81                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
82                     0, 0, 0 );
83 #endif
84
85                 return( -1 );
86         }
87
88         ldbm_datum_init( key );
89         ldbm_datum_init( data );
90
91         key.dptr = (char *) &noid;
92         key.dsize = sizeof(ID);
93
94         data.dptr = (char *) &id;
95         data.dsize = sizeof(ID);
96
97         flags = LDBM_REPLACE;
98         if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
99                 rc = -1;
100         }
101
102         ldbm_cache_close( be, db );
103         return( rc );
104 }
105
106 int
107 next_id_get( Backend *be, ID *idp )
108 {
109         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
110         int rc = 0;
111
112         *idp = NOID;
113
114         if ( li->li_nextid == NOID ) {
115                 if ( ( rc = next_id_read( be, idp ) ) ) {
116                         return( rc );
117                 }
118                 li->li_nextid = *idp;
119         }
120
121         *idp = li->li_nextid;
122
123         return( rc );
124 }
125
126 int
127 next_id( Backend *be, ID *idp )
128 {
129         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
130         int rc = 0;
131
132         if ( li->li_nextid == NOID ) {
133                 if ( ( rc = next_id_read( be, idp ) ) ) {
134                         return( rc );
135                 }
136                 li->li_nextid = *idp;
137         }
138
139         *idp = li->li_nextid++;
140         if ( next_id_write( be, li->li_nextid ) ) {
141                 rc = -1;
142         }
143
144         return( rc );
145 }