]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
Prepare for unifdef -DSLAPD_SCHEMA_NOT_COMPAT
[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 ID
23 next_id_read( Backend *be )
24 {
25         ID id = NOID;
26         Datum key, data;
27         DBCache *db;
28
29         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
30             == NULL ) {
31                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
32                         0, 0, 0 );
33                 return( NOID );
34         }
35
36         ldbm_datum_init( key );
37         key.dptr = (char *) &id;
38         key.dsize = sizeof(ID);
39
40         data = ldbm_cache_fetch( db, key );
41
42         if( data.dptr != NULL ) {
43                 memcpy( &id, data.dptr, sizeof( ID ) );
44                 ldbm_datum_free( db->dbc_db, data );
45
46         } else {
47                 id = 1;
48         }
49
50         ldbm_cache_close( be, db );
51         return id;
52 }
53
54 ID
55 next_id_write( Backend *be, ID id )
56 {
57         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
58         Datum key, data;
59         DBCache *db;
60         ID noid = NOID;
61         int flags;
62
63         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
64             == NULL ) {
65                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
66                     0, 0, 0 );
67                 return( NOID );
68         }
69
70         ldbm_datum_init( key );
71         ldbm_datum_init( data );
72
73         key.dptr = (char *) &noid;
74         key.dsize = sizeof(ID);
75
76         data.dptr = (char *) &id;
77         data.dsize = sizeof(ID);
78
79         flags = LDBM_REPLACE;
80         if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
81                 id = NOID;
82         }
83
84         ldbm_cache_close( be, db );
85         return id;
86 }
87
88 ID
89 next_id_get( Backend *be )
90 {
91         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
92         ID id = NOID;
93
94         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
95
96         if ( li->li_nextid == NOID ) {
97                 li->li_nextid = next_id_read( be );
98         }
99
100         id = li->li_nextid;
101
102         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
103         return id;
104 }
105
106 ID
107 next_id( Backend *be )
108 {
109         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
110         ID id = NOID;
111
112         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
113
114         if ( li->li_nextid == NOID ) {
115                 li->li_nextid = next_id_read( be );
116         }
117
118         if ( li->li_nextid != NOID ) {
119                 id = li->li_nextid++;
120
121                 (void) next_id_write( be, li->li_nextid );
122         }
123
124         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
125         return id;
126
127 }