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