]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
97772bfcc8dc5a902146e6a545594ee5d5612155
[openldap] / servers / slapd / back-ldbm / nextid.c
1 /* nextid.c - keep track of the next id to be given out */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #ifdef HAVE_SYS_PARAM_H
16 #include <sys/param.h>
17 #endif
18
19 #include "slap.h"
20 #include "back-ldbm.h"
21
22 static int
23 next_id_read( Backend *be, ID *idp )
24 {
25         Datum key, data;
26         DBCache *db;
27
28         *idp = NOID;
29
30         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
31             == NULL ) {
32 #ifdef NEW_LOGGING
33                 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
34                            "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX ));
35 #else
36                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
37                         0, 0, 0 );
38 #endif
39
40                 return( -1 );
41         }
42
43         ldbm_datum_init( key );
44         key.dptr = (char *) idp;
45         key.dsize = sizeof(ID);
46
47         data = ldbm_cache_fetch( db, key );
48
49         if( data.dptr != NULL ) {
50                 AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
51                 ldbm_datum_free( db->dbc_db, data );
52
53         } else {
54                 *idp = 1;
55         }
56
57         ldbm_cache_close( be, db );
58         return( 0 );
59 }
60
61 int
62 next_id_write( Backend *be, ID id )
63 {
64         Datum key, data;
65         DBCache *db;
66         ID noid = NOID;
67         int flags, rc = 0;
68
69         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
70             == NULL ) {
71 #ifdef NEW_LOGGING
72                 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
73                            "next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX ));
74 #else
75                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
76                     0, 0, 0 );
77 #endif
78
79                 return( -1 );
80         }
81
82         ldbm_datum_init( key );
83         ldbm_datum_init( data );
84
85         key.dptr = (char *) &noid;
86         key.dsize = sizeof(ID);
87
88         data.dptr = (char *) &id;
89         data.dsize = sizeof(ID);
90
91         flags = LDBM_REPLACE;
92         if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
93                 rc = -1;
94         }
95
96         ldbm_cache_close( be, db );
97         return( rc );
98 }
99
100 int
101 next_id_get( Backend *be, ID *idp )
102 {
103         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
104         int rc = 0;
105
106         *idp = NOID;
107
108         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
109
110         if ( li->li_nextid == NOID ) {
111                 if ( ( rc = next_id_read( be, idp ) ) ) {
112                         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
113                         return( rc );
114                 }
115                 li->li_nextid = *idp;
116         }
117
118         *idp = li->li_nextid;
119
120         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
121         return( rc );
122 }
123
124 int
125 next_id( Backend *be, ID *idp )
126 {
127         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
128         int rc = 0;
129
130         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
131
132         if ( li->li_nextid == NOID ) {
133                 if ( ( rc = next_id_read( be, idp ) ) ) {
134                         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
135                         return( rc );
136                 }
137                 li->li_nextid = *idp;
138         }
139
140         *idp = li->li_nextid++;
141         if ( next_id_write( be, li->li_nextid ) ) {
142                 rc = -1;
143         }
144
145         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
146         return( rc );
147 }