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 )
29 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
32 LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
33 "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX ));
35 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
42 ldbm_datum_init( key );
43 key.dptr = (char *) &id;
44 key.dsize = sizeof(ID);
46 data = ldbm_cache_fetch( db, key );
48 if( data.dptr != NULL ) {
49 AC_MEMCPY( &id, data.dptr, sizeof( ID ) );
50 ldbm_datum_free( db->dbc_db, data );
56 ldbm_cache_close( be, db );
61 next_id_write( Backend *be, ID id )
63 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
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 )
103 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
106 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
108 if ( li->li_nextid == NOID ) {
109 li->li_nextid = next_id_read( be );
114 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
119 next_id( Backend *be )
121 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
124 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
126 if ( li->li_nextid == NOID ) {
127 li->li_nextid = next_id_read( be );
130 if ( li->li_nextid != NOID ) {
131 id = li->li_nextid++;
133 (void) next_id_write( be, li->li_nextid );
136 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );