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