1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
26 static DBC *cursor = NULL;
29 typedef struct dn_id {
34 #define HOLE_SIZE 4096
35 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
36 static unsigned nhmax = HOLE_SIZE;
37 static unsigned nholes;
39 static int index_nattrs;
41 #ifdef BDB_TOOL_IDL_CACHING
42 #define bdb_tool_idl_cmp BDB_SYMBOL(tool_idl_cmp)
43 #define bdb_tool_idl_flush_one BDB_SYMBOL(tool_idl_flush_one)
44 #define bdb_tool_idl_flush BDB_SYMBOL(tool_idl_flush)
46 static int bdb_tool_idl_flush( BackendDB *be );
50 typedef struct bdb_tool_idl_cache_entry {
51 struct bdb_tool_idl_cache_entry *next;
53 } bdb_tool_idl_cache_entry;
55 typedef struct bdb_tool_idl_cache {
57 bdb_tool_idl_cache_entry *head, *tail;
62 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
63 #endif /* BDB_TOOL_IDL_CACHING */
65 static ID bdb_tool_ix_id;
66 static Operation *bdb_tool_ix_op;
67 static int *bdb_tool_index_threads, bdb_tool_index_tcount;
68 static void *bdb_tool_index_rec;
69 static struct bdb_info *bdb_tool_info;
70 static ldap_pvt_thread_mutex_t bdb_tool_index_mutex;
71 static ldap_pvt_thread_cond_t bdb_tool_index_cond;
73 static void * bdb_tool_index_task( void *ctx, void *ptr );
75 int bdb_tool_entry_open(
76 BackendDB *be, int mode )
78 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
80 /* initialize key and data thangs */
83 key.flags = DB_DBT_REALLOC;
84 data.flags = DB_DBT_REALLOC;
87 int rc = bdb->bi_id2entry->bdi_db->cursor(
88 bdb->bi_id2entry->bdi_db, NULL, &cursor,
95 /* Set up for threaded slapindex */
96 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK) {
97 if ( !bdb_tool_info ) {
99 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
100 ldap_pvt_thread_cond_init( &bdb_tool_index_cond );
101 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
102 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
103 bdb_tool_index_tcount = slap_tool_thread_max - 1;
104 for (i=1; i<slap_tool_thread_max; i++) {
105 int *ptr = ch_malloc( sizeof( int ));
107 ldap_pvt_thread_pool_submit( &connection_pool,
108 bdb_tool_index_task, ptr );
117 int bdb_tool_entry_close(
120 if ( bdb_tool_info ) {
122 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
123 bdb_tool_index_tcount = slap_tool_thread_max - 1;
124 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond );
125 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
133 ch_free( data.data );
138 cursor->c_close( cursor );
142 #ifdef BDB_TOOL_IDL_CACHING
143 bdb_tool_idl_flush( be );
148 fprintf( stderr, "Error, entries missing!\n");
149 for (i=0; i<nholes; i++) {
150 fprintf(stderr, " entry %ld: %s\n",
151 holes[i].id, holes[i].dn.bv_val);
159 ID bdb_tool_entry_next(
164 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
166 assert( be != NULL );
167 assert( slapMode & SLAP_TOOL_MODE );
168 assert( bdb != NULL );
170 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
173 /* If we're doing linear indexing and there are more attrs to
174 * index, and we're at the end of the database, start over.
176 if ( index_nattrs && rc == DB_NOTFOUND ) {
177 /* optional - do a checkpoint here? */
178 bdb_attr_info_free( bdb->bi_attrs[0] );
179 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
181 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
190 if( data.data == NULL ) {
194 BDB_DISK2ID( key.data, &id );
198 ID bdb_tool_dn2id_get(
205 EntryInfo *ei = NULL;
208 if ( BER_BVISEMPTY(dn) )
213 op.o_tmpmemctx = NULL;
214 op.o_tmpmfuncs = &ch_mfuncs;
216 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
217 if ( ei ) bdb_cache_entryinfo_unlock( ei );
218 if ( rc == DB_NOTFOUND )
224 int bdb_tool_id2entry_get(
230 int rc = bdb_id2entry( be, NULL, 0, id, e );
232 if ( rc == DB_NOTFOUND && id == 0 ) {
233 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
234 struct berval gluebv = BER_BVC("glue");
235 dummy->e_name.bv_val = ch_strdup( "" );
236 dummy->e_nname.bv_val = ch_strdup( "" );
237 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
238 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
246 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
252 assert( be != NULL );
253 assert( slapMode & SLAP_TOOL_MODE );
254 assert( data.data != NULL );
256 DBT2bv( &data, &bv );
258 #ifdef SLAP_ZONE_ALLOC
259 /* FIXME: will add ctx later */
260 rc = entry_decode( &bv, &e, NULL );
262 rc = entry_decode( &bv, &e );
265 if( rc == LDAP_SUCCESS ) {
269 if ( slapMode & SLAP_TOOL_READONLY ) {
270 EntryInfo *ei = NULL;
276 op.o_tmpmemctx = NULL;
277 op.o_tmpmfuncs = &ch_mfuncs;
279 rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
280 if ( rc == LDAP_SUCCESS ) {
281 bdb_cache_entryinfo_unlock( ei );
293 static int bdb_tool_next_id(
300 struct berval dn = e->e_name;
301 struct berval ndn = e->e_nname;
302 struct berval pdn, npdn;
303 EntryInfo *ei = NULL, eidummy;
306 if (ndn.bv_len == 0) {
311 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
312 if ( ei ) bdb_cache_entryinfo_unlock( ei );
313 if ( rc == DB_NOTFOUND ) {
314 if ( !be_issuffix( op->o_bd, &ndn ) ) {
316 dnParent( &dn, &pdn );
317 dnParent( &ndn, &npdn );
320 rc = bdb_tool_next_id( op, tid, e, text, 1 );
326 /* If parent didn't exist, it was created just now
327 * and its ID is now in e->e_id. Make sure the current
328 * entry gets added under the new parent ID.
330 if ( eid != e->e_id ) {
331 eidummy.bei_id = e->e_id;
335 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
337 snprintf( text->bv_val, text->bv_len,
338 "next_id failed: %s (%d)",
339 db_strerror(rc), rc );
340 Debug( LDAP_DEBUG_ANY,
341 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
344 rc = bdb_dn2id_add( op, tid, ei, e );
346 snprintf( text->bv_val, text->bv_len,
347 "dn2id_add failed: %s (%d)",
348 db_strerror(rc), rc );
349 Debug( LDAP_DEBUG_ANY,
350 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
352 if ( nholes == nhmax - 1 ) {
353 if ( holes == hbuf ) {
354 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
355 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
357 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
361 ber_dupbv( &holes[nholes].dn, &ndn );
362 holes[nholes++].id = e->e_id;
364 } else if ( !hole ) {
367 e->e_id = ei->bei_id;
369 for ( i=0; i<nholes; i++) {
370 if ( holes[i].id == e->e_id ) {
372 free(holes[i].dn.bv_val);
373 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
377 } else if ( holes[i].id > e->e_id ) {
391 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
393 if ( slapMode & SLAP_TOOL_QUICK ) {
398 ir = bdb_tool_index_rec;
399 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
401 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
402 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
403 &a->a_desc->ad_tags, ir );
407 bdb_tool_ix_id = e->e_id;
409 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
410 /* Wait for all threads to be ready */
411 while ( bdb_tool_index_tcount ) {
412 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
413 ldap_pvt_thread_yield();
414 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
416 for ( i=1; i<slap_tool_thread_max; i++ )
417 bdb_tool_index_threads[i] = LDAP_BUSY;
418 bdb_tool_index_tcount = slap_tool_thread_max - 1;
419 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond );
420 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
421 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
424 for ( i=1; i<slap_tool_thread_max; i++ ) {
425 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
426 ldap_pvt_thread_yield();
430 if ( bdb_tool_index_threads[i] )
431 return bdb_tool_index_threads[i];
435 return bdb_index_entry_add( op, txn, e );
439 ID bdb_tool_entry_put(
442 struct berval *text )
445 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
450 assert( be != NULL );
451 assert( slapMode & SLAP_TOOL_MODE );
453 assert( text != NULL );
454 assert( text->bv_val != NULL );
455 assert( text->bv_val[0] == '\0' ); /* overconservative? */
457 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
458 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
460 if (! (slapMode & SLAP_TOOL_QUICK)) {
461 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
462 bdb->bi_db_opflags );
464 snprintf( text->bv_val, text->bv_len,
465 "txn_begin failed: %s (%d)",
466 db_strerror(rc), rc );
467 Debug( LDAP_DEBUG_ANY,
468 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
469 text->bv_val, 0, 0 );
476 op.o_tmpmemctx = NULL;
477 op.o_tmpmfuncs = &ch_mfuncs;
479 /* add dn2id indices */
480 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
486 rc = bdb_id2entry_add( be, tid, e );
488 snprintf( text->bv_val, text->bv_len,
489 "id2entry_add failed: %s (%d)",
490 db_strerror(rc), rc );
491 Debug( LDAP_DEBUG_ANY,
492 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
493 text->bv_val, 0, 0 );
497 if ( !bdb->bi_linear_index )
498 rc = bdb_tool_index_add( &op, tid, e );
500 snprintf( text->bv_val, text->bv_len,
501 "index_entry_add failed: %s (%d)",
502 db_strerror(rc), rc );
503 Debug( LDAP_DEBUG_ANY,
504 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
505 text->bv_val, 0, 0 );
511 if ( !( slapMode & SLAP_TOOL_QUICK )) {
512 rc = TXN_COMMIT( tid, 0 );
514 snprintf( text->bv_val, text->bv_len,
515 "txn_commit failed: %s (%d)",
516 db_strerror(rc), rc );
517 Debug( LDAP_DEBUG_ANY,
518 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
519 text->bv_val, 0, 0 );
525 if ( !( slapMode & SLAP_TOOL_QUICK )) {
527 snprintf( text->bv_val, text->bv_len,
528 "txn_aborted! %s (%d)",
529 db_strerror(rc), rc );
530 Debug( LDAP_DEBUG_ANY,
531 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
532 text->bv_val, 0, 0 );
540 int bdb_tool_entry_reindex(
544 struct bdb_info *bi = (struct bdb_info *) be->be_private;
551 Debug( LDAP_DEBUG_ARGS,
552 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
555 /* No indexes configured, nothing to do. Could return an
556 * error here to shortcut things.
562 /* Get the first attribute to index */
563 if (bi->bi_linear_index && !index_nattrs) {
564 index_nattrs = bi->bi_nattrs - 1;
568 e = bdb_tool_entry_get( be, id );
571 Debug( LDAP_DEBUG_ANY,
572 LDAP_XSTRING(bdb_tool_entry_reindex)
573 ": could not locate id=%ld\n",
578 if (! (slapMode & SLAP_TOOL_QUICK)) {
579 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
581 Debug( LDAP_DEBUG_ANY,
582 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
583 "txn_begin failed: %s (%d)\n",
584 db_strerror(rc), rc, 0 );
590 * just (re)add them for now
591 * assume that some other routine (not yet implemented)
592 * will zap index databases
596 Debug( LDAP_DEBUG_TRACE,
597 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
598 (long) id, e->e_dn, 0 );
602 op.o_tmpmemctx = NULL;
603 op.o_tmpmfuncs = &ch_mfuncs;
605 rc = bdb_tool_index_add( &op, tid, e );
609 if (! (slapMode & SLAP_TOOL_QUICK)) {
610 rc = TXN_COMMIT( tid, 0 );
612 Debug( LDAP_DEBUG_ANY,
613 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
614 ": txn_commit failed: %s (%d)\n",
615 db_strerror(rc), rc, 0 );
621 if (! (slapMode & SLAP_TOOL_QUICK)) {
623 Debug( LDAP_DEBUG_ANY,
624 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
625 ": txn_aborted! %s (%d)\n",
626 db_strerror(rc), rc, 0 );
630 bdb_entry_release( &op, e, 0 );
635 ID bdb_tool_entry_modify(
638 struct berval *text )
641 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
646 assert( be != NULL );
647 assert( slapMode & SLAP_TOOL_MODE );
649 assert( text != NULL );
650 assert( text->bv_val != NULL );
651 assert( text->bv_val[0] == '\0' ); /* overconservative? */
653 assert ( e->e_id != NOID );
655 Debug( LDAP_DEBUG_TRACE,
656 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
657 (long) e->e_id, e->e_dn, 0 );
659 if (! (slapMode & SLAP_TOOL_QUICK)) {
660 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
661 bdb->bi_db_opflags );
663 snprintf( text->bv_val, text->bv_len,
664 "txn_begin failed: %s (%d)",
665 db_strerror(rc), rc );
666 Debug( LDAP_DEBUG_ANY,
667 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
668 text->bv_val, 0, 0 );
675 op.o_tmpmemctx = NULL;
676 op.o_tmpmfuncs = &ch_mfuncs;
679 rc = bdb_id2entry_update( be, tid, e );
681 snprintf( text->bv_val, text->bv_len,
682 "id2entry_add failed: %s (%d)",
683 db_strerror(rc), rc );
684 Debug( LDAP_DEBUG_ANY,
685 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
686 text->bv_val, 0, 0 );
691 /* FIXME: this is bogus, we don't have the old values to delete
692 * from the index because the given entry has already been modified.
694 rc = bdb_index_entry_del( &op, tid, e );
696 snprintf( text->bv_val, text->bv_len,
697 "index_entry_del failed: %s (%d)",
698 db_strerror(rc), rc );
699 Debug( LDAP_DEBUG_ANY,
700 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
701 text->bv_val, 0, 0 );
706 rc = bdb_index_entry_add( &op, tid, e );
708 snprintf( text->bv_val, text->bv_len,
709 "index_entry_add failed: %s (%d)",
710 db_strerror(rc), rc );
711 Debug( LDAP_DEBUG_ANY,
712 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
713 text->bv_val, 0, 0 );
719 if (! (slapMode & SLAP_TOOL_QUICK)) {
720 rc = TXN_COMMIT( tid, 0 );
722 snprintf( text->bv_val, text->bv_len,
723 "txn_commit failed: %s (%d)",
724 db_strerror(rc), rc );
725 Debug( LDAP_DEBUG_ANY,
726 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
727 "%s\n", text->bv_val, 0, 0 );
733 if (! (slapMode & SLAP_TOOL_QUICK)) {
735 snprintf( text->bv_val, text->bv_len,
736 "txn_aborted! %s (%d)",
737 db_strerror(rc), rc );
738 Debug( LDAP_DEBUG_ANY,
739 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
740 text->bv_val, 0, 0 );
748 #ifdef BDB_TOOL_IDL_CACHING
750 bdb_tool_idl_cmp( const void *v1, const void *v2 )
752 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
755 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
756 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
760 bdb_tool_idl_flush_one( void *v1, void *arg )
762 bdb_tool_idl_cache *ic = v1;
764 struct bdb_info *bdb = bdb_tool_info;
765 bdb_tool_idl_cache_entry *ice;
771 /* Freshly allocated, ignore it */
772 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
776 rc = db->cursor( db, NULL, &curs, 0 );
783 bv2DBT( &ic->kstr, &key );
785 data.size = data.ulen = sizeof( ID );
786 data.flags = DB_DBT_USERMEM;
789 rc = curs->c_get( curs, &key, &data, DB_SET );
790 /* If key already exists and we're writing a range... */
791 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
792 /* If it's not currently a range, must delete old info */
795 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
796 curs->c_del( curs, 0 );
799 /* Store range marker */
800 curs->c_put( curs, &key, &data, DB_KEYFIRST );
804 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
807 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
810 curs->c_del( curs, 0 );
812 BDB_ID2DISK( ic->last, &nid );
813 curs->c_put( curs, &key, &data, DB_KEYLAST );
815 } else if ( rc && rc != DB_NOTFOUND ) {
817 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
818 /* range, didn't exist before */
820 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
822 BDB_ID2DISK( ic->first, &nid );
823 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
825 BDB_ID2DISK( ic->last, &nid );
826 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
835 /* Just a normal write */
837 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
842 end = ic->count & (IDBLOCK-1);
846 for ( i=0; i<end; i++ ) {
847 if ( !ice->ids[i] ) continue;
848 BDB_ID2DISK( ice->ids[i], &nid );
849 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
851 if ( rc == DB_KEYEXIST ) {
865 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
866 ic->tail->next = bdb_tool_idl_free_list;
867 bdb_tool_idl_free_list = ic->head;
868 bdb->bi_idl_cache_size -= n;
869 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
872 if ( ic != db->app_private ) {
875 ic->head = ic->tail = NULL;
877 curs->c_close( curs );
882 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
884 Avlnode *root = db->app_private;
887 db->app_private = ic;
888 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
889 avl_free( root, NULL );
890 db->app_private = NULL;
897 bdb_tool_idl_flush( BackendDB *be )
899 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
904 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
905 db = bdb->bi_databases[i]->bdi_db;
906 if ( !db->app_private ) continue;
907 rc = bdb_tool_idl_flush_db( db, NULL );
912 bdb->bi_idl_cache_size = 0;
917 int bdb_tool_idl_add(
924 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
925 bdb_tool_idl_cache *ic, itmp;
926 bdb_tool_idl_cache_entry *ice;
929 if ( !bdb->bi_idl_cache_max_size )
930 return bdb_idl_insert_key( be, db, txn, key, id );
932 DBT2bv( key, &itmp.kstr );
934 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
936 /* No entry yet, create one */
943 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
944 ic->kstr.bv_len = itmp.kstr.bv_len;
945 ic->kstr.bv_val = (char *)(ic+1);
946 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
947 ic->head = ic->tail = NULL;
950 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
953 /* load existing key count here */
954 rc = db->cursor( db, NULL, &curs, 0 );
957 data.ulen = sizeof( ID );
958 data.flags = DB_DBT_USERMEM;
960 rc = curs->c_get( curs, key, &data, DB_SET );
963 ic->count = BDB_IDL_DB_SIZE+1;
967 curs->c_count( curs, &count, 0 );
969 BDB_DISK2ID( &nid, &ic->first );
972 curs->c_close( curs );
974 /* are we a range already? */
975 if ( ic->count > BDB_IDL_DB_SIZE ) {
978 /* Are we at the limit, and converting to a range? */
979 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
981 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
984 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
985 ic->tail->next = bdb_tool_idl_free_list;
986 bdb_tool_idl_free_list = ic->head;
987 bdb->bi_idl_cache_size -= n;
988 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
990 ic->head = ic->tail = NULL;
995 /* No free block, create that too */
996 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
998 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
999 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1000 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1001 rc = bdb_tool_idl_flush_db( db, ic );
1004 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1006 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1008 bdb->bi_idl_cache_size++;
1009 if ( bdb_tool_idl_free_list ) {
1010 ice = bdb_tool_idl_free_list;
1011 bdb_tool_idl_free_list = ice->next;
1013 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1015 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1017 memset( ice, 0, sizeof( *ice ));
1021 ic->tail->next = ice;
1028 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1036 bdb_tool_index_task( void *ctx, void *ptr )
1038 int base = *(int *)ptr;
1042 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1043 bdb_tool_index_tcount--;
1044 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond,
1045 &bdb_tool_index_mutex );
1046 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1047 if ( slapd_shutdown )
1050 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1051 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );