]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
minor cleanup
[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 #ifdef NEW_LOGGING
32                 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
33                            "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX ));
34 #else
35                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
36                         0, 0, 0 );
37 #endif
38
39                 return( NOID );
40         }
41
42         ldbm_datum_init( key );
43         key.dptr = (char *) &id;
44         key.dsize = sizeof(ID);
45
46         data = ldbm_cache_fetch( db, key );
47
48         if( data.dptr != NULL ) {
49                 AC_MEMCPY( &id, data.dptr, sizeof( ID ) );
50                 ldbm_datum_free( db->dbc_db, data );
51
52         } else {
53                 id = 1;
54         }
55
56         ldbm_cache_close( be, db );
57         return id;
58 }
59
60 ID
61 next_id_write( Backend *be, ID id )
62 {
63         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
64         Datum key, data;
65         DBCache *db;
66         ID noid = NOID;
67         int flags;
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( NOID );
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                 id = NOID;
94         }
95
96         ldbm_cache_close( be, db );
97         return id;
98 }
99
100 ID
101 next_id_get( Backend *be )
102 {
103         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
104         ID id = NOID;
105
106         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
107
108         if ( li->li_nextid == NOID ) {
109                 li->li_nextid = next_id_read( be );
110         }
111
112         id = li->li_nextid;
113
114         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
115         return id;
116 }
117
118 ID
119 next_id( Backend *be )
120 {
121         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
122         ID id = NOID;
123
124         ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
125
126         if ( li->li_nextid == NOID ) {
127                 li->li_nextid = next_id_read( be );
128         }
129
130         if ( li->li_nextid != NOID ) {
131                 id = li->li_nextid++;
132
133                 (void) next_id_write( be, li->li_nextid );
134         }
135
136         ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
137         return id;
138 }