]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/index.c
5f8b7cec646450c7ff35bb7a1cea0a03d651ee14
[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                           struct dbcache *db,
15                           char *type,
16                           int indextype,
17                           char *val,
18                           ID id,
19                           int
20                           (*idl_func)(Backend *, struct 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         struct 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     struct dbcache      *db,
192     char                *type,
193     int                 indextype,
194     char                *val,
195     ID                  id,
196     int                 (*idl_func)(Backend *, struct 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         struct dbcache  *db;
261
262         int             (*idl_funct)(Backend *,
263                                     struct dbcache *,
264                                     Datum, ID);
265         char            *at_cn; /* Attribute canonical name */
266         int             mode;
267
268         Debug( LDAP_DEBUG_TRACE,
269                "=> index_change_values( \"%s\", %ld, op=%s )\n", 
270                type, id, ((op == __INDEX_ADD_OP) ? "ADD" : "DELETE" ) );
271
272         
273         if (op == __INDEX_ADD_OP) {
274
275             /* Add values */
276
277             idl_funct =  idl_insert_key;
278             mode = LDBM_WRCREAT;
279
280         } else {
281
282             /* Delete values */
283
284             idl_funct = idl_delete_key;
285             mode = LDBM_WRITER;
286
287         }
288
289         attr_normalize(type);
290         attr_masks( be->be_private, type, &indexmask, &syntax );
291
292         if ( indexmask == 0 ) {
293                 return( 0 );
294         }
295
296         at_cn = at_canonical_name( type );
297
298         if ( (db = ldbm_cache_open( be, at_cn, LDBM_SUFFIX, mode ))
299              == NULL ) {
300                 Debug( LDAP_DEBUG_ANY,
301                        "<= index_change_values (couldn't open(%s%s),md=%s)\n",
302                        at_cn,
303                        LDBM_SUFFIX,
304                        ((mode==LDBM_WRCREAT)?"LDBM_WRCREAT":"LDBM_WRITER") );
305                 return( -1 );
306         }
307
308
309         for ( i = 0; vals[i] != NULL; i++ ) {
310                 /*
311                  * presence index entry
312                  */
313                 if ( indexmask & INDEX_PRESENCE ) {
314
315                         change_value( be, db, at_cn, INDEX_PRESENCE,
316                                       "*", id, idl_funct );
317
318                 }
319
320                 Debug( LDAP_DEBUG_TRACE,
321                        "index_change_values syntax 0x%x syntax bin 0x%x\n",
322                        syntax, SYNTAX_BIN, 0 );
323
324                 if ( syntax & SYNTAX_BIN ) {
325
326                         ldbm_cache_close( be, db );
327                         return( 0 );
328
329                 }
330
331                 bigbuf = NULL;
332                 len = vals[i]->bv_len;
333
334                 /* value + null */
335                 if ( len + 2 > sizeof(vbuf) ) {
336                         bigbuf = (char *) ch_malloc( len + 1 );
337                         val = bigbuf;
338                 } else {
339                         val = vbuf;
340                 }
341                 (void) memcpy( val, vals[i]->bv_val, len );
342                 val[len] = '\0';
343
344                 value_normalize( val, syntax );
345
346                 /* value_normalize could change the length of val */
347                 len = strlen( val );
348
349                 /*
350                  * equality index entry
351                  */
352                 if ( indexmask & INDEX_EQUALITY ) {
353                     
354                         change_value( be, db, at_cn, INDEX_EQUALITY,
355                                       val, id, idl_funct);
356
357                 }
358
359                 /*
360                  * approximate index entry
361                  */
362                 if ( indexmask & INDEX_APPROX ) {
363                         for ( w = first_word( val ); w != NULL;
364                             w = next_word( w ) ) {
365                                 if ( (code = phonetic( w )) != NULL ) {
366                                         change_value( be,
367                                                       db,
368                                                       at_cn,
369                                                       INDEX_APPROX,
370                                                       code,
371                                                       id,
372                                                       idl_funct );
373                                         free( code );
374                                 }
375                         }
376                 }
377
378                 /*
379                  * substrings index entry
380                  */
381                 if ( indexmask & INDEX_SUB ) {
382                         /* leading and trailing */
383                         if ( len > SUBLEN - 2 ) {
384                                 buf[0] = '^';
385                                 for ( j = 0; j < SUBLEN - 1; j++ ) {
386                                         buf[j + 1] = val[j];
387                                 }
388                                 buf[SUBLEN] = '\0';
389
390                                 change_value( be, db, at_cn, INDEX_SUB,
391                                               buf, id, idl_funct );
392
393                                 p = val + len - SUBLEN + 1;
394                                 for ( j = 0; j < SUBLEN - 1; j++ ) {
395                                         buf[j] = p[j];
396                                 }
397                                 buf[SUBLEN - 1] = '$';
398                                 buf[SUBLEN] = '\0';
399
400                                 change_value( be, db, at_cn, INDEX_SUB,
401                                               buf, id, idl_funct );
402                         }
403
404                         /* any */
405                         for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
406                                 for ( j = 0; j < SUBLEN; j++ ) {
407                                         buf[j] = p[j];
408                                 }
409                                 buf[SUBLEN] = '\0';
410
411                                 change_value( be, db, at_cn, INDEX_SUB,
412                                               buf, id, idl_funct );
413                         }
414                 }
415
416                 if ( bigbuf != NULL ) {
417                         free( bigbuf );
418                 }
419         }
420         ldbm_cache_close( be, db );
421
422         return( 0 );
423
424 }/* int index_change_values() */
425
426 static int
427 index2prefix( int indextype )
428 {
429         int     prefix;
430
431         switch ( indextype ) {
432         case INDEX_EQUALITY:
433                 prefix = EQ_PREFIX;
434                 break;
435         case INDEX_APPROX:
436                 prefix = APPROX_PREFIX;
437                 break;
438         case INDEX_SUB:
439                 prefix = SUB_PREFIX;
440                 break;
441         default:
442                 prefix = UNKNOWN_PREFIX;
443                 break;
444         }
445
446         return( prefix );
447 }