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 #define bdb_tool_idl_cmp BDB_SYMBOL(tool_idl_cmp)
42 #define bdb_tool_idl_flush_one BDB_SYMBOL(tool_idl_flush_one)
43 #define bdb_tool_idl_flush BDB_SYMBOL(tool_idl_flush)
45 static int bdb_tool_idl_flush( BackendDB *be );
46 static int bdb_tool_ix_rec( int base );
47 static void * bdb_tool_index_task( void *ctx, void *ptr );
51 typedef struct bdb_tool_idl_cache_entry {
52 struct bdb_tool_idl_cache_entry *next;
54 } bdb_tool_idl_cache_entry;
56 typedef struct bdb_tool_idl_cache {
58 bdb_tool_idl_cache_entry *head, *tail;
63 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
65 static ID bdb_tool_ix_id;
66 static Operation *bdb_tool_ix_op;
67 static volatile int *bdb_tool_index_threads;
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 int bdb_tool_entry_open(
74 BackendDB *be, int mode )
76 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
78 /* initialize key and data thangs */
81 key.flags = DB_DBT_REALLOC;
82 data.flags = DB_DBT_REALLOC;
85 int rc = bdb->bi_id2entry->bdi_db->cursor(
86 bdb->bi_id2entry->bdi_db, NULL, &cursor,
93 /* Set up for slapindex */
94 if ( !(slapMode & SLAP_TOOL_READONLY )) {
96 if ( !bdb_tool_info && ( slapMode & SLAP_TOOL_QUICK )) {
97 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
98 ldap_pvt_thread_cond_init( &bdb_tool_index_cond );
99 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
100 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
101 for (i=1; i<slap_tool_thread_max; i++) {
102 int *ptr = ch_malloc( sizeof( int ));
104 ldap_pvt_thread_pool_submit( &connection_pool,
105 bdb_tool_index_task, ptr );
106 ldap_pvt_thread_yield();
115 int bdb_tool_entry_close(
118 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
120 if ( bdb_tool_info ) {
122 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
123 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond );
124 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
132 ch_free( data.data );
137 cursor->c_close( cursor );
141 bdb_tool_idl_flush( be );
145 fprintf( stderr, "Error, entries missing!\n");
146 for (i=0; i<nholes; i++) {
147 fprintf(stderr, " entry %ld: %s\n",
148 holes[i].id, holes[i].dn.bv_val);
156 ID bdb_tool_entry_next(
161 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
163 assert( be != NULL );
164 assert( slapMode & SLAP_TOOL_MODE );
165 assert( bdb != NULL );
167 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
170 /* If we're doing linear indexing and there are more attrs to
171 * index, and we're at the end of the database, start over.
173 if ( index_nattrs && rc == DB_NOTFOUND ) {
174 /* optional - do a checkpoint here? */
175 bdb_attr_info_free( bdb->bi_attrs[0] );
176 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
178 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
187 if( data.data == NULL ) {
191 BDB_DISK2ID( key.data, &id );
195 ID bdb_tool_dn2id_get(
202 EntryInfo *ei = NULL;
205 if ( BER_BVISEMPTY(dn) )
210 op.o_tmpmemctx = NULL;
211 op.o_tmpmfuncs = &ch_mfuncs;
213 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
214 if ( ei ) bdb_cache_entryinfo_unlock( ei );
215 if ( rc == DB_NOTFOUND )
221 int bdb_tool_id2entry_get(
227 int rc = bdb_id2entry( be, NULL, 0, id, e );
229 if ( rc == DB_NOTFOUND && id == 0 ) {
230 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
231 struct berval gluebv = BER_BVC("glue");
232 dummy->e_name.bv_val = ch_strdup( "" );
233 dummy->e_nname.bv_val = ch_strdup( "" );
234 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
235 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
243 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
249 assert( be != NULL );
250 assert( slapMode & SLAP_TOOL_MODE );
251 assert( data.data != NULL );
253 DBT2bv( &data, &bv );
255 #ifdef SLAP_ZONE_ALLOC
256 /* FIXME: will add ctx later */
257 rc = entry_decode( &bv, &e, NULL );
259 rc = entry_decode( &bv, &e );
262 if( rc == LDAP_SUCCESS ) {
266 if ( slapMode & SLAP_TOOL_READONLY ) {
267 EntryInfo *ei = NULL;
273 op.o_tmpmemctx = NULL;
274 op.o_tmpmfuncs = &ch_mfuncs;
276 rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
277 if ( rc == LDAP_SUCCESS ) {
278 bdb_cache_entryinfo_unlock( ei );
290 static int bdb_tool_next_id(
297 struct berval dn = e->e_name;
298 struct berval ndn = e->e_nname;
299 struct berval pdn, npdn;
300 EntryInfo *ei = NULL, eidummy;
303 if (ndn.bv_len == 0) {
308 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
309 if ( ei ) bdb_cache_entryinfo_unlock( ei );
310 if ( rc == DB_NOTFOUND ) {
311 if ( !be_issuffix( op->o_bd, &ndn ) ) {
313 dnParent( &dn, &pdn );
314 dnParent( &ndn, &npdn );
317 rc = bdb_tool_next_id( op, tid, e, text, 1 );
323 /* If parent didn't exist, it was created just now
324 * and its ID is now in e->e_id. Make sure the current
325 * entry gets added under the new parent ID.
327 if ( eid != e->e_id ) {
328 eidummy.bei_id = e->e_id;
332 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
334 snprintf( text->bv_val, text->bv_len,
335 "next_id failed: %s (%d)",
336 db_strerror(rc), rc );
337 Debug( LDAP_DEBUG_ANY,
338 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
341 rc = bdb_dn2id_add( op, tid, ei, e );
343 snprintf( text->bv_val, text->bv_len,
344 "dn2id_add failed: %s (%d)",
345 db_strerror(rc), rc );
346 Debug( LDAP_DEBUG_ANY,
347 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
349 if ( nholes == nhmax - 1 ) {
350 if ( holes == hbuf ) {
351 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
352 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
354 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
358 ber_dupbv( &holes[nholes].dn, &ndn );
359 holes[nholes++].id = e->e_id;
361 } else if ( !hole ) {
364 e->e_id = ei->bei_id;
366 for ( i=0; i<nholes; i++) {
367 if ( holes[i].id == e->e_id ) {
369 free(holes[i].dn.bv_val);
370 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
374 } else if ( holes[i].id > e->e_id ) {
388 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
390 if ( slapMode & SLAP_TOOL_QUICK ) {
395 ir = bdb_tool_index_rec;
396 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
398 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
399 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
400 &a->a_desc->ad_tags, ir );
404 bdb_tool_ix_id = e->e_id;
406 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
407 for ( i=1; i<slap_tool_thread_max; i++ )
408 bdb_tool_index_threads[i] = LDAP_BUSY;
409 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond );
410 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
411 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
414 for ( i=1; i<slap_tool_thread_max; i++ ) {
415 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
416 ldap_pvt_thread_yield();
420 if ( bdb_tool_index_threads[i] )
421 return bdb_tool_index_threads[i];
425 return bdb_index_entry_add( op, txn, e );
429 ID bdb_tool_entry_put(
432 struct berval *text )
435 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
440 assert( be != NULL );
441 assert( slapMode & SLAP_TOOL_MODE );
443 assert( text != NULL );
444 assert( text->bv_val != NULL );
445 assert( text->bv_val[0] == '\0' ); /* overconservative? */
447 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
448 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
450 if (! (slapMode & SLAP_TOOL_QUICK)) {
451 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
452 bdb->bi_db_opflags );
454 snprintf( text->bv_val, text->bv_len,
455 "txn_begin failed: %s (%d)",
456 db_strerror(rc), rc );
457 Debug( LDAP_DEBUG_ANY,
458 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
459 text->bv_val, 0, 0 );
466 op.o_tmpmemctx = NULL;
467 op.o_tmpmfuncs = &ch_mfuncs;
469 /* add dn2id indices */
470 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
476 rc = bdb_id2entry_add( be, tid, e );
478 snprintf( text->bv_val, text->bv_len,
479 "id2entry_add failed: %s (%d)",
480 db_strerror(rc), rc );
481 Debug( LDAP_DEBUG_ANY,
482 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
483 text->bv_val, 0, 0 );
487 if ( !bdb->bi_linear_index )
488 rc = bdb_tool_index_add( &op, tid, e );
490 snprintf( text->bv_val, text->bv_len,
491 "index_entry_add failed: %s (%d)",
492 db_strerror(rc), rc );
493 Debug( LDAP_DEBUG_ANY,
494 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
495 text->bv_val, 0, 0 );
501 if ( !( slapMode & SLAP_TOOL_QUICK )) {
502 rc = TXN_COMMIT( tid, 0 );
504 snprintf( text->bv_val, text->bv_len,
505 "txn_commit 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 )) {
517 snprintf( text->bv_val, text->bv_len,
518 "txn_aborted! %s (%d)",
519 db_strerror(rc), rc );
520 Debug( LDAP_DEBUG_ANY,
521 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
522 text->bv_val, 0, 0 );
530 int bdb_tool_entry_reindex(
534 struct bdb_info *bi = (struct bdb_info *) be->be_private;
541 Debug( LDAP_DEBUG_ARGS,
542 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
545 /* No indexes configured, nothing to do. Could return an
546 * error here to shortcut things.
552 /* Get the first attribute to index */
553 if (bi->bi_linear_index && !index_nattrs) {
554 index_nattrs = bi->bi_nattrs - 1;
558 e = bdb_tool_entry_get( be, id );
561 Debug( LDAP_DEBUG_ANY,
562 LDAP_XSTRING(bdb_tool_entry_reindex)
563 ": could not locate id=%ld\n",
568 if (! (slapMode & SLAP_TOOL_QUICK)) {
569 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
571 Debug( LDAP_DEBUG_ANY,
572 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
573 "txn_begin failed: %s (%d)\n",
574 db_strerror(rc), rc, 0 );
580 * just (re)add them for now
581 * assume that some other routine (not yet implemented)
582 * will zap index databases
586 Debug( LDAP_DEBUG_TRACE,
587 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
588 (long) id, e->e_dn, 0 );
592 op.o_tmpmemctx = NULL;
593 op.o_tmpmfuncs = &ch_mfuncs;
595 rc = bdb_tool_index_add( &op, tid, e );
599 if (! (slapMode & SLAP_TOOL_QUICK)) {
600 rc = TXN_COMMIT( tid, 0 );
602 Debug( LDAP_DEBUG_ANY,
603 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
604 ": txn_commit failed: %s (%d)\n",
605 db_strerror(rc), rc, 0 );
611 if (! (slapMode & SLAP_TOOL_QUICK)) {
613 Debug( LDAP_DEBUG_ANY,
614 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
615 ": txn_aborted! %s (%d)\n",
616 db_strerror(rc), rc, 0 );
620 bdb_entry_release( &op, e, 0 );
625 ID bdb_tool_entry_modify(
628 struct berval *text )
631 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
636 assert( be != NULL );
637 assert( slapMode & SLAP_TOOL_MODE );
639 assert( text != NULL );
640 assert( text->bv_val != NULL );
641 assert( text->bv_val[0] == '\0' ); /* overconservative? */
643 assert ( e->e_id != NOID );
645 Debug( LDAP_DEBUG_TRACE,
646 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
647 (long) e->e_id, e->e_dn, 0 );
649 if (! (slapMode & SLAP_TOOL_QUICK)) {
650 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
651 bdb->bi_db_opflags );
653 snprintf( text->bv_val, text->bv_len,
654 "txn_begin failed: %s (%d)",
655 db_strerror(rc), rc );
656 Debug( LDAP_DEBUG_ANY,
657 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
658 text->bv_val, 0, 0 );
665 op.o_tmpmemctx = NULL;
666 op.o_tmpmfuncs = &ch_mfuncs;
669 rc = bdb_id2entry_update( be, tid, e );
671 snprintf( text->bv_val, text->bv_len,
672 "id2entry_add failed: %s (%d)",
673 db_strerror(rc), rc );
674 Debug( LDAP_DEBUG_ANY,
675 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
676 text->bv_val, 0, 0 );
681 /* FIXME: this is bogus, we don't have the old values to delete
682 * from the index because the given entry has already been modified.
684 rc = bdb_index_entry_del( &op, tid, e );
686 snprintf( text->bv_val, text->bv_len,
687 "index_entry_del failed: %s (%d)",
688 db_strerror(rc), rc );
689 Debug( LDAP_DEBUG_ANY,
690 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
691 text->bv_val, 0, 0 );
696 rc = bdb_index_entry_add( &op, tid, e );
698 snprintf( text->bv_val, text->bv_len,
699 "index_entry_add failed: %s (%d)",
700 db_strerror(rc), rc );
701 Debug( LDAP_DEBUG_ANY,
702 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
703 text->bv_val, 0, 0 );
709 if (! (slapMode & SLAP_TOOL_QUICK)) {
710 rc = TXN_COMMIT( tid, 0 );
712 snprintf( text->bv_val, text->bv_len,
713 "txn_commit failed: %s (%d)",
714 db_strerror(rc), rc );
715 Debug( LDAP_DEBUG_ANY,
716 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
717 "%s\n", text->bv_val, 0, 0 );
723 if (! (slapMode & SLAP_TOOL_QUICK)) {
725 snprintf( text->bv_val, text->bv_len,
726 "txn_aborted! %s (%d)",
727 db_strerror(rc), rc );
728 Debug( LDAP_DEBUG_ANY,
729 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
730 text->bv_val, 0, 0 );
740 bdb_tool_idl_cmp( const void *v1, const void *v2 )
742 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
745 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
746 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
750 bdb_tool_idl_flush_one( void *v1, void *arg )
752 bdb_tool_idl_cache *ic = v1;
754 struct bdb_info *bdb = bdb_tool_info;
755 bdb_tool_idl_cache_entry *ice;
761 /* Freshly allocated, ignore it */
762 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
766 rc = db->cursor( db, NULL, &curs, 0 );
773 bv2DBT( &ic->kstr, &key );
775 data.size = data.ulen = sizeof( ID );
776 data.flags = DB_DBT_USERMEM;
779 rc = curs->c_get( curs, &key, &data, DB_SET );
780 /* If key already exists and we're writing a range... */
781 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
782 /* If it's not currently a range, must delete old info */
785 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
786 curs->c_del( curs, 0 );
789 /* Store range marker */
790 curs->c_put( curs, &key, &data, DB_KEYFIRST );
794 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
797 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
800 curs->c_del( curs, 0 );
802 BDB_ID2DISK( ic->last, &nid );
803 curs->c_put( curs, &key, &data, DB_KEYLAST );
805 } else if ( rc && rc != DB_NOTFOUND ) {
807 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
808 /* range, didn't exist before */
810 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
812 BDB_ID2DISK( ic->first, &nid );
813 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
815 BDB_ID2DISK( ic->last, &nid );
816 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
825 /* Just a normal write */
827 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
832 end = ic->count & (IDBLOCK-1);
836 for ( i=0; i<end; i++ ) {
837 if ( !ice->ids[i] ) continue;
838 BDB_ID2DISK( ice->ids[i], &nid );
839 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
841 if ( rc == DB_KEYEXIST ) {
855 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
856 ic->tail->next = bdb_tool_idl_free_list;
857 bdb_tool_idl_free_list = ic->head;
858 bdb->bi_idl_cache_size -= n;
859 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
862 if ( ic != db->app_private ) {
865 ic->head = ic->tail = NULL;
867 curs->c_close( curs );
872 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
874 Avlnode *root = db->app_private;
877 db->app_private = ic;
878 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
879 avl_free( root, NULL );
880 db->app_private = NULL;
887 bdb_tool_idl_flush( BackendDB *be )
889 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
894 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
895 db = bdb->bi_databases[i]->bdi_db;
896 if ( !db->app_private ) continue;
897 rc = bdb_tool_idl_flush_db( db, NULL );
902 bdb->bi_idl_cache_size = 0;
907 int bdb_tool_idl_add(
914 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
915 bdb_tool_idl_cache *ic, itmp;
916 bdb_tool_idl_cache_entry *ice;
919 if ( !bdb->bi_idl_cache_max_size )
920 return bdb_idl_insert_key( be, db, txn, key, id );
922 DBT2bv( key, &itmp.kstr );
924 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
926 /* No entry yet, create one */
933 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
934 ic->kstr.bv_len = itmp.kstr.bv_len;
935 ic->kstr.bv_val = (char *)(ic+1);
936 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
937 ic->head = ic->tail = NULL;
940 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
943 /* load existing key count here */
944 rc = db->cursor( db, NULL, &curs, 0 );
947 data.ulen = sizeof( ID );
948 data.flags = DB_DBT_USERMEM;
950 rc = curs->c_get( curs, key, &data, DB_SET );
953 ic->count = BDB_IDL_DB_SIZE+1;
957 curs->c_count( curs, &count, 0 );
959 BDB_DISK2ID( &nid, &ic->first );
962 curs->c_close( curs );
964 /* are we a range already? */
965 if ( ic->count > BDB_IDL_DB_SIZE ) {
968 /* Are we at the limit, and converting to a range? */
969 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
971 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
974 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
975 ic->tail->next = bdb_tool_idl_free_list;
976 bdb_tool_idl_free_list = ic->head;
977 bdb->bi_idl_cache_size -= n;
978 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
980 ic->head = ic->tail = NULL;
985 /* No free block, create that too */
986 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
988 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
989 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
990 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
991 rc = bdb_tool_idl_flush_db( db, ic );
994 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
996 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
998 bdb->bi_idl_cache_size++;
999 if ( bdb_tool_idl_free_list ) {
1000 ice = bdb_tool_idl_free_list;
1001 bdb_tool_idl_free_list = ice->next;
1003 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1005 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1007 memset( ice, 0, sizeof( *ice ));
1011 ic->tail->next = ice;
1018 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1025 bdb_tool_index_task( void *ctx, void *ptr )
1027 int base = *(int *)ptr;
1031 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1032 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond,
1033 &bdb_tool_index_mutex );
1034 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1035 if ( slapd_shutdown )
1038 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1039 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );