1 /* idl.c - ldap id list handling routines */
6 #include <sys/socket.h>
9 #include "ldapconfig.h"
10 #include "back-ldbm.h"
12 extern Datum ldbm_cache_fetch();
19 /* nmax + nids + space for the ids */
20 new = (IDList *) ch_calloc( (2 + nids), sizeof(ID) );
28 idl_allids( Backend *be )
33 idl->b_nmax = ALLIDSBLOCK;
34 idl->b_nids = next_id_get( be );
40 idl_free( IDList *idl )
62 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
64 data = ldbm_cache_fetch( db, key );
66 idl = (IDList *) data.dptr;
84 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
86 data = ldbm_cache_fetch( db, key );
88 if ( (idl = (IDList *) data.dptr) == NULL ) {
93 if ( ! INDIRECT_BLOCK( idl ) ) {
95 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
96 idl->b_nids, idl->b_nmax, 0 );
99 /* make sure we have the current value of highest id */
100 if ( idl->b_nmax == ALLIDSBLOCK ) {
102 idl = idl_allids( be );
108 * this is an indirect block which points to other blocks.
109 * we need to read in all the blocks it points to and construct
110 * a big id list containing all the ids, which we will return.
113 /* count the number of blocks & allocate space for pointers to them */
114 for ( i = 0; idl->b_ids[i] != NOID; i++ )
116 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
118 /* read in all the blocks */
119 kstr = (char *) ch_malloc( key.dsize + 20 );
121 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
122 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
124 k2.dsize = strlen( kstr ) + 1;
126 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
127 Debug( LDAP_DEBUG_ANY,
128 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
132 nids += tmp[i]->b_nids;
137 /* allocate space for the big block */
138 idl = idl_alloc( nids );
142 /* copy in all the ids from the component blocks */
143 for ( i = 0; tmp[i] != NULL; i++ ) {
144 if ( tmp[i] == NULL ) {
148 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
149 tmp[i]->b_nids * sizeof(ID) );
150 nids += tmp[i]->b_nids;
154 free( (char *) tmp );
156 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n", idl->b_nids,
171 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
173 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
175 data.dptr = (char *) idl;
176 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
178 flags = LDBM_REPLACE;
179 if( li->li_flush_wrt ) flags |= LDBM_SYNC;
180 rc = ldbm_cache_store( db, key, data, flags );
182 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
196 /* find where to split the block */
197 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
200 *n1 = idl_alloc( i == 0 ? 1 : i );
201 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
204 * everything before the id being inserted in the first block
205 * unless there is nothing, in which case the id being inserted
208 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
210 (*n1)->b_nids = (i == 0 ? 1 : i);
213 (*n1)->b_ids[0] = id;
215 (*n2)->b_ids[0] = id;
218 /* the id being inserted & everything after in the second block */
219 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
220 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
221 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
225 * idl_change_first - called when an indirect block's first key has
226 * changed, meaning it needs to be stored under a new key, and the
227 * header block pointing to it needs updating.
234 Datum hkey, /* header block key */
235 IDList *h, /* header block */
236 int pos, /* pos in h to update */
237 Datum bkey, /* data block key */
238 IDList *b /* data block */
243 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
245 /* delete old key block */
246 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
247 Debug( LDAP_DEBUG_ANY,
248 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
253 /* write block with new key */
254 sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
255 bkey.dsize = strlen( bkey.dptr ) + 1;
256 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
257 Debug( LDAP_DEBUG_ANY,
258 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
262 /* update + write indirect header block */
263 h->b_ids[pos] = b->b_ids[0];
264 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
265 Debug( LDAP_DEBUG_ANY,
266 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
282 IDList *idl, *tmp, *tmp2, *tmp3;
286 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
287 idl = idl_alloc( 1 );
288 idl->b_ids[idl->b_nids++] = id;
289 rc = idl_store( be, db, key, idl );
296 if ( ! INDIRECT_BLOCK( idl ) ) {
297 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
298 case 0: /* id inserted - store the updated block */
300 rc = idl_store( be, db, key, idl );
303 case 2: /* id already there - nothing to do */
307 case 3: /* id not inserted - block must be split */
308 /* check threshold for marking this an all-id block */
309 if ( db->dbc_maxindirect < 2 ) {
311 idl = idl_allids( be );
312 rc = idl_store( be, db, key, idl );
318 idl_split_block( idl, id, &tmp, &tmp2 );
321 /* create the header indirect block */
322 idl = idl_alloc( 3 );
324 idl->b_nids = INDBLOCK;
325 idl->b_ids[0] = tmp->b_ids[0];
326 idl->b_ids[1] = tmp2->b_ids[0];
327 idl->b_ids[2] = NOID;
330 rc = idl_store( be, db, key, idl );
332 /* store the first id block */
333 kstr = (char *) ch_malloc( key.dsize + 20 );
334 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
337 k2.dsize = strlen( kstr ) + 1;
338 rc = idl_store( be, db, k2, tmp );
340 /* store the second id block */
341 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
344 k2.dsize = strlen( kstr ) + 1;
345 rc = idl_store( be, db, k2, tmp2 );
358 * this is an indirect block which points to other blocks.
359 * we need to read in the block into which the id should be
360 * inserted, then insert the id and store the block. we might
361 * have to split the block if it is full, which means we also
362 * need to write a new "header" block.
365 /* select the block to try inserting into */
366 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
376 kstr = (char *) ch_malloc( key.dsize + 20 );
377 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
379 k2.dsize = strlen( kstr ) + 1;
380 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
381 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
387 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
388 case 0: /* id inserted ok */
389 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
390 Debug( LDAP_DEBUG_ANY,
391 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
395 case 1: /* id inserted - first id in block has changed */
397 * key for this block has changed, so we have to
398 * write the block under the new key, delete the
399 * old key block + update and write the indirect
403 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
406 case 2: /* id not inserted - already there */
409 case 3: /* id not inserted - block is full */
411 * first, see if it will fit in the next block,
412 * without splitting, unless we're trying to insert
413 * into the beginning of the first block.
416 /* is there a next block? */
417 if ( !first && idl->b_ids[i + 1] != NOID ) {
419 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
422 k2.dsize = strlen( kstr ) + 1;
423 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
424 Debug( LDAP_DEBUG_ANY,
425 "idl_fetch_one (%s) returns NULL\n",
430 switch ( (rc = idl_insert( &tmp2, id,
431 db->dbc_maxids )) ) {
432 case 1: /* id inserted first in block */
433 rc = idl_change_first( be, db, key, idl,
437 case 2: /* id already there - how? */
438 case 0: /* id inserted */
440 Debug( LDAP_DEBUG_ANY,
441 "id %d already in next block\n",
450 case 3: /* split the original block */
458 * must split the block, write both new blocks + update
459 * and write the indirect header block.
462 /* count how many indirect blocks */
463 for ( j = 0; idl->b_ids[j] != NOID; j++ )
466 /* check it against all-id thresholed */
467 if ( j + 1 > db->dbc_maxindirect ) {
469 * we've passed the all-id threshold, meaning
470 * that this set of blocks should be replaced
471 * by a single "all-id" block. our job: delete
472 * all the indirect blocks, and replace the header
473 * block by an all-id block.
476 /* delete all indirect blocks */
477 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
478 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
481 k2.dsize = strlen( kstr ) + 1;
483 rc = ldbm_cache_delete( db, k2 );
486 /* store allid block in place of header block */
488 idl = idl_allids( be );
489 rc = idl_store( be, db, key, idl );
497 idl_split_block( tmp, id, &tmp2, &tmp3 );
500 /* create a new updated indirect header block */
501 tmp = idl_alloc( idl->b_nmax + 1 );
502 tmp->b_nids = INDBLOCK;
503 /* everything up to the split block */
504 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
506 /* the two new blocks */
507 tmp->b_ids[i] = tmp2->b_ids[0];
508 tmp->b_ids[i + 1] = tmp3->b_ids[0];
509 /* everything after the split block */
510 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
511 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
513 /* store the header block */
514 rc = idl_store( be, db, key, tmp );
516 /* store the first id block */
517 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
520 k2.dsize = strlen( kstr ) + 1;
521 rc = idl_store( be, db, k2, tmp2 );
523 /* store the second id block */
524 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
527 k2.dsize = strlen( kstr ) + 1;
528 rc = idl_store( be, db, k2, tmp3 );
542 * idl_insert - insert an id into an id list.
543 * returns 0 id inserted
544 * 1 id inserted, first id in block has changed
545 * 2 id not inserted, already there
546 * 3 id not inserted, block must be split
550 idl_insert( IDList **idl, ID id, int maxids )
554 if ( ALLIDS( *idl ) ) {
555 return( 2 ); /* already there */
558 /* is it already there? XXX bin search XXX */
559 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
562 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
563 return( 2 ); /* already there */
566 /* do we need to make room for it? */
567 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
568 /* make room or indicate block needs splitting */
569 if ( (*idl)->b_nmax == maxids ) {
570 return( 3 ); /* block needs splitting */
574 if ( (*idl)->b_nmax > maxids ) {
575 (*idl)->b_nmax = maxids;
577 *idl = (IDList *) ch_realloc( (char *) *idl,
578 ((*idl)->b_nmax + 2) * sizeof(ID) );
581 /* make a slot for the new id */
582 for ( j = (*idl)->b_nids; j != i; j-- ) {
583 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
585 (*idl)->b_ids[i] = id;
587 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
588 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
590 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
594 idl_dup( IDList *idl )
602 new = idl_alloc( idl->b_nmax );
603 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
610 idl_min( IDList *a, IDList *b )
612 return( a->b_nids > b->b_nids ? b : a );
616 * idl_intersection - return a intersection b
629 if ( a == NULL || b == NULL ) {
633 return( idl_dup( b ) );
636 return( idl_dup( a ) );
639 n = idl_dup( idl_min( a, b ) );
641 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
642 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
645 if ( bi == b->b_nids ) {
649 if ( b->b_ids[bi] == a->b_ids[ai] ) {
650 n->b_ids[ni++] = a->b_ids[ai];
664 * idl_union - return a union b
678 return( idl_dup( b ) );
681 return( idl_dup( a ) );
683 if ( ALLIDS( a ) || ALLIDS( b ) ) {
684 return( idl_allids( be ) );
687 if ( b->b_nids < a->b_nids ) {
693 n = idl_alloc( a->b_nids + b->b_nids );
695 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
696 if ( a->b_ids[ai] < b->b_ids[bi] ) {
697 n->b_ids[ni++] = a->b_ids[ai++];
698 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
699 n->b_ids[ni++] = b->b_ids[bi++];
701 n->b_ids[ni++] = a->b_ids[ai];
706 for ( ; ai < a->b_nids; ai++ ) {
707 n->b_ids[ni++] = a->b_ids[ai];
709 for ( ; bi < b->b_nids; bi++ ) {
710 n->b_ids[ni++] = b->b_ids[bi];
718 * idl_notin - return a intersection ~b (or a minus b)
734 if ( b == NULL || ALLIDS( b )) {
735 return( idl_dup( a ) );
739 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
742 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
743 bi < b->b_nmax; ai++ ) {
744 if ( b->b_ids[bi] == ai ) {
751 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
755 if ( ni == n->b_nmax ) {
757 return( idl_allids( be ) );
767 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
768 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
773 if ( bi == b->b_nids ) {
777 if ( b->b_ids[bi] != a->b_ids[ai] ) {
778 n->b_ids[ni++] = a->b_ids[ai];
782 for ( ; ai < a->b_nids; ai++ ) {
783 n->b_ids[ni++] = a->b_ids[ai];
791 idl_firstid( IDList *idl )
793 if ( idl == NULL || idl->b_nids == 0 ) {
797 if ( ALLIDS( idl ) ) {
798 return( idl->b_nids == 1 ? NOID : 1 );
801 return( idl->b_ids[0] );
805 idl_nextid( IDList *idl, ID id )
809 if ( ALLIDS( idl ) ) {
810 return( ++id < idl->b_nids ? id : NOID );
813 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
818 if ( i >= idl->b_nids ) {
821 return( idl->b_ids[i] );