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