mirror of
https://git.openldap.org/openldap/openldap.git
synced 2024-12-15 03:01:09 +08:00
0f17fac37d
SLAPD_NEXTID_CHUNK. Code protects NEXTID file to ensure its equal to or greater than nextid. Updated on close to actual nextid. next_id_save() could be called periodically if desired. Default chunk size is 32. Define to 1 to disable chunking.
185 lines
3.4 KiB
C
185 lines
3.4 KiB
C
/* id.c - keep track of the next id to be given out */
|
|
|
|
#include "portable.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
#include <sys/param.h>
|
|
#endif
|
|
|
|
#include "slap.h"
|
|
#include "back-ldbm.h"
|
|
|
|
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 ( (fp = fopen( file, "r" )) == NULL ) {
|
|
Debug( LDAP_DEBUG_ANY,
|
|
"next_id_read: could not open \"%s\"\n",
|
|
file, 0, 0 );
|
|
return NOID;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
id = atol( buf );
|
|
fclose( fp );
|
|
|
|
if(id < 1) {
|
|
Debug( LDAP_DEBUG_ANY,
|
|
"next_id_read %lu: atol(%s) return non-positive integer\n",
|
|
id, buf, 0 );
|
|
return NOID;
|
|
}
|
|
|
|
return id;
|
|
}
|
|
|
|
static int
|
|
next_id_write( Backend *be, ID id )
|
|
{
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
char buf[20];
|
|
char* file = li->li_nextid_file;
|
|
FILE* fp;
|
|
int rc;
|
|
|
|
if ( (fp = fopen( file, "w" )) == NULL ) {
|
|
Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): could not open \"%s\"\n",
|
|
id, file, 0 );
|
|
return -1;
|
|
}
|
|
|
|
rc = 0;
|
|
|
|
if ( fprintf( fp, "%ld\n", id ) == EOF ) {
|
|
Debug( LDAP_DEBUG_ANY, "next_id_write(%lu): cannot fprintf\n",
|
|
id, 0, 0 );
|
|
rc = -1;
|
|
}
|
|
|
|
if( fclose( fp ) != 0 ) {
|
|
Debug( LDAP_DEBUG_ANY, "next_id_write %lu: cannot fclose\n",
|
|
id, 0, 0 );
|
|
rc = -1;
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
next_id_save( Backend *be )
|
|
{
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
ID id = next_id_get( be );
|
|
int rc = next_id_write( be, id );
|
|
|
|
if (rc == 0) {
|
|
li->li_nextid_wrote = id;
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
ID
|
|
next_id( Backend *be )
|
|
{
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
ID id;
|
|
|
|
pthread_mutex_lock( &li->li_nextid_mutex );
|
|
|
|
/* 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 SLAPD_NEXTID_CHUNK > 1
|
|
li->li_nextid_wrote = li->li_nextid;
|
|
#endif
|
|
}
|
|
|
|
id = 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
|
|
|
|
pthread_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;
|
|
|
|
pthread_mutex_lock( &li->li_nextid_mutex );
|
|
|
|
if ( id != li->li_nextid - 1 ) {
|
|
pthread_mutex_unlock( &li->li_nextid_mutex );
|
|
return;
|
|
}
|
|
|
|
li->li_nextid--;
|
|
|
|
#if !( SLAPD_NEXTID_CHUCK > 1 )
|
|
(void) next_id_write( be, li->li_nextid );
|
|
#endif
|
|
|
|
pthread_mutex_unlock( &li->li_nextid_mutex );
|
|
#endif
|
|
}
|
|
|
|
ID
|
|
next_id_get( Backend *be )
|
|
{
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
ID id;
|
|
|
|
pthread_mutex_lock( &li->li_nextid_mutex );
|
|
|
|
/* 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 SLAPD_NEXTID_CHUNK > 1
|
|
li->li_nextid_wrote = li->li_nextid;
|
|
#endif
|
|
}
|
|
|
|
id = li->li_nextid;
|
|
|
|
pthread_mutex_unlock( &li->li_nextid_mutex );
|
|
|
|
return( id );
|
|
}
|