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/string.h>
12 #include <ac/socket.h>
14 #ifdef HAVE_SYS_PARAM_H
15 #include <sys/param.h>
19 #include "back-ldbm.h"
22 next_id_read( Backend *be )
24 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
29 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
31 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
36 ldbm_datum_init( key );
37 key.dptr = (char *) &id;
38 key.dsize = sizeof(ID);
40 data = ldbm_cache_fetch( db, key );
42 if( data.dptr != NULL ) {
43 memcpy( &id, data.dptr, sizeof( ID ) );
44 ldbm_datum_free( db->dbc_db, data );
50 ldbm_cache_close( be, db );
55 next_id_write( Backend *be, ID id )
57 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
63 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
65 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
70 ldbm_datum_init( key );
71 ldbm_datum_init( data );
73 key.dptr = (char *) &noid;
74 key.dsize = sizeof(ID);
76 data.dptr = (char *) &id;
77 data.dsize = sizeof(ID);
80 if( li->li_dbcachewsync ) flags |= LDBM_SYNC;
82 if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
86 ldbm_cache_close( be, db );
91 next_id_get( Backend *be )
93 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
96 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
98 if ( li->li_nextid == NOID ) {
99 li->li_nextid = next_id_read( be );
104 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
109 next_id( Backend *be )
111 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
114 ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
116 if ( li->li_nextid == NOID ) {
117 li->li_nextid = next_id_read( be );
120 if ( li->li_nextid != NOID ) {
121 id = li->li_nextid++;
123 (void) next_id_write( be, li->li_nextid );
126 ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );