1 /* idl.c - ldap id list handling routines */
5 #include <sys/socket.h>
7 #include "ldapconfig.h"
10 extern Datum ldbm_cache_fetch();
17 /* nmax + nids + space for the ids */
18 new = (IDList *) ch_calloc( (2 + nids), sizeof(ID) );
26 idl_allids( Backend *be )
31 idl->b_nmax = ALLIDSBLOCK;
32 idl->b_nids = next_id_get( be );
38 idl_free( IDList *idl )
60 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
62 data = ldbm_cache_fetch( db, key );
64 idl = (IDList *) data.dptr;
82 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
84 data = ldbm_cache_fetch( db, key );
86 if ( (idl = (IDList *) data.dptr) == NULL ) {
91 if ( ! INDIRECT_BLOCK( idl ) ) {
93 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
94 idl->b_nids, idl->b_nmax, 0 );
97 /* make sure we have the current value of highest id */
98 if ( idl->b_nmax == ALLIDSBLOCK ) {
100 idl = idl_allids( be );
106 * this is an indirect block which points to other blocks.
107 * we need to read in all the blocks it points to and construct
108 * a big id list containing all the ids, which we will return.
111 /* count the number of blocks & allocate space for pointers to them */
112 for ( i = 0; idl->b_ids[i] != NOID; i++ )
114 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
116 /* read in all the blocks */
117 kstr = (char *) ch_malloc( key.dsize + 20 );
119 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
120 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
122 k2.dsize = strlen( kstr ) + 1;
124 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
125 Debug( LDAP_DEBUG_ANY,
126 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
130 nids += tmp[i]->b_nids;
135 /* allocate space for the big block */
136 idl = idl_alloc( nids );
140 /* copy in all the ids from the component blocks */
141 for ( i = 0; tmp[i] != NULL; i++ ) {
142 if ( tmp[i] == NULL ) {
146 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
147 tmp[i]->b_nids * sizeof(ID) );
148 nids += tmp[i]->b_nids;
152 free( (char *) tmp );
154 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n", idl->b_nids,
169 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
171 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
173 data.dptr = (char *) idl;
174 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
176 flags = LDBM_REPLACE;
177 if( li->li_flush_wrt ) flags |= LDBM_SYNC;
179 rc = ldbm_cache_store( db, key, data, flags );
181 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
195 /* find where to split the block */
196 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
199 *n1 = idl_alloc( i == 0 ? 1 : i );
200 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
203 * everything before the id being inserted in the first block
204 * unless there is nothing, in which case the id being inserted
207 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
209 (*n1)->b_nids = (i == 0 ? 1 : i);
212 (*n1)->b_ids[0] = id;
214 (*n2)->b_ids[0] = id;
217 /* the id being inserted & everything after in the second block */
218 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
219 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
220 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
224 * idl_change_first - called when an indirect block's first key has
225 * changed, meaning it needs to be stored under a new key, and the
226 * header block pointing to it needs updating.
233 Datum hkey, /* header block key */
234 IDList *h, /* header block */
235 int pos, /* pos in h to update */
236 Datum bkey, /* data block key */
237 IDList *b /* data block */
242 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
244 /* delete old key block */
245 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
246 Debug( LDAP_DEBUG_ANY,
247 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
252 /* write block with new key */
253 sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
254 bkey.dsize = strlen( bkey.dptr ) + 1;
255 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
256 Debug( LDAP_DEBUG_ANY,
257 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
261 /* update + write indirect header block */
262 h->b_ids[pos] = b->b_ids[0];
263 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
264 Debug( LDAP_DEBUG_ANY,
265 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
281 IDList *idl, *tmp, *tmp2, *tmp3;
285 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
286 idl = idl_alloc( 1 );
287 idl->b_ids[idl->b_nids++] = id;
288 rc = idl_store( be, db, key, idl );
295 if ( ! INDIRECT_BLOCK( idl ) ) {
296 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
297 case 0: /* id inserted - store the updated block */
299 rc = idl_store( be, db, key, idl );
302 case 2: /* id already there - nothing to do */
306 case 3: /* id not inserted - block must be split */
307 /* check threshold for marking this an all-id block */
308 if ( db->dbc_maxindirect < 2 ) {
310 idl = idl_allids( be );
311 rc = idl_store( be, db, key, idl );
317 idl_split_block( idl, id, &tmp, &tmp2 );
320 /* create the header indirect block */
321 idl = idl_alloc( 3 );
323 idl->b_nids = INDBLOCK;
324 idl->b_ids[0] = tmp->b_ids[0];
325 idl->b_ids[1] = tmp2->b_ids[0];
326 idl->b_ids[2] = NOID;
329 rc = idl_store( be, db, key, idl );
331 /* store the first id block */
332 kstr = (char *) ch_malloc( key.dsize + 20 );
333 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
336 k2.dsize = strlen( kstr ) + 1;
337 rc = idl_store( be, db, k2, tmp );
339 /* store the second id block */
340 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
343 k2.dsize = strlen( kstr ) + 1;
344 rc = idl_store( be, db, k2, tmp2 );
357 * this is an indirect block which points to other blocks.
358 * we need to read in the block into which the id should be
359 * inserted, then insert the id and store the block. we might
360 * have to split the block if it is full, which means we also
361 * need to write a new "header" block.
364 /* select the block to try inserting into */
365 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
375 kstr = (char *) ch_malloc( key.dsize + 20 );
376 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
378 k2.dsize = strlen( kstr ) + 1;
379 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
380 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
386 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
387 case 0: /* id inserted ok */
388 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
389 Debug( LDAP_DEBUG_ANY,
390 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
394 case 1: /* id inserted - first id in block has changed */
396 * key for this block has changed, so we have to
397 * write the block under the new key, delete the
398 * old key block + update and write the indirect
402 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
405 case 2: /* id not inserted - already there */
408 case 3: /* id not inserted - block is full */
410 * first, see if it will fit in the next block,
411 * without splitting, unless we're trying to insert
412 * into the beginning of the first block.
415 /* is there a next block? */
416 if ( !first && idl->b_ids[i + 1] != NOID ) {
418 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
421 k2.dsize = strlen( kstr ) + 1;
422 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
423 Debug( LDAP_DEBUG_ANY,
424 "idl_fetch_one (%s) returns NULL\n",
429 switch ( (rc = idl_insert( &tmp2, id,
430 db->dbc_maxids )) ) {
431 case 1: /* id inserted first in block */
432 rc = idl_change_first( be, db, key, idl,
436 case 2: /* id already there - how? */
437 case 0: /* id inserted */
439 Debug( LDAP_DEBUG_ANY,
440 "id %d already in next block\n",
449 case 3: /* split the original block */
457 * must split the block, write both new blocks + update
458 * and write the indirect header block.
461 /* count how many indirect blocks */
462 for ( j = 0; idl->b_ids[j] != NOID; j++ )
465 /* check it against all-id thresholed */
466 if ( j + 1 > db->dbc_maxindirect ) {
468 * we've passed the all-id threshold, meaning
469 * that this set of blocks should be replaced
470 * by a single "all-id" block. our job: delete
471 * all the indirect blocks, and replace the header
472 * block by an all-id block.
475 /* delete all indirect blocks */
476 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
477 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
480 k2.dsize = strlen( kstr ) + 1;
482 rc = ldbm_cache_delete( db, k2 );
485 /* store allid block in place of header block */
487 idl = idl_allids( be );
488 rc = idl_store( be, db, key, idl );
496 idl_split_block( tmp, id, &tmp2, &tmp3 );
499 /* create a new updated indirect header block */
500 tmp = idl_alloc( idl->b_nmax + 1 );
501 tmp->b_nids = INDBLOCK;
502 /* everything up to the split block */
503 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
505 /* the two new blocks */
506 tmp->b_ids[i] = tmp2->b_ids[0];
507 tmp->b_ids[i + 1] = tmp3->b_ids[0];
508 /* everything after the split block */
509 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
510 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
512 /* store the header block */
513 rc = idl_store( be, db, key, tmp );
515 /* store the first id block */
516 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
519 k2.dsize = strlen( kstr ) + 1;
520 rc = idl_store( be, db, k2, tmp2 );
522 /* store the second id block */
523 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
526 k2.dsize = strlen( kstr ) + 1;
527 rc = idl_store( be, db, k2, tmp3 );
541 * idl_insert - insert an id into an id list.
542 * returns 0 id inserted
543 * 1 id inserted, first id in block has changed
544 * 2 id not inserted, already there
545 * 3 id not inserted, block must be split
549 idl_insert( IDList **idl, ID id, int maxids )
553 if ( ALLIDS( *idl ) ) {
554 return( 2 ); /* already there */
557 /* is it already there? XXX bin search XXX */
558 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
561 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
562 return( 2 ); /* already there */
565 /* do we need to make room for it? */
566 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
567 /* make room or indicate block needs splitting */
568 if ( (*idl)->b_nmax == maxids ) {
569 return( 3 ); /* block needs splitting */
573 if ( (*idl)->b_nmax > maxids ) {
574 (*idl)->b_nmax = maxids;
576 *idl = (IDList *) ch_realloc( (char *) *idl,
577 ((*idl)->b_nmax + 2) * sizeof(ID) );
580 /* make a slot for the new id */
581 for ( j = (*idl)->b_nids; j != i; j-- ) {
582 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
584 (*idl)->b_ids[i] = id;
586 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
587 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
589 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
593 idl_dup( IDList *idl )
601 new = idl_alloc( idl->b_nmax );
602 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
609 idl_min( IDList *a, IDList *b )
611 return( a->b_nids > b->b_nids ? b : a );
615 * idl_intersection - return a intersection b
628 if ( a == NULL || b == NULL ) {
632 return( idl_dup( b ) );
635 return( idl_dup( a ) );
638 n = idl_dup( idl_min( a, b ) );
640 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
641 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
644 if ( bi == b->b_nids ) {
648 if ( b->b_ids[bi] == a->b_ids[ai] ) {
649 n->b_ids[ni++] = a->b_ids[ai];
663 * idl_union - return a union b
677 return( idl_dup( b ) );
680 return( idl_dup( a ) );
682 if ( ALLIDS( a ) || ALLIDS( b ) ) {
683 return( idl_allids( be ) );
686 if ( b->b_nids < a->b_nids ) {
692 n = idl_alloc( a->b_nids + b->b_nids );
694 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
695 if ( a->b_ids[ai] < b->b_ids[bi] ) {
696 n->b_ids[ni++] = a->b_ids[ai++];
697 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
698 n->b_ids[ni++] = b->b_ids[bi++];
700 n->b_ids[ni++] = a->b_ids[ai];
705 for ( ; ai < a->b_nids; ai++ ) {
706 n->b_ids[ni++] = a->b_ids[ai];
708 for ( ; bi < b->b_nids; bi++ ) {
709 n->b_ids[ni++] = b->b_ids[bi];
717 * idl_notin - return a intersection ~b (or a minus b)
733 if ( b == NULL || ALLIDS( b )) {
734 return( idl_dup( a ) );
738 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
741 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
742 bi < b->b_nmax; ai++ ) {
743 if ( b->b_ids[bi] == ai ) {
750 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
754 if ( ni == n->b_nmax ) {
756 return( idl_allids( be ) );
766 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
767 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
772 if ( bi == b->b_nids ) {
776 if ( b->b_ids[bi] != a->b_ids[ai] ) {
777 n->b_ids[ni++] = a->b_ids[ai];
781 for ( ; ai < a->b_nids; ai++ ) {
782 n->b_ids[ni++] = a->b_ids[ai];
790 idl_firstid( IDList *idl )
792 if ( idl == NULL || idl->b_nids == 0 ) {
796 if ( ALLIDS( idl ) ) {
797 return( idl->b_nids == 1 ? NOID : 1 );
800 return( idl->b_ids[0] );
804 idl_nextid( IDList *idl, ID id )
808 if ( ALLIDS( idl ) ) {
809 return( ++id < idl->b_nids ? id : NOID );
812 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
817 if ( i >= idl->b_nids ) {
820 return( idl->b_ids[i] );