1 /* idl.c - ldap id list handling routines */
6 #include "ldapconfig.h"
9 extern Datum ldbm_cache_fetch();
16 /* nmax + nids + space for the ids */
17 new = (IDList *) ch_calloc( (2 + nids), sizeof(ID) );
25 idl_allids( Backend *be )
30 idl->b_nmax = ALLIDSBLOCK;
31 idl->b_nids = next_id_get( be );
37 idl_free( IDList *idl )
59 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
61 data = ldbm_cache_fetch( db, key );
63 idl = (IDList *) data.dptr;
81 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
83 data = ldbm_cache_fetch( db, key );
85 if ( (idl = (IDList *) data.dptr) == NULL ) {
90 if ( ! INDIRECT_BLOCK( idl ) ) {
92 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
93 idl->b_nids, idl->b_nmax, 0 );
96 /* make sure we have the current value of highest id */
97 if ( idl->b_nmax == ALLIDSBLOCK ) {
99 idl = idl_allids( be );
105 * this is an indirect block which points to other blocks.
106 * we need to read in all the blocks it points to and construct
107 * a big id list containing all the ids, which we will return.
110 /* count the number of blocks & allocate space for pointers to them */
111 for ( i = 0; idl->b_ids[i] != NOID; i++ )
113 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
115 /* read in all the blocks */
116 kstr = (char *) ch_malloc( key.dsize + 20 );
118 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
119 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
121 k2.dsize = strlen( kstr ) + 1;
123 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
124 Debug( LDAP_DEBUG_ANY,
125 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
129 nids += tmp[i]->b_nids;
134 /* allocate space for the big block */
135 idl = idl_alloc( nids );
139 /* copy in all the ids from the component blocks */
140 for ( i = 0; tmp[i] != NULL; i++ ) {
141 if ( tmp[i] == NULL ) {
145 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
146 tmp[i]->b_nids * sizeof(ID) );
147 nids += tmp[i]->b_nids;
151 free( (char *) tmp );
153 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n", idl->b_nids,
169 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
171 data.dptr = (char *) idl;
172 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
174 #ifdef LDBM_PESSIMISTIC
175 rc = ldbm_cache_store( db, key, data, LDBM_REPLACE | LDBM_SYNC );
177 rc = ldbm_cache_store( db, key, data, LDBM_REPLACE );
180 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
194 /* find where to split the block */
195 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
198 *n1 = idl_alloc( i == 0 ? 1 : i );
199 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
202 * everything before the id being inserted in the first block
203 * unless there is nothing, in which case the id being inserted
206 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
208 (*n1)->b_nids = (i == 0 ? 1 : i);
211 (*n1)->b_ids[0] = id;
213 (*n2)->b_ids[0] = id;
216 /* the id being inserted & everything after in the second block */
217 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
218 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
219 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
223 * idl_change_first - called when an indirect block's first key has
224 * changed, meaning it needs to be stored under a new key, and the
225 * header block pointing to it needs updating.
232 Datum hkey, /* header block key */
233 IDList *h, /* header block */
234 int pos, /* pos in h to update */
235 Datum bkey, /* data block key */
236 IDList *b /* data block */
241 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
243 /* delete old key block */
244 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
245 Debug( LDAP_DEBUG_ANY,
246 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
251 /* write block with new key */
252 sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
253 bkey.dsize = strlen( bkey.dptr ) + 1;
254 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
255 Debug( LDAP_DEBUG_ANY,
256 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
260 /* update + write indirect header block */
261 h->b_ids[pos] = b->b_ids[0];
262 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
263 Debug( LDAP_DEBUG_ANY,
264 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
280 IDList *idl, *tmp, *tmp2, *tmp3;
284 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
285 idl = idl_alloc( 1 );
286 idl->b_ids[idl->b_nids++] = id;
287 rc = idl_store( be, db, key, idl );
294 if ( ! INDIRECT_BLOCK( idl ) ) {
295 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
296 case 0: /* id inserted - store the updated block */
298 rc = idl_store( be, db, key, idl );
301 case 2: /* id already there - nothing to do */
305 case 3: /* id not inserted - block must be split */
306 /* check threshold for marking this an all-id block */
307 if ( db->dbc_maxindirect < 2 ) {
309 idl = idl_allids( be );
310 rc = idl_store( be, db, key, idl );
316 idl_split_block( idl, id, &tmp, &tmp2 );
319 /* create the header indirect block */
320 idl = idl_alloc( 3 );
322 idl->b_nids = INDBLOCK;
323 idl->b_ids[0] = tmp->b_ids[0];
324 idl->b_ids[1] = tmp2->b_ids[0];
325 idl->b_ids[2] = NOID;
328 rc = idl_store( be, db, key, idl );
330 /* store the first id block */
331 kstr = (char *) ch_malloc( key.dsize + 20 );
332 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
335 k2.dsize = strlen( kstr ) + 1;
336 rc = idl_store( be, db, k2, tmp );
338 /* store the second id block */
339 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
342 k2.dsize = strlen( kstr ) + 1;
343 rc = idl_store( be, db, k2, tmp2 );
356 * this is an indirect block which points to other blocks.
357 * we need to read in the block into which the id should be
358 * inserted, then insert the id and store the block. we might
359 * have to split the block if it is full, which means we also
360 * need to write a new "header" block.
363 /* select the block to try inserting into */
364 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
374 kstr = (char *) ch_malloc( key.dsize + 20 );
375 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
377 k2.dsize = strlen( kstr ) + 1;
378 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
379 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
385 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
386 case 0: /* id inserted ok */
387 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
388 Debug( LDAP_DEBUG_ANY,
389 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
393 case 1: /* id inserted - first id in block has changed */
395 * key for this block has changed, so we have to
396 * write the block under the new key, delete the
397 * old key block + update and write the indirect
401 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
404 case 2: /* id not inserted - already there */
407 case 3: /* id not inserted - block is full */
409 * first, see if it will fit in the next block,
410 * without splitting, unless we're trying to insert
411 * into the beginning of the first block.
414 /* is there a next block? */
415 if ( !first && idl->b_ids[i + 1] != NOID ) {
417 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
420 k2.dsize = strlen( kstr ) + 1;
421 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
422 Debug( LDAP_DEBUG_ANY,
423 "idl_fetch_one (%s) returns NULL\n",
428 switch ( (rc = idl_insert( &tmp2, id,
429 db->dbc_maxids )) ) {
430 case 1: /* id inserted first in block */
431 rc = idl_change_first( be, db, key, idl,
435 case 2: /* id already there - how? */
436 case 0: /* id inserted */
438 Debug( LDAP_DEBUG_ANY,
439 "id %d already in next block\n",
448 case 3: /* split the original block */
456 * must split the block, write both new blocks + update
457 * and write the indirect header block.
460 /* count how many indirect blocks */
461 for ( j = 0; idl->b_ids[j] != NOID; j++ )
464 /* check it against all-id thresholed */
465 if ( j + 1 > db->dbc_maxindirect ) {
467 * we've passed the all-id threshold, meaning
468 * that this set of blocks should be replaced
469 * by a single "all-id" block. our job: delete
470 * all the indirect blocks, and replace the header
471 * block by an all-id block.
474 /* delete all indirect blocks */
475 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
476 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
479 k2.dsize = strlen( kstr ) + 1;
481 rc = ldbm_cache_delete( db, k2 );
484 /* store allid block in place of header block */
486 idl = idl_allids( be );
487 rc = idl_store( be, db, key, idl );
495 idl_split_block( tmp, id, &tmp2, &tmp3 );
498 /* create a new updated indirect header block */
499 tmp = idl_alloc( idl->b_nmax + 1 );
500 tmp->b_nids = INDBLOCK;
501 /* everything up to the split block */
502 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
504 /* the two new blocks */
505 tmp->b_ids[i] = tmp2->b_ids[0];
506 tmp->b_ids[i + 1] = tmp3->b_ids[0];
507 /* everything after the split block */
508 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
509 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
511 /* store the header block */
512 rc = idl_store( be, db, key, tmp );
514 /* store the first id block */
515 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
518 k2.dsize = strlen( kstr ) + 1;
519 rc = idl_store( be, db, k2, tmp2 );
521 /* store the second id block */
522 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
525 k2.dsize = strlen( kstr ) + 1;
526 rc = idl_store( be, db, k2, tmp3 );
540 * idl_insert - insert an id into an id list.
541 * returns 0 id inserted
542 * 1 id inserted, first id in block has changed
543 * 2 id not inserted, already there
544 * 3 id not inserted, block must be split
548 idl_insert( IDList **idl, ID id, int maxids )
552 if ( ALLIDS( *idl ) ) {
553 return( 2 ); /* already there */
556 /* is it already there? XXX bin search XXX */
557 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
560 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
561 return( 2 ); /* already there */
564 /* do we need to make room for it? */
565 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
566 /* make room or indicate block needs splitting */
567 if ( (*idl)->b_nmax == maxids ) {
568 return( 3 ); /* block needs splitting */
572 if ( (*idl)->b_nmax > maxids ) {
573 (*idl)->b_nmax = maxids;
575 *idl = (IDList *) ch_realloc( (char *) *idl,
576 ((*idl)->b_nmax + 2) * sizeof(ID) );
579 /* make a slot for the new id */
580 for ( j = (*idl)->b_nids; j != i; j-- ) {
581 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
583 (*idl)->b_ids[i] = id;
585 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
586 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
588 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
592 idl_dup( IDList *idl )
600 new = idl_alloc( idl->b_nmax );
601 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
608 idl_min( IDList *a, IDList *b )
610 return( a->b_nids > b->b_nids ? b : a );
614 * idl_intersection - return a intersection b
627 if ( a == NULL || b == NULL ) {
631 return( idl_dup( b ) );
634 return( idl_dup( a ) );
637 n = idl_dup( idl_min( a, b ) );
639 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
640 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
643 if ( bi == b->b_nids ) {
647 if ( b->b_ids[bi] == a->b_ids[ai] ) {
648 n->b_ids[ni++] = a->b_ids[ai];
662 * idl_union - return a union b
676 return( idl_dup( b ) );
679 return( idl_dup( a ) );
681 if ( ALLIDS( a ) || ALLIDS( b ) ) {
682 return( idl_allids( be ) );
685 if ( b->b_nids < a->b_nids ) {
691 n = idl_alloc( a->b_nids + b->b_nids );
693 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
694 if ( a->b_ids[ai] < b->b_ids[bi] ) {
695 n->b_ids[ni++] = a->b_ids[ai++];
696 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
697 n->b_ids[ni++] = b->b_ids[bi++];
699 n->b_ids[ni++] = a->b_ids[ai];
704 for ( ; ai < a->b_nids; ai++ ) {
705 n->b_ids[ni++] = a->b_ids[ai];
707 for ( ; bi < b->b_nids; bi++ ) {
708 n->b_ids[ni++] = b->b_ids[bi];
716 * idl_notin - return a intersection ~b (or a minus b)
732 if ( b == NULL || ALLIDS( b )) {
733 return( idl_dup( a ) );
737 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
740 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
741 bi < b->b_nmax; ai++ ) {
742 if ( b->b_ids[bi] == ai ) {
749 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
753 if ( ni == n->b_nmax ) {
755 return( idl_allids( be ) );
765 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
766 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
771 if ( bi == b->b_nids ) {
775 if ( b->b_ids[bi] != a->b_ids[ai] ) {
776 n->b_ids[ni++] = a->b_ids[ai];
780 for ( ; ai < a->b_nids; ai++ ) {
781 n->b_ids[ni++] = a->b_ids[ai];
789 idl_firstid( IDList *idl )
791 if ( idl == NULL || idl->b_nids == 0 ) {
795 if ( ALLIDS( idl ) ) {
796 return( idl->b_nids == 1 ? NOID : 1 );
799 return( idl->b_ids[0] );
803 idl_nextid( IDList *idl, ID id )
807 if ( ALLIDS( idl ) ) {
808 return( ++id < idl->b_nids ? id : NOID );
811 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
816 if ( i >= idl->b_nids ) {
819 return( idl->b_ids[i] );