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