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