]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/idl.c
Use #ifdef, not #if
[openldap] / servers / slapd / back-bdb2 / idl.c
1 /* idl.c - ldap id list handling routines */
2 /* $OpenLDAP$ */
3
4 #include "portable.h"
5
6 #include <stdio.h>
7
8 #include <ac/string.h>
9 #include <ac/socket.h>
10
11 #include "slap.h"
12 #include "back-bdb2.h"
13
14 static ID_BLOCK* idl_dup( ID_BLOCK *idl );
15
16 /* Allocate an ID_BLOCK with room for nids ids */
17 ID_BLOCK *
18 bdb2i_idl_alloc( unsigned int nids )
19 {
20         ID_BLOCK        *new;
21
22         /* nmax + nids + space for the ids */
23         new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
24         ID_BLOCK_NMAX(new) = nids;
25         ID_BLOCK_NIDS(new) = 0;
26
27         return( new );
28 }
29
30
31 /* Allocate an empty ALLIDS ID_BLOCK */
32 ID_BLOCK        *
33 bdb2i_idl_allids( BackendDB *be )
34 {
35         ID_BLOCK        *idl;
36
37         idl = bdb2i_idl_alloc( 0 );
38         ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
39         ID_BLOCK_NIDS(idl) = bdb2i_next_id_get( be );
40
41         return( idl );
42 }
43
44
45 /* Free an ID_BLOCK */
46 void
47 bdb2i_idl_free( ID_BLOCK *idl )
48 {
49         if ( idl == NULL ) {
50                 Debug( LDAP_DEBUG_TRACE,
51                         "bdb2i_idl_free: called with NULL pointer\n",
52                         0, 0, 0 );
53                 return;
54         }
55
56         free( (char *) idl );
57 }
58
59
60 /* Fetch an single ID_BLOCK from the cache */
61 static ID_BLOCK *
62 idl_fetch_one(
63     BackendDB           *be,
64     struct dbcache      *db,
65     Datum               key
66 )
67 {
68         Datum   data;
69         ID_BLOCK        *idl;
70
71         ldbm_datum_init( data );
72
73         /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
74
75         data = bdb2i_cache_fetch( db, key );
76
77         if( data.dptr == NULL ) {
78                 return NULL;
79         }
80
81         idl = idl_dup( (ID_BLOCK *) data.dptr);
82         ldbm_datum_free( db->dbc_db, data );
83
84         return( idl );
85 }
86
87
88 /* Fetch a set of ID_BLOCKs from the cache
89  *      if not INDIRECT
90  *              if block return is an ALLIDS block,
91  *                      return an new ALLIDS block
92  *              otherwise
93  *                      return block
94  *      construct super block from all blocks referenced by INDIRECT block
95  *      return super block
96  */
97 ID_BLOCK *
98 bdb2i_idl_fetch(
99     BackendDB           *be,
100     struct dbcache      *db,
101     Datum               key
102 )
103 {
104         Datum   data;
105         ID_BLOCK        *idl;
106         ID_BLOCK        **tmp;
107         char    *kstr;
108         int     i, nids;
109
110         idl = idl_fetch_one( be, db, key );
111
112         if ( idl == NULL ) { 
113                 return NULL;
114         }
115
116         if ( ID_BLOCK_ALLIDS(idl) ) {
117                 /* all ids block */
118                 /* make sure we have the current value of highest id */
119                 bdb2i_idl_free( idl );
120                 idl = bdb2i_idl_allids( be );
121
122                 return( idl );
123         }
124
125         if ( ! ID_BLOCK_INDIRECT( idl ) ) {
126                 /* regular block */
127                 return( idl );
128         }
129
130         /*
131          * this is an indirect block which points to other blocks.
132          * we need to read in all the blocks it points to and construct
133          * a big id list containing all the ids, which we will return.
134          */
135
136         /* count the number of blocks & allocate space for pointers to them */
137         for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
138                 ;       /* NULL */
139         tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
140
141         /* read in all the blocks */
142         kstr = (char *) ch_malloc( key.dsize + CONT_SIZE );
143         nids = 0;
144         for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
145                 ldbm_datum_init( data );
146
147                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
148                         ID_BLOCK_ID(idl, i), key.dptr );
149                 data.dptr = kstr;
150                 data.dsize = strlen( kstr ) + 1;
151
152                 if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
153                         Debug( LDAP_DEBUG_ANY,
154                             "bdb2i_idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
155                         continue;
156                 }
157
158                 nids += ID_BLOCK_NIDS(tmp[i]);
159         }
160         tmp[i] = NULL;
161         free( kstr );
162         bdb2i_idl_free( idl );
163
164         /* allocate space for the big block */
165         idl = bdb2i_idl_alloc( nids );
166         ID_BLOCK_NIDS(idl) = nids;
167         nids = 0;
168
169         /* copy in all the ids from the component blocks */
170         for ( i = 0; tmp[i] != NULL; i++ ) {
171                 if ( tmp[i] == NULL ) {
172                         continue;
173                 }
174
175                 SAFEMEMCPY(
176                         (char *) &ID_BLOCK_ID(idl, nids),
177                         (char *) &ID_BLOCK_ID(tmp[i], 0),
178                     ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
179                 nids += ID_BLOCK_NIDS(tmp[i]);
180
181                 bdb2i_idl_free( tmp[i] );
182         }
183         free( (char *) tmp );
184
185         Debug( LDAP_DEBUG_TRACE, "<= bdb2i_idl_fetch %ld ids (%ld max)\n",
186                ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl), 0 );
187         return( idl );
188 }
189
190
191 /* store a single block */
192 static int
193 idl_store(
194     BackendDB           *be,
195     struct dbcache      *db,
196     Datum               key, 
197     ID_BLOCK            *idl
198 )
199 {
200         int     rc, flags;
201         Datum   data;
202         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
203
204         ldbm_datum_init( data );
205
206         /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
207
208         data.dptr = (char *) idl;
209         data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAX(idl)) * sizeof(ID);
210         
211 #ifdef LDBM_DEBUG
212         Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
213                 rc, 0, 0, 0, 0 );
214 #endif
215
216         flags = LDBM_REPLACE;
217         rc = bdb2i_cache_store( db, key, data, flags );
218
219         /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
220         return( rc );
221 }
222
223
224 /* split the block at id
225  *    locate ID greater than or equal to id.
226  */
227 static void
228 idl_split_block(
229     ID_BLOCK    *b,
230     ID          id,
231     ID_BLOCK    **right,
232     ID_BLOCK    **left
233 )
234 {
235         unsigned int    nr, nl;
236
237         /* find where to split the block *//* XXX linear search XXX */
238         for ( nr = 0; nr < ID_BLOCK_NIDS(b) && id > ID_BLOCK_ID(b, nr); nr++ )
239                 ;       /* NULL */
240
241         nl = ID_BLOCK_NIDS(b) - nr;
242
243         *right = bdb2i_idl_alloc( nr == 0 ? 1 : nr );
244         *left = bdb2i_idl_alloc( nl + (nr == 0 ? 0 : 1));
245
246         /*
247          * everything before the id being inserted in the first block
248          * unless there is nothing, in which case the id being inserted
249          * goes there.
250          */
251         if ( nr == 0 ) {
252                 ID_BLOCK_NIDS(*right) = 1;
253                 ID_BLOCK_ID(*right, 0) = id;
254         } else {
255                 SAFEMEMCPY(
256                         (char *) &ID_BLOCK_ID(*right, 0),
257                         (char *) &ID_BLOCK_ID(b, 0),
258                         nr * sizeof(ID) );
259                 ID_BLOCK_NIDS(*right) = nr;
260                 ID_BLOCK_ID(*left, 0) = id;
261         }
262
263         /* the id being inserted & everything after in the second block */
264         SAFEMEMCPY(
265                 (char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
266             (char *) &ID_BLOCK_ID(b, nr),
267                 nl * sizeof(ID) );
268         ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
269 }
270
271
272 /*
273  * idl_change_first - called when an indirect block's first key has
274  * changed, meaning it needs to be stored under a new key, and the
275  * header block pointing to it needs updating.
276  */
277 static int
278 idl_change_first(
279     BackendDB           *be,
280     struct dbcache      *db,
281     Datum               hkey,           /* header block key     */
282     ID_BLOCK            *h,             /* header block         */
283     int                 pos,            /* pos in h to update   */
284     Datum               bkey,           /* data block key       */
285     ID_BLOCK            *b              /* data block           */
286 )
287 {
288         int     rc;
289
290         /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
291
292         /* delete old key block */
293         if ( (rc = bdb2i_cache_delete( db, bkey )) != 0 ) {
294                 Debug( LDAP_DEBUG_ANY,
295                     "bdb2i_db_delete of (%s) returns %d\n", bkey.dptr, rc,
296                     0 );
297                 return( rc );
298         }
299
300         /* write block with new key */
301         sprintf( bkey.dptr, "%c%ld%s", CONT_PREFIX,
302                         ID_BLOCK_ID(b, 0), hkey.dptr );
303         bkey.dsize = strlen( bkey.dptr ) + 1;
304         if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
305                 Debug( LDAP_DEBUG_ANY,
306                     "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
307                 return( rc );
308         }
309
310         /* update + write indirect header block */
311         ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
312         if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
313                 Debug( LDAP_DEBUG_ANY,
314                     "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
315                 return( rc );
316         }
317
318         return( 0 );
319 }
320
321
322 int
323 bdb2i_idl_insert_key(
324     BackendDB           *be,
325     struct dbcache      *db,
326     Datum               key,
327     ID                  id
328 )
329 {
330         int     i, j, first, rc;
331         ID_BLOCK        *idl, *tmp, *tmp2, *tmp3;
332         char    *kstr;
333         Datum   k2;
334
335         ldbm_datum_init( k2 );
336
337         if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
338 #ifdef LDBM_DEBUG
339                 Statslog( LDAP_DEBUG_STATS, "=> bdb2i_idl_insert_key(): no key yet\n",
340                         0, 0, 0, 0, 0 );
341 #endif
342
343                 idl = bdb2i_idl_alloc( 1 );
344                 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
345                 rc = idl_store( be, db, key, idl );
346
347                 bdb2i_idl_free( idl );
348                 return( rc );
349         }
350
351         if ( ID_BLOCK_ALLIDS( idl ) ) {
352                 /* ALLIDS */
353                 bdb2i_idl_free( idl );
354                 return 0;
355         }
356
357         if ( ! ID_BLOCK_INDIRECT( idl ) ) {
358                 /* regular block */
359                 switch ( bdb2i_idl_insert( &idl, id, db->dbc_maxids ) ) {
360                 case 0:         /* id inserted - store the updated block */
361                 case 1:
362                         rc = idl_store( be, db, key, idl );
363                         break;
364
365                 case 2:         /* id already there - nothing to do */
366                         rc = 0;
367                         break;
368
369                 case 3:         /* id not inserted - block must be split */
370                         /* check threshold for marking this an all-id block */
371                         if ( db->dbc_maxindirect < 2 ) {
372                                 bdb2i_idl_free( idl );
373                                 idl = bdb2i_idl_allids( be );
374                                 rc = idl_store( be, db, key, idl );
375                                 break;
376                         }
377
378                         idl_split_block( idl, id, &tmp, &tmp2 );
379                         bdb2i_idl_free( idl );
380
381                         /* create the header indirect block */
382                         idl = bdb2i_idl_alloc( 3 );
383                         ID_BLOCK_NMAX(idl) = 3;
384                         ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
385                         ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
386                         ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
387                         ID_BLOCK_ID(idl, 2) = NOID;
388
389                         /* store it */
390                         rc = idl_store( be, db, key, idl );
391
392                         /* store the first id block */
393                         kstr = (char *) ch_malloc( key.dsize + CONT_SIZE );
394                         sprintf( kstr, "%c%ld%s", CONT_PREFIX,
395                             ID_BLOCK_ID(tmp, 0), key.dptr );
396                         k2.dptr = kstr;
397                         k2.dsize = strlen( kstr ) + 1;
398                         rc = idl_store( be, db, k2, tmp );
399
400                         /* store the second id block */
401                         sprintf( kstr, "%c%ld%s", CONT_PREFIX,
402                             ID_BLOCK_ID(tmp2, 0), key.dptr );
403                         k2.dptr = kstr;
404                         k2.dsize = strlen( kstr ) + 1;
405                         rc = idl_store( be, db, k2, tmp2 );
406
407                         free( kstr );
408                         bdb2i_idl_free( tmp );
409                         bdb2i_idl_free( tmp2 );
410                         break;
411                 }
412
413                 bdb2i_idl_free( idl );
414                 return( rc );
415         }
416
417         /*
418          * this is an indirect block which points to other blocks.
419          * we need to read in the block into which the id should be
420          * inserted, then insert the id and store the block.  we might
421          * have to split the block if it is full, which means we also
422          * need to write a new "header" block.
423          */
424
425         /* select the block to try inserting into *//* XXX linear search XXX */
426         for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
427                 ;       /* NULL */
428         if ( i != 0 ) {
429                 i--;
430                 first = 0;
431         } else {
432                 first = 1;
433         }
434
435         /* get the block */
436         kstr = (char *) ch_malloc( key.dsize + CONT_SIZE );
437         sprintf( kstr, "%c%ld%s", CONT_PREFIX,
438                 ID_BLOCK_ID(idl, i), key.dptr );
439         k2.dptr = kstr;
440         k2.dsize = strlen( kstr ) + 1;
441         if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
442                 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
443                     k2.dptr, 0, 0 );
444                 free( kstr );
445                 bdb2i_idl_free( idl );
446                 return( -1 );
447         }
448
449         /* insert the id */
450         switch ( bdb2i_idl_insert( &tmp, id, db->dbc_maxids ) ) {
451         case 0:         /* id inserted ok */
452                 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
453                         Debug( LDAP_DEBUG_ANY,
454                             "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
455                 }
456                 break;
457
458         case 1:         /* id inserted - first id in block has changed */
459                 /*
460                  * key for this block has changed, so we have to
461                  * write the block under the new key, delete the
462                  * old key block + update and write the indirect
463                  * header block.
464                  */
465
466                 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
467                 break;
468
469         case 2:         /* id not inserted - already there, do nothing */
470                 rc = 0;
471                 break;
472
473         case 3:         /* id not inserted - block is full */
474                 /*
475                  * first, see if it will fit in the next block,
476                  * without splitting, unless we're trying to insert
477                  * into the beginning of the first block.
478                  */
479
480                 /* is there a next block? */
481                 if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
482                         /* read it in */
483                         sprintf( kstr, "%c%ld%s", CONT_PREFIX,
484                             ID_BLOCK_ID(idl, i + 1), key.dptr );
485                         k2.dptr = kstr;
486                         k2.dsize = strlen( kstr ) + 1;
487                         if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
488                                 Debug( LDAP_DEBUG_ANY,
489                                     "idl_fetch_one (%s) returns NULL\n",
490                                     k2.dptr, 0, 0 );
491                                 /* split the original block */
492                                 goto split;
493                         }
494
495                         switch ( (rc = bdb2i_idl_insert( &tmp2, id,
496                             db->dbc_maxids )) ) {
497                         case 1:         /* id inserted first in block */
498                                 rc = idl_change_first( be, db, key, idl,
499                                     i + 1, k2, tmp2 );
500                                 /* FALL */
501
502                         case 2:         /* id already there - how? */
503                         case 0:         /* id inserted */
504                                 if ( rc == 2 ) {
505                                         Debug( LDAP_DEBUG_ANY,
506                                             "id %ld already in next block\n",
507                                             id, 0, 0 );
508                                 }
509                                 free( kstr );
510                                 bdb2i_idl_free( tmp );
511                                 bdb2i_idl_free( tmp2 );
512                                 bdb2i_idl_free( idl );
513                                 return( 0 );
514
515                         case 3:         /* split the original block */
516                                 break;
517                         }
518
519                         bdb2i_idl_free( tmp2 );
520                 }
521
522 split:
523                 /*
524                  * must split the block, write both new blocks + update
525                  * and write the indirect header block.
526                  */
527
528                 rc = 0; /* optimistic */
529
530
531                 /* count how many indirect blocks *//* XXX linear count XXX */
532                 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
533                         ;       /* NULL */
534
535                 /* check it against all-id thresholed */
536                 if ( j + 1 > db->dbc_maxindirect ) {
537                         /*
538                          * we've passed the all-id threshold, meaning
539                          * that this set of blocks should be replaced
540                          * by a single "all-id" block.  our job: delete
541                          * all the indirect blocks, and replace the header
542                          * block by an all-id block.
543                          */
544
545                         /* delete all indirect blocks */
546                         for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
547                                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
548                                     ID_BLOCK_ID(idl, j), key.dptr );
549                                 k2.dptr = kstr;
550                                 k2.dsize = strlen( kstr ) + 1;
551
552                                 rc = bdb2i_cache_delete( db, k2 );
553                         }
554
555                         /* store allid block in place of header block */
556                         bdb2i_idl_free( idl );
557                         idl = bdb2i_idl_allids( be );
558                         rc = idl_store( be, db, key, idl );
559
560                         free( kstr );
561                         bdb2i_idl_free( idl );
562                         bdb2i_idl_free( tmp );
563                         return( rc );
564                 }
565
566                 idl_split_block( tmp, id, &tmp2, &tmp3 );
567                 bdb2i_idl_free( tmp );
568
569                 /* create a new updated indirect header block */
570                 tmp = bdb2i_idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
571                 ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
572                 /* everything up to the split block */
573                 SAFEMEMCPY(
574                         (char *) &ID_BLOCK_ID(tmp, 0),
575                         (char *) &ID_BLOCK_ID(idl, 0),
576                     i * sizeof(ID) );
577                 /* the two new blocks */
578                 ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
579                 ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
580                 /* everything after the split block */
581                 SAFEMEMCPY(
582                         (char *) &ID_BLOCK_ID(tmp, i + 2),
583                         (char *) &ID_BLOCK_ID(idl, i + 1),
584                     (ID_BLOCK_NMAX(idl) - i - 1) * sizeof(ID) );
585
586                 /* store the header block */
587                 rc = idl_store( be, db, key, tmp );
588
589                 /* store the first id block */
590                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
591                     ID_BLOCK_ID(tmp2, 0), key.dptr );
592                 k2.dptr = kstr;
593                 k2.dsize = strlen( kstr ) + 1;
594                 rc = idl_store( be, db, k2, tmp2 );
595
596                 /* store the second id block */
597                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
598                     ID_BLOCK_ID(tmp3, 0), key.dptr );
599                 k2.dptr = kstr;
600                 k2.dsize = strlen( kstr ) + 1;
601                 rc = idl_store( be, db, k2, tmp3 );
602
603                 bdb2i_idl_free( tmp2 );
604                 bdb2i_idl_free( tmp3 );
605                 break;
606         }
607
608         free( kstr );
609         bdb2i_idl_free( tmp );
610         bdb2i_idl_free( idl );
611         return( rc );
612 }
613
614
615 /*
616  * bdb2i_idl_insert - insert an id into an id list.
617  *
618  * returns
619  *              0       id inserted
620  *              1       id inserted, first id in block has changed
621  *              2       id not inserted, already there
622  *              3       id not inserted, block must be split
623  */
624 int
625 bdb2i_idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
626 {
627         unsigned int    i;
628
629         if ( ID_BLOCK_ALLIDS( *idl ) ) {
630                 return( 2 );    /* already there */
631         }
632
633         /* is it already there? *//* XXX linear search XXX */
634         for ( i = 0; i < ID_BLOCK_NIDS(*idl) && id > ID_BLOCK_ID(*idl, i); i++ ) {
635                 ;       /* NULL */
636         }
637         if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
638                 return( 2 );    /* already there */
639         }
640
641         /* do we need to make room for it? */
642         if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
643                 /* make room or indicate block needs splitting */
644                 if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
645                         return( 3 );    /* block needs splitting */
646                 }
647
648                 ID_BLOCK_NMAX(*idl) *= 2;
649                 if ( ID_BLOCK_NMAX(*idl) > maxids ) {
650                         ID_BLOCK_NMAX(*idl) = maxids;
651                 }
652                 *idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
653                     (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
654         }
655
656         /* make a slot for the new id */
657         SAFEMEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
658                     (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
659
660         ID_BLOCK_ID(*idl, i) = id;
661         ID_BLOCK_NIDS(*idl)++;
662         (void) memset(
663                 (char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
664                 '\0',
665             (ID_BLOCK_NMAX(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
666
667         return( i == 0 ? 1 : 0 );       /* inserted - first id changed or not */
668 }
669
670
671 int
672 bdb2i_idl_delete_key (
673         BackendDB         *be,
674         struct dbcache  *db,
675         Datum           key,
676         ID              id
677 )
678 {
679         Datum  data;
680         ID_BLOCK *idl, *tmp;
681         unsigned i;
682         int j, nids;
683         char    *kstr;
684
685         if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
686         {
687                 /* It wasn't found.  Hmm... */
688                 return -1;
689         }
690
691         if ( ID_BLOCK_ALLIDS( idl ) ) {
692                 bdb2i_idl_free( idl );
693                 return 0;
694         }
695
696         if ( ! ID_BLOCK_INDIRECT( idl ) ) {
697                 for ( i=0; i < ID_BLOCK_NIDS(idl); i++ ) {
698                         if ( ID_BLOCK_ID(idl, i) == id ) {
699                                 if( --ID_BLOCK_NIDS(idl) == 0 ) {
700                                         bdb2i_cache_delete( db, key );
701
702                                 } else {
703                                         SAFEMEMCPY (
704                                                 &ID_BLOCK_ID(idl, i),
705                                                 &ID_BLOCK_ID(idl, i+1),
706                                                 (ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
707
708                                         ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
709
710                                         idl_store( be, db, key, idl );
711                                 }
712
713                                 bdb2i_idl_free( idl );
714                                 return 0;
715                         }
716                         /*  We didn't find the ID.  Hmmm... */
717                 }
718                 bdb2i_idl_free( idl );
719                 return -1;
720         }
721         
722         /* We have to go through an indirect block and find the ID
723            in the list of IDL's
724            */
725         for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
726                 ;       /* NULL */
727         kstr = (char *) ch_malloc( key.dsize + CONT_SIZE );
728         for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
729         {
730                 ldbm_datum_init( data );
731                 sprintf( kstr, "%c%ld%s", CONT_PREFIX, ID_BLOCK_ID(idl, j), key.dptr );
732                 data.dptr = kstr;
733                 data.dsize = strlen( kstr ) + 1;
734
735                 if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
736                         Debug( LDAP_DEBUG_ANY,
737                             "bdb2i_idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
738                         continue;
739                 }
740                 /*
741                    Now try to find the ID in tmp
742                 */
743                 for ( i=0; i < ID_BLOCK_NIDS(tmp); i++ )
744                 {
745                         if ( ID_BLOCK_ID(tmp, i) == id )
746                         {
747                                 SAFEMEMCPY(
748                                         &ID_BLOCK_ID(tmp, i),
749                                         &ID_BLOCK_ID(tmp, i+1),
750                                         (ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
751                                 ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
752                                 ID_BLOCK_NIDS(tmp)--;
753
754                                 if ( ID_BLOCK_NIDS(tmp) ) {
755                                         idl_store ( be, db, data, tmp );
756
757                                 } else {
758                                         bdb2i_cache_delete( db, data );
759                                         SAFEMEMCPY(
760                                                 &ID_BLOCK_ID(idl, j),
761                                                 &ID_BLOCK_ID(idl, j+1),
762                                                 (nids-(j+1)) * sizeof(ID));
763                                         ID_BLOCK_ID(idl, nids-1) = NOID;
764                                         nids--;
765                                         if ( ! nids )
766                                                 bdb2i_cache_delete( db, key );
767                                         else
768                                                 idl_store( be, db, key, idl );
769                                 }
770                                 bdb2i_idl_free( tmp );
771                                 free( kstr );
772                                 bdb2i_idl_free( idl );
773                                 return 0;
774                         }
775                 }
776                 bdb2i_idl_free( tmp );
777         }
778         free( kstr );
779         bdb2i_idl_free( idl );
780         return -1;
781 }
782
783
784 /* return a duplicate of a single ID_BLOCK */
785 static ID_BLOCK *
786 idl_dup( ID_BLOCK *idl )
787 {
788         ID_BLOCK        *new;
789
790         if ( idl == NULL ) {
791                 return( NULL );
792         }
793
794         new = bdb2i_idl_alloc( ID_BLOCK_NMAX(idl) );
795         SAFEMEMCPY(
796                 (char *) new,
797                 (char *) idl,
798                 (ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
799
800         return( new );
801 }
802
803
804 /* return the smaller ID_BLOCK */
805 static ID_BLOCK *
806 idl_min( ID_BLOCK *a, ID_BLOCK *b )
807 {
808         return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
809 }
810
811
812 /*
813  * bdb2i_idl_intersection - return a intersection b
814  */
815 ID_BLOCK *
816 bdb2i_idl_intersection(
817     BackendDB   *be,
818     ID_BLOCK    *a,
819     ID_BLOCK    *b
820 )
821 {
822         unsigned int    ai, bi, ni;
823         ID_BLOCK                *n;
824
825         if ( a == NULL || b == NULL ) {
826                 return( NULL );
827         }
828         if ( ID_BLOCK_ALLIDS( a ) ) {
829                 return( idl_dup( b ) );
830         }
831         if ( ID_BLOCK_ALLIDS( b ) ) {
832                 return( idl_dup( a ) );
833         }
834
835         n = idl_dup( idl_min( a, b ) );
836
837         for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
838                 for ( ;
839                         bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
840                         bi++ )
841                 {
842                         ;       /* NULL */
843                 }
844
845                 if ( bi == ID_BLOCK_NIDS(b) ) {
846                         break;
847                 }
848
849                 if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
850                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
851                 }
852         }
853
854         if ( ni == 0 ) {
855                 bdb2i_idl_free( n );
856                 return( NULL );
857         }
858         ID_BLOCK_NIDS(n) = ni;
859
860         return( n );
861 }
862
863
864 /*
865  * bdb2i_idl_union - return a union b
866  */
867 ID_BLOCK *
868 bdb2i_idl_union(
869     BackendDB   *be,
870     ID_BLOCK    *a,
871     ID_BLOCK    *b
872 )
873 {
874         unsigned int    ai, bi, ni;
875         ID_BLOCK                *n;
876
877         if ( a == NULL ) {
878                 return( idl_dup( b ) );
879         }
880         if ( b == NULL ) {
881                 return( idl_dup( a ) );
882         }
883         if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
884                 return( bdb2i_idl_allids( be ) );
885         }
886
887         if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
888                 n = a;
889                 a = b;
890                 b = n;
891         }
892
893         n = bdb2i_idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
894
895         for ( ni = 0, ai = 0, bi = 0;
896                 ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
897                 )
898         {
899                 if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
900                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
901
902                 } else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
903                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
904
905                 } else {
906                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
907                         ai++, bi++;
908                 }
909         }
910
911         for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
912                 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
913         }
914         for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
915                 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
916         }
917         ID_BLOCK_NIDS(n) = ni;
918
919         return( n );
920 }
921
922
923 /*
924  * bdb2i_idl_notin - return a intersection ~b (or a minus b)
925  */
926 ID_BLOCK *
927 bdb2i_idl_notin(
928     BackendDB   *be,
929     ID_BLOCK    *a,
930     ID_BLOCK    *b
931 )
932 {
933         unsigned int    ni, ai, bi;
934         ID_BLOCK                *n;
935
936         if ( a == NULL ) {
937                 return( NULL );
938         }
939         if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
940                 return( idl_dup( a ) );
941         }
942
943         if ( ID_BLOCK_ALLIDS( a ) ) {
944                 n = bdb2i_idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
945                 ni = 0;
946
947                 for ( ai = 1, bi = 0;
948                         ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n) && bi < ID_BLOCK_NMAX(b);
949                         ai++ )
950                 {
951                         if ( ID_BLOCK_ID(b, bi) == ai ) {
952                                 bi++;
953                         } else {
954                                 ID_BLOCK_ID(n, ni++) = ai;
955                         }
956                 }
957
958                 for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n); ai++ ) {
959                         ID_BLOCK_ID(n, ni++) = ai;
960                 }
961
962                 if ( ni == ID_BLOCK_NMAX(n) ) {
963                         bdb2i_idl_free( n );
964                         return( bdb2i_idl_allids( be ) );
965                 } else {
966                         ID_BLOCK_NIDS(n) = ni;
967                         return( n );
968                 }
969         }
970
971         n = idl_dup( a );
972
973         ni = 0;
974         for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
975                 for ( ;
976                         bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
977                     bi++ )
978                 {
979                         ;       /* NULL */
980                 }
981
982                 if ( bi == ID_BLOCK_NIDS(b) ) {
983                         break;
984                 }
985
986                 if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
987                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
988                 }
989         }
990
991         for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
992                 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
993         }
994         ID_BLOCK_NIDS(n) = ni;
995
996         return( n );
997 }
998
999
1000 /*      return the first ID in the block
1001  *      if ALLIDS block
1002  *              NIDS > 1 return 1
1003  *              otherwise return NOID 
1004  *      otherwise return first ID
1005  *
1006  *  cursor is set to 1
1007  */         
1008 ID
1009 bdb2i_idl_firstid( ID_BLOCK *idl, ID *cursor )
1010 {
1011         *cursor = 1;
1012
1013         if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
1014                 return( NOID );
1015         }
1016
1017         if ( ID_BLOCK_ALLIDS( idl ) ) { 
1018                 return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
1019         }
1020
1021         return( ID_BLOCK_ID(idl, 0) );
1022 }
1023
1024
1025 /*      return next ID after id
1026  *      if ALLIDS block, cursor is id
1027  *              increment id
1028  *              if id < NIDS return id
1029  *              otherwise NOID.
1030  *      otherwise cursor is index into block
1031  *              if index < nids
1032  *                      return id at index then increment
1033  */ 
1034 ID
1035 bdb2i_idl_nextid( ID_BLOCK *idl, ID *cursor )
1036 {
1037         if ( ID_BLOCK_ALLIDS( idl ) ) {
1038                 if( ++(*cursor) < ID_BLOCK_NIDS(idl) ) {
1039                         return *cursor;
1040                 } else {
1041                         return NOID;
1042                 }
1043         }
1044
1045         if ( *cursor < ID_BLOCK_NIDS(idl) ) {
1046                 return ID_BLOCK_ID(idl, (*cursor)++);
1047         }
1048         return NOID;
1049 }