1 /* idl.c - ldap id list handling routines */
11 #include <sys/socket.h>
14 #include "ldapconfig.h"
15 #include "back-ldbm.h"
17 extern Datum ldbm_cache_fetch();
24 /* nmax + nids + space for the ids */
25 new = (IDList *) ch_calloc( (2 + nids), sizeof(ID) );
33 idl_allids( Backend *be )
38 idl->b_nmax = ALLIDSBLOCK;
39 idl->b_nids = next_id_get( be );
45 idl_free( IDList *idl )
68 memset( &k2, 0, sizeof( k2 ) );
69 memset( &data, 0, sizeof( data ) );
72 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
74 data = ldbm_cache_fetch( db, key );
76 idl = (IDList *) data.dptr;
95 memset( &k2, 0, sizeof( k2 ) );
96 memset( &data, 0, sizeof( data ) );
99 /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch\n", 0, 0, 0 ); */
101 data = ldbm_cache_fetch( db, key );
103 if ( (idl = (IDList *) data.dptr) == NULL ) {
108 if ( ! INDIRECT_BLOCK( idl ) ) {
110 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n",
111 idl->b_nids, idl->b_nmax, 0 );
114 /* make sure we have the current value of highest id */
115 if ( idl->b_nmax == ALLIDSBLOCK ) {
117 idl = idl_allids( be );
123 * this is an indirect block which points to other blocks.
124 * we need to read in all the blocks it points to and construct
125 * a big id list containing all the ids, which we will return.
128 /* count the number of blocks & allocate space for pointers to them */
129 for ( i = 0; idl->b_ids[i] != NOID; i++ )
131 tmp = (IDList **) ch_malloc( (i + 1) * sizeof(IDList *) );
133 /* read in all the blocks */
134 kstr = (char *) ch_malloc( key.dsize + 20 );
136 for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
137 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
139 k2.dsize = strlen( kstr ) + 1;
141 if ( (tmp[i] = idl_fetch_one( be, db, k2 )) == NULL ) {
142 Debug( LDAP_DEBUG_ANY,
143 "idl_fetch of (%s) returns NULL\n", k2.dptr, 0, 0 );
147 nids += tmp[i]->b_nids;
152 /* allocate space for the big block */
153 idl = idl_alloc( nids );
157 /* copy in all the ids from the component blocks */
158 for ( i = 0; tmp[i] != NULL; i++ ) {
159 if ( tmp[i] == NULL ) {
163 SAFEMEMCPY( (char *) &idl->b_ids[nids], (char *) tmp[i]->b_ids,
164 tmp[i]->b_nids * sizeof(ID) );
165 nids += tmp[i]->b_nids;
169 free( (char *) tmp );
171 Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %d ids (%d max)\n", idl->b_nids,
186 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
189 memset( &data, 0, sizeof( data ) );
192 /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
194 data.dptr = (char *) idl;
195 data.dsize = (2 + idl->b_nmax) * sizeof(ID);
198 Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
202 flags = LDBM_REPLACE;
203 if( li->li_flush_wrt ) flags |= LDBM_SYNC;
204 rc = ldbm_cache_store( db, key, data, flags );
206 /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
220 /* find where to split the block */
221 for ( i = 0; i < b->b_nids && id > b->b_ids[i]; i++ )
224 *n1 = idl_alloc( i == 0 ? 1 : i );
225 *n2 = idl_alloc( b->b_nids - i + (i == 0 ? 0 : 1));
228 * everything before the id being inserted in the first block
229 * unless there is nothing, in which case the id being inserted
232 SAFEMEMCPY( (char *) &(*n1)->b_ids[0], (char *) &b->b_ids[0],
234 (*n1)->b_nids = (i == 0 ? 1 : i);
237 (*n1)->b_ids[0] = id;
239 (*n2)->b_ids[0] = id;
242 /* the id being inserted & everything after in the second block */
243 SAFEMEMCPY( (char *) &(*n2)->b_ids[i == 0 ? 0 : 1],
244 (char *) &b->b_ids[i], (b->b_nids - i) * sizeof(ID) );
245 (*n2)->b_nids = b->b_nids - i + (i == 0 ? 0 : 1);
249 * idl_change_first - called when an indirect block's first key has
250 * changed, meaning it needs to be stored under a new key, and the
251 * header block pointing to it needs updating.
258 Datum hkey, /* header block key */
259 IDList *h, /* header block */
260 int pos, /* pos in h to update */
261 Datum bkey, /* data block key */
262 IDList *b /* data block */
267 /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
269 /* delete old key block */
270 if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
271 Debug( LDAP_DEBUG_ANY,
272 "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
277 /* write block with new key */
278 sprintf( bkey.dptr, "%c%s%d", CONT_PREFIX, hkey.dptr, b->b_ids[0] );
279 bkey.dsize = strlen( bkey.dptr ) + 1;
280 if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
281 Debug( LDAP_DEBUG_ANY,
282 "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
286 /* update + write indirect header block */
287 h->b_ids[pos] = b->b_ids[0];
288 if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
289 Debug( LDAP_DEBUG_ANY,
290 "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
306 IDList *idl, *tmp, *tmp2, *tmp3;
311 memset( &k2, 0, sizeof( k2 ) );
314 if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
316 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
320 idl = idl_alloc( 1 );
321 idl->b_ids[idl->b_nids++] = id;
322 rc = idl_store( be, db, key, idl );
329 if ( ! INDIRECT_BLOCK( idl ) ) {
330 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
331 case 0: /* id inserted - store the updated block */
333 rc = idl_store( be, db, key, idl );
336 case 2: /* id already there - nothing to do */
340 case 3: /* id not inserted - block must be split */
341 /* check threshold for marking this an all-id block */
342 if ( db->dbc_maxindirect < 2 ) {
344 idl = idl_allids( be );
345 rc = idl_store( be, db, key, idl );
351 idl_split_block( idl, id, &tmp, &tmp2 );
354 /* create the header indirect block */
355 idl = idl_alloc( 3 );
357 idl->b_nids = INDBLOCK;
358 idl->b_ids[0] = tmp->b_ids[0];
359 idl->b_ids[1] = tmp2->b_ids[0];
360 idl->b_ids[2] = NOID;
363 rc = idl_store( be, db, key, idl );
365 /* store the first id block */
366 kstr = (char *) ch_malloc( key.dsize + 20 );
367 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
370 k2.dsize = strlen( kstr ) + 1;
371 rc = idl_store( be, db, k2, tmp );
373 /* store the second id block */
374 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
377 k2.dsize = strlen( kstr ) + 1;
378 rc = idl_store( be, db, k2, tmp2 );
391 * this is an indirect block which points to other blocks.
392 * we need to read in the block into which the id should be
393 * inserted, then insert the id and store the block. we might
394 * have to split the block if it is full, which means we also
395 * need to write a new "header" block.
398 /* select the block to try inserting into */
399 for ( i = 0; idl->b_ids[i] != NOID && id > idl->b_ids[i]; i++ )
409 kstr = (char *) ch_malloc( key.dsize + 20 );
410 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr, idl->b_ids[i] );
412 k2.dsize = strlen( kstr ) + 1;
413 if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
414 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
420 switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
421 case 0: /* id inserted ok */
422 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
423 Debug( LDAP_DEBUG_ANY,
424 "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
428 case 1: /* id inserted - first id in block has changed */
430 * key for this block has changed, so we have to
431 * write the block under the new key, delete the
432 * old key block + update and write the indirect
436 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
439 case 2: /* id not inserted - already there */
442 case 3: /* id not inserted - block is full */
444 * first, see if it will fit in the next block,
445 * without splitting, unless we're trying to insert
446 * into the beginning of the first block.
449 /* is there a next block? */
450 if ( !first && idl->b_ids[i + 1] != NOID ) {
452 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
455 k2.dsize = strlen( kstr ) + 1;
456 if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
457 Debug( LDAP_DEBUG_ANY,
458 "idl_fetch_one (%s) returns NULL\n",
463 switch ( (rc = idl_insert( &tmp2, id,
464 db->dbc_maxids )) ) {
465 case 1: /* id inserted first in block */
466 rc = idl_change_first( be, db, key, idl,
470 case 2: /* id already there - how? */
471 case 0: /* id inserted */
473 Debug( LDAP_DEBUG_ANY,
474 "id %d already in next block\n",
483 case 3: /* split the original block */
491 * must split the block, write both new blocks + update
492 * and write the indirect header block.
495 /* count how many indirect blocks */
496 for ( j = 0; idl->b_ids[j] != NOID; j++ )
499 /* check it against all-id thresholed */
500 if ( j + 1 > db->dbc_maxindirect ) {
502 * we've passed the all-id threshold, meaning
503 * that this set of blocks should be replaced
504 * by a single "all-id" block. our job: delete
505 * all the indirect blocks, and replace the header
506 * block by an all-id block.
509 /* delete all indirect blocks */
510 for ( j = 0; idl->b_ids[j] != NOID; j++ ) {
511 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
514 k2.dsize = strlen( kstr ) + 1;
516 rc = ldbm_cache_delete( db, k2 );
519 /* store allid block in place of header block */
521 idl = idl_allids( be );
522 rc = idl_store( be, db, key, idl );
530 idl_split_block( tmp, id, &tmp2, &tmp3 );
533 /* create a new updated indirect header block */
534 tmp = idl_alloc( idl->b_nmax + 1 );
535 tmp->b_nids = INDBLOCK;
536 /* everything up to the split block */
537 SAFEMEMCPY( (char *) tmp->b_ids, (char *) idl->b_ids,
539 /* the two new blocks */
540 tmp->b_ids[i] = tmp2->b_ids[0];
541 tmp->b_ids[i + 1] = tmp3->b_ids[0];
542 /* everything after the split block */
543 SAFEMEMCPY( (char *) &tmp->b_ids[i + 2], (char *)
544 &idl->b_ids[i + 1], (idl->b_nmax - i - 1) * sizeof(ID) );
546 /* store the header block */
547 rc = idl_store( be, db, key, tmp );
549 /* store the first id block */
550 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
553 k2.dsize = strlen( kstr ) + 1;
554 rc = idl_store( be, db, k2, tmp2 );
556 /* store the second id block */
557 sprintf( kstr, "%c%s%d", CONT_PREFIX, key.dptr,
560 k2.dsize = strlen( kstr ) + 1;
561 rc = idl_store( be, db, k2, tmp3 );
575 * idl_insert - insert an id into an id list.
576 * returns 0 id inserted
577 * 1 id inserted, first id in block has changed
578 * 2 id not inserted, already there
579 * 3 id not inserted, block must be split
583 idl_insert( IDList **idl, ID id, int maxids )
587 if ( ALLIDS( *idl ) ) {
588 return( 2 ); /* already there */
591 /* is it already there? XXX bin search XXX */
592 for ( i = 0; i < (*idl)->b_nids && id > (*idl)->b_ids[i]; i++ ) {
595 if ( i < (*idl)->b_nids && (*idl)->b_ids[i] == id ) {
596 return( 2 ); /* already there */
599 /* do we need to make room for it? */
600 if ( (*idl)->b_nids == (*idl)->b_nmax ) {
601 /* make room or indicate block needs splitting */
602 if ( (*idl)->b_nmax == maxids ) {
603 return( 3 ); /* block needs splitting */
607 if ( (*idl)->b_nmax > maxids ) {
608 (*idl)->b_nmax = maxids;
610 *idl = (IDList *) ch_realloc( (char *) *idl,
611 ((*idl)->b_nmax + 2) * sizeof(ID) );
614 /* make a slot for the new id */
615 for ( j = (*idl)->b_nids; j != i; j-- ) {
616 (*idl)->b_ids[j] = (*idl)->b_ids[j-1];
618 (*idl)->b_ids[i] = id;
620 (void) memset( (char *) &(*idl)->b_ids[(*idl)->b_nids], '\0',
621 ((*idl)->b_nmax - (*idl)->b_nids) * sizeof(ID) );
623 return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
627 idl_dup( IDList *idl )
635 new = idl_alloc( idl->b_nmax );
636 SAFEMEMCPY( (char *) new, (char *) idl, (idl->b_nmax + 2)
643 idl_min( IDList *a, IDList *b )
645 return( a->b_nids > b->b_nids ? b : a );
649 * idl_intersection - return a intersection b
662 if ( a == NULL || b == NULL ) {
666 return( idl_dup( b ) );
669 return( idl_dup( a ) );
672 n = idl_dup( idl_min( a, b ) );
674 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
675 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai]; bi++ )
678 if ( bi == b->b_nids ) {
682 if ( b->b_ids[bi] == a->b_ids[ai] ) {
683 n->b_ids[ni++] = a->b_ids[ai];
697 * idl_union - return a union b
711 return( idl_dup( b ) );
714 return( idl_dup( a ) );
716 if ( ALLIDS( a ) || ALLIDS( b ) ) {
717 return( idl_allids( be ) );
720 if ( b->b_nids < a->b_nids ) {
726 n = idl_alloc( a->b_nids + b->b_nids );
728 for ( ni = 0, ai = 0, bi = 0; ai < a->b_nids && bi < b->b_nids; ) {
729 if ( a->b_ids[ai] < b->b_ids[bi] ) {
730 n->b_ids[ni++] = a->b_ids[ai++];
731 } else if ( b->b_ids[bi] < a->b_ids[ai] ) {
732 n->b_ids[ni++] = b->b_ids[bi++];
734 n->b_ids[ni++] = a->b_ids[ai];
739 for ( ; ai < a->b_nids; ai++ ) {
740 n->b_ids[ni++] = a->b_ids[ai];
742 for ( ; bi < b->b_nids; bi++ ) {
743 n->b_ids[ni++] = b->b_ids[bi];
751 * idl_notin - return a intersection ~b (or a minus b)
767 if ( b == NULL || ALLIDS( b )) {
768 return( idl_dup( a ) );
772 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
775 for ( ai = 1, bi = 0; ai < a->b_nids && ni < n->b_nmax &&
776 bi < b->b_nmax; ai++ ) {
777 if ( b->b_ids[bi] == ai ) {
784 for ( ; ai < a->b_nids && ni < n->b_nmax; ai++ ) {
788 if ( ni == n->b_nmax ) {
790 return( idl_allids( be ) );
800 for ( ai = 0, bi = 0; ai < a->b_nids; ai++ ) {
801 for ( ; bi < b->b_nids && b->b_ids[bi] < a->b_ids[ai];
806 if ( bi == b->b_nids ) {
810 if ( b->b_ids[bi] != a->b_ids[ai] ) {
811 n->b_ids[ni++] = a->b_ids[ai];
815 for ( ; ai < a->b_nids; ai++ ) {
816 n->b_ids[ni++] = a->b_ids[ai];
824 idl_firstid( IDList *idl )
826 if ( idl == NULL || idl->b_nids == 0 ) {
830 if ( ALLIDS( idl ) ) {
831 return( idl->b_nids == 1 ? NOID : 1 );
834 return( idl->b_ids[0] );
838 idl_nextid( IDList *idl, ID id )
842 if ( ALLIDS( idl ) ) {
843 return( ++id < idl->b_nids ? id : NOID );
846 for ( i = 0; i < idl->b_nids && idl->b_ids[i] < id; i++ ) {
851 if ( i >= idl->b_nids ) {
854 return( idl->b_ids[i] );