1 /* nextid.c - keep track of the next id to be given out */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
15 #ifdef HAVE_SYS_PARAM_H
16 #include <sys/param.h>
20 #include "back-ldbm.h"
23 next_id_read( Backend *be, ID *idp )
30 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
33 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
34 "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX ));
36 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
43 ldbm_datum_init( key );
44 key.dptr = (char *) idp;
45 key.dsize = sizeof(ID);
47 data = ldbm_cache_fetch( db, key );
49 if( data.dptr != NULL ) {
50 AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
51 ldbm_datum_free( db->dbc_db, data );
57 ldbm_cache_close( be, db );
62 next_id_write( Backend *be, ID id )
69 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
72 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
73 "next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX ));
75 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
82 ldbm_datum_init( key );
83 ldbm_datum_init( data );
85 key.dptr = (char *) &noid;
86 key.dsize = sizeof(ID);
88 data.dptr = (char *) &id;
89 data.dsize = sizeof(ID);
92 if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
96 ldbm_cache_close( be, db );
101 next_id_get( Backend *be, ID *idp )
103 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
108 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
110 if ( li->li_nextid == NOID ) {
111 if ( ( rc = next_id_read( be, idp ) ) ) {
112 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
115 li->li_nextid = *idp;
118 *idp = li->li_nextid;
120 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
125 next_id( Backend *be, ID *idp )
127 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
130 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
132 if ( li->li_nextid == NOID ) {
133 if ( ( rc = next_id_read( be, idp ) ) ) {
134 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
137 li->li_nextid = *idp;
140 *idp = li->li_nextid++;
141 if ( next_id_write( be, li->li_nextid ) ) {
145 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );