1 /* nextid.c - keep track of the next id to be given out */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
13 #ifdef HAVE_SYS_PARAM_H
14 #include <sys/param.h>
18 #include "back-ldbm.h"
21 next_id_read( Backend *be )
23 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
28 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
30 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
35 ldbm_datum_init( key );
36 key.dptr = (char *) &id;
37 key.dsize = sizeof(ID);
39 data = ldbm_cache_fetch( db, key );
41 if( data.dptr != NULL ) {
42 memcpy( &id, data.dptr, sizeof( ID ) );
43 ldbm_datum_free( db->dbc_db, data );
49 ldbm_cache_close( be, db );
54 next_id_write( Backend *be, ID id )
56 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
62 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
64 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
69 ldbm_datum_init( key );
70 ldbm_datum_init( data );
72 key.dptr = (char *) &noid;
73 key.dsize = sizeof(ID);
75 data.dptr = (char *) &id;
76 data.dsize = sizeof(ID);
79 if( li->li_dbcachewsync ) flags |= LDBM_SYNC;
81 if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
85 ldbm_cache_close( be, db );
90 next_id_get( Backend *be )
92 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
95 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
97 if ( li->li_nextid == NOID ) {
98 li->li_nextid = next_id_read( be );
103 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
108 next_id( Backend *be )
110 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
113 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
115 if ( li->li_nextid == NOID ) {
116 li->li_nextid = next_id_read( be );
119 if ( li->li_nextid != NOID ) {
120 id = li->li_nextid++;
122 (void) next_id_write( be, li->li_nextid );
125 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );