]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/index.c
1a3a468fc20f606bf17ee4f04f688da72248cf8e
[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      change_value(Backend *be,
14                           DBCache *db,
15                           char *type,
16                           int indextype,
17                           char *val,
18                           ID id,
19                           int
20                           (*idl_func)(Backend *, DBCache *, Datum, ID));
21 static int      index2prefix(int indextype);
22
23 int
24 index_add_entry(
25     Backend     *be,
26     Entry       *e
27 )
28 {
29         Attribute       *ap;
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_change_values() call
39          */
40
41         bv.bv_val = ch_strdup( e->e_ndn );
42         bv.bv_len = strlen( bv.bv_val );
43         bvals[0] = &bv;
44         bvals[1] = NULL;
45
46         /* add the dn to the indexes */
47         {
48                 char *dn = ch_strdup("dn");
49                 index_change_values( be, dn, bvals, e->e_id, __INDEX_ADD_OP );
50                 free( dn );
51         }
52
53         free( bv.bv_val );
54
55         /* add each attribute to the indexes */
56         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
57
58                 index_change_values( be, ap->a_type, ap->a_vals, e->e_id,
59                                      __INDEX_ADD_OP );
60         }
61
62         Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
63             e->e_ndn, 0 );
64         return( 0 );
65 }
66
67 int
68 index_add_mods(
69     Backend     *be,
70     LDAPModList *ml,
71     ID          id
72 )
73 {
74         int     rc;
75
76         for ( ; ml != NULL; ml = ml->ml_next ) {
77                 LDAPMod *mod = &ml->ml_mod;
78
79                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
80                 case LDAP_MOD_REPLACE:
81                         /* XXX: Delete old index data==>problem when this 
82                          * gets called we lost values already!
83                          */
84                 case LDAP_MOD_ADD:
85                         rc = index_change_values( be,
86                                                mod->mod_type,
87                                                mod->mod_bvalues,
88                                                id,
89                                                __INDEX_ADD_OP);
90                         break;
91                 case LDAP_MOD_DELETE:
92                         rc =  index_change_values( be,
93                                                    mod->mod_type,
94                                                    mod->mod_bvalues,
95                                                    id,
96                                                    __INDEX_DELETE_OP );
97                         break;
98                 case LDAP_MOD_SOFTADD:  /* SOFTADD means index was there */
99                         rc = 0;
100                         break;
101                 }
102
103                 if ( rc != 0 ) {
104                         return( rc );
105                 }
106         }
107
108         return( 0 );
109 }
110
111 ID_BLOCK *
112 index_read(
113     Backend     *be,
114     char        *type,
115     int         indextype,
116     char        *val
117 )
118 {
119         DBCache *db;
120         Datum           key;
121         ID_BLOCK                *idl;
122         int             indexmask, syntax;
123         char            prefix;
124         char            *realval, *tmpval;
125         char            buf[BUFSIZ];
126
127         char            *at_cn;
128
129         ldbm_datum_init( key );
130
131         prefix = index2prefix( indextype );
132         Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
133             type, prefix, val );
134
135         attr_masks( be->be_private, type, &indexmask, &syntax );
136         if ( ! (indextype & indexmask) ) {
137                 idl =  idl_allids( be );
138                 Debug( LDAP_DEBUG_TRACE,
139                     "<= index_read %ld candidates (allids - not indexed)\n",
140                     idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
141                 return( idl );
142         }
143
144         attr_normalize( type );
145         at_cn = at_canonical_name( type );
146
147         if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, LDBM_WRCREAT ))
148             == NULL ) {
149                 Debug( LDAP_DEBUG_ANY,
150                     "<= index_read NULL (could not open %s%s)\n", at_cn,
151                     LDBM_SUFFIX, 0 );
152                 return( NULL );
153         }
154
155         realval = val;
156         tmpval = NULL;
157         if ( prefix != UNKNOWN_PREFIX ) {
158               unsigned int      len = strlen( val );
159
160               if ( (len + 2) < sizeof(buf) ) {
161                         realval = buf;
162                 } else {
163                         /* value + prefix + null */
164                         tmpval = (char *) ch_malloc( len + 2 );
165                         realval = tmpval;
166                 }
167               realval[0] = prefix;
168               strcpy( &realval[1], val );
169         }
170
171         key.dptr = realval;
172         key.dsize = strlen( realval ) + 1;
173
174         idl = idl_fetch( be, db, key );
175         if ( tmpval != NULL ) {
176               free( tmpval );
177         }
178
179         ldbm_cache_close( be, db );
180
181         Debug( LDAP_DEBUG_TRACE, "<= index_read %ld candidates\n",
182                idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
183         return( idl );
184 }
185
186 /* Add or remove stuff from index files */
187
188 static int
189 change_value(
190     Backend             *be,
191     DBCache     *db,
192     char                *type,
193     int                 indextype,
194     char                *val,
195     ID                  id,
196     int                 (*idl_func)(Backend *, DBCache *, Datum, ID)
197 )
198 {
199         int     rc;
200         Datum   key;
201         char    *tmpval = NULL;
202         char    *realval = val;
203         char    buf[BUFSIZ];
204
205         char    prefix = index2prefix( indextype );
206
207         ldbm_datum_init( key );
208
209         Debug( LDAP_DEBUG_TRACE,
210                "=> change_value( \"%c%s\", op=%s )\n",
211                prefix, val, (idl_func == idl_insert_key ? "ADD":"DELETE") );
212
213         if ( prefix != UNKNOWN_PREFIX ) {
214               unsigned int     len = strlen( val );
215
216               if ( (len + 2) < sizeof(buf) ) {
217                         realval = buf;
218               } else {
219                         /* value + prefix + null */
220                         tmpval = (char *) ch_malloc( len + 2 );
221                         realval = tmpval;
222               }
223               realval[0] = prefix;
224               strcpy( &realval[1], val );
225         }
226
227         key.dptr = realval;
228         key.dsize = strlen( realval ) + 1;
229
230         rc = idl_func( be, db, key, id );
231
232         if ( tmpval != NULL ) {
233                 free( tmpval );
234         }
235
236         ldap_pvt_thread_yield();
237
238         Debug( LDAP_DEBUG_TRACE, "<= change_value %d\n", rc, 0, 0 );
239
240         return( rc );
241
242 }/* static int change_value() */
243
244
245 int
246 index_change_values(
247     Backend             *be,
248     char                *type,
249     struct berval       **vals,
250     ID                  id,
251     unsigned int        op
252 )
253 {
254         char            *val, *p, *code, *w;
255         unsigned        i, j, len;
256         int             indexmask, syntax;
257         char            buf[SUBLEN + 1];
258         char            vbuf[BUFSIZ];
259         char            *bigbuf;
260         DBCache *db;
261
262         int             (*idl_funct)(Backend *,
263                                     DBCache *,
264                                     Datum, ID);
265         char            *at_cn; /* Attribute canonical name */
266         int             mode;
267
268         if( vals == NULL ) {
269                 Debug( LDAP_DEBUG_TRACE,
270                         "=> index_change_values( %s, NULL, %ld, op=%s )\n", 
271                         type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
272                 return 0;
273         }
274
275         Debug( LDAP_DEBUG_TRACE,
276                "=> index_change_values( \"%s\", %ld, op=%s )\n", 
277                type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
278
279         
280         if (op == __INDEX_ADD_OP) {
281
282             /* Add values */
283
284             idl_funct =  idl_insert_key;
285             mode = LDBM_WRCREAT;
286
287         } else {
288
289             /* Delete values */
290
291             idl_funct = idl_delete_key;
292             mode = LDBM_WRITER;
293
294         }
295
296         attr_normalize(type);
297         attr_masks( be->be_private, type, &indexmask, &syntax );
298
299         if ( indexmask == 0 ) {
300                 return( 0 );
301         }
302
303         at_cn = at_canonical_name( type );
304
305         if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
306              == NULL ) {
307                 Debug( LDAP_DEBUG_ANY,
308                        "<= index_change_values (couldn't open(%s%s),md=%s)\n",
309                        at_cn,
310                        LDBM_SUFFIX,
311                        ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
312                 return( -1 );
313         }
314
315
316         for ( i = 0; vals[i] != NULL; i++ ) {
317                 /*
318                  * presence index entry
319                  */
320                 if ( indexmask & INDEX_PRESENCE ) {
321
322                         change_value( be, db, at_cn, INDEX_PRESENCE,
323                                       "*", id, idl_funct );
324
325                 }
326
327                 Debug( LDAP_DEBUG_TRACE,
328                        "index_change_values syntax 0x%x syntax bin 0x%x\n",
329                        syntax, SYNTAX_BIN, 0 );
330
331                 if ( syntax & SYNTAX_BIN ) {
332
333                         ldbm_cache_close( be, db );
334                         return( 0 );
335
336                 }
337
338                 bigbuf = NULL;
339                 len = vals[i]->bv_len;
340
341                 /* value + null */
342                 if ( len + 2 > sizeof(vbuf) ) {
343                         bigbuf = (char *) ch_malloc( len + 1 );
344                         val = bigbuf;
345                 } else {
346                         val = vbuf;
347                 }
348                 (void) memcpy( val, vals[i]->bv_val, len );
349                 val[len] = '\0';
350
351                 value_normalize( val, syntax );
352
353                 /* value_normalize could change the length of val */
354                 len = strlen( val );
355
356                 /*
357                  * equality index entry
358                  */
359                 if ( indexmask & INDEX_EQUALITY ) {
360                     
361                         change_value( be, db, at_cn, INDEX_EQUALITY,
362                                       val, id, idl_funct);
363
364                 }
365
366                 /*
367                  * approximate index entry
368                  */
369                 if ( indexmask & INDEX_APPROX ) {
370                         for ( w = first_word( val ); w != NULL;
371                             w = next_word( w ) ) {
372                                 if ( (code = phonetic( w )) != NULL ) {
373                                         change_value( be,
374                                                       db,
375                                                       at_cn,
376                                                       INDEX_APPROX,
377                                                       code,
378                                                       id,
379                                                       idl_funct );
380                                         free( code );
381                                 }
382                         }
383                 }
384
385                 /*
386                  * substrings index entry
387                  */
388                 if ( indexmask & INDEX_SUB ) {
389                         /* leading and trailing */
390                         if ( len > SUBLEN - 2 ) {
391                                 buf[0] = '^';
392                                 for ( j = 0; j < SUBLEN - 1; j++ ) {
393                                         buf[j + 1] = val[j];
394                                 }
395                                 buf[SUBLEN] = '\0';
396
397                                 change_value( be, db, at_cn, INDEX_SUB,
398                                               buf, id, idl_funct );
399
400                                 p = val + len - SUBLEN + 1;
401                                 for ( j = 0; j < SUBLEN - 1; j++ ) {
402                                         buf[j] = p[j];
403                                 }
404                                 buf[SUBLEN - 1] = '$';
405                                 buf[SUBLEN] = '\0';
406
407                                 change_value( be, db, at_cn, INDEX_SUB,
408                                               buf, id, idl_funct );
409                         }
410
411                         /* any */
412                         for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
413                                 for ( j = 0; j < SUBLEN; j++ ) {
414                                         buf[j] = p[j];
415                                 }
416                                 buf[SUBLEN] = '\0';
417
418                                 change_value( be, db, at_cn, INDEX_SUB,
419                                               buf, id, idl_funct );
420                         }
421                 }
422
423                 if ( bigbuf != NULL ) {
424                         free( bigbuf );
425                 }
426         }
427
428         ldbm_cache_close( be, db );
429
430         return( 0 );
431
432 }/* int index_change_values() */
433
434 static int
435 index2prefix( int indextype )
436 {
437         int     prefix;
438
439         switch ( indextype ) {
440         case INDEX_EQUALITY:
441                 prefix = EQ_PREFIX;
442                 break;
443         case INDEX_APPROX:
444                 prefix = APPROX_PREFIX;
445                 break;
446         case INDEX_SUB:
447                 prefix = SUB_PREFIX;
448                 break;
449         default:
450                 prefix = UNKNOWN_PREFIX;
451                 break;
452         }
453
454         return( prefix );
455 }