]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
Add GIANT rwlock! This should resolve nasty concurrency issues.
[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-2002 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 #include <ac/param.h>
15
16 #include "slap.h"
17 #include "back-ldbm.h"
18
19 static int
20 next_id_read( Backend *be, ID *idp )
21 {
22         Datum key, data;
23         DBCache *db;
24
25         *idp = NOID;
26
27         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
28             == NULL ) {
29 #ifdef NEW_LOGGING
30                 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
31                            "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX ));
32 #else
33                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
34                         0, 0, 0 );
35 #endif
36
37                 return( -1 );
38         }
39
40         ldbm_datum_init( key );
41         key.dptr = (char *) idp;
42         key.dsize = sizeof(ID);
43
44         data = ldbm_cache_fetch( db, key );
45
46         if( data.dptr != NULL ) {
47                 AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
48                 ldbm_datum_free( db->dbc_db, data );
49
50         } else {
51                 *idp = 1;
52         }
53
54         ldbm_cache_close( be, db );
55         return( 0 );
56 }
57
58 int
59 next_id_write( Backend *be, ID id )
60 {
61         Datum key, data;
62         DBCache *db;
63         ID noid = NOID;
64         int flags, rc = 0;
65
66         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
67             == NULL ) {
68 #ifdef NEW_LOGGING
69                 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
70                            "next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX ));
71 #else
72                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
73                     0, 0, 0 );
74 #endif
75
76                 return( -1 );
77         }
78
79         ldbm_datum_init( key );
80         ldbm_datum_init( data );
81
82         key.dptr = (char *) &noid;
83         key.dsize = sizeof(ID);
84
85         data.dptr = (char *) &id;
86         data.dsize = sizeof(ID);
87
88         flags = LDBM_REPLACE;
89         if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
90                 rc = -1;
91         }
92
93         ldbm_cache_close( be, db );
94         return( rc );
95 }
96
97 int
98 next_id_get( Backend *be, ID *idp )
99 {
100         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
101         int rc = 0;
102
103         *idp = NOID;
104
105         if ( li->li_nextid == NOID ) {
106                 if ( ( rc = next_id_read( be, idp ) ) ) {
107                         return( rc );
108                 }
109                 li->li_nextid = *idp;
110         }
111
112         *idp = li->li_nextid;
113
114         return( rc );
115 }
116
117 int
118 next_id( Backend *be, ID *idp )
119 {
120         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
121         int rc = 0;
122
123         if ( li->li_nextid == NOID ) {
124                 if ( ( rc = next_id_read( be, idp ) ) ) {
125                         return( rc );
126                 }
127                 li->li_nextid = *idp;
128         }
129
130         *idp = li->li_nextid++;
131         if ( next_id_write( be, li->li_nextid ) ) {
132                 rc = -1;
133         }
134
135         return( rc );
136 }