]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/attr.c
Change recover logic
[openldap] / servers / slapd / back-bdb / attr.c
1 /* attr.c - backend routines for dealing with attributes */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-bdb.h"
17
18 /* for the cache of attribute information (which are indexed, etc.) */
19 typedef struct bdb_attrinfo {
20         AttributeDescription *ai_desc; /* attribute description cn;lang-en */
21         slap_mask_t ai_indexmask;       /* how the attr is indexed      */
22 } AttrInfo;
23
24 static int
25 ainfo_type_cmp(
26         AttributeDescription *desc,
27         AttrInfo        *a
28 )
29 {
30         return desc - a->ai_desc;
31 }
32
33 static int
34 ainfo_cmp(
35         AttrInfo        *a,
36         AttrInfo        *b
37 )
38 {
39         return a->ai_desc - b->ai_desc;
40 }
41
42 void
43 bdb_attr_mask(
44         struct bdb_info *bdb,
45         AttributeDescription *desc,
46         slap_mask_t *indexmask )
47 {
48         AttrInfo        *a;
49
50         a = (AttrInfo *) avl_find( bdb->bi_attrs, desc,
51                 (AVL_CMP) ainfo_type_cmp );
52         
53         *indexmask = a != NULL ? a->ai_indexmask : 0;
54 }
55
56 int
57 bdb_attr_index_config(
58         struct bdb_info *bdb,
59         const char              *fname,
60         int                     lineno,
61         int                     argc,
62         char            **argv )
63 {
64         int rc;
65         int     i;
66         slap_mask_t mask;
67         char **attrs;
68         char **indexes = NULL;
69
70         attrs = str2charray( argv[0], "," );
71
72         if( attrs == NULL ) {
73                 fprintf( stderr, "%s: line %d: "
74                         "no attributes specified: %s\n",
75                         fname, lineno, argv[0] );
76                 return LDAP_PARAM_ERROR;
77         }
78
79         if ( argc > 1 ) {
80                 indexes = str2charray( argv[1], "," );
81
82                 if( indexes == NULL ) {
83                         fprintf( stderr, "%s: line %d: "
84                                 "no indexes specified: %s\n",
85                                 fname, lineno, argv[1] );
86                         return LDAP_PARAM_ERROR;
87                 }
88         }
89
90         if( indexes == NULL ) {
91                 mask = bdb->bi_defaultmask;
92
93         } else {
94                 mask = 0;
95
96                 for ( i = 0; indexes[i] != NULL; i++ ) {
97                         slap_mask_t index;
98                         rc = slap_str2index( indexes[i], &index );
99
100                         if( rc != LDAP_SUCCESS ) {
101                                 fprintf( stderr, "%s: line %d: "
102                                         "index type \"%s\" undefined\n",
103                                         fname, lineno, indexes[i] );
104                                 return LDAP_PARAM_ERROR;
105                         }
106
107                         mask |= index;
108                 }
109         }
110
111         if( !mask ) {
112                 fprintf( stderr, "%s: line %d: "
113                         "no indexes selected\n",
114                         fname, lineno );
115                 return LDAP_PARAM_ERROR;
116         }
117
118         for ( i = 0; attrs[i] != NULL; i++ ) {
119                 AttrInfo        *a;
120                 AttributeDescription *ad;
121                 const char *text;
122
123                 if( strcasecmp( attrs[i], "default" ) == 0 ) {
124                         bdb->bi_defaultmask = mask;
125                         continue;
126                 }
127
128                 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
129
130                 ad = NULL;
131                 rc = slap_str2ad( attrs[i], &ad, &text );
132
133                 if( rc != LDAP_SUCCESS ) {
134                         fprintf( stderr, "%s: line %d: "
135                                 "index attribute \"%s\" undefined\n",
136                                 fname, lineno, attrs[i] );
137                         return rc;
138                 }
139
140                 if( slap_ad_is_binary( ad ) ) {
141                         fprintf( stderr, "%s: line %d: "
142                                 "index of attribute \"%s\" disallowed\n",
143                                 fname, lineno, attrs[i] );
144                         return LDAP_UNWILLING_TO_PERFORM;
145                 }
146
147                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
148                         ( ad->ad_type->sat_approx
149                                 && ad->ad_type->sat_approx->smr_indexer
150                                 && ad->ad_type->sat_approx->smr_filter )
151                         && ( ad->ad_type->sat_equality
152                                 && ad->ad_type->sat_equality->smr_indexer
153                                 && ad->ad_type->sat_equality->smr_filter ) ) )
154                 {
155                         fprintf( stderr, "%s: line %d: "
156                                 "approx index of attribute \"%s\" disallowed\n",
157                                 fname, lineno, attrs[i] );
158                         return LDAP_INAPPROPRIATE_MATCHING;
159                 }
160
161                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
162                         ad->ad_type->sat_equality
163                                 && ad->ad_type->sat_equality->smr_indexer
164                                 && ad->ad_type->sat_equality->smr_filter ) )
165                 {
166                         fprintf( stderr, "%s: line %d: "
167                                 "equality index of attribute \"%s\" disallowed\n",
168                                 fname, lineno, attrs[i] );
169                         return LDAP_INAPPROPRIATE_MATCHING;
170                 }
171
172                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
173                         ad->ad_type->sat_substr
174                                 && ad->ad_type->sat_substr->smr_indexer
175                                 && ad->ad_type->sat_substr->smr_filter ) )
176                 {
177                         fprintf( stderr, "%s: line %d: "
178                                 "substr index of attribute \"%s\" disallowed\n",
179                                 fname, lineno, attrs[i] );
180                         return LDAP_INAPPROPRIATE_MATCHING;
181                 }
182
183 #ifdef NEW_LOGGING
184                 LDAP_LOG( BACK_BDB, DETAIL1, 
185                         "attr_index_config: index %s 0x%04lx\n",
186                         ad->ad_cname.bv_val, mask, 0 );
187 #else
188                 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
189                         ad->ad_cname.bv_val, mask, 0 ); 
190 #endif
191
192
193                 a->ai_desc = ad;
194                 a->ai_indexmask = mask;
195
196                 rc = avl_insert( &bdb->bi_attrs, (caddr_t) a,
197                         (AVL_CMP) ainfo_cmp, (AVL_DUP) avl_dup_error );
198
199                 if( rc ) {
200                         fprintf( stderr, "%s: line %d: duplicate index definition "
201                                 "for attr \"%s\" (ignored)\n",
202                                 fname, lineno, attrs[i] );
203
204                         return LDAP_PARAM_ERROR;
205                 }
206         }
207
208         charray_free( attrs );
209         if ( indexes != NULL ) charray_free( indexes );
210
211         return LDAP_SUCCESS;
212 }
213
214 void
215 bdb_attr_index_destroy( Avlnode *tree )
216 {
217         avl_free( tree, free );
218 }
219