openldap/servers/slapd/back-ldbm/nextid.c

128 lines
2.3 KiB
C
Raw Normal View History

/* nextid.c - keep track of the next id to be given out */
/* $OpenLDAP$ */
1999-08-07 07:07:46 +08:00
/*
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
1998-08-09 08:43:13 +08:00
1998-10-25 09:41:42 +08:00
#include "portable.h"
1998-08-09 08:43:13 +08:00
#include <stdio.h>
1998-10-25 09:41:42 +08:00
1999-08-18 05:21:43 +08:00
#include <ac/string.h>
1998-10-25 09:41:42 +08:00
#include <ac/socket.h>
#ifdef HAVE_SYS_PARAM_H
1998-08-09 08:43:13 +08:00
#include <sys/param.h>
1998-10-25 09:41:42 +08:00
#endif
1998-08-09 08:43:13 +08:00
#include "slap.h"
#include "back-ldbm.h"
static ID
next_id_read( Backend *be )
{
ID id = NOID;
Datum key, data;
DBCache *db;
if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
0, 0, 0 );
return( NOID );
}
ldbm_datum_init( key );
key.dptr = (char *) &id;
key.dsize = sizeof(ID);
data = ldbm_cache_fetch( db, key );
if( data.dptr != NULL ) {
memcpy( &id, data.dptr, sizeof( ID ) );
ldbm_datum_free( db->dbc_db, data );
} else {
id = 1;
}
ldbm_cache_close( be, db );
return id;
}
ID
next_id_write( Backend *be, ID id )
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
Datum key, data;
DBCache *db;
ID noid = NOID;
int flags;
if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
0, 0, 0 );
return( NOID );
}
ldbm_datum_init( key );
ldbm_datum_init( data );
1998-08-09 08:43:13 +08:00
key.dptr = (char *) &noid;
key.dsize = sizeof(ID);
1998-11-26 07:45:57 +08:00
data.dptr = (char *) &id;
data.dsize = sizeof(ID);
1998-11-26 07:45:57 +08:00
flags = LDBM_REPLACE;
if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
id = NOID;
1998-08-09 08:43:13 +08:00
}
ldbm_cache_close( be, db );
return id;
1998-08-09 08:43:13 +08:00
}
ID
next_id_get( Backend *be )
1998-08-09 08:43:13 +08:00
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
ID id = NOID;
1998-08-09 08:43:13 +08:00
ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
1998-11-26 07:45:57 +08:00
if ( li->li_nextid == NOID ) {
li->li_nextid = next_id_read( be );
1998-08-09 08:43:13 +08:00
}
id = li->li_nextid;
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
return id;
1998-08-09 08:43:13 +08:00
}
ID
next_id( Backend *be )
1998-08-09 08:43:13 +08:00
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
ID id = NOID;
1998-08-09 08:43:13 +08:00
ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
1998-11-26 07:45:57 +08:00
if ( li->li_nextid == NOID ) {
li->li_nextid = next_id_read( be );
1998-08-09 08:43:13 +08:00
}
1998-11-26 07:45:57 +08:00
if ( li->li_nextid != NOID ) {
id = li->li_nextid++;
1998-11-26 07:45:57 +08:00
(void) next_id_write( be, li->li_nextid );
}
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
return id;
1998-08-09 08:43:13 +08:00
}