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,
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 rc = ldbm_cache_store( db, key, data, LDBM_REPLACE | LDBM_SYNC );
177 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
191 /* find where to split the block */
192 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
195 *n1 = idl_alloc( i == 0 ? 1 : i );
196 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
199 * everything before the id being inserted in the first block
200 * unless there is nothing, in which case the id being inserted
203 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
205 (*n1)->b_nids = (i == 0 ? 1 : i);
208 (*n1)->b_ids[0] = id;
210 (*n2)->b_ids[0] = id;
213 /* the id being inserted & everything after in the second block */
214 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
215 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
216 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
220 * idl_change_first - called when an indirect block's first key has
221 * changed, meaning it needs to be stored under a new key, and the
222 * header block pointing to it needs updating.
229 Datum hkey, /* header block key */
230 IDList *h, /* header block */
231 int pos, /* pos in h to update */
232 Datum bkey, /* data block key */
233 IDList *b /* data block */
238 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
240 /* delete old key block */
241 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
242 Debug( LDAP_DEBUG_ANY,
243 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
248 /* write block with new key */
249 sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
250 bkey.dsize = strlen( bkey.dptr ) + 1;
251 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
252 Debug( LDAP_DEBUG_ANY,
253 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
257 /* update + write indirect header block */
258 h->b_ids[pos] = b->b_ids[0];
259 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
260 Debug( LDAP_DEBUG_ANY,
261 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
277 IDList *idl, *tmp, *tmp2, *tmp3;
281 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
282 idl = idl_alloc( 1 );
283 idl->b_ids[idl->b_nids++] = id;
284 rc = idl_store( be, db, key, idl );
291 if ( ! INDIRECT_BLOCK( idl ) ) {
292 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
293 case 0: /* id inserted - store the updated block */
295 rc = idl_store( be, db, key, idl );
298 case 2: /* id already there - nothing to do */
302 case 3: /* id not inserted - block must be split */
303 /* check threshold for marking this an all-id block */
304 if ( db->dbc_maxindirect < 2 ) {
306 idl = idl_allids( be );
307 rc = idl_store( be, db, key, idl );
313 idl_split_block( idl, id, &tmp, &tmp2 );
316 /* create the header indirect block */
317 idl = idl_alloc( 3 );
319 idl->b_nids = INDBLOCK;
320 idl->b_ids[0] = tmp->b_ids[0];
321 idl->b_ids[1] = tmp2->b_ids[0];
322 idl->b_ids[2] = NOID;
325 rc = idl_store( be, db, key, idl );
327 /* store the first id block */
328 kstr = (char *) ch_malloc( key.dsize + 20 );
329 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
332 k2.dsize = strlen( kstr ) + 1;
333 rc = idl_store( be, db, k2, tmp );
335 /* store the second id block */
336 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
339 k2.dsize = strlen( kstr ) + 1;
340 rc = idl_store( be, db, k2, tmp2 );
353 * this is an indirect block which points to other blocks.
354 * we need to read in the block into which the id should be
355 * inserted, then insert the id and store the block. we might
356 * have to split the block if it is full, which means we also
357 * need to write a new "header" block.
360 /* select the block to try inserting into */
361 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
371 kstr = (char *) ch_malloc( key.dsize + 20 );
372 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
374 k2.dsize = strlen( kstr ) + 1;
375 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
376 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
382 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
383 case 0: /* id inserted ok */
384 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
385 Debug( LDAP_DEBUG_ANY,
386 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
390 case 1: /* id inserted - first id in block has changed */
392 * key for this block has changed, so we have to
393 * write the block under the new key, delete the
394 * old key block + update and write the indirect
398 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
401 case 2: /* id not inserted - already there */
404 case 3: /* id not inserted - block is full */
406 * first, see if it will fit in the next block,
407 * without splitting, unless we're trying to insert
408 * into the beginning of the first block.
411 /* is there a next block? */
412 if ( !first && idl->b_ids[i + 1] != NOID ) {
414 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
417 k2.dsize = strlen( kstr ) + 1;
418 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
419 Debug( LDAP_DEBUG_ANY,
420 "idl_fetch_one (%s) returns NULL\n",
425 switch ( (rc = idl_insert( &tmp2, id,
426 db->dbc_maxids )) ) {
427 case 1: /* id inserted first in block */
428 rc = idl_change_first( be, db, key, idl,
432 case 2: /* id already there - how? */
433 case 0: /* id inserted */
435 Debug( LDAP_DEBUG_ANY,
436 "id %d already in next block\n",
445 case 3: /* split the original block */
453 * must split the block, write both new blocks + update
454 * and write the indirect header block.
457 /* count how many indirect blocks */
458 for ( j = 0; idl->b_ids[j] != NOID; j++ )
461 /* check it against all-id thresholed */
462 if ( j + 1 > db->dbc_maxindirect ) {
464 * we've passed the all-id threshold, meaning
465 * that this set of blocks should be replaced
466 * by a single "all-id" block. our job: delete
467 * all the indirect blocks, and replace the header
468 * block by an all-id block.
471 /* delete all indirect blocks */
472 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
473 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
476 k2.dsize = strlen( kstr ) + 1;
478 rc = ldbm_cache_delete( db, k2 );
481 /* store allid block in place of header block */
483 idl = idl_allids( be );
484 rc = idl_store( be, db, key, idl );
492 idl_split_block( tmp, id, &tmp2, &tmp3 );
495 /* create a new updated indirect header block */
496 tmp = idl_alloc( idl->b_nmax + 1 );
497 tmp->b_nids = INDBLOCK;
498 /* everything up to the split block */
499 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
501 /* the two new blocks */
502 tmp->b_ids[i] = tmp2->b_ids[0];
503 tmp->b_ids[i + 1] = tmp3->b_ids[0];
504 /* everything after the split block */
505 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
506 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
508 /* store the header block */
509 rc = idl_store( be, db, key, tmp );
511 /* store the first id block */
512 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
515 k2.dsize = strlen( kstr ) + 1;
516 rc = idl_store( be, db, k2, tmp2 );
518 /* store the second id block */
519 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
522 k2.dsize = strlen( kstr ) + 1;
523 rc = idl_store( be, db, k2, tmp3 );
537 * idl_insert - insert an id into an id list.
538 * returns 0 id inserted
539 * 1 id inserted, first id in block has changed
540 * 2 id not inserted, already there
541 * 3 id not inserted, block must be split
545 idl_insert( IDList **idl, ID id, int maxids )
549 if ( ALLIDS( *idl ) ) {
550 return( 2 ); /* already there */
553 /* is it already there? XXX bin search XXX */
554 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
557 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
558 return( 2 ); /* already there */
561 /* do we need to make room for it? */
562 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
563 /* make room or indicate block needs splitting */
564 if ( (*idl)->b_nmax == maxids ) {
565 return( 3 ); /* block needs splitting */
569 if ( (*idl)->b_nmax > maxids ) {
570 (*idl)->b_nmax = maxids;
572 *idl = (IDList *) ch_realloc( (char *) *idl,
573 ((*idl)->b_nmax + 2) * sizeof(ID) );
576 /* make a slot for the new id */
577 for ( j = (*idl)->b_nids; j != i; j-- ) {
578 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
580 (*idl)->b_ids[i] = id;
582 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
583 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
585 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
589 idl_dup( IDList *idl )
597 new = idl_alloc( idl->b_nmax );
598 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
605 idl_min( IDList *a, IDList *b )
607 return( a->b_nids > b->b_nids ? b : a );
611 * idl_intersection - return a intersection b
624 if ( a == NULL || b == NULL ) {
628 return( idl_dup( b ) );
631 return( idl_dup( a ) );
634 n = idl_dup( idl_min( a, b ) );
636 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
637 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
640 if ( bi == b->b_nids ) {
644 if ( b->b_ids[bi] == a->b_ids[ai] ) {
645 n->b_ids[ni++] = a->b_ids[ai];
659 * idl_union - return a union b
673 return( idl_dup( b ) );
676 return( idl_dup( a ) );
678 if ( ALLIDS( a ) || ALLIDS( b ) ) {
679 return( idl_allids( be ) );
682 if ( b->b_nids < a->b_nids ) {
688 n = idl_alloc( a->b_nids + b->b_nids );
690 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
691 if ( a->b_ids[ai] < b->b_ids[bi] ) {
692 n->b_ids[ni++] = a->b_ids[ai++];
693 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
694 n->b_ids[ni++] = b->b_ids[bi++];
696 n->b_ids[ni++] = a->b_ids[ai];
701 for ( ; ai < a->b_nids; ai++ ) {
702 n->b_ids[ni++] = a->b_ids[ai];
704 for ( ; bi < b->b_nids; bi++ ) {
705 n->b_ids[ni++] = b->b_ids[bi];
713 * idl_notin - return a intersection ~b (or a minus b)
729 if ( b == NULL || ALLIDS( b )) {
730 return( idl_dup( a ) );
734 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
737 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
738 bi < b->b_nmax; ai++ ) {
739 if ( b->b_ids[bi] == ai ) {
746 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
750 if ( ni == n->b_nmax ) {
752 return( idl_allids( be ) );
762 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
763 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
768 if ( bi == b->b_nids ) {
772 if ( b->b_ids[bi] != a->b_ids[ai] ) {
773 n->b_ids[ni++] = a->b_ids[ai];
777 for ( ; ai < a->b_nids; ai++ ) {
778 n->b_ids[ni++] = a->b_ids[ai];
786 idl_firstid( IDList *idl )
788 if ( idl == NULL || idl->b_nids == 0 ) {
792 if ( ALLIDS( idl ) ) {
793 return( idl->b_nids == 1 ? NOID : 1 );
796 return( idl->b_ids[0] );
800 idl_nextid( IDList *idl, ID id )
804 if ( ALLIDS( idl ) ) {
805 return( ++id < idl->b_nids ? id : NOID );
808 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
813 if ( i >= idl->b_nids ) {
816 return( idl->b_ids[i] );