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