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>
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;
394 if ( slapMode & SLAP_TOOL_QUICK ) {
399 ir = bdb_tool_index_rec;
400 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
402 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
403 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
404 &a->a_desc->ad_tags, ir );
408 bdb_tool_ix_id = e->e_id;
410 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
411 /* Wait for all threads to be ready */
412 while ( bdb_tool_index_tcount ) {
413 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
414 ldap_pvt_thread_yield();
415 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
417 for ( i=1; i<slap_tool_thread_max; i++ )
418 bdb_tool_index_threads[i] = LDAP_BUSY;
419 bdb_tool_index_tcount = slap_tool_thread_max - 1;
420 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond );
421 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
422 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
425 for ( i=1; i<slap_tool_thread_max; i++ ) {
426 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
427 ldap_pvt_thread_yield();
431 if ( bdb_tool_index_threads[i] )
432 return bdb_tool_index_threads[i];
436 return bdb_index_entry_add( op, txn, e );
440 ID bdb_tool_entry_put(
443 struct berval *text )
446 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
451 assert( be != NULL );
452 assert( slapMode & SLAP_TOOL_MODE );
454 assert( text != NULL );
455 assert( text->bv_val != NULL );
456 assert( text->bv_val[0] == '\0' ); /* overconservative? */
458 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
459 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
461 if (! (slapMode & SLAP_TOOL_QUICK)) {
462 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
463 bdb->bi_db_opflags );
465 snprintf( text->bv_val, text->bv_len,
466 "txn_begin failed: %s (%d)",
467 db_strerror(rc), rc );
468 Debug( LDAP_DEBUG_ANY,
469 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
470 text->bv_val, 0, 0 );
477 op.o_tmpmemctx = NULL;
478 op.o_tmpmfuncs = &ch_mfuncs;
480 /* add dn2id indices */
481 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
486 if ( !bdb->bi_linear_index )
487 rc = bdb_tool_index_add( &op, tid, e );
489 snprintf( text->bv_val, text->bv_len,
490 "index_entry_add failed: %s (%d)",
491 db_strerror(rc), rc );
492 Debug( LDAP_DEBUG_ANY,
493 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
494 text->bv_val, 0, 0 );
499 rc = bdb_id2entry_add( be, tid, e );
501 snprintf( text->bv_val, text->bv_len,
502 "id2entry_add failed: %s (%d)",
503 db_strerror(rc), rc );
504 Debug( LDAP_DEBUG_ANY,
505 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
506 text->bv_val, 0, 0 );
512 if ( !( slapMode & SLAP_TOOL_QUICK )) {
513 rc = TXN_COMMIT( tid, 0 );
515 snprintf( text->bv_val, text->bv_len,
516 "txn_commit failed: %s (%d)",
517 db_strerror(rc), rc );
518 Debug( LDAP_DEBUG_ANY,
519 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
520 text->bv_val, 0, 0 );
526 if ( !( slapMode & SLAP_TOOL_QUICK )) {
528 snprintf( text->bv_val, text->bv_len,
529 "txn_aborted! %s (%d)",
530 db_strerror(rc), rc );
531 Debug( LDAP_DEBUG_ANY,
532 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
533 text->bv_val, 0, 0 );
541 int bdb_tool_entry_reindex(
545 struct bdb_info *bi = (struct bdb_info *) be->be_private;
552 Debug( LDAP_DEBUG_ARGS,
553 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
556 /* No indexes configured, nothing to do. Could return an
557 * error here to shortcut things.
563 /* Get the first attribute to index */
564 if (bi->bi_linear_index && !index_nattrs) {
565 index_nattrs = bi->bi_nattrs - 1;
569 e = bdb_tool_entry_get( be, id );
572 Debug( LDAP_DEBUG_ANY,
573 LDAP_XSTRING(bdb_tool_entry_reindex)
574 ": could not locate id=%ld\n",
579 if (! (slapMode & SLAP_TOOL_QUICK)) {
580 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
582 Debug( LDAP_DEBUG_ANY,
583 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
584 "txn_begin failed: %s (%d)\n",
585 db_strerror(rc), rc, 0 );
591 * just (re)add them for now
592 * assume that some other routine (not yet implemented)
593 * will zap index databases
597 Debug( LDAP_DEBUG_TRACE,
598 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
599 (long) id, e->e_dn, 0 );
603 op.o_tmpmemctx = NULL;
604 op.o_tmpmfuncs = &ch_mfuncs;
606 rc = bdb_tool_index_add( &op, tid, e );
610 if (! (slapMode & SLAP_TOOL_QUICK)) {
611 rc = TXN_COMMIT( tid, 0 );
613 Debug( LDAP_DEBUG_ANY,
614 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
615 ": txn_commit failed: %s (%d)\n",
616 db_strerror(rc), rc, 0 );
622 if (! (slapMode & SLAP_TOOL_QUICK)) {
624 Debug( LDAP_DEBUG_ANY,
625 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
626 ": txn_aborted! %s (%d)\n",
627 db_strerror(rc), rc, 0 );
631 bdb_entry_release( &op, e, 0 );
636 ID bdb_tool_entry_modify(
639 struct berval *text )
642 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
647 assert( be != NULL );
648 assert( slapMode & SLAP_TOOL_MODE );
650 assert( text != NULL );
651 assert( text->bv_val != NULL );
652 assert( text->bv_val[0] == '\0' ); /* overconservative? */
654 assert ( e->e_id != NOID );
656 Debug( LDAP_DEBUG_TRACE,
657 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
658 (long) e->e_id, e->e_dn, 0 );
660 if (! (slapMode & SLAP_TOOL_QUICK)) {
661 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
662 bdb->bi_db_opflags );
664 snprintf( text->bv_val, text->bv_len,
665 "txn_begin failed: %s (%d)",
666 db_strerror(rc), rc );
667 Debug( LDAP_DEBUG_ANY,
668 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
669 text->bv_val, 0, 0 );
676 op.o_tmpmemctx = NULL;
677 op.o_tmpmfuncs = &ch_mfuncs;
680 rc = bdb_id2entry_update( be, tid, e );
682 snprintf( text->bv_val, text->bv_len,
683 "id2entry_add failed: %s (%d)",
684 db_strerror(rc), rc );
685 Debug( LDAP_DEBUG_ANY,
686 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
687 text->bv_val, 0, 0 );
693 if (! (slapMode & SLAP_TOOL_QUICK)) {
694 rc = TXN_COMMIT( tid, 0 );
696 snprintf( text->bv_val, text->bv_len,
697 "txn_commit failed: %s (%d)",
698 db_strerror(rc), rc );
699 Debug( LDAP_DEBUG_ANY,
700 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
701 "%s\n", text->bv_val, 0, 0 );
707 if (! (slapMode & SLAP_TOOL_QUICK)) {
709 snprintf( text->bv_val, text->bv_len,
710 "txn_aborted! %s (%d)",
711 db_strerror(rc), rc );
712 Debug( LDAP_DEBUG_ANY,
713 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
714 text->bv_val, 0, 0 );
722 #ifdef BDB_TOOL_IDL_CACHING
724 bdb_tool_idl_cmp( const void *v1, const void *v2 )
726 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
729 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
730 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
734 bdb_tool_idl_flush_one( void *v1, void *arg )
736 bdb_tool_idl_cache *ic = v1;
738 struct bdb_info *bdb = bdb_tool_info;
739 bdb_tool_idl_cache_entry *ice;
745 /* Freshly allocated, ignore it */
746 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
750 rc = db->cursor( db, NULL, &curs, 0 );
757 bv2DBT( &ic->kstr, &key );
759 data.size = data.ulen = sizeof( ID );
760 data.flags = DB_DBT_USERMEM;
763 rc = curs->c_get( curs, &key, &data, DB_SET );
764 /* If key already exists and we're writing a range... */
765 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
766 /* If it's not currently a range, must delete old info */
769 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
770 curs->c_del( curs, 0 );
773 /* Store range marker */
774 curs->c_put( curs, &key, &data, DB_KEYFIRST );
778 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
781 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
784 curs->c_del( curs, 0 );
786 BDB_ID2DISK( ic->last, &nid );
787 curs->c_put( curs, &key, &data, DB_KEYLAST );
789 } else if ( rc && rc != DB_NOTFOUND ) {
791 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
792 /* range, didn't exist before */
794 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
796 BDB_ID2DISK( ic->first, &nid );
797 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
799 BDB_ID2DISK( ic->last, &nid );
800 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
809 /* Just a normal write */
811 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
816 end = ic->count & (IDBLOCK-1);
820 for ( i=0; i<end; i++ ) {
821 if ( !ice->ids[i] ) continue;
822 BDB_ID2DISK( ice->ids[i], &nid );
823 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
825 if ( rc == DB_KEYEXIST ) {
839 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
840 ic->tail->next = bdb_tool_idl_free_list;
841 bdb_tool_idl_free_list = ic->head;
842 bdb->bi_idl_cache_size -= n;
843 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
846 if ( ic != db->app_private ) {
849 ic->head = ic->tail = NULL;
851 curs->c_close( curs );
856 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
858 Avlnode *root = db->app_private;
861 db->app_private = ic;
862 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
863 avl_free( root, NULL );
864 db->app_private = NULL;
871 bdb_tool_idl_flush( BackendDB *be )
873 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
878 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
879 db = bdb->bi_databases[i]->bdi_db;
880 if ( !db->app_private ) continue;
881 rc = bdb_tool_idl_flush_db( db, NULL );
886 bdb->bi_idl_cache_size = 0;
891 int bdb_tool_idl_add(
898 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
899 bdb_tool_idl_cache *ic, itmp;
900 bdb_tool_idl_cache_entry *ice;
903 if ( !bdb->bi_idl_cache_max_size )
904 return bdb_idl_insert_key( be, db, txn, key, id );
906 DBT2bv( key, &itmp.kstr );
908 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
910 /* No entry yet, create one */
917 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
918 ic->kstr.bv_len = itmp.kstr.bv_len;
919 ic->kstr.bv_val = (char *)(ic+1);
920 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
921 ic->head = ic->tail = NULL;
924 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
927 /* load existing key count here */
928 rc = db->cursor( db, NULL, &curs, 0 );
931 data.ulen = sizeof( ID );
932 data.flags = DB_DBT_USERMEM;
934 rc = curs->c_get( curs, key, &data, DB_SET );
937 ic->count = BDB_IDL_DB_SIZE+1;
941 curs->c_count( curs, &count, 0 );
943 BDB_DISK2ID( &nid, &ic->first );
946 curs->c_close( curs );
948 /* are we a range already? */
949 if ( ic->count > BDB_IDL_DB_SIZE ) {
952 /* Are we at the limit, and converting to a range? */
953 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
955 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
958 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
959 ic->tail->next = bdb_tool_idl_free_list;
960 bdb_tool_idl_free_list = ic->head;
961 bdb->bi_idl_cache_size -= n;
962 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
964 ic->head = ic->tail = NULL;
969 /* No free block, create that too */
970 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
972 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
973 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
974 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
975 rc = bdb_tool_idl_flush_db( db, ic );
978 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
980 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
982 bdb->bi_idl_cache_size++;
983 if ( bdb_tool_idl_free_list ) {
984 ice = bdb_tool_idl_free_list;
985 bdb_tool_idl_free_list = ice->next;
987 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
989 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
991 memset( ice, 0, sizeof( *ice ));
995 ic->tail->next = ice;
1002 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1010 bdb_tool_index_task( void *ctx, void *ptr )
1012 int base = *(int *)ptr;
1016 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1017 bdb_tool_index_tcount--;
1018 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond,
1019 &bdb_tool_index_mutex );
1020 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1021 if ( slapd_shutdown )
1024 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1025 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );