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