-/* id.c - keep track of the next id to be given out */
-/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* nextid.c - keep track of the next id to be given out */
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2004 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
*/
#include "portable.h"
#include <stdio.h>
+#include <ac/string.h>
#include <ac/socket.h>
-
-#ifdef HAVE_SYS_PARAM_H
-#include <sys/param.h>
-#endif
+#include <ac/param.h>
#include "slap.h"
#include "back-ldbm.h"
-static ID next_id_read( Backend *be );
-static int next_id_write( Backend *be, ID id );
-static ID next_id_get_save( Backend *be, int do_save );
+static int
+next_id_read( Backend *be, ID *idp )
+{
+ Datum key, data;
+ DBCache *db;
+ *idp = NOID;
-static ID
-next_id_read( Backend *be )
-{
- struct ldbminfo *li = (struct ldbminfo *) be->be_private;
- ID id;
- char buf[20];
- char* file = li->li_nextid_file;
- FILE* fp;
-
- if ( ldbm_ignore_nextid_file )
- return NOID;
-
- if ( (fp = fopen( file, "r" )) == NULL ) {
- Debug( LDAP_DEBUG_ANY,
- "next_id_read: could not open \"%s\"\n",
- file, 0, 0 );
- return NOID;
- }
+ if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
+ == NULL ) {
+#ifdef NEW_LOGGING
+ LDAP_LOG( BACK_LDBM, CRIT,
+ "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
+#else
+ Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
+ 0, 0, 0 );
+#endif
- if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
- Debug( LDAP_DEBUG_ANY,
- "next_id_read: could not fgets nextid from \"%s\"\n",
- file, 0, 0 );
- fclose( fp );
- return NOID;
+ return( -1 );
}
- id = atol( buf );
- fclose( fp );
+ ldbm_datum_init( key );
+ key.dptr = (char *) idp;
+ key.dsize = sizeof(ID);
+
+ data = ldbm_cache_fetch( db, key );
+
+ if( data.dptr != NULL ) {
+ AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
+ ldbm_datum_free( db->dbc_db, data );
- if(id < 1) {
- Debug( LDAP_DEBUG_ANY,
- "next_id_read %ld: atol(%s) return non-positive integer\n",
- id, buf, 0 );
- return NOID;
+ } else {
+ *idp = 1;
}
- return id;
+ ldbm_cache_close( be, db );
+ return( 0 );
}
-static int
+int
next_id_write( Backend *be, ID id )
{
- struct ldbminfo *li = (struct ldbminfo *) be->be_private;
- char* file = li->li_nextid_file;
- FILE* fp;
- int rc;
+ Datum key, data;
+ DBCache *db;
+ ID noid = NOID;
+ int flags, rc = 0;
+
+ if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
+ == NULL ) {
+#ifdef NEW_LOGGING
+ LDAP_LOG( BACK_LDBM, CRIT,
+ "next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
+#else
+ Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
+ 0, 0, 0 );
+#endif
- if ( ldbm_ignore_nextid_file )
- return 0;
+ return( -1 );
+ }
- if ( (fp = fopen( file, "w" )) == NULL ) {
- Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): could not open \"%s\"\n",
- id, file, 0 );
- return -1;
- }
+ ldbm_datum_init( key );
+ ldbm_datum_init( data );
- rc = 0;
+ key.dptr = (char *) &noid;
+ key.dsize = sizeof(ID);
- if ( fprintf( fp, "%ld\n", id ) == EOF ) {
- Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): cannot fprintf\n",
- id, 0, 0 );
- rc = -1;
- }
+ data.dptr = (char *) &id;
+ data.dsize = sizeof(ID);
- if( fclose( fp ) != 0 ) {
- Debug( LDAP_DEBUG_ANY, "next_id_write %ld: cannot fclose\n",
- id, 0, 0 );
+ flags = LDBM_REPLACE;
+ if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
rc = -1;
}
- return rc;
+ ldbm_cache_close( be, db );
+ return( rc );
}
int
-next_id_save( Backend *be )
-{
- return( next_id_get_save( be, 1 ) == NOID ? -1 : 0 );
-}
-
-ID
-next_id( Backend *be )
+next_id_get( Backend *be, ID *idp )
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
- ID id;
+ int rc = 0;
- ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
+ *idp = NOID;
- /* first time in here since startup - try to read the nexid */
if ( li->li_nextid == NOID ) {
- li->li_nextid = next_id_read( be );
-
- if ( li->li_nextid == NOID ) {
- li->li_nextid = 1;
+ if ( ( rc = next_id_read( be, idp ) ) ) {
+ return( rc );
}
-
-#if SLAPD_NEXTID_CHUNK > 1
- li->li_nextid_wrote = li->li_nextid;
-#endif
+ li->li_nextid = *idp;
}
- id = li->li_nextid++;
+ *idp = li->li_nextid;
-#if SLAPD_NEXTID_CHUNK > 1
- if ( li->li_nextid > li->li_nextid_wrote ) {
- li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
- (void) next_id_write( be, li->li_nextid_wrote );
- }
-#else
- (void) next_id_write( be, li->li_nextid );
-#endif
-
- ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
- return( id );
-}
-
-void
-next_id_return( Backend *be, ID id )
-{
-#ifdef SLAPD_NEXTID_RETURN
- struct ldbminfo *li = (struct ldbminfo *) be->be_private;
-
- ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
-
- if ( id != li->li_nextid - 1 ) {
- ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
- return;
- }
-
- li->li_nextid--;
-
-#if !( SLAPD_NEXTID_CHUNK > 1 )
- (void) next_id_write( be, li->li_nextid );
-#endif
-
- ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
-#endif
-}
-
-ID
-next_id_get( Backend *be )
-{
- return next_id_get_save( be, 0 );
+ return( rc );
}
-static ID
-next_id_get_save( Backend *be, int do_save )
+int
+next_id( Backend *be, ID *idp )
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
- ID id;
-
- ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
+ int rc = 0;
- /* first time in here since startup - try to read the nexid */
if ( li->li_nextid == NOID ) {
- li->li_nextid = next_id_read( be );
-
- if ( li->li_nextid == NOID ) {
- li->li_nextid = 1;
+ if ( ( rc = next_id_read( be, idp ) ) ) {
+ return( rc );
}
-
-#if SLAPD_NEXTID_CHUNK > 1
- li->li_nextid_wrote = li->li_nextid;
-#endif
+ li->li_nextid = *idp;
}
- id = li->li_nextid;
-
- if ( do_save ) {
- if ( next_id_write( be, id ) == 0 ) {
- li->li_nextid_wrote = id;
- } else {
- id = NOID;
- }
+ *idp = li->li_nextid++;
+ if ( next_id_write( be, li->li_nextid ) ) {
+ rc = -1;
}
- ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
-
- return( id );
+ return( rc );
}