1 /* nextid.c - keep track of the next id to be given out */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
17 #include "back-ldbm.h"
20 next_id_read( Backend *be, ID *idp )
27 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
30 LDAP_LOG( BACK_LDBM, CRIT,
31 "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
33 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
40 ldbm_datum_init( key );
41 key.dptr = (char *) idp;
42 key.dsize = sizeof(ID);
44 data = ldbm_cache_fetch( db, key );
46 if( data.dptr != NULL ) {
47 AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
48 ldbm_datum_free( db->dbc_db, data );
54 ldbm_cache_close( be, db );
59 next_id_write( Backend *be, ID id )
66 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
69 LDAP_LOG( BACK_LDBM, CRIT,
70 "next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
72 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
79 ldbm_datum_init( key );
80 ldbm_datum_init( data );
82 key.dptr = (char *) &noid;
83 key.dsize = sizeof(ID);
85 data.dptr = (char *) &id;
86 data.dsize = sizeof(ID);
89 if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
93 ldbm_cache_close( be, db );
98 next_id_get( Backend *be, ID *idp )
100 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
105 if ( li->li_nextid == NOID ) {
106 if ( ( rc = next_id_read( be, idp ) ) ) {
109 li->li_nextid = *idp;
112 *idp = li->li_nextid;
118 next_id( Backend *be, ID *idp )
120 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
123 if ( li->li_nextid == NOID ) {
124 if ( ( rc = next_id_read( be, idp ) ) ) {
127 li->li_nextid = *idp;
130 *idp = li->li_nextid++;
131 if ( next_id_write( be, li->li_nextid ) ) {