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

432 lines
8.9 KiB
C
Raw Normal View History

1998-08-09 08:43:13 +08:00
/* dn2id.c - routines to deal with the dn2id index */
/* $OpenLDAP$ */
1999-08-07 07:07:46 +08:00
/*
2002-01-05 05:17:25 +08:00
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
1999-08-07 07:07:46 +08:00
* 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
#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,
2002-01-01 21:31:20 +08:00
struct berval *dn,
1998-08-09 08:43:13 +08:00
ID id
)
{
int rc, flags;
DBCache *db;
1998-08-09 08:43:13 +08:00
Datum key, data;
char *buf, *ptr, *pdn;
1998-08-09 08:43:13 +08:00
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
2002-01-01 21:31:20 +08:00
"dn2id_add: (%s):%ld\n", dn->bv_val, id ));
2001-01-18 01:01:19 +08:00
#else
2002-01-01 21:31:20 +08:00
Debug( LDAP_DEBUG_TRACE, "=> dn2id_add( \"%s\", %ld )\n", dn->bv_val, id, 0 );
2001-01-18 01:01:19 +08:00
#endif
assert( id != NOID );
1998-08-09 08:43:13 +08:00
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
"dn2id_add: couldn't open/create dn2id%s\n", LDBM_SUFFIX ));
#else
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_ANY, "Could not open/create dn2id%s\n",
LDBM_SUFFIX, 0, 0 );
2001-01-18 01:01:19 +08:00
#endif
1998-08-09 08:43:13 +08:00
return( -1 );
}
ldbm_datum_init( key );
2002-01-01 21:31:20 +08:00
key.dsize = dn->bv_len + 2;
buf = ch_malloc( key.dsize );
key.dptr = buf;
buf[0] = DN_BASE_PREFIX;
ptr = buf + 1;
2002-01-01 21:31:20 +08:00
strcpy( ptr, dn->bv_val );
ldbm_datum_init( data );
1998-08-09 08:43:13 +08:00
data.dptr = (char *) &id;
data.dsize = sizeof(ID);
flags = LDBM_INSERT;
rc = ldbm_cache_store( db, key, data, flags );
1998-08-09 08:43:13 +08:00
if ( rc != -1 && !be_issuffix( be, ptr )) {
buf[0] = DN_SUBTREE_PREFIX;
ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
rc = idl_insert_key( be, db, key, id );
ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
}
if ( rc != -1 ) {
pdn = dn_parent( be, ptr );
if( pdn != NULL ) {
pdn[-1] = DN_ONE_PREFIX;
key.dsize -= pdn - ptr;
key.dptr = pdn - 1;
ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
rc = idl_insert_key( be, db, key, id );
ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
}
}
while ( rc != -1 && pdn != NULL ) {
if ( be_issuffix( be, pdn ))
break;
pdn[-1] = DN_SUBTREE_PREFIX;
ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
rc = idl_insert_key( be, db, key, id );
ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
if( rc != 0 ) break;
ptr = pdn;
pdn = dn_parent( be, pdn );
key.dsize -= pdn - ptr;
key.dptr = pdn - 1;
}
free( buf );
ldbm_cache_close( be, db );
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
"dn2id_add: return %d\n", rc ));
#else
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_TRACE, "<= dn2id_add %d\n", rc, 0, 0 );
2001-01-18 01:01:19 +08:00
#endif
1998-08-09 08:43:13 +08:00
return( rc );
}
int
1998-08-09 08:43:13 +08:00
dn2id(
Backend *be,
2002-01-01 21:31:20 +08:00
struct berval *dn,
ID *idp
1998-08-09 08:43:13 +08:00
)
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
DBCache *db;
1998-08-09 08:43:13 +08:00
Datum key, data;
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
2002-01-01 21:31:20 +08:00
"dn2id: (%s)\n", dn->bv_val ));
2001-01-18 01:01:19 +08:00
#else
2002-01-01 21:31:20 +08:00
Debug( LDAP_DEBUG_TRACE, "=> dn2id( \"%s\" )\n", dn->bv_val, 0, 0 );
2001-01-18 01:01:19 +08:00
#endif
assert( idp );
1998-08-09 08:43:13 +08:00
/* first check the cache */
if ( (*idp = cache_find_entry_ndn2id( be, &li->li_cache, dn )) != NOID ) {
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
"dn2id: (%s)%ld in cache.\n", dn, *idp ));
2001-01-18 01:01:19 +08:00
#else
Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld (in cache)\n", *idp,
0, 0 );
2001-01-18 01:01:19 +08:00
#endif
return( 0 );
1998-08-09 08:43:13 +08:00
}
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
"dn2id: couldn't open dn2id%s\n", LDBM_SUFFIX ));
#else
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_ANY, "<= dn2id could not open dn2id%s\n",
LDBM_SUFFIX, 0, 0 );
2001-01-18 01:01:19 +08:00
#endif
/*
* return code !0 if ldbm cache open failed;
* callers should handle this
*/
*idp = NOID;
return( -1 );
1998-08-09 08:43:13 +08:00
}
ldbm_datum_init( key );
2002-01-01 21:31:20 +08:00
key.dsize = dn->bv_len + 2;
key.dptr = ch_malloc( key.dsize );
2002-01-01 21:31:20 +08:00
sprintf( key.dptr, "%c%s", DN_BASE_PREFIX, dn->bv_val );
1998-08-09 08:43:13 +08:00
data = ldbm_cache_fetch( db, key );
ldbm_cache_close( be, db );
free( key.dptr );
1998-08-09 08:43:13 +08:00
if ( data.dptr == NULL ) {
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
"dn2id: (%s) NOID\n", dn ));
#else
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_TRACE, "<= dn2id NOID\n", 0, 0, 0 );
2001-01-18 01:01:19 +08:00
#endif
*idp = NOID;
return( 0 );
1998-08-09 08:43:13 +08:00
}
AC_MEMCPY( (char *) idp, data.dptr, sizeof(ID) );
1998-08-09 08:43:13 +08:00
assert( *idp != NOID );
1998-08-09 08:43:13 +08:00
ldbm_datum_free( db->dbc_db, data );
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
"dn2id: %ld\n", *idp ));
2001-01-18 01:01:19 +08:00
#else
Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld\n", *idp, 0, 0 );
2001-01-18 01:01:19 +08:00
#endif
return( 0 );
1998-08-09 08:43:13 +08:00
}
int
dn2idl(
Backend *be,
2001-12-27 07:37:59 +08:00
struct berval *dn,
int prefix,
ID_BLOCK **idlp
)
{
DBCache *db;
Datum key;
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
2001-12-27 07:37:59 +08:00
"dn2idl: \"%c%s\"\n", prefix, dn->bv_val ));
2001-01-18 01:01:19 +08:00
#else
2001-12-27 07:37:59 +08:00
Debug( LDAP_DEBUG_TRACE, "=> dn2idl( \"%c%s\" )\n", prefix, dn->bv_val, 0 );
2001-01-18 01:01:19 +08:00
#endif
assert( idlp != NULL );
*idlp = NULL;
2001-12-27 07:37:59 +08:00
if ( prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val) ) {
2002-01-02 19:00:36 +08:00
*idlp = ch_malloc( 2*sizeof(ID) );
(*idlp)[0] = ID_BLOCK_ALLIDS_VALUE;
(*idlp)[1] = 0;
return 0;
}
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
"dn2idl: could not open dn2id%s\n", LDBM_SUFFIX ));
#else
Debug( LDAP_DEBUG_ANY, "<= dn2idl could not open dn2id%s\n",
LDBM_SUFFIX, 0, 0 );
2001-01-18 01:01:19 +08:00
#endif
return -1;
}
ldbm_datum_init( key );
2001-12-27 07:37:59 +08:00
key.dsize = dn->bv_len + 2;
key.dptr = ch_malloc( key.dsize );
2001-12-27 07:37:59 +08:00
sprintf( key.dptr, "%c%s", prefix, dn->bv_val );
*idlp = idl_fetch( be, db, key );
ldbm_cache_close( be, db );
free( key.dptr );
return( 0 );
}
1998-08-09 08:43:13 +08:00
int
dn2id_delete(
Backend *be,
2002-01-01 21:31:20 +08:00
struct berval *dn,
ID id
1998-08-09 08:43:13 +08:00
)
{
DBCache *db;
1998-08-09 08:43:13 +08:00
Datum key;
int rc;
char *buf, *ptr, *pdn;
1998-08-09 08:43:13 +08:00
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
2002-01-01 21:31:20 +08:00
"dn2id_delete: (%s)%ld\n", dn->bv_val, id ));
2001-01-18 01:01:19 +08:00
#else
2002-01-01 21:31:20 +08:00
Debug( LDAP_DEBUG_TRACE, "=> dn2id_delete( \"%s\", %ld )\n", dn->bv_val, id, 0 );
2001-01-18 01:01:19 +08:00
#endif
1998-08-09 08:43:13 +08:00
assert( id != NOID );
1998-08-09 08:43:13 +08:00
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
== NULL ) {
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
"dn2id_delete: couldn't open db2id%s\n", LDBM_SUFFIX ));
#else
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_ANY,
"<= dn2id_delete could not open dn2id%s\n", LDBM_SUFFIX,
0, 0 );
2001-01-18 01:01:19 +08:00
#endif
1998-08-09 08:43:13 +08:00
return( -1 );
}
ldbm_datum_init( key );
2002-01-01 21:31:20 +08:00
key.dsize = dn->bv_len + 2;
buf = ch_malloc( key.dsize );
key.dptr = buf;
buf[0] = DN_BASE_PREFIX;
ptr = buf + 1;
2002-01-01 21:31:20 +08:00
strcpy( ptr, dn->bv_val );
rc = ldbm_cache_delete( db, key );
if( !be_issuffix( be, ptr )) {
buf[0] = DN_SUBTREE_PREFIX;
ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
(void) idl_delete_key( be, db, key, id );
ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
}
pdn = dn_parent( be, ptr );
if( pdn != NULL ) {
pdn[-1] = DN_ONE_PREFIX;
key.dsize -= pdn - ptr;
key.dptr = pdn - 1;
ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
(void) idl_delete_key( be, db, key, id );
ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
}
while ( pdn != NULL ) {
if ( be_issuffix( be, pdn ))
break;
pdn[-1] = DN_SUBTREE_PREFIX;
ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex );
(void) idl_delete_key( be, db, key, id );
ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex );
ptr = pdn;
pdn = dn_parent( be, pdn );
key.dsize -= pdn - ptr;
key.dptr = pdn - 1;
}
free( buf );
1999-01-19 04:49:07 +08:00
1998-08-09 08:43:13 +08:00
ldbm_cache_close( be, db );
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
"dn2id_delete: return %d\n", rc ));
#else
1998-08-09 08:43:13 +08:00
Debug( LDAP_DEBUG_TRACE, "<= dn2id_delete %d\n", rc, 0, 0 );
2001-01-18 01:01:19 +08:00
#endif
1998-08-09 08:43:13 +08:00
return( rc );
}
/*
* dn2entry - look up dn in the cache/indexes and return the corresponding
* entry.
*/
Entry *
dn2entry_rw(
1998-08-09 08:43:13 +08:00
Backend *be,
2002-01-01 21:31:20 +08:00
struct berval *dn,
Entry **matched,
2001-01-18 01:01:19 +08:00
int rw
1998-08-09 08:43:13 +08:00
)
{
ID id;
Entry *e = NULL;
2002-01-01 21:31:20 +08:00
struct berval pdn;
1998-08-09 08:43:13 +08:00
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
"dn2entry_rw: %s entry %s\n", rw ? "w" : "r",
2002-01-01 21:31:20 +08:00
dn->bv_val ));
2001-01-18 01:01:19 +08:00
#else
1999-01-19 01:33:36 +08:00
Debug(LDAP_DEBUG_TRACE, "dn2entry_%s: dn: \"%s\"\n",
2002-01-01 21:31:20 +08:00
rw ? "w" : "r", dn->bv_val, 0);
2001-01-18 01:01:19 +08:00
#endif
if( matched != NULL ) {
/* caller cares about match */
*matched = NULL;
}
if ( dn2id( be, dn, &id ) ) {
/* something bad happened to ldbm cache */
return( NULL );
} else if ( id != NOID ) {
/* try to return the entry */
if ((e = id2entry_rw( be, id, rw )) != NULL ) {
return( e );
}
1998-08-09 08:43:13 +08:00
2001-01-18 01:01:19 +08:00
#ifdef NEW_LOGGING
LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
"dn2entry_rw: no entry for valid id (%ld), dn (%s)\n",
2002-01-01 21:31:20 +08:00
id, dn->bv_val ));
2001-01-18 01:01:19 +08:00
#else
Debug(LDAP_DEBUG_ANY,
"dn2entry_%s: no entry for valid id (%ld), dn \"%s\"\n",
2002-01-01 21:31:20 +08:00
rw ? "w" : "r", id, dn->bv_val);
2001-01-18 01:01:19 +08:00
#endif
/* must have been deleted from underneath us */
/* treat as if NOID was found */
}
/* caller doesn't care about match */
if( matched == NULL ) return NULL;
1998-08-09 08:43:13 +08:00
/* entry does not exist - see how much of the dn does exist */
/* dn_parent checks returns NULL if dn is suffix */
2002-01-01 21:31:20 +08:00
if ( (pdn.bv_val = dn_parent( be, dn->bv_val )) != NULL && *pdn.bv_val ) {
pdn.bv_len = dn->bv_len - (pdn.bv_val - dn->bv_val);
/* get entry with reader lock */
2002-01-01 21:31:20 +08:00
if ( (e = dn2entry_r( be, &pdn, matched )) != NULL ) {
*matched = e;
1998-08-09 08:43:13 +08:00
}
}
return NULL;
1998-08-09 08:43:13 +08:00
}