]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/index.c
Cursor updates
[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
180         assert( mask != 0 );
181
182         rc = mdb_cursor_open( txn, dbi, &mc );
183         if ( rc ) goto done;
184
185         if ( opid == SLAP_INDEX_ADD_OP )
186                 keyfunc = mdb_idl_insert_keys;
187         else
188                 keyfunc = mdb_idl_delete_keys;
189
190         if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
191                 rc = keyfunc( mc, (MDB_val *)presence_key, id );
192                 if( rc ) {
193                         goto done;
194                 }
195         }
196
197         if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
198                 rc = ad->ad_type->sat_equality->smr_indexer(
199                         LDAP_FILTER_EQUALITY,
200                         mask,
201                         ad->ad_type->sat_syntax,
202                         ad->ad_type->sat_equality,
203                         atname, vals, &keys, op->o_tmpmemctx );
204
205                 if( rc == LDAP_SUCCESS && keys != NULL ) {
206                         rc = keyfunc( mc, (MDB_val *)keys, id );
207                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
208                         if ( rc ) {
209                                 goto done;
210                         }
211                 }
212                 rc = LDAP_SUCCESS;
213         }
214
215         if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
216                 rc = ad->ad_type->sat_approx->smr_indexer(
217                         LDAP_FILTER_APPROX,
218                         mask,
219                         ad->ad_type->sat_syntax,
220                         ad->ad_type->sat_approx,
221                         atname, vals, &keys, op->o_tmpmemctx );
222
223                 if( rc == LDAP_SUCCESS && keys != NULL ) {
224                         rc = keyfunc( mc, (MDB_val *)keys, id );
225                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
226                         if ( rc ) {
227                                 goto done;
228                         }
229                 }
230
231                 rc = LDAP_SUCCESS;
232         }
233
234         if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
235                 rc = ad->ad_type->sat_substr->smr_indexer(
236                         LDAP_FILTER_SUBSTRINGS,
237                         mask,
238                         ad->ad_type->sat_syntax,
239                         ad->ad_type->sat_substr,
240                         atname, vals, &keys, op->o_tmpmemctx );
241
242                 if( rc == LDAP_SUCCESS && keys != NULL ) {
243                         rc = keyfunc( mc, (MDB_val *)keys, id );
244                         ber_bvarray_free_x( keys, op->o_tmpmemctx );
245                         if( rc ) {
246                                 goto done;
247                         }
248                 }
249
250                 rc = LDAP_SUCCESS;
251         }
252
253 done:
254         mdb_cursor_close( mc );
255         switch( rc ) {
256         /* The callers all know how to deal with these results */
257         case 0:
258                 break;
259         /* Anything else is bad news */
260         default:
261                 rc = LDAP_OTHER;
262         }
263         return rc;
264 }
265
266 static int index_at_values(
267         Operation *op,
268         MDB_txn *txn,
269         AttributeDescription *ad,
270         AttributeType *type,
271         struct berval *tags,
272         BerVarray vals,
273         ID id,
274         int opid )
275 {
276         int rc;
277         slap_mask_t mask = 0;
278         int ixop = opid;
279         AttrInfo *ai = NULL;
280
281         if ( opid == MDB_INDEX_UPDATE_OP )
282                 ixop = SLAP_INDEX_ADD_OP;
283
284         if( type->sat_sup ) {
285                 /* recurse */
286                 rc = index_at_values( op, txn, NULL,
287                         type->sat_sup, tags,
288                         vals, id, opid );
289
290                 if( rc ) return rc;
291         }
292
293         /* If this type has no AD, we've never used it before */
294         if( type->sat_ad ) {
295                 ai = mdb_attr_mask( op->o_bd->be_private, type->sat_ad );
296                 if ( ai ) {
297 #ifdef LDAP_COMP_MATCH
298                         /* component indexing */
299                         if ( ai->ai_cr ) {
300                                 ComponentReference *cr;
301                                 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
302                                         rc = indexer( op, txn, ai->ai_dbi, cr->cr_ad, &type->sat_cname,
303                                                 cr->cr_nvals, id, ixop,
304                                                 cr->cr_indexmask );
305                                 }
306                         }
307 #endif
308                         ad = type->sat_ad;
309                         /* If we're updating the index, just set the new bits that aren't
310                          * already in the old mask.
311                          */
312                         if ( opid == MDB_INDEX_UPDATE_OP )
313                                 mask = ai->ai_newmask & ~ai->ai_indexmask;
314                         else
315                         /* For regular updates, if there is a newmask use it. Otherwise
316                          * just use the old mask.
317                          */
318                                 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
319                         if( mask ) {
320                                 rc = indexer( op, txn, ai->ai_dbi, ad, &type->sat_cname,
321                                         vals, id, ixop, mask );
322
323                                 if( rc ) return rc;
324                         }
325                 }
326         }
327
328         if( tags->bv_len ) {
329                 AttributeDescription *desc;
330
331                 desc = ad_find_tags( type, tags );
332                 if( desc ) {
333                         ai = mdb_attr_mask( op->o_bd->be_private, desc );
334
335                         if( ai ) {
336                                 if ( opid == MDB_INDEX_UPDATE_OP )
337                                         mask = ai->ai_newmask & ~ai->ai_indexmask;
338                                 else
339                                         mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
340                                 if ( mask ) {
341                                         rc = indexer( op, txn, ai->ai_dbi, desc, &desc->ad_cname,
342                                                 vals, id, ixop, mask );
343
344                                         if( rc ) {
345                                                 return rc;
346                                         }
347                                 }
348                         }
349                 }
350         }
351
352         return LDAP_SUCCESS;
353 }
354
355 int mdb_index_values(
356         Operation *op,
357         MDB_txn *txn,
358         AttributeDescription *desc,
359         BerVarray vals,
360         ID id,
361         int opid )
362 {
363         int rc;
364
365         /* Never index ID 0 */
366         if ( id == 0 )
367                 return 0;
368
369         rc = index_at_values( op, txn, desc,
370                 desc->ad_type, &desc->ad_tags,
371                 vals, id, opid );
372
373         return rc;
374 }
375
376 /* Get the list of which indices apply to this attr */
377 int
378 mdb_index_recset(
379         struct mdb_info *mdb,
380         Attribute *a,
381         AttributeType *type,
382         struct berval *tags,
383         IndexRec *ir )
384 {
385         int rc, slot;
386         AttrList *al;
387
388         if( type->sat_sup ) {
389                 /* recurse */
390                 rc = mdb_index_recset( mdb, a, type->sat_sup, tags, ir );
391                 if( rc ) return rc;
392         }
393         /* If this type has no AD, we've never used it before */
394         if( type->sat_ad ) {
395                 slot = mdb_attr_slot( mdb, type->sat_ad, NULL );
396                 if ( slot >= 0 ) {
397                         ir[slot].ai = mdb->mi_attrs[slot];
398                         al = ch_malloc( sizeof( AttrList ));
399                         al->attr = a;
400                         al->next = ir[slot].attrs;
401                         ir[slot].attrs = al;
402                 }
403         }
404         if( tags->bv_len ) {
405                 AttributeDescription *desc;
406
407                 desc = ad_find_tags( type, tags );
408                 if( desc ) {
409                         slot = mdb_attr_slot( mdb, desc, NULL );
410                         if ( slot >= 0 ) {
411                                 ir[slot].ai = mdb->mi_attrs[slot];
412                                 al = ch_malloc( sizeof( AttrList ));
413                                 al->attr = a;
414                                 al->next = ir[slot].attrs;
415                                 ir[slot].attrs = al;
416                         }
417                 }
418         }
419         return LDAP_SUCCESS;
420 }
421
422 /* Apply the indices for the recset */
423 int mdb_index_recrun(
424         Operation *op,
425         struct mdb_info *mdb,
426         IndexRec *ir0,
427         ID id,
428         int base )
429 {
430         IndexRec *ir;
431         AttrList *al;
432         int i, rc = 0;
433
434         /* Never index ID 0 */
435         if ( id == 0 )
436                 return 0;
437
438         for (i=base; i<mdb->mi_nattrs; i+=slap_tool_thread_max) {
439                 ir = ir0 + i;
440                 if ( !ir->ai ) continue;
441                 while (( al = ir->attrs )) {
442                         ir->attrs = al->next;
443                         rc = indexer( op, NULL, ir->ai->ai_dbi, ir->ai->ai_desc,
444                                 &ir->ai->ai_desc->ad_type->sat_cname,
445                                 al->attr->a_nvals, id, SLAP_INDEX_ADD_OP,
446                                 ir->ai->ai_indexmask );
447                         free( al );
448                         if ( rc ) break;
449                 }
450         }
451         return rc;
452 }
453
454 int
455 mdb_index_entry(
456         Operation *op,
457         MDB_txn *txn,
458         int opid,
459         Entry   *e )
460 {
461         int rc;
462         Attribute *ap = e->e_attrs;
463 #if 0 /* ifdef LDAP_COMP_MATCH */
464         ComponentReference *cr_list = NULL;
465         ComponentReference *cr = NULL, *dupped_cr = NULL;
466         void* decoded_comp;
467         ComponentSyntaxInfo* csi_attr;
468         Syntax* syn;
469         AttributeType* at;
470         int i, num_attr;
471         void* mem_op;
472         struct berval value = {0};
473 #endif
474
475         /* Never index ID 0 */
476         if ( e->e_id == 0 )
477                 return 0;
478
479         Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
480                 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
481                 (long) e->e_id, e->e_dn );
482
483         /* add each attribute to the indexes */
484         for ( ; ap != NULL; ap = ap->a_next ) {
485 #if 0 /* ifdef LDAP_COMP_MATCH */
486                 AttrInfo *ai;
487                 /* see if attribute has components to be indexed */
488                 ai = mdb_attr_mask( op->o_bd->be_private, ap->a_desc->ad_type->sat_ad );
489                 if ( !ai ) continue;
490                 cr_list = ai->ai_cr;
491                 if ( attr_converter && cr_list ) {
492                         syn = ap->a_desc->ad_type->sat_syntax;
493                         ap->a_comp_data = op->o_tmpalloc( sizeof( ComponentData ), op->o_tmpmemctx );
494                         /* Memory chunk(nibble) pre-allocation for decoders */
495                         mem_op = nibble_mem_allocator ( 1024*16, 1024*4 );
496                         ap->a_comp_data->cd_mem_op = mem_op;
497                         for( cr = cr_list ; cr ; cr = cr->cr_next ) {
498                                 /* count how many values in an attribute */
499                                 for( num_attr=0; ap->a_vals[num_attr].bv_val != NULL; num_attr++ );
500                                 num_attr++;
501                                 cr->cr_nvals = (BerVarray)op->o_tmpalloc( sizeof( struct berval )*num_attr, op->o_tmpmemctx );
502                                 for( i=0; ap->a_vals[i].bv_val != NULL; i++ ) {
503                                         /* decoding attribute value */
504                                         decoded_comp = attr_converter ( ap, syn, &ap->a_vals[i] );
505                                         if ( !decoded_comp )
506                                                 return LDAP_DECODING_ERROR;
507                                         /* extracting the referenced component */
508                                         dupped_cr = dup_comp_ref( op, cr );
509                                         csi_attr = ((ComponentSyntaxInfo*)decoded_comp)->csi_comp_desc->cd_extract_i( mem_op, dupped_cr, decoded_comp );
510                                         if ( !csi_attr )
511                                                 return LDAP_DECODING_ERROR;
512                                         cr->cr_asn_type_id = csi_attr->csi_comp_desc->cd_type_id;
513                                         cr->cr_ad = (AttributeDescription*)get_component_description ( cr->cr_asn_type_id );
514                                         if ( !cr->cr_ad )
515                                                 return LDAP_INVALID_SYNTAX;
516                                         at = cr->cr_ad->ad_type;
517                                         /* encoding the value of component in GSER */
518                                         rc = component_encoder( mem_op, csi_attr, &value );
519                                         if ( rc != LDAP_SUCCESS )
520                                                 return LDAP_ENCODING_ERROR;
521                                         /* Normalize the encoded component values */
522                                         if ( at->sat_equality && at->sat_equality->smr_normalize ) {
523                                                 rc = at->sat_equality->smr_normalize (
524                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
525                                                         at->sat_syntax, at->sat_equality,
526                                                         &value, &cr->cr_nvals[i], op->o_tmpmemctx );
527                                         } else {
528                                                 cr->cr_nvals[i] = value;
529                                         }
530                                 }
531                                 /* The end of BerVarray */
532                                 cr->cr_nvals[num_attr-1].bv_val = NULL;
533                                 cr->cr_nvals[num_attr-1].bv_len = 0;
534                         }
535                         op->o_tmpfree( ap->a_comp_data, op->o_tmpmemctx );
536                         nibble_mem_free ( mem_op );
537                         ap->a_comp_data = NULL;
538                 }
539 #endif
540                 rc = mdb_index_values( op, txn, ap->a_desc,
541                         ap->a_nvals, e->e_id, opid );
542
543                 if( rc != LDAP_SUCCESS ) {
544                         Debug( LDAP_DEBUG_TRACE,
545                                 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
546                                 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
547                                 (long) e->e_id, e->e_dn );
548                         return rc;
549                 }
550         }
551
552         Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
553                 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
554                 (long) e->e_id, e->e_dn );
555
556         return LDAP_SUCCESS;
557 }