1998-08-09 08:43:13 +08:00
|
|
|
/* index.c - routines for dealing with attribute indexes */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $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"
|
|
|
|
|
2000-09-14 05:51:14 +08:00
|
|
|
static slap_mask_t index_mask(
|
2000-06-05 06:59:38 +08:00
|
|
|
Backend *be,
|
|
|
|
AttributeDescription *desc,
|
|
|
|
char **dbname,
|
2001-11-16 15:45:37 +08:00
|
|
|
struct berval *atname )
|
2000-06-05 06:59:38 +08:00
|
|
|
{
|
|
|
|
AttributeType *at;
|
2000-08-23 05:26:25 +08:00
|
|
|
slap_mask_t mask = 0;
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2001-11-16 15:45:37 +08:00
|
|
|
attr_mask( be->be_private, desc, &mask );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
|
|
|
if( mask ) {
|
2001-11-16 15:45:37 +08:00
|
|
|
*atname = desc->ad_cname;
|
2001-10-22 21:23:05 +08:00
|
|
|
*dbname = desc->ad_cname.bv_val;
|
2000-06-05 06:59:38 +08:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2002-12-14 03:15:20 +08:00
|
|
|
/* If there is a tagging option, did we ever index the base
|
2001-11-16 15:45:37 +08:00
|
|
|
* type? If so, check for mask, otherwise it's not there.
|
|
|
|
*/
|
2002-12-14 03:15:20 +08:00
|
|
|
if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
|
|
|
|
/* has tagging option */
|
2001-11-16 15:45:37 +08:00
|
|
|
attr_mask( be->be_private, desc->ad_type->sat_ad, &mask );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2002-12-14 03:15:20 +08:00
|
|
|
if( mask && ( mask ^ SLAP_INDEX_NOTAGS ) ) {
|
2000-06-05 06:59:38 +08:00
|
|
|
*atname = desc->ad_type->sat_cname;
|
2001-11-16 15:45:37 +08:00
|
|
|
*dbname = desc->ad_type->sat_cname.bv_val;
|
2000-06-05 06:59:38 +08:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see if supertype defined mask for its subtypes */
|
2002-01-14 18:37:08 +08:00
|
|
|
for( at = desc->ad_type->sat_sup; at != NULL ; at = at->sat_sup ) {
|
2001-11-16 15:45:37 +08:00
|
|
|
/* If no AD, we've never indexed this type */
|
|
|
|
if (!at->sat_ad)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
attr_mask( be->be_private, at->sat_ad, &mask );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2002-01-15 04:26:51 +08:00
|
|
|
if( mask && ( mask ^ SLAP_INDEX_NOSUBTYPES ) ) {
|
|
|
|
*atname = at->sat_cname;
|
|
|
|
*dbname = at->sat_cname.bv_val;
|
|
|
|
return mask;
|
|
|
|
}
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-01-14 07:23:23 +08:00
|
|
|
int index_is_indexed(
|
|
|
|
Backend *be,
|
|
|
|
AttributeDescription *desc )
|
|
|
|
{
|
|
|
|
slap_mask_t mask;
|
|
|
|
char *dbname;
|
|
|
|
struct berval prefix;
|
|
|
|
|
|
|
|
mask = index_mask( be, desc, &dbname, &prefix );
|
|
|
|
|
|
|
|
if( mask == 0 ) {
|
|
|
|
return LDAP_INAPPROPRIATE_MATCHING;
|
|
|
|
}
|
|
|
|
|
2002-01-15 15:29:15 +08:00
|
|
|
return LDAP_SUCCESS;
|
2002-01-14 07:23:23 +08:00
|
|
|
}
|
|
|
|
|
2000-06-05 06:59:38 +08:00
|
|
|
int index_param(
|
|
|
|
Backend *be,
|
|
|
|
AttributeDescription *desc,
|
|
|
|
int ftype,
|
|
|
|
char **dbnamep,
|
2000-08-23 05:26:25 +08:00
|
|
|
slap_mask_t *maskp,
|
2001-11-16 15:45:37 +08:00
|
|
|
struct berval *prefixp )
|
2000-06-05 06:59:38 +08:00
|
|
|
{
|
2000-08-23 05:26:25 +08:00
|
|
|
slap_mask_t mask;
|
2000-06-05 06:59:38 +08:00
|
|
|
char *dbname;
|
|
|
|
|
2001-11-16 15:45:37 +08:00
|
|
|
mask = index_mask( be, desc, &dbname, prefixp );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
|
|
|
if( mask == 0 ) {
|
|
|
|
return LDAP_INAPPROPRIATE_MATCHING;
|
|
|
|
}
|
|
|
|
|
2001-12-05 08:06:03 +08:00
|
|
|
switch( ftype ) {
|
2000-06-05 06:59:38 +08:00
|
|
|
case LDAP_FILTER_PRESENT:
|
|
|
|
if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_FILTER_APPROX:
|
|
|
|
if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* fall thru */
|
|
|
|
|
|
|
|
case LDAP_FILTER_EQUALITY:
|
|
|
|
if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_FILTER_SUBSTRINGS:
|
|
|
|
if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return LDAP_OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LDAP_INAPPROPRIATE_MATCHING;
|
|
|
|
|
|
|
|
done:
|
|
|
|
*dbnamep = dbname;
|
|
|
|
*maskp = mask;
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int indexer(
|
|
|
|
Backend *be,
|
|
|
|
char *dbname,
|
2001-11-16 15:45:37 +08:00
|
|
|
struct berval *atname,
|
2002-01-14 09:43:17 +08:00
|
|
|
BerVarray vals,
|
2000-06-05 06:59:38 +08:00
|
|
|
ID id,
|
|
|
|
int op,
|
2000-08-23 05:26:25 +08:00
|
|
|
slap_mask_t mask )
|
2000-06-05 06:59:38 +08:00
|
|
|
{
|
|
|
|
int rc, i;
|
|
|
|
const char *text;
|
|
|
|
DBCache *db;
|
|
|
|
AttributeDescription *ad = NULL;
|
2001-12-29 23:01:10 +08:00
|
|
|
struct berval *keys;
|
2000-06-05 06:59:38 +08:00
|
|
|
|
|
|
|
assert( mask );
|
|
|
|
|
2001-11-16 15:45:37 +08:00
|
|
|
rc = slap_bv2ad( atname, &ad, &text );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
|
|
|
if( rc != LDAP_SUCCESS ) return rc;
|
|
|
|
|
|
|
|
db = ldbm_cache_open( be, dbname, LDBM_SUFFIX, LDBM_WRCREAT );
|
|
|
|
|
|
|
|
if ( db == NULL ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, ERR,
|
|
|
|
"index_read: Could not open db %s%s\n", dbname, LDBM_SUFFIX, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
2000-06-05 06:59:38 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"<= index_read NULL (could not open %s%s)\n",
|
|
|
|
dbname, LDBM_SUFFIX, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
2000-06-05 06:59:38 +08:00
|
|
|
return LDAP_OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
|
2001-11-16 15:45:37 +08:00
|
|
|
key_change( be, db, atname, id, op );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
|
|
|
|
rc = ad->ad_type->sat_equality->smr_indexer(
|
2000-08-23 05:26:25 +08:00
|
|
|
LDAP_FILTER_EQUALITY,
|
2000-06-05 06:59:38 +08:00
|
|
|
mask,
|
|
|
|
ad->ad_type->sat_syntax,
|
|
|
|
ad->ad_type->sat_equality,
|
2001-11-16 15:45:37 +08:00
|
|
|
atname, vals, &keys );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2000-07-25 09:24:47 +08:00
|
|
|
if( rc == LDAP_SUCCESS && keys != NULL ) {
|
2001-12-29 23:01:10 +08:00
|
|
|
for( i=0; keys[i].bv_val != NULL; i++ ) {
|
|
|
|
key_change( be, db, &keys[i], id, op );
|
2001-01-18 01:01:19 +08:00
|
|
|
}
|
2002-01-14 09:43:17 +08:00
|
|
|
ber_bvarray_free( keys );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
|
|
|
|
rc = ad->ad_type->sat_approx->smr_indexer(
|
2000-08-23 05:26:25 +08:00
|
|
|
LDAP_FILTER_APPROX,
|
2000-06-05 06:59:38 +08:00
|
|
|
mask,
|
|
|
|
ad->ad_type->sat_syntax,
|
|
|
|
ad->ad_type->sat_approx,
|
2001-11-16 15:45:37 +08:00
|
|
|
atname, vals, &keys );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2000-07-25 09:24:47 +08:00
|
|
|
if( rc == LDAP_SUCCESS && keys != NULL ) {
|
2001-12-29 23:01:10 +08:00
|
|
|
for( i=0; keys[i].bv_val != NULL; i++ ) {
|
|
|
|
key_change( be, db, &keys[i], id, op );
|
2001-01-18 01:01:19 +08:00
|
|
|
}
|
2002-01-14 09:43:17 +08:00
|
|
|
ber_bvarray_free( keys );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
|
|
|
|
rc = ad->ad_type->sat_substr->smr_indexer(
|
2000-08-23 05:26:25 +08:00
|
|
|
LDAP_FILTER_SUBSTRINGS,
|
2000-06-05 06:59:38 +08:00
|
|
|
mask,
|
|
|
|
ad->ad_type->sat_syntax,
|
|
|
|
ad->ad_type->sat_substr,
|
2001-11-16 15:45:37 +08:00
|
|
|
atname, vals, &keys );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2000-07-25 09:24:47 +08:00
|
|
|
if( rc == LDAP_SUCCESS && keys != NULL ) {
|
2001-12-29 23:01:10 +08:00
|
|
|
for( i=0; keys[i].bv_val != NULL; i++ ) {
|
|
|
|
key_change( be, db, &keys[i], id, op );
|
2001-01-18 01:01:19 +08:00
|
|
|
}
|
2002-01-14 09:43:17 +08:00
|
|
|
ber_bvarray_free( keys );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ldbm_cache_close( be, db );
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int index_at_values(
|
|
|
|
Backend *be,
|
|
|
|
AttributeType *type,
|
2002-12-14 03:15:20 +08:00
|
|
|
struct berval *tags,
|
2002-01-14 09:43:17 +08:00
|
|
|
BerVarray vals,
|
2000-06-05 06:59:38 +08:00
|
|
|
ID id,
|
2002-01-15 09:50:09 +08:00
|
|
|
int op )
|
2000-06-05 06:59:38 +08:00
|
|
|
{
|
2001-11-16 15:45:37 +08:00
|
|
|
slap_mask_t mask = 0;
|
2000-06-05 06:59:38 +08:00
|
|
|
|
|
|
|
if( type->sat_sup ) {
|
|
|
|
/* recurse */
|
|
|
|
(void) index_at_values( be,
|
2002-12-14 03:15:20 +08:00
|
|
|
type->sat_sup, tags,
|
2002-01-15 09:50:09 +08:00
|
|
|
vals, id, op );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
|
2001-11-16 15:45:37 +08:00
|
|
|
/* If this type has no AD, we've never used it before */
|
2001-12-05 08:06:03 +08:00
|
|
|
if( type->sat_ad ) {
|
2001-11-16 15:45:37 +08:00
|
|
|
attr_mask( be->be_private, type->sat_ad, &mask );
|
2001-12-05 08:06:03 +08:00
|
|
|
}
|
2000-06-05 06:59:38 +08:00
|
|
|
|
|
|
|
if( mask ) {
|
2002-01-15 09:50:09 +08:00
|
|
|
indexer( be, type->sat_cname.bv_val,
|
2001-11-16 15:45:37 +08:00
|
|
|
&type->sat_cname,
|
2000-06-05 06:59:38 +08:00
|
|
|
vals, id, op,
|
|
|
|
mask );
|
|
|
|
}
|
2000-02-05 13:01:41 +08:00
|
|
|
|
2002-12-14 03:15:20 +08:00
|
|
|
if( tags->bv_len ) {
|
2001-11-16 15:45:37 +08:00
|
|
|
AttributeDescription *desc;
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2002-01-15 09:50:09 +08:00
|
|
|
mask = 0;
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2002-12-14 03:15:20 +08:00
|
|
|
desc = ad_find_tags(type, tags);
|
2001-12-05 08:06:03 +08:00
|
|
|
if( desc ) {
|
2002-01-15 09:50:09 +08:00
|
|
|
attr_mask( be->be_private, desc, &mask );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
|
2002-01-15 09:50:09 +08:00
|
|
|
if( mask ) {
|
|
|
|
indexer( be, desc->ad_cname.bv_val, &desc->ad_cname,
|
2000-06-05 06:59:38 +08:00
|
|
|
vals, id, op,
|
2001-11-16 15:45:37 +08:00
|
|
|
mask );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index_values(
|
|
|
|
Backend *be,
|
|
|
|
AttributeDescription *desc,
|
2002-01-14 09:43:17 +08:00
|
|
|
BerVarray vals,
|
2000-06-05 06:59:38 +08:00
|
|
|
ID id,
|
|
|
|
int op )
|
|
|
|
{
|
|
|
|
(void) index_at_values( be,
|
2002-12-14 03:15:20 +08:00
|
|
|
desc->ad_type, &desc->ad_tags,
|
2002-01-15 09:50:09 +08:00
|
|
|
vals, id, op );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
int
|
2000-06-05 06:59:38 +08:00
|
|
|
index_entry(
|
1998-08-09 08:43:13 +08:00
|
|
|
Backend *be,
|
2000-06-05 06:59:38 +08:00
|
|
|
int op,
|
2001-12-05 08:06:03 +08:00
|
|
|
Entry *e,
|
|
|
|
Attribute *ap )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, ENTRY,
|
|
|
|
"index_entry: %s (%s)%ld\n", op == SLAP_INDEX_ADD_OP ? "add" : "del",
|
|
|
|
e->e_dn, e->e_id );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
2000-06-05 06:59:38 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
|
|
|
|
op == SLAP_INDEX_ADD_OP ? "add" : "del",
|
|
|
|
e->e_id, e->e_dn );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* add each attribute to the indexes */
|
2000-09-14 05:51:14 +08:00
|
|
|
for ( ; ap != NULL; ap = ap->a_next ) {
|
2000-06-05 06:59:38 +08:00
|
|
|
index_values( be, ap->a_desc, ap->a_vals, e->e_id, op );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, ENTRY, "index_entry: success\n", 0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
2000-06-05 06:59:38 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
|
|
|
|
op == SLAP_INDEX_ADD_OP ? "add" : "del",
|
|
|
|
e->e_id, e->e_dn );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
2000-06-05 06:59:38 +08:00
|
|
|
return LDAP_SUCCESS;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|