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

225 lines
4.1 KiB
C
Raw Normal View History

1998-08-09 08:43:13 +08:00
/* dn2id.c - routines to deal with the dn2id index */
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
#include <ac/string.h>
#include <ac/socket.h>
1998-08-09 08:43:13 +08:00
#include "slap.h"
#include "back-ldbm.h"
#include "proto-back-ldbm.h"
1998-08-09 08:43:13 +08:00
int
dn2id_add(
Backend *be,
char *dn,
ID id
)
{
int rc, flags;
1998-08-09 08:43:13 +08:00
struct dbcache *db;
Datum key, data;
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
1998-08-09 08:43:13 +08:00
1998-10-25 09:41:42 +08:00
#ifdef HAVE_BERKELEY_DB2
memset( &key, 0, sizeof( key ) );
memset( &data, 0, sizeof( data ) );
#endif
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_TRACE, "=> dn2id_add( \"%s\", %ld )\n", dn, id, 0 );
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
Debug( LDAP_DEBUG_ANY, "Could not open/create dn2id%s\n",
LDBM_SUFFIX, 0, 0 );
return( -1 );
}
dn = ch_strdup( dn );
1998-08-09 08:43:13 +08:00
dn_normalize_case( dn );
key.dptr = dn;
key.dsize = strlen( dn ) + 1;
data.dptr = (char *) &id;
data.dsize = sizeof(ID);
flags = LDBM_INSERT;
if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
rc = ldbm_cache_store( db, key, data, flags );
1998-08-09 08:43:13 +08:00
free( dn );
ldbm_cache_close( be, db );
Debug( LDAP_DEBUG_TRACE, "<= dn2id_add %d\n", rc, 0, 0 );
return( rc );
}
ID
dn2id(
Backend *be,
char *dn
)
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
struct dbcache *db;
ID id;
Datum key, data;
1998-10-25 09:41:42 +08:00
#ifdef HAVE_BERKELEY_DB2
memset( &key, 0, sizeof( key ) );
memset( &data, 0, sizeof( data ) );
#endif
1998-08-09 08:43:13 +08:00
dn = ch_strdup( dn );
1998-08-19 01:51:53 +08:00
Debug( LDAP_DEBUG_TRACE, "=> dn2id( \"%s\" )\n", dn, 0, 0 );
dn_normalize_case( dn );
1998-08-09 08:43:13 +08:00
/* first check the cache */
if ( (id = cache_find_entry_dn2id( be, &li->li_cache, dn )) != NOID ) {
1998-08-09 08:43:13 +08:00
free( dn );
Debug( LDAP_DEBUG_TRACE, "<= dn2id %lu (in cache)\n", id,
0, 0 );
1998-08-09 08:43:13 +08:00
return( id );
}
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
1998-08-09 08:43:13 +08:00
free( dn );
Debug( LDAP_DEBUG_ANY, "<= dn2id could not open dn2id%s\n",
LDBM_SUFFIX, 0, 0 );
1998-08-09 08:43:13 +08:00
return( NOID );
}
key.dptr = dn;
key.dsize = strlen( dn ) + 1;
data = ldbm_cache_fetch( db, key );
ldbm_cache_close( be, db );
free( dn );
if ( data.dptr == NULL ) {
Debug( LDAP_DEBUG_TRACE, "<= dn2id NOID\n", 0, 0, 0 );
return( NOID );
}
(void) memcpy( (char *) &id, data.dptr, sizeof(ID) );
ldbm_datum_free( db->dbc_db, data );
Debug( LDAP_DEBUG_TRACE, "<= dn2id %lu\n", id, 0, 0 );
1998-08-09 08:43:13 +08:00
return( id );
}
int
dn2id_delete(
Backend *be,
char *dn
)
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
struct dbcache *db;
Datum key;
int rc;
1998-10-25 09:41:42 +08:00
#ifdef HAVE_BERKELEY_DB2
memset( &key, 0, sizeof( key ) );
#endif
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_TRACE, "=> dn2id_delete( \"%s\" )\n", dn, 0, 0 );
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
Debug( LDAP_DEBUG_ANY,
"<= dn2id_delete could not open dn2id%s\n", LDBM_SUFFIX,
0, 0 );
return( -1 );
}
dn_normalize_case( dn );
key.dptr = dn;
key.dsize = strlen( dn ) + 1;
rc = ldbm_cache_delete( db, key );
ldbm_cache_close( be, db );
Debug( LDAP_DEBUG_TRACE, "<= dn2id_delete %d\n", rc, 0, 0 );
return( rc );
}
/*
* dn2entry - look up dn in the cache/indexes and return the corresponding
* entry.
*/
static Entry *
1998-08-09 08:43:13 +08:00
dn2entry(
Backend *be,
char *dn,
char **matched,
int rw
1998-08-09 08:43:13 +08:00
)
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
ID id;
Entry *e = NULL;
1998-08-09 08:43:13 +08:00
char *pdn;
Debug(LDAP_DEBUG_TRACE, "dn2entry_%s: dn: %s\n",
rw ? "w" : "r", dn, 0);
*matched = NULL;
if ( (id = dn2id( be, dn )) != NOID &&
(e = id2entry( be, id, rw )) != NULL )
{
1998-08-09 08:43:13 +08:00
return( e );
}
/* stop when we get to the suffix */
if ( be_issuffix( be, dn ) ) {
return( NULL );
}
/* entry does not exist - see how much of the dn does exist */
if ( (pdn = dn_parent( be, dn )) != NULL ) {
/* get entry with reader lock */
if ( (e = dn2entry_r( be, pdn, matched )) != NULL ) {
1998-08-09 08:43:13 +08:00
*matched = pdn;
/* free entry with reader lock */
cache_return_entry_r( &li->li_cache, e );
1998-08-09 08:43:13 +08:00
} else {
free( pdn );
}
}
return( NULL );
}
Entry *
dn2entry_r(
Backend *be,
char *dn,
char **matched
)
{
return( dn2entry( be, dn, matched, 0 ) );
}
Entry *
dn2entry_w(
Backend *be,
char *dn,
char **matched
)
{
return( dn2entry( be, dn, matched, 1 ) );
}