]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
ebe6f3897119b5cbab989be08a2c4f497ee0850c
[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-1999 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( li->li_dbcachewsync ) flags |= LDBM_SYNC;
81
82         if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
83                 id = NOID;
84         }
85
86         ldbm_cache_close( be, db );
87         return id;
88 }
89
90 ID
91 next_id_get( Backend *be )
92 {
93         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
94         ID id = NOID;
95
96         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
97
98         if ( li->li_nextid == NOID ) {
99                 li->li_nextid = next_id_read( be );
100         }
101
102         id = li->li_nextid;
103
104         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
105         return id;
106 }
107
108 ID
109 next_id( Backend *be )
110 {
111         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
112         ID id = NOID;
113
114         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
115
116         if ( li->li_nextid == NOID ) {
117                 li->li_nextid = next_id_read( be );
118         }
119
120         if ( li->li_nextid != NOID ) {
121                 id = li->li_nextid++;
122
123                 (void) next_id_write( be, li->li_nextid );
124         }
125
126         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
127         return id;
128
129 }