Home | History | Annotate | Line # | Download | only in back-wt
index.c revision 1.1
      1  1.1  christos /*	$NetBSD: index.c,v 1.1 2021/08/14 16:05:24 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /* OpenLDAP WiredTiger backend */
      4  1.1  christos /* $OpenLDAP$ */
      5  1.1  christos /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  1.1  christos  *
      7  1.1  christos  * Copyright 2002-2021 The OpenLDAP Foundation.
      8  1.1  christos  * All rights reserved.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted only as authorized by the OpenLDAP
     12  1.1  christos  * Public License.
     13  1.1  christos  *
     14  1.1  christos  * A copy of this license is available in the file LICENSE in the
     15  1.1  christos  * top-level directory of the distribution or, alternatively, at
     16  1.1  christos  * <http://www.OpenLDAP.org/license.html>.
     17  1.1  christos  */
     18  1.1  christos /* ACKNOWLEDGEMENTS:
     19  1.1  christos  * This work was developed by HAMANO Tsukasa <hamano (at) osstech.co.jp>
     20  1.1  christos  * based on back-bdb for inclusion in OpenLDAP Software.
     21  1.1  christos  * WiredTiger is a product of MongoDB Inc.
     22  1.1  christos  */
     23  1.1  christos 
     24  1.1  christos #include <sys/cdefs.h>
     25  1.1  christos __RCSID("$NetBSD: index.c,v 1.1 2021/08/14 16:05:24 christos Exp $");
     26  1.1  christos 
     27  1.1  christos #include "portable.h"
     28  1.1  christos 
     29  1.1  christos #include <stdio.h>
     30  1.1  christos #include "back-wt.h"
     31  1.1  christos #include "slap-config.h"
     32  1.1  christos 
     33  1.1  christos static char presence_keyval[] = {0,0};
     34  1.1  christos static struct berval presence_key = BER_BVC(presence_keyval);
     35  1.1  christos 
     36  1.1  christos AttrInfo *wt_index_mask(
     37  1.1  christos 	Backend *be,
     38  1.1  christos 	AttributeDescription *desc,
     39  1.1  christos 	struct berval *atname )
     40  1.1  christos {
     41  1.1  christos 	AttributeType *at;
     42  1.1  christos 	AttrInfo *ai = wt_attr_mask( be->be_private, desc );
     43  1.1  christos 
     44  1.1  christos 	if( ai ) {
     45  1.1  christos 		*atname = desc->ad_cname;
     46  1.1  christos 		return ai;
     47  1.1  christos 	}
     48  1.1  christos 
     49  1.1  christos 	/* If there is a tagging option, did we ever index the base
     50  1.1  christos 	 * type? If so, check for mask, otherwise it's not there.
     51  1.1  christos 	 */
     52  1.1  christos 	if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
     53  1.1  christos 		/* has tagging option */
     54  1.1  christos 		ai = wt_attr_mask( be->be_private, desc->ad_type->sat_ad );
     55  1.1  christos 
     56  1.1  christos 		if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOTAGS ) ) {
     57  1.1  christos 			*atname = desc->ad_type->sat_cname;
     58  1.1  christos 			return ai;
     59  1.1  christos 		}
     60  1.1  christos 	}
     61  1.1  christos 
     62  1.1  christos 	/* see if supertype defined mask for its subtypes */
     63  1.1  christos 	for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
     64  1.1  christos 		/* If no AD, we've never indexed this type */
     65  1.1  christos 		if ( !at->sat_ad ) continue;
     66  1.1  christos 
     67  1.1  christos 		ai = wt_attr_mask( be->be_private, at->sat_ad );
     68  1.1  christos 
     69  1.1  christos 		if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOSUBTYPES ) ) {
     70  1.1  christos 			*atname = at->sat_cname;
     71  1.1  christos 			return ai;
     72  1.1  christos 		}
     73  1.1  christos 	}
     74  1.1  christos 
     75  1.1  christos 	return 0;
     76  1.1  christos }
     77  1.1  christos 
     78  1.1  christos /* This function is only called when evaluating search filters.
     79  1.1  christos  */
     80  1.1  christos int wt_index_param(
     81  1.1  christos 	Backend *be,
     82  1.1  christos 	AttributeDescription *desc,
     83  1.1  christos 	int ftype,
     84  1.1  christos 	slap_mask_t *maskp,
     85  1.1  christos 	struct berval *prefixp )
     86  1.1  christos {
     87  1.1  christos 	AttrInfo *ai;
     88  1.1  christos 	int rc;
     89  1.1  christos 	slap_mask_t mask, type = 0;
     90  1.1  christos 
     91  1.1  christos 	ai = wt_index_mask( be, desc, prefixp );
     92  1.1  christos 
     93  1.1  christos 	if ( !ai ) {
     94  1.1  christos 		/* TODO: add monitor */
     95  1.1  christos 		return LDAP_INAPPROPRIATE_MATCHING;
     96  1.1  christos 	}
     97  1.1  christos 	mask = ai->ai_indexmask;
     98  1.1  christos 
     99  1.1  christos 	switch( ftype ) {
    100  1.1  christos 	case LDAP_FILTER_PRESENT:
    101  1.1  christos 		type = SLAP_INDEX_PRESENT;
    102  1.1  christos 		if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
    103  1.1  christos 			*prefixp = presence_key;
    104  1.1  christos 			*maskp = mask;
    105  1.1  christos 			return LDAP_SUCCESS;
    106  1.1  christos 		}
    107  1.1  christos 		break;
    108  1.1  christos 
    109  1.1  christos 	case LDAP_FILTER_APPROX:
    110  1.1  christos 		type = SLAP_INDEX_APPROX;
    111  1.1  christos 		if ( desc->ad_type->sat_approx ) {
    112  1.1  christos 			if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
    113  1.1  christos 				*maskp = mask;
    114  1.1  christos 				return LDAP_SUCCESS;
    115  1.1  christos 			}
    116  1.1  christos 			break;
    117  1.1  christos 		}
    118  1.1  christos 
    119  1.1  christos 		/* Use EQUALITY rule and index for approximate match */
    120  1.1  christos 		/* fall thru */
    121  1.1  christos 
    122  1.1  christos 	case LDAP_FILTER_EQUALITY:
    123  1.1  christos 		type = SLAP_INDEX_EQUALITY;
    124  1.1  christos 		if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
    125  1.1  christos 			*maskp = mask;
    126  1.1  christos 			return LDAP_SUCCESS;
    127  1.1  christos 		}
    128  1.1  christos 		break;
    129  1.1  christos 
    130  1.1  christos 	case LDAP_FILTER_SUBSTRINGS:
    131  1.1  christos 		type = SLAP_INDEX_SUBSTR;
    132  1.1  christos 		if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
    133  1.1  christos 			*maskp = mask;
    134  1.1  christos 			return LDAP_SUCCESS;
    135  1.1  christos 		}
    136  1.1  christos 		break;
    137  1.1  christos 
    138  1.1  christos 	default:
    139  1.1  christos 		return LDAP_OTHER;
    140  1.1  christos 	}
    141  1.1  christos 
    142  1.1  christos 	/* TODO: add monitor index */
    143  1.1  christos 	return LDAP_INAPPROPRIATE_MATCHING;
    144  1.1  christos }
    145  1.1  christos 
    146  1.1  christos static int indexer(
    147  1.1  christos 	Operation *op,
    148  1.1  christos 	wt_ctx *wc,
    149  1.1  christos 	AttributeDescription *ad,
    150  1.1  christos 	struct berval *atname,
    151  1.1  christos 	BerVarray vals,
    152  1.1  christos 	ID id,
    153  1.1  christos 	int opid,
    154  1.1  christos 	slap_mask_t mask )
    155  1.1  christos {
    156  1.1  christos 	int rc, i;
    157  1.1  christos 	struct berval *keys;
    158  1.1  christos 	WT_CURSOR *cursor = NULL;
    159  1.1  christos 	WT_SESSION *session = wc->session;
    160  1.1  christos 	assert( mask != 0 );
    161  1.1  christos 
    162  1.1  christos 	cursor = wt_ctx_index_cursor(wc, atname, 1);
    163  1.1  christos 	if( !cursor ) {
    164  1.1  christos 		Debug( LDAP_DEBUG_ANY,
    165  1.1  christos 			   LDAP_XSTRING(indexer)
    166  1.1  christos 			   ": open index cursor failed: %s\n",
    167  1.1  christos 			   atname->bv_val );
    168  1.1  christos 		goto done;
    169  1.1  christos 	}
    170  1.1  christos 
    171  1.1  christos 	if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
    172  1.1  christos 		rc = wt_key_change( op->o_bd, cursor, &presence_key, id, opid );
    173  1.1  christos 		if( rc ) {
    174  1.1  christos 			goto done;
    175  1.1  christos 		}
    176  1.1  christos 	}
    177  1.1  christos 
    178  1.1  christos 	if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
    179  1.1  christos 		rc = ad->ad_type->sat_equality->smr_indexer(
    180  1.1  christos 			LDAP_FILTER_EQUALITY,
    181  1.1  christos 			mask,
    182  1.1  christos 			ad->ad_type->sat_syntax,
    183  1.1  christos 			ad->ad_type->sat_equality,
    184  1.1  christos 			atname, vals, &keys, op->o_tmpmemctx );
    185  1.1  christos 
    186  1.1  christos 		if( rc == LDAP_SUCCESS && keys != NULL ) {
    187  1.1  christos 			for( i=0; keys[i].bv_val != NULL; i++ ) {
    188  1.1  christos 				rc = wt_key_change( op->o_bd, cursor, &keys[i], id, opid );
    189  1.1  christos 				if( rc ) {
    190  1.1  christos 					ber_bvarray_free_x( keys, op->o_tmpmemctx );
    191  1.1  christos 					goto done;
    192  1.1  christos 				}
    193  1.1  christos 			}
    194  1.1  christos 			ber_bvarray_free_x( keys, op->o_tmpmemctx );
    195  1.1  christos 		}
    196  1.1  christos 		rc = LDAP_SUCCESS;
    197  1.1  christos 	}
    198  1.1  christos 
    199  1.1  christos 	if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
    200  1.1  christos 		rc = ad->ad_type->sat_approx->smr_indexer(
    201  1.1  christos 			LDAP_FILTER_APPROX,
    202  1.1  christos 			mask,
    203  1.1  christos 			ad->ad_type->sat_syntax,
    204  1.1  christos 			ad->ad_type->sat_approx,
    205  1.1  christos 			atname, vals, &keys, op->o_tmpmemctx );
    206  1.1  christos 
    207  1.1  christos 		if( rc == LDAP_SUCCESS && keys != NULL ) {
    208  1.1  christos 			for( i=0; keys[i].bv_val != NULL; i++ ) {
    209  1.1  christos 				rc = wt_key_change( op->o_bd, cursor, &keys[i], id, opid );
    210  1.1  christos 				if( rc ) {
    211  1.1  christos 					ber_bvarray_free_x( keys, op->o_tmpmemctx );
    212  1.1  christos 					goto done;
    213  1.1  christos 				}
    214  1.1  christos 			}
    215  1.1  christos 			ber_bvarray_free_x( keys, op->o_tmpmemctx );
    216  1.1  christos 		}
    217  1.1  christos 
    218  1.1  christos 		rc = LDAP_SUCCESS;
    219  1.1  christos 	}
    220  1.1  christos 
    221  1.1  christos 	if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
    222  1.1  christos 		rc = ad->ad_type->sat_substr->smr_indexer(
    223  1.1  christos 			LDAP_FILTER_SUBSTRINGS,
    224  1.1  christos 			mask,
    225  1.1  christos 			ad->ad_type->sat_syntax,
    226  1.1  christos 			ad->ad_type->sat_substr,
    227  1.1  christos 			atname, vals, &keys, op->o_tmpmemctx );
    228  1.1  christos 
    229  1.1  christos 		if( rc == LDAP_SUCCESS && keys != NULL ) {
    230  1.1  christos 			for( i=0; keys[i].bv_val != NULL; i++ ) {
    231  1.1  christos 				rc = wt_key_change( op->o_bd, cursor, &keys[i], id, opid );
    232  1.1  christos 				if( rc ) {
    233  1.1  christos 					ber_bvarray_free_x( keys, op->o_tmpmemctx );
    234  1.1  christos 					goto done;
    235  1.1  christos 				}
    236  1.1  christos 			}
    237  1.1  christos 			ber_bvarray_free_x( keys, op->o_tmpmemctx );
    238  1.1  christos 		}
    239  1.1  christos 
    240  1.1  christos 		rc = LDAP_SUCCESS;
    241  1.1  christos 	}
    242  1.1  christos 
    243  1.1  christos done:
    244  1.1  christos 	if(cursor){
    245  1.1  christos 		cursor->close(cursor);
    246  1.1  christos 	}
    247  1.1  christos 	return rc;
    248  1.1  christos }
    249  1.1  christos 
    250  1.1  christos static int index_at_values(
    251  1.1  christos 	Operation *op,
    252  1.1  christos 	wt_ctx *wc,
    253  1.1  christos 	AttributeDescription *ad,
    254  1.1  christos 	AttributeType *type,
    255  1.1  christos 	struct berval *tags,
    256  1.1  christos 	BerVarray vals,
    257  1.1  christos 	ID id,
    258  1.1  christos 	int opid )
    259  1.1  christos {
    260  1.1  christos 	int rc;
    261  1.1  christos 	slap_mask_t mask = 0;
    262  1.1  christos 	int ixop = opid;
    263  1.1  christos 	AttrInfo *ai = NULL;
    264  1.1  christos 
    265  1.1  christos 	if ( opid == WT_INDEX_UPDATE_OP )
    266  1.1  christos 		ixop = SLAP_INDEX_ADD_OP;
    267  1.1  christos 
    268  1.1  christos 	if( type->sat_sup ) {
    269  1.1  christos 		/* recurse */
    270  1.1  christos 		rc = index_at_values( op, wc, NULL,
    271  1.1  christos 							  type->sat_sup, tags,
    272  1.1  christos 							  vals, id, opid );
    273  1.1  christos 
    274  1.1  christos 		if( rc ) return rc;
    275  1.1  christos 	}
    276  1.1  christos 
    277  1.1  christos 	/* If this type has no AD, we've never used it before */
    278  1.1  christos 	if( type->sat_ad ) {
    279  1.1  christos 		ai = wt_attr_mask( op->o_bd->be_private, type->sat_ad );
    280  1.1  christos 		if ( ai ) {
    281  1.1  christos 			#ifdef LDAP_COMP_MATCH
    282  1.1  christos 			/* component indexing */
    283  1.1  christos 			if ( ai->ai_cr ) {
    284  1.1  christos 				ComponentReference *cr;
    285  1.1  christos 				for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
    286  1.1  christos 					rc = indexer( op, wc, cr->cr_ad, &type->sat_cname,
    287  1.1  christos 								  cr->cr_nvals, id, ixop,
    288  1.1  christos 								  cr->cr_indexmask );
    289  1.1  christos 				}
    290  1.1  christos 			}
    291  1.1  christos 			#endif
    292  1.1  christos 			ad = type->sat_ad;
    293  1.1  christos 			/* If we're updating the index, just set the new bits that aren't
    294  1.1  christos              * already in the old mask.
    295  1.1  christos              */
    296  1.1  christos 			if ( opid == WT_INDEX_UPDATE_OP )
    297  1.1  christos 				mask = ai->ai_newmask & ~ai->ai_indexmask;
    298  1.1  christos 			else
    299  1.1  christos 				/* For regular updates, if there is a newmask use it. Otherwise
    300  1.1  christos 				 * just use the old mask.
    301  1.1  christos 				 */
    302  1.1  christos 				mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
    303  1.1  christos 			if( mask ) {
    304  1.1  christos 				rc = indexer( op, wc, ad, &type->sat_cname,
    305  1.1  christos 							  vals, id, ixop, mask );
    306  1.1  christos 				if( rc ) return rc;
    307  1.1  christos 			}
    308  1.1  christos 		}
    309  1.1  christos 	}
    310  1.1  christos 
    311  1.1  christos 	if( tags->bv_len ) {
    312  1.1  christos 		AttributeDescription *desc;
    313  1.1  christos 
    314  1.1  christos 		desc = ad_find_tags( type, tags );
    315  1.1  christos 		if( desc ) {
    316  1.1  christos 			ai = wt_attr_mask( op->o_bd->be_private, desc );
    317  1.1  christos 
    318  1.1  christos 			if( ai ) {
    319  1.1  christos 				if ( opid == WT_INDEX_UPDATE_OP )
    320  1.1  christos 					mask = ai->ai_newmask & ~ai->ai_indexmask;
    321  1.1  christos 				else
    322  1.1  christos 					mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
    323  1.1  christos 				if ( mask ) {
    324  1.1  christos 					rc = indexer( op, wc, desc, &desc->ad_cname,
    325  1.1  christos 								  vals, id, ixop, mask );
    326  1.1  christos 
    327  1.1  christos 					if( rc ) {
    328  1.1  christos 						return rc;
    329  1.1  christos 					}
    330  1.1  christos 				}
    331  1.1  christos 			}
    332  1.1  christos 		}
    333  1.1  christos 	}
    334  1.1  christos 
    335  1.1  christos 	return LDAP_SUCCESS;
    336  1.1  christos }
    337  1.1  christos 
    338  1.1  christos int wt_index_values(
    339  1.1  christos 	Operation *op,
    340  1.1  christos 	wt_ctx *wc,
    341  1.1  christos 	AttributeDescription *desc,
    342  1.1  christos 	BerVarray vals,
    343  1.1  christos 	ID id,
    344  1.1  christos 	int opid )
    345  1.1  christos {
    346  1.1  christos 	int rc;
    347  1.1  christos 
    348  1.1  christos 	/* Never index ID 0 */
    349  1.1  christos 	if ( id == 0 )
    350  1.1  christos 		return 0;
    351  1.1  christos 
    352  1.1  christos 	rc = index_at_values( op, wc, desc,
    353  1.1  christos 						  desc->ad_type, &desc->ad_tags,
    354  1.1  christos 						  vals, id, opid );
    355  1.1  christos 
    356  1.1  christos 	return rc;
    357  1.1  christos }
    358  1.1  christos 
    359  1.1  christos int
    360  1.1  christos wt_index_entry( Operation *op, wt_ctx *wc, int opid, Entry *e )
    361  1.1  christos {
    362  1.1  christos 	int rc;
    363  1.1  christos 	Attribute *ap = e->e_attrs;
    364  1.1  christos 
    365  1.1  christos 	if ( e->e_id == 0 )
    366  1.1  christos 		return 0;
    367  1.1  christos 
    368  1.1  christos 	Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
    369  1.1  christos 		   opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
    370  1.1  christos 		   (long) e->e_id, e->e_dn ? e->e_dn : "" );
    371  1.1  christos 
    372  1.1  christos 	for ( ; ap != NULL; ap = ap->a_next ) {
    373  1.1  christos 		rc = wt_index_values( op, wc, ap->a_desc,
    374  1.1  christos 							  ap->a_nvals, e->e_id, opid );
    375  1.1  christos 		if( rc != LDAP_SUCCESS ) {
    376  1.1  christos 			Debug( LDAP_DEBUG_TRACE,
    377  1.1  christos 				   "<= index_entry_%s( %ld, \"%s\" ) failure\n",
    378  1.1  christos 				   opid == SLAP_INDEX_ADD_OP ? "add" : "del",
    379  1.1  christos 				   (long) e->e_id, e->e_dn );
    380  1.1  christos 			return rc;
    381  1.1  christos 		}
    382  1.1  christos 	}
    383  1.1  christos 
    384  1.1  christos 	Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
    385  1.1  christos 		   opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
    386  1.1  christos 		   (long) e->e_id, e->e_dn ? e->e_dn : "" );
    387  1.1  christos 	return 0;
    388  1.1  christos }
    389  1.1  christos 
    390  1.1  christos /*
    391  1.1  christos  * Local variables:
    392  1.1  christos  * indent-tabs-mode: t
    393  1.1  christos  * tab-width: 4
    394  1.1  christos  * c-basic-offset: 4
    395  1.1  christos  * End:
    396  1.1  christos  */
    397