1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2006 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>
27 static DBC *cursor = NULL;
29 static EntryHeader eh;
32 typedef struct dn_id {
37 #define HOLE_SIZE 4096
38 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
39 static unsigned nhmax = HOLE_SIZE;
40 static unsigned nholes;
42 static int index_nattrs;
44 #ifdef BDB_TOOL_IDL_CACHING
45 #define bdb_tool_idl_cmp BDB_SYMBOL(tool_idl_cmp)
46 #define bdb_tool_idl_flush_one BDB_SYMBOL(tool_idl_flush_one)
47 #define bdb_tool_idl_flush BDB_SYMBOL(tool_idl_flush)
49 static int bdb_tool_idl_flush( BackendDB *be );
53 typedef struct bdb_tool_idl_cache_entry {
54 struct bdb_tool_idl_cache_entry *next;
56 } bdb_tool_idl_cache_entry;
58 typedef struct bdb_tool_idl_cache {
60 bdb_tool_idl_cache_entry *head, *tail;
65 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
66 #endif /* BDB_TOOL_IDL_CACHING */
68 static ID bdb_tool_ix_id;
69 static Operation *bdb_tool_ix_op;
70 static int *bdb_tool_index_threads, bdb_tool_index_tcount;
71 static void *bdb_tool_index_rec;
72 static struct bdb_info *bdb_tool_info;
73 static ldap_pvt_thread_mutex_t bdb_tool_index_mutex;
74 static ldap_pvt_thread_cond_t bdb_tool_index_cond_main;
75 static ldap_pvt_thread_cond_t bdb_tool_index_cond_work;
77 static void * bdb_tool_index_task( void *ctx, void *ptr );
79 int bdb_tool_entry_open(
80 BackendDB *be, int mode )
82 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
84 /* initialize key and data thangs */
87 key.flags = DB_DBT_REALLOC;
88 data.flags = DB_DBT_USERMEM;
91 int rc = bdb->bi_id2entry->bdi_db->cursor(
92 bdb->bi_id2entry->bdi_db, NULL, &cursor,
99 /* Set up for threaded slapindex */
100 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK
101 && bdb->bi_nattrs ) {
102 if ( !bdb_tool_info ) {
104 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
105 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
106 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
107 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
108 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
109 bdb_tool_index_tcount = slap_tool_thread_max - 1;
110 for (i=1; i<slap_tool_thread_max; i++) {
111 int *ptr = ch_malloc( sizeof( int ));
113 ldap_pvt_thread_pool_submit( &connection_pool,
114 bdb_tool_index_task, ptr );
123 int bdb_tool_entry_close(
126 if ( bdb_tool_info ) {
128 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
129 bdb_tool_index_tcount = slap_tool_thread_max - 1;
130 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
131 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
139 ch_free( eh.bv.bv_val );
144 cursor->c_close( cursor );
148 #ifdef BDB_TOOL_IDL_CACHING
149 bdb_tool_idl_flush( be );
154 fprintf( stderr, "Error, entries missing!\n");
155 for (i=0; i<nholes; i++) {
156 fprintf(stderr, " entry %ld: %s\n",
157 holes[i].id, holes[i].dn.bv_val);
165 ID bdb_tool_entry_next(
170 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
173 assert( be != NULL );
174 assert( slapMode & SLAP_TOOL_MODE );
175 assert( bdb != NULL );
178 data.ulen = data.dlen = sizeof( buf );
180 data.flags |= DB_DBT_PARTIAL;
181 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
184 /* If we're doing linear indexing and there are more attrs to
185 * index, and we're at the end of the database, start over.
187 if ( index_nattrs && rc == DB_NOTFOUND ) {
188 /* optional - do a checkpoint here? */
189 bdb_attr_info_free( bdb->bi_attrs[0] );
190 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
192 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
203 eh.bv.bv_len = data.size;
204 rc = entry_header( &eh );
205 eoff = eh.data - eh.bv.bv_val;
211 BDB_DISK2ID( key.data, &id );
215 ID bdb_tool_dn2id_get(
222 EntryInfo *ei = NULL;
225 if ( BER_BVISEMPTY(dn) )
230 op.o_tmpmemctx = NULL;
231 op.o_tmpmfuncs = &ch_mfuncs;
233 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
234 if ( ei ) bdb_cache_entryinfo_unlock( ei );
235 if ( rc == DB_NOTFOUND )
241 int bdb_tool_id2entry_get(
247 int rc = bdb_id2entry( be, NULL, 0, id, e );
249 if ( rc == DB_NOTFOUND && id == 0 ) {
250 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
251 struct berval gluebv = BER_BVC("glue");
252 dummy->e_name.bv_val = ch_strdup( "" );
253 dummy->e_nname.bv_val = ch_strdup( "" );
254 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
255 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
263 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
269 assert( be != NULL );
270 assert( slapMode & SLAP_TOOL_MODE );
273 data.flags ^= DB_DBT_PARTIAL;
275 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
276 if ( rc != ENOMEM ) goto leave;
278 /* Allocate a block and retrieve the data */
279 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
280 eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
281 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
283 data.ulen = data.size;
285 /* Skip past already parsed nattr/nvals */
288 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
289 if ( rc ) goto leave;
291 #ifdef SLAP_ZONE_ALLOC
292 /* FIXME: will add ctx later */
293 rc = entry_decode( &eh, &e, NULL );
295 rc = entry_decode( &eh, &e );
298 if( rc == LDAP_SUCCESS ) {
301 if ( slapMode & SLAP_TOOL_READONLY ) {
302 EntryInfo *ei = NULL;
308 op.o_tmpmemctx = NULL;
309 op.o_tmpmfuncs = &ch_mfuncs;
311 rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
312 if ( rc == LDAP_SUCCESS ) {
313 bdb_cache_entryinfo_unlock( ei );
327 static int bdb_tool_next_id(
334 struct berval dn = e->e_name;
335 struct berval ndn = e->e_nname;
336 struct berval pdn, npdn;
337 EntryInfo *ei = NULL, eidummy;
340 if (ndn.bv_len == 0) {
345 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
346 if ( ei ) bdb_cache_entryinfo_unlock( ei );
347 if ( rc == DB_NOTFOUND ) {
348 if ( !be_issuffix( op->o_bd, &ndn ) ) {
350 dnParent( &dn, &pdn );
351 dnParent( &ndn, &npdn );
354 rc = bdb_tool_next_id( op, tid, e, text, 1 );
360 /* If parent didn't exist, it was created just now
361 * and its ID is now in e->e_id. Make sure the current
362 * entry gets added under the new parent ID.
364 if ( eid != e->e_id ) {
365 eidummy.bei_id = e->e_id;
369 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
371 snprintf( text->bv_val, text->bv_len,
372 "next_id failed: %s (%d)",
373 db_strerror(rc), rc );
374 Debug( LDAP_DEBUG_ANY,
375 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
378 rc = bdb_dn2id_add( op, tid, ei, e );
380 snprintf( text->bv_val, text->bv_len,
381 "dn2id_add failed: %s (%d)",
382 db_strerror(rc), rc );
383 Debug( LDAP_DEBUG_ANY,
384 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
386 if ( nholes == nhmax - 1 ) {
387 if ( holes == hbuf ) {
388 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
389 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
391 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
395 ber_dupbv( &holes[nholes].dn, &ndn );
396 holes[nholes++].id = e->e_id;
398 } else if ( !hole ) {
401 e->e_id = ei->bei_id;
403 for ( i=0; i<nholes; i++) {
404 if ( holes[i].id == e->e_id ) {
406 free(holes[i].dn.bv_val);
407 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
411 } else if ( holes[i].id > e->e_id ) {
425 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
427 if ( slapMode & SLAP_TOOL_QUICK ) {
432 ir = bdb_tool_index_rec;
433 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
435 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
436 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
437 &a->a_desc->ad_tags, ir );
441 bdb_tool_ix_id = e->e_id;
443 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
444 /* Wait for all threads to be ready */
445 while ( bdb_tool_index_tcount ) {
446 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
447 &bdb_tool_index_mutex );
449 for ( i=1; i<slap_tool_thread_max; i++ )
450 bdb_tool_index_threads[i] = LDAP_BUSY;
451 bdb_tool_index_tcount = slap_tool_thread_max - 1;
452 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
453 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
454 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
457 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
458 for ( i=1; i<slap_tool_thread_max; i++ ) {
459 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
460 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
461 &bdb_tool_index_mutex );
465 if ( bdb_tool_index_threads[i] ) {
466 rc = bdb_tool_index_threads[i];
470 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
473 return bdb_index_entry_add( op, txn, e );
477 ID bdb_tool_entry_put(
480 struct berval *text )
483 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
488 assert( be != NULL );
489 assert( slapMode & SLAP_TOOL_MODE );
491 assert( text != NULL );
492 assert( text->bv_val != NULL );
493 assert( text->bv_val[0] == '\0' ); /* overconservative? */
495 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
496 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
498 if (! (slapMode & SLAP_TOOL_QUICK)) {
499 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
500 bdb->bi_db_opflags );
502 snprintf( text->bv_val, text->bv_len,
503 "txn_begin failed: %s (%d)",
504 db_strerror(rc), rc );
505 Debug( LDAP_DEBUG_ANY,
506 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
507 text->bv_val, 0, 0 );
514 op.o_tmpmemctx = NULL;
515 op.o_tmpmfuncs = &ch_mfuncs;
517 /* add dn2id indices */
518 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
523 if ( !bdb->bi_linear_index )
524 rc = bdb_tool_index_add( &op, tid, e );
526 snprintf( text->bv_val, text->bv_len,
527 "index_entry_add failed: %s (%d)",
528 db_strerror(rc), rc );
529 Debug( LDAP_DEBUG_ANY,
530 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
531 text->bv_val, 0, 0 );
536 rc = bdb_id2entry_add( be, tid, e );
538 snprintf( text->bv_val, text->bv_len,
539 "id2entry_add failed: %s (%d)",
540 db_strerror(rc), rc );
541 Debug( LDAP_DEBUG_ANY,
542 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
543 text->bv_val, 0, 0 );
549 if ( !( slapMode & SLAP_TOOL_QUICK )) {
550 rc = TXN_COMMIT( tid, 0 );
552 snprintf( text->bv_val, text->bv_len,
553 "txn_commit failed: %s (%d)",
554 db_strerror(rc), rc );
555 Debug( LDAP_DEBUG_ANY,
556 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
557 text->bv_val, 0, 0 );
563 if ( !( slapMode & SLAP_TOOL_QUICK )) {
565 snprintf( text->bv_val, text->bv_len,
566 "txn_aborted! %s (%d)",
567 db_strerror(rc), rc );
568 Debug( LDAP_DEBUG_ANY,
569 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
570 text->bv_val, 0, 0 );
578 int bdb_tool_entry_reindex(
582 struct bdb_info *bi = (struct bdb_info *) be->be_private;
589 Debug( LDAP_DEBUG_ARGS,
590 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
593 /* No indexes configured, nothing to do. Could return an
594 * error here to shortcut things.
600 /* Get the first attribute to index */
601 if (bi->bi_linear_index && !index_nattrs) {
602 index_nattrs = bi->bi_nattrs - 1;
606 e = bdb_tool_entry_get( be, id );
609 Debug( LDAP_DEBUG_ANY,
610 LDAP_XSTRING(bdb_tool_entry_reindex)
611 ": could not locate id=%ld\n",
616 if (! (slapMode & SLAP_TOOL_QUICK)) {
617 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
619 Debug( LDAP_DEBUG_ANY,
620 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
621 "txn_begin failed: %s (%d)\n",
622 db_strerror(rc), rc, 0 );
628 * just (re)add them for now
629 * assume that some other routine (not yet implemented)
630 * will zap index databases
634 Debug( LDAP_DEBUG_TRACE,
635 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
636 (long) id, e->e_dn, 0 );
640 op.o_tmpmemctx = NULL;
641 op.o_tmpmfuncs = &ch_mfuncs;
643 rc = bdb_tool_index_add( &op, tid, e );
647 if (! (slapMode & SLAP_TOOL_QUICK)) {
648 rc = TXN_COMMIT( tid, 0 );
650 Debug( LDAP_DEBUG_ANY,
651 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
652 ": txn_commit failed: %s (%d)\n",
653 db_strerror(rc), rc, 0 );
659 if (! (slapMode & SLAP_TOOL_QUICK)) {
661 Debug( LDAP_DEBUG_ANY,
662 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
663 ": txn_aborted! %s (%d)\n",
664 db_strerror(rc), rc, 0 );
668 bdb_entry_release( &op, e, 0 );
673 ID bdb_tool_entry_modify(
676 struct berval *text )
679 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
684 assert( be != NULL );
685 assert( slapMode & SLAP_TOOL_MODE );
687 assert( text != NULL );
688 assert( text->bv_val != NULL );
689 assert( text->bv_val[0] == '\0' ); /* overconservative? */
691 assert ( e->e_id != NOID );
693 Debug( LDAP_DEBUG_TRACE,
694 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
695 (long) e->e_id, e->e_dn, 0 );
697 if (! (slapMode & SLAP_TOOL_QUICK)) {
698 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
699 bdb->bi_db_opflags );
701 snprintf( text->bv_val, text->bv_len,
702 "txn_begin failed: %s (%d)",
703 db_strerror(rc), rc );
704 Debug( LDAP_DEBUG_ANY,
705 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
706 text->bv_val, 0, 0 );
713 op.o_tmpmemctx = NULL;
714 op.o_tmpmfuncs = &ch_mfuncs;
717 rc = bdb_id2entry_update( be, tid, e );
719 snprintf( text->bv_val, text->bv_len,
720 "id2entry_add failed: %s (%d)",
721 db_strerror(rc), rc );
722 Debug( LDAP_DEBUG_ANY,
723 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
724 text->bv_val, 0, 0 );
730 if (! (slapMode & SLAP_TOOL_QUICK)) {
731 rc = TXN_COMMIT( tid, 0 );
733 snprintf( text->bv_val, text->bv_len,
734 "txn_commit failed: %s (%d)",
735 db_strerror(rc), rc );
736 Debug( LDAP_DEBUG_ANY,
737 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
738 "%s\n", text->bv_val, 0, 0 );
744 if (! (slapMode & SLAP_TOOL_QUICK)) {
746 snprintf( text->bv_val, text->bv_len,
747 "txn_aborted! %s (%d)",
748 db_strerror(rc), rc );
749 Debug( LDAP_DEBUG_ANY,
750 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
751 text->bv_val, 0, 0 );
759 #ifdef BDB_TOOL_IDL_CACHING
761 bdb_tool_idl_cmp( const void *v1, const void *v2 )
763 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
766 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
767 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
771 bdb_tool_idl_flush_one( void *v1, void *arg )
773 bdb_tool_idl_cache *ic = v1;
775 struct bdb_info *bdb = bdb_tool_info;
776 bdb_tool_idl_cache_entry *ice;
782 /* Freshly allocated, ignore it */
783 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
787 rc = db->cursor( db, NULL, &curs, 0 );
794 bv2DBT( &ic->kstr, &key );
796 data.size = data.ulen = sizeof( ID );
797 data.flags = DB_DBT_USERMEM;
800 rc = curs->c_get( curs, &key, &data, DB_SET );
801 /* If key already exists and we're writing a range... */
802 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
803 /* If it's not currently a range, must delete old info */
806 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
807 curs->c_del( curs, 0 );
810 /* Store range marker */
811 curs->c_put( curs, &key, &data, DB_KEYFIRST );
815 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
818 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
821 curs->c_del( curs, 0 );
823 BDB_ID2DISK( ic->last, &nid );
824 curs->c_put( curs, &key, &data, DB_KEYLAST );
826 } else if ( rc && rc != DB_NOTFOUND ) {
828 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
829 /* range, didn't exist before */
831 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
833 BDB_ID2DISK( ic->first, &nid );
834 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
836 BDB_ID2DISK( ic->last, &nid );
837 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
846 /* Just a normal write */
848 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
853 end = ic->count & (IDBLOCK-1);
857 for ( i=0; i<end; i++ ) {
858 if ( !ice->ids[i] ) continue;
859 BDB_ID2DISK( ice->ids[i], &nid );
860 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
862 if ( rc == DB_KEYEXIST ) {
876 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
877 ic->tail->next = bdb_tool_idl_free_list;
878 bdb_tool_idl_free_list = ic->head;
879 bdb->bi_idl_cache_size -= n;
880 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
883 if ( ic != db->app_private ) {
886 ic->head = ic->tail = NULL;
888 curs->c_close( curs );
893 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
895 Avlnode *root = db->app_private;
898 db->app_private = ic;
899 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
900 avl_free( root, NULL );
901 db->app_private = NULL;
908 bdb_tool_idl_flush( BackendDB *be )
910 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
915 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
916 db = bdb->bi_databases[i]->bdi_db;
917 if ( !db->app_private ) continue;
918 rc = bdb_tool_idl_flush_db( db, NULL );
923 bdb->bi_idl_cache_size = 0;
928 int bdb_tool_idl_add(
935 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
936 bdb_tool_idl_cache *ic, itmp;
937 bdb_tool_idl_cache_entry *ice;
940 if ( !bdb->bi_idl_cache_max_size )
941 return bdb_idl_insert_key( be, db, txn, key, id );
943 DBT2bv( key, &itmp.kstr );
945 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
947 /* No entry yet, create one */
954 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
955 ic->kstr.bv_len = itmp.kstr.bv_len;
956 ic->kstr.bv_val = (char *)(ic+1);
957 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
958 ic->head = ic->tail = NULL;
961 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
964 /* load existing key count here */
965 rc = db->cursor( db, NULL, &curs, 0 );
968 data.ulen = sizeof( ID );
969 data.flags = DB_DBT_USERMEM;
971 rc = curs->c_get( curs, key, &data, DB_SET );
974 ic->count = BDB_IDL_DB_SIZE+1;
978 curs->c_count( curs, &count, 0 );
980 BDB_DISK2ID( &nid, &ic->first );
983 curs->c_close( curs );
985 /* are we a range already? */
986 if ( ic->count > BDB_IDL_DB_SIZE ) {
989 /* Are we at the limit, and converting to a range? */
990 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
992 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
995 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
996 ic->tail->next = bdb_tool_idl_free_list;
997 bdb_tool_idl_free_list = ic->head;
998 bdb->bi_idl_cache_size -= n;
999 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1001 ic->head = ic->tail = NULL;
1006 /* No free block, create that too */
1007 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1009 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1010 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1011 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1012 rc = bdb_tool_idl_flush_db( db, ic );
1015 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1017 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1019 bdb->bi_idl_cache_size++;
1020 if ( bdb_tool_idl_free_list ) {
1021 ice = bdb_tool_idl_free_list;
1022 bdb_tool_idl_free_list = ice->next;
1024 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1026 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1028 memset( ice, 0, sizeof( *ice ));
1032 ic->tail->next = ice;
1039 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1047 bdb_tool_index_task( void *ctx, void *ptr )
1049 int base = *(int *)ptr;
1053 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1054 bdb_tool_index_tcount--;
1055 if ( !bdb_tool_index_tcount )
1056 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1057 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1058 &bdb_tool_index_mutex );
1059 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1060 if ( slapd_shutdown )
1063 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1064 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );