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,
168 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
170 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
172 data.dptr = (char *) idl;
173 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
175 flags = LDBM_REPLACE;
176 if( li->li_flush_wrt ) flags |= LDBM_SYNC;
177 rc = ldbm_cache_store( db, key, data, flags );
179 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
193 /* find where to split the block */
194 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
197 *n1 = idl_alloc( i == 0 ? 1 : i );
198 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
201 * everything before the id being inserted in the first block
202 * unless there is nothing, in which case the id being inserted
205 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
207 (*n1)->b_nids = (i == 0 ? 1 : i);
210 (*n1)->b_ids[0] = id;
212 (*n2)->b_ids[0] = id;
215 /* the id being inserted & everything after in the second block */
216 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
217 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
218 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
222 * idl_change_first - called when an indirect block's first key has
223 * changed, meaning it needs to be stored under a new key, and the
224 * header block pointing to it needs updating.
231 Datum hkey, /* header block key */
232 IDList *h, /* header block */
233 int pos, /* pos in h to update */
234 Datum bkey, /* data block key */
235 IDList *b /* data block */
240 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
242 /* delete old key block */
243 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
244 Debug( LDAP_DEBUG_ANY,
245 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
250 /* write block with new key */
251 sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
252 bkey.dsize = strlen( bkey.dptr ) + 1;
253 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
254 Debug( LDAP_DEBUG_ANY,
255 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
259 /* update + write indirect header block */
260 h->b_ids[pos] = b->b_ids[0];
261 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
262 Debug( LDAP_DEBUG_ANY,
263 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
279 IDList *idl, *tmp, *tmp2, *tmp3;
283 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
284 idl = idl_alloc( 1 );
285 idl->b_ids[idl->b_nids++] = id;
286 rc = idl_store( be, db, key, idl );
293 if ( ! INDIRECT_BLOCK( idl ) ) {
294 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
295 case 0: /* id inserted - store the updated block */
297 rc = idl_store( be, db, key, idl );
300 case 2: /* id already there - nothing to do */
304 case 3: /* id not inserted - block must be split */
305 /* check threshold for marking this an all-id block */
306 if ( db->dbc_maxindirect < 2 ) {
308 idl = idl_allids( be );
309 rc = idl_store( be, db, key, idl );
315 idl_split_block( idl, id, &tmp, &tmp2 );
318 /* create the header indirect block */
319 idl = idl_alloc( 3 );
321 idl->b_nids = INDBLOCK;
322 idl->b_ids[0] = tmp->b_ids[0];
323 idl->b_ids[1] = tmp2->b_ids[0];
324 idl->b_ids[2] = NOID;
327 rc = idl_store( be, db, key, idl );
329 /* store the first id block */
330 kstr = (char *) ch_malloc( key.dsize + 20 );
331 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
334 k2.dsize = strlen( kstr ) + 1;
335 rc = idl_store( be, db, k2, tmp );
337 /* store the second id block */
338 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
341 k2.dsize = strlen( kstr ) + 1;
342 rc = idl_store( be, db, k2, tmp2 );
355 * this is an indirect block which points to other blocks.
356 * we need to read in the block into which the id should be
357 * inserted, then insert the id and store the block. we might
358 * have to split the block if it is full, which means we also
359 * need to write a new "header" block.
362 /* select the block to try inserting into */
363 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
373 kstr = (char *) ch_malloc( key.dsize + 20 );
374 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
376 k2.dsize = strlen( kstr ) + 1;
377 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
378 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
384 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
385 case 0: /* id inserted ok */
386 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
387 Debug( LDAP_DEBUG_ANY,
388 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
392 case 1: /* id inserted - first id in block has changed */
394 * key for this block has changed, so we have to
395 * write the block under the new key, delete the
396 * old key block + update and write the indirect
400 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
403 case 2: /* id not inserted - already there */
406 case 3: /* id not inserted - block is full */
408 * first, see if it will fit in the next block,
409 * without splitting, unless we're trying to insert
410 * into the beginning of the first block.
413 /* is there a next block? */
414 if ( !first && idl->b_ids[i + 1] != NOID ) {
416 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
419 k2.dsize = strlen( kstr ) + 1;
420 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
421 Debug( LDAP_DEBUG_ANY,
422 "idl_fetch_one (%s) returns NULL\n",
427 switch ( (rc = idl_insert( &tmp2, id,
428 db->dbc_maxids )) ) {
429 case 1: /* id inserted first in block */
430 rc = idl_change_first( be, db, key, idl,
434 case 2: /* id already there - how? */
435 case 0: /* id inserted */
437 Debug( LDAP_DEBUG_ANY,
438 "id %d already in next block\n",
447 case 3: /* split the original block */
455 * must split the block, write both new blocks + update
456 * and write the indirect header block.
459 /* count how many indirect blocks */
460 for ( j = 0; idl->b_ids[j] != NOID; j++ )
463 /* check it against all-id thresholed */
464 if ( j + 1 > db->dbc_maxindirect ) {
466 * we've passed the all-id threshold, meaning
467 * that this set of blocks should be replaced
468 * by a single "all-id" block. our job: delete
469 * all the indirect blocks, and replace the header
470 * block by an all-id block.
473 /* delete all indirect blocks */
474 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
475 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
478 k2.dsize = strlen( kstr ) + 1;
480 rc = ldbm_cache_delete( db, k2 );
483 /* store allid block in place of header block */
485 idl = idl_allids( be );
486 rc = idl_store( be, db, key, idl );
494 idl_split_block( tmp, id, &tmp2, &tmp3 );
497 /* create a new updated indirect header block */
498 tmp = idl_alloc( idl->b_nmax + 1 );
499 tmp->b_nids = INDBLOCK;
500 /* everything up to the split block */
501 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
503 /* the two new blocks */
504 tmp->b_ids[i] = tmp2->b_ids[0];
505 tmp->b_ids[i + 1] = tmp3->b_ids[0];
506 /* everything after the split block */
507 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
508 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
510 /* store the header block */
511 rc = idl_store( be, db, key, tmp );
513 /* store the first id block */
514 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
517 k2.dsize = strlen( kstr ) + 1;
518 rc = idl_store( be, db, k2, tmp2 );
520 /* store the second id block */
521 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
524 k2.dsize = strlen( kstr ) + 1;
525 rc = idl_store( be, db, k2, tmp3 );
539 * idl_insert - insert an id into an id list.
540 * returns 0 id inserted
541 * 1 id inserted, first id in block has changed
542 * 2 id not inserted, already there
543 * 3 id not inserted, block must be split
547 idl_insert( IDList **idl, ID id, int maxids )
551 if ( ALLIDS( *idl ) ) {
552 return( 2 ); /* already there */
555 /* is it already there? XXX bin search XXX */
556 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
559 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
560 return( 2 ); /* already there */
563 /* do we need to make room for it? */
564 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
565 /* make room or indicate block needs splitting */
566 if ( (*idl)->b_nmax == maxids ) {
567 return( 3 ); /* block needs splitting */
571 if ( (*idl)->b_nmax > maxids ) {
572 (*idl)->b_nmax = maxids;
574 *idl = (IDList *) ch_realloc( (char *) *idl,
575 ((*idl)->b_nmax + 2) * sizeof(ID) );
578 /* make a slot for the new id */
579 for ( j = (*idl)->b_nids; j != i; j-- ) {
580 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
582 (*idl)->b_ids[i] = id;
584 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
585 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
587 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
591 idl_dup( IDList *idl )
599 new = idl_alloc( idl->b_nmax );
600 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
607 idl_min( IDList *a, IDList *b )
609 return( a->b_nids > b->b_nids ? b : a );
613 * idl_intersection - return a intersection b
626 if ( a == NULL || b == NULL ) {
630 return( idl_dup( b ) );
633 return( idl_dup( a ) );
636 n = idl_dup( idl_min( a, b ) );
638 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
639 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
642 if ( bi == b->b_nids ) {
646 if ( b->b_ids[bi] == a->b_ids[ai] ) {
647 n->b_ids[ni++] = a->b_ids[ai];
661 * idl_union - return a union b
675 return( idl_dup( b ) );
678 return( idl_dup( a ) );
680 if ( ALLIDS( a ) || ALLIDS( b ) ) {
681 return( idl_allids( be ) );
684 if ( b->b_nids < a->b_nids ) {
690 n = idl_alloc( a->b_nids + b->b_nids );
692 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
693 if ( a->b_ids[ai] < b->b_ids[bi] ) {
694 n->b_ids[ni++] = a->b_ids[ai++];
695 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
696 n->b_ids[ni++] = b->b_ids[bi++];
698 n->b_ids[ni++] = a->b_ids[ai];
703 for ( ; ai < a->b_nids; ai++ ) {
704 n->b_ids[ni++] = a->b_ids[ai];
706 for ( ; bi < b->b_nids; bi++ ) {
707 n->b_ids[ni++] = b->b_ids[bi];
715 * idl_notin - return a intersection ~b (or a minus b)
731 if ( b == NULL || ALLIDS( b )) {
732 return( idl_dup( a ) );
736 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
739 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
740 bi < b->b_nmax; ai++ ) {
741 if ( b->b_ids[bi] == ai ) {
748 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
752 if ( ni == n->b_nmax ) {
754 return( idl_allids( be ) );
764 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
765 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
770 if ( bi == b->b_nids ) {
774 if ( b->b_ids[bi] != a->b_ids[ai] ) {
775 n->b_ids[ni++] = a->b_ids[ai];
779 for ( ; ai < a->b_nids; ai++ ) {
780 n->b_ids[ni++] = a->b_ids[ai];
788 idl_firstid( IDList *idl )
790 if ( idl == NULL || idl->b_nids == 0 ) {
794 if ( ALLIDS( idl ) ) {
795 return( idl->b_nids == 1 ? NOID : 1 );
798 return( idl->b_ids[0] );
802 idl_nextid( IDList *idl, ID id )
806 if ( ALLIDS( idl ) ) {
807 return( ++id < idl->b_nids ? id : NOID );
810 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
815 if ( i >= idl->b_nids ) {
818 return( idl->b_ids[i] );