]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/index.c
acb7844e8e9d6641d2c83cdec8deea136716228e
[openldap] / servers / slapd / back-ldbm / index.c
1 /* index.c - routines for dealing with attribute indexes */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12
13 static int      add_value(Backend *be, struct dbcache *db, char *type, int indextype, char *val, ID id);
14 static int      index2prefix(int indextype);
15
16 int
17 index_add_entry(
18     Backend     *be,
19     Entry       *e
20 )
21 {
22         Attribute       *ap;
23         char            *dnval;
24         struct berval   bv;
25         struct berval   *bvals[2];
26
27         Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
28             e->e_dn, 0 );
29
30         /*
31          * dn index entry - make it look like an attribute so it works
32          * with index_add_values() call
33          */
34
35         bv.bv_val = ch_strdup( e->e_dn );
36         bv.bv_len = strlen( bv.bv_val );
37         (void) dn_normalize_case( bv.bv_val );
38         bvals[0] = &bv;
39         bvals[1] = NULL;
40
41         /* add the dn to the indexes */
42         index_add_values( be, "dn", bvals, e->e_id );
43
44         free( bv.bv_val );
45
46         /* add each attribute to the indexes */
47         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
48                 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
49         }
50
51         Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
52             e->e_dn, 0 );
53         return( 0 );
54 }
55
56 int
57 index_add_mods(
58     Backend     *be,
59     LDAPMod     *mods,
60     ID          id
61 )
62 {
63         int     rc;
64
65         for ( ; mods != NULL; mods = mods->mod_next ) {
66                 switch ( mods->mod_op & ~LDAP_MOD_BVALUES ) {
67                 case LDAP_MOD_ADD:
68                 case LDAP_MOD_REPLACE:
69                         rc = index_add_values( be, mods->mod_type,
70                             mods->mod_bvalues, id );
71                         break;
72
73                 case LDAP_MOD_DELETE:
74                         rc = 0;
75                         break;
76                 }
77
78                 if ( rc != 0 ) {
79                         return( rc );
80                 }
81         }
82
83         return( 0 );
84 }
85
86 IDList *
87 index_read(
88     Backend     *be,
89     char        *type,
90     int         indextype,
91     char        *val
92 )
93 {
94         struct dbcache  *db;
95         Datum           key;
96         IDList          *idl;
97         int             indexmask, syntax;
98         char            prefix;
99         char            *realval, *tmpval;
100         char            buf[BUFSIZ];
101
102 #ifdef HAVE_BERKELEY_DB2
103         memset( &key, 0, sizeof( key ) );
104 #endif
105
106         prefix = index2prefix( indextype );
107         Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
108             type, prefix, val );
109
110         attr_masks( be->be_private, type, &indexmask, &syntax );
111         if ( ! (indextype & indexmask) ) {
112                 idl =  idl_allids( be );
113                 Debug( LDAP_DEBUG_TRACE,
114                     "<= index_read %lu candidates (allids - not indexed)\n",
115                     idl ? idl->b_nids : 0, 0, 0 );
116                 return( idl );
117         }
118
119         attr_normalize( type );
120         if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
121             == NULL ) {
122                 Debug( LDAP_DEBUG_ANY,
123                     "<= index_read NULL (could not open %s%s)\n", type,
124                     LDBM_SUFFIX, 0 );
125                 return( NULL );
126         }
127
128         realval = val;
129         tmpval = NULL;
130         if ( prefix != '\0' ) {
131               unsigned int      len = strlen( val );
132
133               if ( (len + 2) < sizeof(buf) ) {
134                         realval = buf;
135                 } else {
136                         /* value + prefix + null */
137                         tmpval = (char *) ch_malloc( len + 2 );
138                         realval = tmpval;
139                 }
140               realval[0] = prefix;
141               strcpy( &realval[1], val );
142         }
143
144         key.dptr = realval;
145         key.dsize = strlen( realval ) + 1;
146
147         idl = idl_fetch( be, db, key );
148       if ( tmpval != NULL ) {
149               free( tmpval );
150       }
151
152         ldbm_cache_close( be, db );
153
154         Debug( LDAP_DEBUG_TRACE, "<= index_read %lu candidates\n",
155             idl ? idl->b_nids : 0, 0, 0 );
156         return( idl );
157 }
158
159 static int
160 add_value(
161     Backend             *be,
162     struct dbcache      *db,
163     char                *type,
164     int                 indextype,
165     char                *val,
166     ID                  id
167 )
168 {
169         int     rc;
170         Datum   key;
171         IDList  *idl;
172         char    prefix;
173         char    *realval, *tmpval, *s;
174         char    buf[BUFSIZ];
175
176 #ifdef HAVE_BERKELEY_DB2
177         memset( &key, 0, sizeof( key ) );
178 #endif
179
180         prefix = index2prefix( indextype );
181         Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
182
183         realval = val;
184         tmpval = NULL;
185         idl = NULL;
186         if ( prefix != '\0' ) {
187               unsigned int     len = strlen( val );
188
189               if ( (len + 2) < sizeof(buf) ) {
190                         realval = buf;
191                 } else {
192                         /* value + prefix + null */
193                         tmpval = (char *) ch_malloc( len + 2 );
194                         realval = tmpval;
195                 }
196               realval[0] = prefix;
197               strcpy( &realval[1], val );
198         }
199
200         key.dptr = realval;
201         key.dsize = strlen( realval ) + 1;
202
203         rc = idl_insert_key( be, db, key, id );
204
205         if ( tmpval != NULL ) {
206                 free( tmpval );
207         }
208         idl_free( idl );
209
210         pthread_yield();
211
212         /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
213         return( rc );
214 }
215
216 int
217 index_add_values(
218     Backend             *be,
219     char                *type,
220     struct berval       **vals,
221     ID                  id
222 )
223 {
224         char            *val, *p, *code, *w;
225         unsigned        i, j, len;
226         int             indexmask, syntax;
227         char            buf[SUBLEN + 1];
228         char            vbuf[BUFSIZ];
229         char            *bigbuf;
230         struct dbcache  *db;
231
232         Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
233             id, 0 );
234
235         attr_masks( be->be_private, type, &indexmask, &syntax );
236         if ( indexmask == 0 ) {
237                 return( 0 );
238         }
239
240         if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
241             == NULL ) {
242                 Debug( LDAP_DEBUG_ANY,
243                     "<= index_add_values -1 (could not open/create %s%s)\n",
244                     type, LDBM_SUFFIX, 0 );
245                 return( -1 );
246         }
247
248
249         for ( i = 0; vals[i] != NULL; i++ ) {
250                 /*
251                  * presence index entry
252                  */
253                 if ( indexmask & INDEX_PRESENCE ) {
254                         add_value( be, db, type, INDEX_PRESENCE, "*", id );
255                 }
256
257                 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
258                     syntax, SYNTAX_BIN, 0 );
259                 if ( syntax & SYNTAX_BIN ) {
260                         ldbm_cache_close( be, db );
261                         return( 0 );
262                 }
263
264                 bigbuf = NULL;
265                 len = vals[i]->bv_len;
266
267                 /* value + null */
268                 if ( len + 2 > sizeof(vbuf) ) {
269                         bigbuf = (char *) ch_malloc( len + 1 );
270                         val = bigbuf;
271                 } else {
272                         val = vbuf;
273                 }
274                 (void) memcpy( val, vals[i]->bv_val, len );
275                 val[len] = '\0';
276
277                 value_normalize( val, syntax );
278
279                 /*
280                  * equality index entry
281                  */
282                 if ( indexmask & INDEX_EQUALITY ) {
283                         add_value( be, db, type, INDEX_EQUALITY, val, id );
284                 }
285
286                 /*
287                  * approximate index entry
288                  */
289                 if ( indexmask & INDEX_APPROX ) {
290                         for ( w = first_word( val ); w != NULL;
291                             w = next_word( w ) ) {
292                                 if ( (code = phonetic( w )) != NULL ) {
293                                         add_value( be, db, type, INDEX_APPROX,
294                                             code, id );
295                                         free( code );
296                                 }
297                         }
298                 }
299
300                 /*
301                  * substrings index entry
302                  */
303                 if ( indexmask & INDEX_SUB ) {
304                         /* leading and trailing */
305                         if ( len > SUBLEN - 2 ) {
306                                 buf[0] = '^';
307                                 for ( j = 0; j < SUBLEN - 1; j++ ) {
308                                         buf[j + 1] = val[j];
309                                 }
310                                 buf[SUBLEN] = '\0';
311
312                                 add_value( be, db, type, INDEX_SUB, buf, id );
313
314                                 p = val + len - SUBLEN + 1;
315                                 for ( j = 0; j < SUBLEN - 1; j++ ) {
316                                         buf[j] = p[j];
317                                 }
318                                 buf[SUBLEN - 1] = '$';
319                                 buf[SUBLEN] = '\0';
320
321                                 add_value( be, db, type, INDEX_SUB, buf, id );
322                         }
323
324                         /* any */
325                         for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
326                                 for ( j = 0; j < SUBLEN; j++ ) {
327                                         buf[j] = p[j];
328                                 }
329                                 buf[SUBLEN] = '\0';
330
331                                 add_value( be, db, type, INDEX_SUB, buf, id );
332                         }
333                 }
334
335                 if ( bigbuf != NULL ) {
336                         free( bigbuf );
337                 }
338         }
339         ldbm_cache_close( be, db );
340
341         return( 0 );
342 }
343
344 static int
345 index2prefix( int indextype )
346 {
347         int     prefix;
348
349         switch ( indextype ) {
350         case INDEX_EQUALITY:
351                 prefix = EQ_PREFIX;
352                 break;
353         case INDEX_APPROX:
354                 prefix = APPROX_PREFIX;
355                 break;
356         case INDEX_SUB:
357                 prefix = SUB_PREFIX;
358                 break;
359         default:
360                 prefix = '\0';
361                 break;
362         }
363
364         return( prefix );
365 }