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