Commit 2054d40b authored by Kurt Zeilenga's avatar Kurt Zeilenga
Browse files

Added support for SleepyCat DB2 DBI.

	Derived from public posting to ldap@umich.edu
	by Kurt Spanier <kurt.spanier@zdv.uni-tuebingen.de>
	See Make-common.dist for DB2 options
parent 8c5868b5
......@@ -144,6 +144,8 @@ SLAPD_BACKENDS= -DLDAP_LDBM -DLDAP_SHELL -DLDAP_PASSWD
# standard unix ndbm
# Fallback settings, defaults are set in build/*/Make-platform
# The NDBM interface may not work on some (any) platforms.
# You may be happier with Berkeley DB B-trees.
LDBMBACKEND?=-DLDBM_USE_NDBM
LDBMINCLUDE?=
LDBMLIB?=
......@@ -163,8 +165,13 @@ LDBMLIB?=
#LDBMINCLUDE=-I/usr/local/gdbm/include
#LDBMLIB=-lgdbm
#undef these if you have SLEEPYCAT DB2 installed
#LDBMBACKEND=-DLDBM_USE_DBBTREE -DHAVE_SLEEPYCAT_DB2
#undef these if you have SleepyCat DB2 installed the updated DBI
#LDBMBACKEND=-DLDBM_USE_DBBTREE -DLDBM_USE_DB2
#LDBMINCLUDE=-I/usr/local/include
#LDBMLIB=-L/usr/local/lib -ldb
#undef these if you have SleepyCat DB2 installed (with compat185)
#LDBMBACKEND=-DLDBM_USE_DBBTREE -DLDBM_USE_DB2_COMPAT185
#LDBMINCLUDE=-I/usr/local/include
#LDBMLIB=-L/usr/local/lib -ldb
......
......@@ -47,10 +47,14 @@ extern gdbm_error gdbm_errno;
#include <limits.h>
#include <fcntl.h>
#ifdef HAVE_SLEEPYCAT_DB2
#ifdef LDBM_USE_DB2_COMPAT185
# include <db_185.h>
#else
# include <db.h>
# ifdef LDBM_USE_DB2
# define R_NOOVERWRITE DB_NOOVERWRITE
# define DEFAULT_DB_PAGE_SIZE 1024
# endif
#endif
typedef DBT Datum;
......@@ -62,11 +66,18 @@ typedef DB *LDBM;
#define DB_TYPE DB_HASH
/* for ldbm_open */
#define LDBM_READER O_RDONLY
#define LDBM_WRITER O_RDWR
#define LDBM_WRCREAT (O_RDWR|O_CREAT)
#define LDBM_NEWDB (O_RDWR|O_TRUNC|O_CREAT)
#define LDBM_FAST 0
#ifdef LDBM_USE_DB2
# define LDBM_READER DB_RDONLY
# define LDBM_WRITER 0x00000 /* hopefully */
# define LDBM_WRCREAT (DB_NOMMAP|DB_CREATE|DB_THREAD)
# define LDBM_NEWDB (DB_TRUNCATE|DB_CREATE|DB_THREAD)
#else
# define LDBM_READER O_RDONLY
# define LDBM_WRITER O_RDWR
# define LDBM_WRCREAT (O_RDWR|O_CREAT)
# define LDBM_NEWDB (O_RDWR|O_TRUNC|O_CREAT)
# define LDBM_FAST 0
#endif
#define LDBM_SUFFIX ".dbh"
......@@ -90,12 +101,18 @@ extern int errno;
#include <sys/types.h>
#include <limits.h>
#include <fcntl.h>
#ifdef HAVE_SLEEPYCAT_DB2
#ifdef LDBM_USE_DB2_COMPAT185
# include <db_185.h>
#else
# include <db.h>
# ifdef LDBM_USE_DB2
# define R_NOOVERWRITE DB_NOOVERWRITE
# define DEFAULT_DB_PAGE_SIZE 1024
# endif
#endif
typedef DBT Datum;
#define dsize size
#define dptr data
......@@ -105,11 +122,19 @@ typedef DB *LDBM;
#define DB_TYPE DB_BTREE
/* for ldbm_open */
#define LDBM_READER O_RDONLY
#define LDBM_WRITER O_RDWR
#define LDBM_WRCREAT (O_RDWR|O_CREAT)
#define LDBM_NEWDB (O_RDWR|O_TRUNC|O_CREAT)
#define LDBM_FAST 0
#ifdef LDBM_USE_DB2
# define LDBM_READER DB_RDONLY
# define LDBM_WRITER 0x00000 /* hopefully */
# define LDBM_WRCREAT (DB_NOMMAP|DB_CREATE|DB_THREAD)
# define LDBM_NEWDB (DB_TRUNCATE|DB_CREATE|DB_THREAD)
#else
# define LDBM_READER O_RDONLY
# define LDBM_WRITER O_RDWR
# define LDBM_WRCREAT (O_RDWR|O_CREAT)
# define LDBM_NEWDB (O_RDWR|O_TRUNC|O_CREAT)
#endif
# define LDBM_FAST 0
#define LDBM_SUFFIX ".dbb"
#define LDBM_ORDERED 1
......@@ -159,6 +184,7 @@ typedef DBM *LDBM;
#endif /* db btree */
#endif /* gdbm */
int ldbm_errno( LDBM ldbm );
LDBM ldbm_open( char *name, int rw, int mode, int dbcachesize );
void ldbm_close( LDBM ldbm );
void ldbm_sync( LDBM ldbm );
......@@ -167,8 +193,14 @@ Datum ldbm_datum_dup( LDBM ldbm, Datum data );
Datum ldbm_fetch( LDBM ldbm, Datum key );
int ldbm_store( LDBM ldbm, Datum key, Datum data, int flags );
int ldbm_delete( LDBM ldbm, Datum key );
Datum ldbm_firstkey( LDBM ldbm );
Datum ldbm_nextkey( LDBM ldbm, Datum key );
int ldbm_errno( LDBM ldbm );
#if LDBM_USE_DB2
void *ldbm_malloc( size_t size );
Datum ldbm_firstkey( LDBM ldbm, DBC **dbch );
Datum ldbm_nextkey( LDBM ldbm, Datum key, DBC *dbcp );
#else
Datum ldbm_firstkey( LDBM ldbm );
Datum ldbm_nextkey( LDBM ldbm, Datum key );
#endif
#endif /* _ldbm_h_ */
/* ldbm.c - ldap dbm compatibility routines */
/* Patched for Berkeley DB version 2.0; /KSp; 98/02/23
*
* - basic implementation; 1998/02/23, /KSp
* - DB_DBT_MALLOC ; 1998/03/22, /KSp
*/
#include <stdio.h>
#include "ldbm.h"
......@@ -121,10 +127,41 @@ ldbm_errno( LDBM ldbm )
* *
*****************************************************************/
#ifdef LDBM_USE_DB2
/*************************************************
* *
* A malloc routine for use with DB_DBT_MALLOC *
* *
*************************************************/
#include <stdlib.h>
void *
ldbm_malloc( size_t size )
{
return( calloc( 1, size ));
}
#endif
LDBM
ldbm_open( char *name, int rw, int mode, int dbcachesize )
{
LDBM ret;
LDBM ret = NULL;
#ifdef LDBM_USE_DB2
DB_INFO dbinfo;
memset( &dbinfo, 0, sizeof( dbinfo ));
dbinfo.db_cachesize = dbcachesize;
dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
dbinfo.db_malloc = ldbm_malloc;
db_open( name, DB_TYPE, rw, mode, NULL, &dbinfo, &ret );
#else
void *info;
BTREEINFO binfo;
HASHINFO hinfo;
......@@ -140,14 +177,22 @@ ldbm_open( char *name, int rw, int mode, int dbcachesize )
} else {
info = NULL;
}
ret = dbopen( name, rw, mode, DB_TYPE, info );
#endif
return( ret );
}
void
ldbm_close( LDBM ldbm )
{
#ifdef LDBM_USE_DB2
(*ldbm->close)( ldbm, 0 );
#else
(*ldbm->close)( ldbm );
#endif
}
void
......@@ -167,6 +212,10 @@ ldbm_datum_dup( LDBM ldbm, Datum data )
{
Datum dup;
#ifdef LDBM_USE_DB2
memset( &dup, 0, sizeof( dup ));
#endif
if ( data.dsize == 0 ) {
dup.dsize = 0;
dup.dptr = NULL;
......@@ -186,9 +235,18 @@ ldbm_fetch( LDBM ldbm, Datum key )
Datum data;
int rc;
#ifdef LDBM_USE_DB2
memset( &data, 0, sizeof( data ));
data.flags = DB_DBT_MALLOC;
if ( (rc = (*ldbm->get)( ldbm, NULL, &key, &data, 0 )) != 0 ) {
if ( data.dptr ) free( data.dptr );
#else
if ( (rc = (*ldbm->get)( ldbm, &key, &data, 0 )) == 0 ) {
data = ldbm_datum_dup( ldbm, data );
} else {
#endif
data.dptr = NULL;
data.dsize = 0;
}
......@@ -201,7 +259,12 @@ ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
{
int rc;
#ifdef LDBM_USE_DB2
rc = (*ldbm->put)( ldbm, NULL, &key, &data, flags & ~LDBM_SYNC );
rc = (-1 ) * rc;
#else
rc = (*ldbm->put)( ldbm, &key, &data, flags & ~LDBM_SYNC );
#endif
if ( flags & LDBM_SYNC )
(*ldbm->sync)( ldbm, 0 );
return( rc );
......@@ -212,38 +275,88 @@ ldbm_delete( LDBM ldbm, Datum key )
{
int rc;
#ifdef LDBM_USE_DB2
rc = (*ldbm->del)( ldbm, NULL, &key, 0 );
rc = (-1 ) * rc;
#else
rc = (*ldbm->del)( ldbm, &key, 0 );
#endif
(*ldbm->sync)( ldbm, 0 );
return( rc );
}
Datum
#ifdef LDBM_USE_DB2
ldbm_firstkey( LDBM ldbm, DBC **dbch )
#else
ldbm_firstkey( LDBM ldbm )
#endif
{
Datum key, data;
int rc;
#ifdef LDBM_USE_DB2
DBC *dbci;
memset( &key, 0, sizeof( key ));
memset( &data, 0, sizeof( data ));
key.flags = data.flags = DB_DBT_MALLOC;
/* acquire a cursor for the DB */
if ( (*ldbm->cursor)( ldbm, NULL, &dbci )) {
return( key );
} else {
*dbch = dbci;
if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
if ( data.dptr ) free( data.dptr );
#else
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_FIRST )) == 0 ) {
key = ldbm_datum_dup( ldbm, key );
#endif
} else {
key.dptr = NULL;
key.dsize = 0;
}
#ifdef LDBM_USE_DB2
}
#endif
return( key );
}
Datum
#ifdef LDBM_USE_DB2
ldbm_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
#else
ldbm_nextkey( LDBM ldbm, Datum key )
#endif
{
Datum data;
int rc;
#ifdef LDBM_USE_DB2
void *oldKey = key.dptr;
memset( &data, 0, sizeof( data ));
data.flags = DB_DBT_MALLOC;
if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
if ( data.dptr ) free( data.dptr );
#else
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_NEXT )) == 0 ) {
key = ldbm_datum_dup( ldbm, key );
#endif
} else {
key.dptr = NULL;
key.dsize = 0;
}
#ifdef LDBM_USE_DB2
if ( oldKey ) free( oldKey );
#endif
return( key );
}
......
......@@ -6,7 +6,13 @@
#include "ldbm.h"
#define DEFAULT_CACHE_SIZE 1000
#define DEFAULT_DBCACHE_SIZE 100000
#ifdef LDBM_USE_DB2
# define DEFAULT_DBCACHE_SIZE (100 * DEFAULT_DB_PAGE_SIZE)
#else
# define DEFAULT_DBCACHE_SIZE 100000
#endif
#define DEFAULT_DB_DIRECTORY "/usr/tmp"
#define DEFAULT_MODE 0600
......
......@@ -176,6 +176,9 @@ ldbm_cache_fetch(
)
{
Datum data;
#ifdef LDBM_USE_DB2
memset( &data, 0, sizeof( data ) );
#endif
pthread_mutex_lock( &db->dbc_mutex );
#ifdef reentrant_database
......@@ -216,6 +219,24 @@ ldbm_cache_store(
}
#endif
#ifdef LDBM_DEBUG
Statslog( LDAP_DEBUG_STATS,
"=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
key.dptr, key.dsize, 0, 0, 0 );
Statslog( LDAP_DEBUG_STATS,
"=> ldbm_cache_store(): key.dptr=0x%08x, data.dptr=0x%0 8x\n",
key.dptr, data.dptr, 0, 0, 0 );
Statslog( LDAP_DEBUG_STATS,
"=> ldbm_cache_store(): data.dptr=%s, data.dsize=%d\n",
data.dptr, data.dsize, 0, 0, 0 );
Statslog( LDAP_DEBUG_STATS,
"=> ldbm_cache_store(): flags=0x%08x\n",
flags, 0, 0, 0, 0 );
#endif /* LDBM_DEBUG */
rc = ldbm_store( db->dbc_db, key, data, flags );
pthread_mutex_unlock( &db->dbc_mutex );
......
......@@ -25,6 +25,11 @@ dn2id_add(
Datum key, data;
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
memset( &data, 0, sizeof( data ) );
#endif
Debug( LDAP_DEBUG_TRACE, "=> dn2id_add( \"%s\", %ld )\n", dn, id, 0 );
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
......@@ -66,6 +71,10 @@ dn2id(
ID id;
Datum key, data;
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
memset( &data, 0, sizeof( data ) );
#endif
dn = strdup( dn );
dn_normalize_case( dn );
......@@ -122,6 +131,10 @@ dn2id_delete(
Datum key;
int rc;
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
#endif
Debug( LDAP_DEBUG_TRACE, "=> dn2id_delete( \"%s\" )\n", dn, 0, 0 );
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
......
......@@ -23,6 +23,11 @@ id2children_add(
IDList *idl;
char buf[20];
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
memset( &data, 0, sizeof( data ) );
#endif
Debug( LDAP_DEBUG_TRACE, "=> id2children_add( %d, %d )\n", p ? p->e_id
: 0, e->e_id, 0 );
......@@ -63,6 +68,10 @@ has_children(
IDList *idl;
char buf[20];
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
#endif
Debug( LDAP_DEBUG_TRACE, "=> has_children( %d )\n", p->e_id , 0, 0 );
if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
......
......@@ -22,6 +22,11 @@ id2entry_add( Backend *be, Entry *e )
Datum key, data;
int len, rc, flags;
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
memset( &data, 0, sizeof( data ) );
#endif
Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %d, \"%s\" )\n", e->e_id,
e->e_dn, 0 );
......@@ -61,6 +66,10 @@ id2entry_delete( Backend *be, Entry *e )
Datum key;
int rc;
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
#endif
Debug( LDAP_DEBUG_TRACE, "=> id2entry_delete( %d, \"%s\" )\n", e->e_id,
e->e_dn, 0 );
......@@ -95,6 +104,11 @@ id2entry( Backend *be, ID id )
Datum key, data;
Entry *e;
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
memset( &data, 0, sizeof( data ) );
#endif
Debug( LDAP_DEBUG_TRACE, "=> id2entry( %ld )\n", id, 0, 0 );
if ( (e = cache_find_entry_id( &li->li_cache, id )) != NULL ) {
......
......@@ -59,6 +59,11 @@ idl_fetch_one(
char *kstr;
int i, nids;
#ifdef LDBM_USE_DB2
memset( &k2, 0, sizeof( k2 ) );
memset( &data, 0, sizeof( data ) );
#endif
/* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
data = ldbm_cache_fetch( db, key );
......@@ -81,6 +86,11 @@ idl_fetch(
char *kstr;
int i, nids;
#ifdef LDBM_USE_DB2
memset( &k2, 0, sizeof( k2 ) );
memset( &data, 0, sizeof( data ) );
#endif
/* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
data = ldbm_cache_fetch( db, key );
......@@ -170,11 +180,20 @@ idl_store(
Datum data;
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
#ifdef LDBM_USE_DB2
memset( &data, 0, sizeof( data ) );
#endif
/* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
data.dptr = (char *) idl;
data.dsize = (2 + idl->b_nmax) * sizeof(ID);
#ifdef LDBM_DEBUG
Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
rc, 0, 0, 0, 0 );
#endif
flags = LDBM_REPLACE;
if( li->li_flush_wrt ) flags |= LDBM_SYNC;
rc = ldbm_cache_store( db, key, data, flags );
......@@ -283,7 +302,16 @@ idl_insert_key(
char *kstr;
Datum k2;
#ifdef LDBM_USE_DB2
memset( &k2, 0, sizeof( k2 ) );
#endif
if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
#ifdef LDBM_DEBUG
Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
0, 0, 0, 0, 0 );
#endif
idl = idl_alloc( 1 );
idl->b_ids[idl->b_nids++] = id;
rc = idl_store( be, db, key, idl );
......
......@@ -105,6 +105,10 @@ index_read(
char *realval, *tmpval;
char buf[BUFSIZ];
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
#endif
prefix = index2prefix( indextype );
Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
type, prefix, val );
......@@ -175,6 +179,10 @@ add_value(
char *realval, *tmpval, *s;
char buf[BUFSIZ];
#ifdef LDBM_USE_DB2
memset( &key, 0, sizeof( key ) );
#endif
prefix = index2prefix( indextype );
Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
......
......@@ -556,6 +556,11 @@ diff_centroids(
int amax, acur, dmax, dcur;
char **vals;
#ifdef LDBM_USE_DB2
DBC *ocursorp;
DBC *ncursorp;
#endif /* LDBM_USE_DB2 */
if ( verbose ) {
printf( "Generating mods for differential %s centroid...", attr );
fflush( stdout );
......@@ -600,8 +605,14 @@ diff_centroids(
olast.dptr = NULL;
nlast.dptr = NULL;
#ifdef LDBM_USE_DB2
for ( okey = ldbm_firstkey( oldbm, &ocursorp ),
nkey = ldbm_firstkey( nldbm, &ncursorp );
okey.dptr != NULL && nkey.dptr != NULL; )
#else
for ( okey = ldbm_firstkey( oldbm ), nkey = ldbm_firstkey( nldbm );
okey.dptr != NULL && nkey.dptr != NULL; )
#endif
{
rc = strcmp( okey.dptr, nkey.dptr );
......@@ -616,8 +627,13 @@ diff_centroids(
}
nlast = nkey;
#ifdef LDBM_USE_DB2
okey = ldbm_nextkey( oldbm, olast, ocursorp );
nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
#else
okey = ldbm_nextkey( oldbm, olast );
nkey = ldbm_nextkey( nldbm, nlast );
#endif
} else if ( rc > 0 ) {
/* new value is not in old centroid - add it */
if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
......@@ -629,7 +645,12 @@ diff_centroids(
ldbm_datum_free( nldbm, nlast );
}
nlast = nkey;
#ifdef LDBM_USE_DB2
nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
#else
nkey = ldbm_nextkey( nldbm, nlast );
#endif
} else {
/* old value is not in new centroid - delete it */
if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
......@@ -641,7 +662,12 @@ diff_centroids(
ldbm_datum_free( oldbm, olast );
}
olast = okey;
#ifdef LDBM_USE_DB2
okey = ldbm_nextkey( oldbm, olast, ocursorp );
#else
okey = ldbm_nextkey( oldbm</