]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/index.c
Fixed index file naming bug and added support for LDAP_MOD_SOFTADD.
[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                 case LDAP_MOD_SOFTADD:  /* SOFTADD means index was there */
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         /* XXX do we need idl ??? */
171         ID_BLOCK        *idl = NULL;
172         char    *tmpval = NULL;
173         char    *realval = val;
174         char    buf[BUFSIZ];
175
176         char    prefix = index2prefix( indextype );
177
178         ldbm_datum_init( key );
179
180         Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
181
182         if ( prefix != UNKNOWN_PREFIX ) {
183               unsigned int     len = strlen( val );
184
185               if ( (len + 2) < sizeof(buf) ) {
186                         realval = buf;
187                 } else {
188                         /* value + prefix + null */
189                         tmpval = (char *) ch_malloc( len + 2 );
190                         realval = tmpval;
191                 }
192               realval[0] = prefix;
193               strcpy( &realval[1], val );
194         }
195
196         key.dptr = realval;
197         key.dsize = strlen( realval ) + 1;
198
199         rc = idl_insert_key( be, db, key, id );
200
201         if ( tmpval != NULL ) {
202                 free( tmpval );
203         }
204
205         if( idl != NULL ) {
206                 idl_free( idl );
207         }
208
209         ldap_pvt_thread_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         unsigned        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         attr_normalize(type);
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 = UNKNOWN_PREFIX;
360                 break;
361         }
362
363         return( prefix );
364 }