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