]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/index.c
Use IDL caching in tool/quick mode
[openldap] / servers / slapd / back-mdb / index.c
1 /* index.c - routines for dealing with attribute indexes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2011 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-mdb.h"
26 #include "lutil_hash.h"
27
28 static char presence_keyval[] = {0,0};
29 static struct berval presence_key[2] = {BER_BVC(presence_keyval), BER_BVNULL};
30
31 AttrInfo *mdb_index_mask(
32         Backend *be,
33         AttributeDescription *desc,
34         struct berval *atname )
35 {
36         AttributeType *at;
37         AttrInfo *ai = mdb_attr_mask( be->be_private, desc );
38
39         if( ai ) {
40                 *atname = desc->ad_cname;
41                 return ai;
42         }
43
44         /* If there is a tagging option, did we ever index the base
45          * type? If so, check for mask, otherwise it's not there.
46          */
47         if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
48                 /* has tagging option */
49                 ai = mdb_attr_mask( be->be_private, desc->ad_type->sat_ad );
50
51                 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOTAGS ) ) {
52                         *atname = desc->ad_type->sat_cname;
53                         return ai;
54                 }
55         }
56
57         /* see if supertype defined mask for its subtypes */
58         for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
59                 /* If no AD, we've never indexed this type */
60                 if ( !at->sat_ad ) continue;
61
62                 ai = mdb_attr_mask( be->be_private, at->sat_ad );
63
64                 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOSUBTYPES ) ) {
65                         *atname = at->sat_cname;
66                         return ai;
67                 }
68         }
69
70         return 0;
71 }
72
73 /* This function is only called when evaluating search filters.
74  */
75 int mdb_index_param(
76         Backend *be,
77         AttributeDescription *desc,
78         int ftype,
79         MDB_dbi *dbip,
80         slap_mask_t *maskp,
81         struct berval *prefixp )
82 {
83         AttrInfo *ai;
84         slap_mask_t mask, type = 0;
85
86         ai = mdb_index_mask( be, desc, prefixp );
87
88         if ( !ai ) {
89 #ifdef MDB_MONITOR_IDX
90                 switch ( ftype ) {
91                 case LDAP_FILTER_PRESENT:
92                         type = SLAP_INDEX_PRESENT;
93                         break;
94                 case LDAP_FILTER_APPROX:
95                         type = SLAP_INDEX_APPROX;
96                         break;
97                 case LDAP_FILTER_EQUALITY:
98                         type = SLAP_INDEX_EQUALITY;
99                         break;
100                 case LDAP_FILTER_SUBSTRINGS:
101                         type = SLAP_INDEX_SUBSTR;
102                         break;
103                 default:
104                         return LDAP_INAPPROPRIATE_MATCHING;
105                 }
106                 mdb_monitor_idx_add( be->be_private, desc, type );
107 #endif /* MDB_MONITOR_IDX */
108
109                 return LDAP_INAPPROPRIATE_MATCHING;
110         }
111         mask = ai->ai_indexmask;
112
113         switch( ftype ) {
114         case LDAP_FILTER_PRESENT:
115                 type = SLAP_INDEX_PRESENT;
116                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
117                         *prefixp = presence_key[0];
118                         goto done;
119                 }
120                 break;
121
122         case LDAP_FILTER_APPROX:
123                 type = SLAP_INDEX_APPROX;
124                 if ( desc->ad_type->sat_approx ) {
125                         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
126                                 goto done;
127                         }
128                         break;
129                 }
130
131                 /* Use EQUALITY rule and index for approximate match */
132                 /* fall thru */
133
134         case LDAP_FILTER_EQUALITY:
135                 type = SLAP_INDEX_EQUALITY;
136                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
137                         goto done;
138                 }
139                 break;
140
141         case LDAP_FILTER_SUBSTRINGS:
142                 type = SLAP_INDEX_SUBSTR;
143                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
144                         goto done;
145                 }
146                 break;
147
148         default:
149                 return LDAP_OTHER;
150         }
151
152 #ifdef MDB_MONITOR_IDX
153         mdb_monitor_idx_add( be->be_private, desc, type );
154 #endif /* MDB_MONITOR_IDX */
155
156         return LDAP_INAPPROPRIATE_MATCHING;
157
158 done:
159         *dbip = ai->ai_dbi;
160         *maskp = mask;
161         return LDAP_SUCCESS;
162 }
163
164 static int indexer(
165         Operation *op,
166         MDB_txn *txn,
167         MDB_dbi dbi,
168         AttributeDescription *ad,
169         struct berval *atname,
170         BerVarray vals,
171         ID id,
172         int opid,
173         slap_mask_t mask )
174 {
175         int rc, i;
176         struct berval *keys;
177         MDB_cursor *mc;
178         mdb_idl_keyfunc *keyfunc;
179         char *err;
180
181         assert( mask != 0 );
182
183         err = "c_open";
184         rc = mdb_cursor_open( txn, dbi, &mc );
185         if ( rc ) goto done;
186
187         if ( opid == SLAP_INDEX_ADD_OP ) {
188                 if ( slapMode & SLAP_TOOL_QUICK )
189                         keyfunc = mdb_tool_idl_add;
190                 else
191                         keyfunc = mdb_idl_insert_keys;
192         } else
193                 keyfunc = mdb_idl_delete_keys;
194
195         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
196                 rc = keyfunc( mc, presence_key, id );
197                 if( rc ) {
198                         err = "presence";
199                         goto done;
200                 }
201         }
202
203         if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
204                 rc = ad->ad_type->sat_equality->smr_indexer(
205                         LDAP_FILTER_EQUALITY,
206                         mask,
207                         ad->ad_type->sat_syntax,
208                         ad->ad_type->sat_equality,
209                         atname, vals, &keys, op->o_tmpmemctx );
210
211                 if( rc == LDAP_SUCCESS && keys != NULL ) {
212                         rc = keyfunc( mc, keys, id );
213                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
214                         if ( rc ) {
215                                 err = "equality";
216                                 goto done;
217                         }
218                 }
219                 rc = LDAP_SUCCESS;
220         }
221
222         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
223                 rc = ad->ad_type->sat_approx->smr_indexer(
224                         LDAP_FILTER_APPROX,
225                         mask,
226                         ad->ad_type->sat_syntax,
227                         ad->ad_type->sat_approx,
228                         atname, vals, &keys, op->o_tmpmemctx );
229
230                 if( rc == LDAP_SUCCESS && keys != NULL ) {
231                         rc = keyfunc( mc, keys, id );
232                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
233                         if ( rc ) {
234                                 err = "approx";
235                                 goto done;
236                         }
237                 }
238
239                 rc = LDAP_SUCCESS;
240         }
241
242         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
243                 rc = ad->ad_type->sat_substr->smr_indexer(
244                         LDAP_FILTER_SUBSTRINGS,
245                         mask,
246                         ad->ad_type->sat_syntax,
247                         ad->ad_type->sat_substr,
248                         atname, vals, &keys, op->o_tmpmemctx );
249
250                 if( rc == LDAP_SUCCESS && keys != NULL ) {
251                         rc = keyfunc( mc, keys, id );
252                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
253                         if( rc ) {
254                                 err = "substr";
255                                 goto done;
256                         }
257                 }
258
259                 rc = LDAP_SUCCESS;
260         }
261
262 done:
263         mdb_cursor_close( mc );
264         switch( rc ) {
265         /* The callers all know how to deal with these results */
266         case 0:
267                 break;
268         /* Anything else is bad news */
269         default:
270                 rc = LDAP_OTHER;
271         }
272         return rc;
273 }
274
275 static int index_at_values(
276         Operation *op,
277         MDB_txn *txn,
278         AttributeDescription *ad,
279         AttributeType *type,
280         struct berval *tags,
281         BerVarray vals,
282         ID id,
283         int opid )
284 {
285         int rc;
286         slap_mask_t mask = 0;
287         int ixop = opid;
288         AttrInfo *ai = NULL;
289
290         if ( opid == MDB_INDEX_UPDATE_OP )
291                 ixop = SLAP_INDEX_ADD_OP;
292
293         if( type->sat_sup ) {
294                 /* recurse */
295                 rc = index_at_values( op, txn, NULL,
296                         type->sat_sup, tags,
297                         vals, id, opid );
298
299                 if( rc ) return rc;
300         }
301
302         /* If this type has no AD, we've never used it before */
303         if( type->sat_ad ) {
304                 ai = mdb_attr_mask( op->o_bd->be_private, type->sat_ad );
305                 if ( ai ) {
306 #ifdef LDAP_COMP_MATCH
307                         /* component indexing */
308                         if ( ai->ai_cr ) {
309                                 ComponentReference *cr;
310                                 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
311                                         rc = indexer( op, txn, ai->ai_dbi, cr->cr_ad, &type->sat_cname,
312                                                 cr->cr_nvals, id, ixop,
313                                                 cr->cr_indexmask );
314                                 }
315                         }
316 #endif
317                         ad = type->sat_ad;
318                         /* If we're updating the index, just set the new bits that aren't
319                          * already in the old mask.
320                          */
321                         if ( opid == MDB_INDEX_UPDATE_OP )
322                                 mask = ai->ai_newmask & ~ai->ai_indexmask;
323                         else
324                         /* For regular updates, if there is a newmask use it. Otherwise
325                          * just use the old mask.
326                          */
327                                 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
328                         if( mask ) {
329                                 rc = indexer( op, txn, ai->ai_dbi, ad, &type->sat_cname,
330                                         vals, id, ixop, mask );
331
332                                 if( rc ) return rc;
333                         }
334                 }
335         }
336
337         if( tags->bv_len ) {
338                 AttributeDescription *desc;
339
340                 desc = ad_find_tags( type, tags );
341                 if( desc ) {
342                         ai = mdb_attr_mask( op->o_bd->be_private, desc );
343
344                         if( ai ) {
345                                 if ( opid == MDB_INDEX_UPDATE_OP )
346                                         mask = ai->ai_newmask & ~ai->ai_indexmask;
347                                 else
348                                         mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
349                                 if ( mask ) {
350                                         rc = indexer( op, txn, ai->ai_dbi, desc, &desc->ad_cname,
351                                                 vals, id, ixop, mask );
352
353                                         if( rc ) {
354                                                 return rc;
355                                         }
356                                 }
357                         }
358                 }
359         }
360
361         return LDAP_SUCCESS;
362 }
363
364 int mdb_index_values(
365         Operation *op,
366         MDB_txn *txn,
367         AttributeDescription *desc,
368         BerVarray vals,
369         ID id,
370         int opid )
371 {
372         int rc;
373
374         /* Never index ID 0 */
375         if ( id == 0 )
376                 return 0;
377
378         rc = index_at_values( op, txn, desc,
379                 desc->ad_type, &desc->ad_tags,
380                 vals, id, opid );
381
382         return rc;
383 }
384
385 /* Get the list of which indices apply to this attr */
386 int
387 mdb_index_recset(
388         struct mdb_info *mdb,
389         Attribute *a,
390         AttributeType *type,
391         struct berval *tags,
392         IndexRec *ir )
393 {
394         int rc, slot;
395         AttrList *al;
396
397         if( type->sat_sup ) {
398                 /* recurse */
399                 rc = mdb_index_recset( mdb, a, type->sat_sup, tags, ir );
400                 if( rc ) return rc;
401         }
402         /* If this type has no AD, we've never used it before */
403         if( type->sat_ad ) {
404                 slot = mdb_attr_slot( mdb, type->sat_ad, NULL );
405                 if ( slot >= 0 ) {
406                         ir[slot].ai = mdb->mi_attrs[slot];
407                         al = ch_malloc( sizeof( AttrList ));
408                         al->attr = a;
409                         al->next = ir[slot].attrs;
410                         ir[slot].attrs = al;
411                 }
412         }
413         if( tags->bv_len ) {
414                 AttributeDescription *desc;
415
416                 desc = ad_find_tags( type, tags );
417                 if( desc ) {
418                         slot = mdb_attr_slot( mdb, desc, NULL );
419                         if ( slot >= 0 ) {
420                                 ir[slot].ai = mdb->mi_attrs[slot];
421                                 al = ch_malloc( sizeof( AttrList ));
422                                 al->attr = a;
423                                 al->next = ir[slot].attrs;
424                                 ir[slot].attrs = al;
425                         }
426                 }
427         }
428         return LDAP_SUCCESS;
429 }
430
431 /* Apply the indices for the recset */
432 int mdb_index_recrun(
433         Operation *op,
434         struct mdb_info *mdb,
435         IndexRec *ir0,
436         ID id,
437         int base )
438 {
439         IndexRec *ir;
440         AttrList *al;
441         int i, rc = 0;
442
443         /* Never index ID 0 */
444         if ( id == 0 )
445                 return 0;
446
447         for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max) {
448                 ir = ir0 + i;
449                 if ( !ir->ai ) continue;
450                 while (( al = ir->attrs )) {
451                         ir->attrs = al->next;
452                         rc = indexer( op, NULL, ir->ai->ai_dbi, ir->ai->ai_desc,
453                                 &ir->ai->ai_desc->ad_type->sat_cname,
454                                 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
455                                 ir->ai->ai_indexmask );
456                         free( al );
457                         if ( rc ) break;
458                 }
459         }
460         return rc;
461 }
462
463 int
464 mdb_index_entry(
465         Operation *op,
466         MDB_txn *txn,
467         int opid,
468         Entry   *e )
469 {
470         int rc;
471         Attribute *ap = e->e_attrs;
472 #if 0 /* ifdef LDAP_COMP_MATCH */
473         ComponentReference *cr_list = NULL;
474         ComponentReference *cr = NULL, *dupped_cr = NULL;
475         void* decoded_comp;
476         ComponentSyntaxInfo* csi_attr;
477         Syntax* syn;
478         AttributeType* at;
479         int i, num_attr;
480         void* mem_op;
481         struct berval value = {0};
482 #endif
483
484         /* Never index ID 0 */
485         if ( e->e_id == 0 )
486                 return 0;
487
488         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
489                 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
490                 (long) e->e_id, e->e_dn );
491
492         /* add each attribute to the indexes */
493         for ( ; ap != NULL; ap = ap->a_next ) {
494 #if 0 /* ifdef LDAP_COMP_MATCH */
495                 AttrInfo *ai;
496                 /* see if attribute has components to be indexed */
497                 ai = mdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
498                 if ( !ai ) continue;
499                 cr_list = ai->ai_cr;
500                 if ( attr_converter && cr_list ) {
501                         syn = ap->a_desc->ad_type->sat_syntax;
502                         ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
503                         /* Memory chunk(nibble) pre-allocation for decoders */
504                         mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
505                         ap->a_comp_data->cd_mem_op = mem_op;
506                         for( cr = cr_list ; cr ; cr = cr->cr_next ) {
507                                 /* count how many values in an attribute */
508                                 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
509                                 num_attr++;
510                                 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
511                                 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
512                                         /* decoding attribute value */
513                                         decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
514                                         if ( !decoded_comp )
515                                                 return LDAP_DECODING_ERROR;
516                                         /* extracting the referenced component */
517                                         dupped_cr = dup_comp_ref( op, cr );
518                                         csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
519                                         if ( !csi_attr )
520                                                 return LDAP_DECODING_ERROR;
521                                         cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
522                                         cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
523                                         if ( !cr->cr_ad )
524                                                 return LDAP_INVALID_SYNTAX;
525                                         at = cr->cr_ad->ad_type;
526                                         /* encoding the value of component in GSER */
527                                         rc = component_encoder( mem_op, csi_attr, &value );
528                                         if ( rc != LDAP_SUCCESS )
529                                                 return LDAP_ENCODING_ERROR;
530                                         /* Normalize the encoded component values */
531                                         if ( at->sat_equality && at->sat_equality->smr_normalize ) {
532                                                 rc = at->sat_equality->smr_normalize (
533                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
534                                                         at->sat_syntax, at->sat_equality,
535                                                         &value, &cr->cr_nvals[i], op->o_tmpmemctx );
536                                         } else {
537                                                 cr->cr_nvals[i] = value;
538                                         }
539                                 }
540                                 /* The end of BerVarray */
541                                 cr->cr_nvals[num_attr-1].bv_val = NULL;
542                                 cr->cr_nvals[num_attr-1].bv_len = 0;
543                         }
544                         op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
545                         nibble_mem_free ( mem_op );
546                         ap->a_comp_data = NULL;
547                 }
548 #endif
549                 rc = mdb_index_values( op, txn, ap->a_desc,
550                         ap->a_nvals, e->e_id, opid );
551
552                 if( rc != LDAP_SUCCESS ) {
553                         Debug( LDAP_DEBUG_TRACE,
554                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
555                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
556                                 (long) e->e_id, e->e_dn );
557                         return rc;
558                 }
559         }
560
561         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
562                 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
563                 (long) e->e_id, e->e_dn );
564
565         return LDAP_SUCCESS;
566 }