1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 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
98 if ( !bdb_tool_info ) {
100 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
101 ldap_pvt_thread_cond_init( &bdb_tool_index_cond );
102 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
103 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
104 bdb_tool_index_tcount = slap_tool_thread_max - 1;
105 for (i=1; i<slap_tool_thread_max; i++) {
106 int *ptr = ch_malloc( sizeof( int ));
108 ldap_pvt_thread_pool_submit( &connection_pool,
109 bdb_tool_index_task, ptr );
118 int bdb_tool_entry_close(
121 if ( bdb_tool_info ) {
123 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
124 bdb_tool_index_tcount = slap_tool_thread_max - 1;
125 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond );
126 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
134 ch_free( data.data );
139 cursor->c_close( cursor );
143 #ifdef BDB_TOOL_IDL_CACHING
144 bdb_tool_idl_flush( be );
149 fprintf( stderr, "Error, entries missing!\n");
150 for (i=0; i<nholes; i++) {
151 fprintf(stderr, " entry %ld: %s\n",
152 holes[i].id, holes[i].dn.bv_val);
160 ID bdb_tool_entry_next(
165 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
167 assert( be != NULL );
168 assert( slapMode & SLAP_TOOL_MODE );
169 assert( bdb != NULL );
171 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
174 /* If we're doing linear indexing and there are more attrs to
175 * index, and we're at the end of the database, start over.
177 if ( index_nattrs && rc == DB_NOTFOUND ) {
178 /* optional - do a checkpoint here? */
179 bdb_attr_info_free( bdb->bi_attrs[0] );
180 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
182 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
191 if( data.data == NULL ) {
195 BDB_DISK2ID( key.data, &id );
199 ID bdb_tool_dn2id_get(
206 EntryInfo *ei = NULL;
209 if ( BER_BVISEMPTY(dn) )
214 op.o_tmpmemctx = NULL;
215 op.o_tmpmfuncs = &ch_mfuncs;
217 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
218 if ( ei ) bdb_cache_entryinfo_unlock( ei );
219 if ( rc == DB_NOTFOUND )
225 int bdb_tool_id2entry_get(
231 int rc = bdb_id2entry( be, NULL, 0, id, e );
233 if ( rc == DB_NOTFOUND && id == 0 ) {
234 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
235 struct berval gluebv = BER_BVC("glue");
236 dummy->e_name.bv_val = ch_strdup( "" );
237 dummy->e_nname.bv_val = ch_strdup( "" );
238 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
239 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
247 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
253 assert( be != NULL );
254 assert( slapMode & SLAP_TOOL_MODE );
255 assert( data.data != NULL );
257 DBT2bv( &data, &bv );
259 #ifdef SLAP_ZONE_ALLOC
260 /* FIXME: will add ctx later */
261 rc = entry_decode( &bv, &e, NULL );
263 rc = entry_decode( &bv, &e );
266 if( rc == LDAP_SUCCESS ) {
270 if ( slapMode & SLAP_TOOL_READONLY ) {
271 EntryInfo *ei = NULL;
277 op.o_tmpmemctx = NULL;
278 op.o_tmpmfuncs = &ch_mfuncs;
280 rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
281 if ( rc == LDAP_SUCCESS ) {
282 bdb_cache_entryinfo_unlock( ei );
294 static int bdb_tool_next_id(
301 struct berval dn = e->e_name;
302 struct berval ndn = e->e_nname;
303 struct berval pdn, npdn;
304 EntryInfo *ei = NULL, eidummy;
307 if (ndn.bv_len == 0) {
312 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
313 if ( ei ) bdb_cache_entryinfo_unlock( ei );
314 if ( rc == DB_NOTFOUND ) {
315 if ( !be_issuffix( op->o_bd, &ndn ) ) {
317 dnParent( &dn, &pdn );
318 dnParent( &ndn, &npdn );
321 rc = bdb_tool_next_id( op, tid, e, text, 1 );
327 /* If parent didn't exist, it was created just now
328 * and its ID is now in e->e_id. Make sure the current
329 * entry gets added under the new parent ID.
331 if ( eid != e->e_id ) {
332 eidummy.bei_id = e->e_id;
336 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
338 snprintf( text->bv_val, text->bv_len,
339 "next_id failed: %s (%d)",
340 db_strerror(rc), rc );
341 Debug( LDAP_DEBUG_ANY,
342 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
345 rc = bdb_dn2id_add( op, tid, ei, e );
347 snprintf( text->bv_val, text->bv_len,
348 "dn2id_add failed: %s (%d)",
349 db_strerror(rc), rc );
350 Debug( LDAP_DEBUG_ANY,
351 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
353 if ( nholes == nhmax - 1 ) {
354 if ( holes == hbuf ) {
355 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
356 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
358 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
362 ber_dupbv( &holes[nholes].dn, &ndn );
363 holes[nholes++].id = e->e_id;
365 } else if ( !hole ) {
368 e->e_id = ei->bei_id;
370 for ( i=0; i<nholes; i++) {
371 if ( holes[i].id == e->e_id ) {
373 free(holes[i].dn.bv_val);
374 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
378 } else if ( holes[i].id > e->e_id ) {
392 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
397 if ( slapMode & SLAP_TOOL_QUICK ) {
402 ir = bdb_tool_index_rec;
403 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
405 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
406 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
407 &a->a_desc->ad_tags, ir );
411 bdb_tool_ix_id = e->e_id;
413 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
414 /* Wait for all threads to be ready */
415 while ( bdb_tool_index_tcount ) {
416 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
417 ldap_pvt_thread_yield();
418 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
420 for ( i=1; i<slap_tool_thread_max; i++ )
421 bdb_tool_index_threads[i] = LDAP_BUSY;
422 bdb_tool_index_tcount = slap_tool_thread_max - 1;
423 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond );
424 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
425 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
428 for ( i=1; i<slap_tool_thread_max; i++ ) {
429 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
430 ldap_pvt_thread_yield();
434 if ( bdb_tool_index_threads[i] )
435 return bdb_tool_index_threads[i];
439 return bdb_index_entry_add( op, txn, e );
443 ID bdb_tool_entry_put(
446 struct berval *text )
449 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
454 assert( be != NULL );
455 assert( slapMode & SLAP_TOOL_MODE );
457 assert( text != NULL );
458 assert( text->bv_val != NULL );
459 assert( text->bv_val[0] == '\0' ); /* overconservative? */
461 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
462 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
464 if (! (slapMode & SLAP_TOOL_QUICK)) {
465 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
466 bdb->bi_db_opflags );
468 snprintf( text->bv_val, text->bv_len,
469 "txn_begin failed: %s (%d)",
470 db_strerror(rc), rc );
471 Debug( LDAP_DEBUG_ANY,
472 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
473 text->bv_val, 0, 0 );
480 op.o_tmpmemctx = NULL;
481 op.o_tmpmfuncs = &ch_mfuncs;
483 /* add dn2id indices */
484 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
489 if ( !bdb->bi_linear_index )
490 rc = bdb_tool_index_add( &op, tid, e );
492 snprintf( text->bv_val, text->bv_len,
493 "index_entry_add failed: %s (%d)",
494 db_strerror(rc), rc );
495 Debug( LDAP_DEBUG_ANY,
496 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
497 text->bv_val, 0, 0 );
502 rc = bdb_id2entry_add( be, tid, e );
504 snprintf( text->bv_val, text->bv_len,
505 "id2entry_add failed: %s (%d)",
506 db_strerror(rc), rc );
507 Debug( LDAP_DEBUG_ANY,
508 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
509 text->bv_val, 0, 0 );
515 if ( !( slapMode & SLAP_TOOL_QUICK )) {
516 rc = TXN_COMMIT( tid, 0 );
518 snprintf( text->bv_val, text->bv_len,
519 "txn_commit failed: %s (%d)",
520 db_strerror(rc), rc );
521 Debug( LDAP_DEBUG_ANY,
522 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
523 text->bv_val, 0, 0 );
529 if ( !( slapMode & SLAP_TOOL_QUICK )) {
531 snprintf( text->bv_val, text->bv_len,
532 "txn_aborted! %s (%d)",
533 db_strerror(rc), rc );
534 Debug( LDAP_DEBUG_ANY,
535 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
536 text->bv_val, 0, 0 );
544 int bdb_tool_entry_reindex(
548 struct bdb_info *bi = (struct bdb_info *) be->be_private;
555 Debug( LDAP_DEBUG_ARGS,
556 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
559 /* No indexes configured, nothing to do. Could return an
560 * error here to shortcut things.
566 /* Get the first attribute to index */
567 if (bi->bi_linear_index && !index_nattrs) {
568 index_nattrs = bi->bi_nattrs - 1;
572 e = bdb_tool_entry_get( be, id );
575 Debug( LDAP_DEBUG_ANY,
576 LDAP_XSTRING(bdb_tool_entry_reindex)
577 ": could not locate id=%ld\n",
582 if (! (slapMode & SLAP_TOOL_QUICK)) {
583 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
585 Debug( LDAP_DEBUG_ANY,
586 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
587 "txn_begin failed: %s (%d)\n",
588 db_strerror(rc), rc, 0 );
594 * just (re)add them for now
595 * assume that some other routine (not yet implemented)
596 * will zap index databases
600 Debug( LDAP_DEBUG_TRACE,
601 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
602 (long) id, e->e_dn, 0 );
606 op.o_tmpmemctx = NULL;
607 op.o_tmpmfuncs = &ch_mfuncs;
609 rc = bdb_tool_index_add( &op, tid, e );
613 if (! (slapMode & SLAP_TOOL_QUICK)) {
614 rc = TXN_COMMIT( tid, 0 );
616 Debug( LDAP_DEBUG_ANY,
617 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
618 ": txn_commit failed: %s (%d)\n",
619 db_strerror(rc), rc, 0 );
625 if (! (slapMode & SLAP_TOOL_QUICK)) {
627 Debug( LDAP_DEBUG_ANY,
628 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
629 ": txn_aborted! %s (%d)\n",
630 db_strerror(rc), rc, 0 );
634 bdb_entry_release( &op, e, 0 );
639 ID bdb_tool_entry_modify(
642 struct berval *text )
645 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
650 assert( be != NULL );
651 assert( slapMode & SLAP_TOOL_MODE );
653 assert( text != NULL );
654 assert( text->bv_val != NULL );
655 assert( text->bv_val[0] == '\0' ); /* overconservative? */
657 assert ( e->e_id != NOID );
659 Debug( LDAP_DEBUG_TRACE,
660 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
661 (long) e->e_id, e->e_dn, 0 );
663 if (! (slapMode & SLAP_TOOL_QUICK)) {
664 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
665 bdb->bi_db_opflags );
667 snprintf( text->bv_val, text->bv_len,
668 "txn_begin failed: %s (%d)",
669 db_strerror(rc), rc );
670 Debug( LDAP_DEBUG_ANY,
671 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
672 text->bv_val, 0, 0 );
679 op.o_tmpmemctx = NULL;
680 op.o_tmpmfuncs = &ch_mfuncs;
683 rc = bdb_id2entry_update( be, tid, e );
685 snprintf( text->bv_val, text->bv_len,
686 "id2entry_add failed: %s (%d)",
687 db_strerror(rc), rc );
688 Debug( LDAP_DEBUG_ANY,
689 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
690 text->bv_val, 0, 0 );
696 if (! (slapMode & SLAP_TOOL_QUICK)) {
697 rc = TXN_COMMIT( tid, 0 );
699 snprintf( text->bv_val, text->bv_len,
700 "txn_commit failed: %s (%d)",
701 db_strerror(rc), rc );
702 Debug( LDAP_DEBUG_ANY,
703 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
704 "%s\n", text->bv_val, 0, 0 );
710 if (! (slapMode & SLAP_TOOL_QUICK)) {
712 snprintf( text->bv_val, text->bv_len,
713 "txn_aborted! %s (%d)",
714 db_strerror(rc), rc );
715 Debug( LDAP_DEBUG_ANY,
716 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
717 text->bv_val, 0, 0 );
725 #ifdef BDB_TOOL_IDL_CACHING
727 bdb_tool_idl_cmp( const void *v1, const void *v2 )
729 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
732 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
733 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
737 bdb_tool_idl_flush_one( void *v1, void *arg )
739 bdb_tool_idl_cache *ic = v1;
741 struct bdb_info *bdb = bdb_tool_info;
742 bdb_tool_idl_cache_entry *ice;
748 /* Freshly allocated, ignore it */
749 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
753 rc = db->cursor( db, NULL, &curs, 0 );
760 bv2DBT( &ic->kstr, &key );
762 data.size = data.ulen = sizeof( ID );
763 data.flags = DB_DBT_USERMEM;
766 rc = curs->c_get( curs, &key, &data, DB_SET );
767 /* If key already exists and we're writing a range... */
768 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
769 /* If it's not currently a range, must delete old info */
772 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
773 curs->c_del( curs, 0 );
776 /* Store range marker */
777 curs->c_put( curs, &key, &data, DB_KEYFIRST );
781 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
784 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
787 curs->c_del( curs, 0 );
789 BDB_ID2DISK( ic->last, &nid );
790 curs->c_put( curs, &key, &data, DB_KEYLAST );
792 } else if ( rc && rc != DB_NOTFOUND ) {
794 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
795 /* range, didn't exist before */
797 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
799 BDB_ID2DISK( ic->first, &nid );
800 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
802 BDB_ID2DISK( ic->last, &nid );
803 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
812 /* Just a normal write */
814 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
819 end = ic->count & (IDBLOCK-1);
823 for ( i=0; i<end; i++ ) {
824 if ( !ice->ids[i] ) continue;
825 BDB_ID2DISK( ice->ids[i], &nid );
826 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
828 if ( rc == DB_KEYEXIST ) {
842 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
843 ic->tail->next = bdb_tool_idl_free_list;
844 bdb_tool_idl_free_list = ic->head;
845 bdb->bi_idl_cache_size -= n;
846 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
849 if ( ic != db->app_private ) {
852 ic->head = ic->tail = NULL;
854 curs->c_close( curs );
859 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
861 Avlnode *root = db->app_private;
864 db->app_private = ic;
865 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
866 avl_free( root, NULL );
867 db->app_private = NULL;
874 bdb_tool_idl_flush( BackendDB *be )
876 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
881 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
882 db = bdb->bi_databases[i]->bdi_db;
883 if ( !db->app_private ) continue;
884 rc = bdb_tool_idl_flush_db( db, NULL );
889 bdb->bi_idl_cache_size = 0;
894 int bdb_tool_idl_add(
901 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
902 bdb_tool_idl_cache *ic, itmp;
903 bdb_tool_idl_cache_entry *ice;
906 if ( !bdb->bi_idl_cache_max_size )
907 return bdb_idl_insert_key( be, db, txn, key, id );
909 DBT2bv( key, &itmp.kstr );
911 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
913 /* No entry yet, create one */
920 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
921 ic->kstr.bv_len = itmp.kstr.bv_len;
922 ic->kstr.bv_val = (char *)(ic+1);
923 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
924 ic->head = ic->tail = NULL;
927 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
930 /* load existing key count here */
931 rc = db->cursor( db, NULL, &curs, 0 );
934 data.ulen = sizeof( ID );
935 data.flags = DB_DBT_USERMEM;
937 rc = curs->c_get( curs, key, &data, DB_SET );
940 ic->count = BDB_IDL_DB_SIZE+1;
944 curs->c_count( curs, &count, 0 );
946 BDB_DISK2ID( &nid, &ic->first );
949 curs->c_close( curs );
951 /* are we a range already? */
952 if ( ic->count > BDB_IDL_DB_SIZE ) {
955 /* Are we at the limit, and converting to a range? */
956 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
958 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
961 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
962 ic->tail->next = bdb_tool_idl_free_list;
963 bdb_tool_idl_free_list = ic->head;
964 bdb->bi_idl_cache_size -= n;
965 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
967 ic->head = ic->tail = NULL;
972 /* No free block, create that too */
973 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
975 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
976 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
977 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
978 rc = bdb_tool_idl_flush_db( db, ic );
981 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
983 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
985 bdb->bi_idl_cache_size++;
986 if ( bdb_tool_idl_free_list ) {
987 ice = bdb_tool_idl_free_list;
988 bdb_tool_idl_free_list = ice->next;
990 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
992 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
994 memset( ice, 0, sizeof( *ice ));
998 ic->tail->next = ice;
1005 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1013 bdb_tool_index_task( void *ctx, void *ptr )
1015 int base = *(int *)ptr;
1019 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1020 bdb_tool_index_tcount--;
1021 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond,
1022 &bdb_tool_index_mutex );
1023 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1024 if ( slapd_shutdown )
1027 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1028 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );