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 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 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 for (i=1; i<slap_tool_thread_max; i++) {
104 int *ptr = ch_malloc( sizeof( int ));
106 ldap_pvt_thread_pool_submit( &connection_pool,
107 bdb_tool_index_task, ptr );
108 ldap_pvt_thread_yield();
117 int bdb_tool_entry_close(
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 #ifdef BDB_TOOL_IDL_CACHING
142 bdb_tool_idl_flush( be );
147 fprintf( stderr, "Error, entries missing!\n");
148 for (i=0; i<nholes; i++) {
149 fprintf(stderr, " entry %ld: %s\n",
150 holes[i].id, holes[i].dn.bv_val);
158 ID bdb_tool_entry_next(
163 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
165 assert( be != NULL );
166 assert( slapMode & SLAP_TOOL_MODE );
167 assert( bdb != NULL );
169 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
172 /* If we're doing linear indexing and there are more attrs to
173 * index, and we're at the end of the database, start over.
175 if ( index_nattrs && rc == DB_NOTFOUND ) {
176 /* optional - do a checkpoint here? */
177 bdb_attr_info_free( bdb->bi_attrs[0] );
178 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
180 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
189 if( data.data == NULL ) {
193 BDB_DISK2ID( key.data, &id );
197 ID bdb_tool_dn2id_get(
204 EntryInfo *ei = NULL;
207 if ( BER_BVISEMPTY(dn) )
212 op.o_tmpmemctx = NULL;
213 op.o_tmpmfuncs = &ch_mfuncs;
215 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
216 if ( ei ) bdb_cache_entryinfo_unlock( ei );
217 if ( rc == DB_NOTFOUND )
223 int bdb_tool_id2entry_get(
229 int rc = bdb_id2entry( be, NULL, 0, id, e );
231 if ( rc == DB_NOTFOUND && id == 0 ) {
232 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
233 struct berval gluebv = BER_BVC("glue");
234 dummy->e_name.bv_val = ch_strdup( "" );
235 dummy->e_nname.bv_val = ch_strdup( "" );
236 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
237 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
245 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
251 assert( be != NULL );
252 assert( slapMode & SLAP_TOOL_MODE );
253 assert( data.data != NULL );
255 DBT2bv( &data, &bv );
257 #ifdef SLAP_ZONE_ALLOC
258 /* FIXME: will add ctx later */
259 rc = entry_decode( &bv, &e, NULL );
261 rc = entry_decode( &bv, &e );
264 if( rc == LDAP_SUCCESS ) {
268 if ( slapMode & SLAP_TOOL_READONLY ) {
269 EntryInfo *ei = NULL;
275 op.o_tmpmemctx = NULL;
276 op.o_tmpmfuncs = &ch_mfuncs;
278 rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
279 if ( rc == LDAP_SUCCESS ) {
280 bdb_cache_entryinfo_unlock( ei );
292 static int bdb_tool_next_id(
299 struct berval dn = e->e_name;
300 struct berval ndn = e->e_nname;
301 struct berval pdn, npdn;
302 EntryInfo *ei = NULL, eidummy;
305 if (ndn.bv_len == 0) {
310 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
311 if ( ei ) bdb_cache_entryinfo_unlock( ei );
312 if ( rc == DB_NOTFOUND ) {
313 if ( !be_issuffix( op->o_bd, &ndn ) ) {
315 dnParent( &dn, &pdn );
316 dnParent( &ndn, &npdn );
319 rc = bdb_tool_next_id( op, tid, e, text, 1 );
325 /* If parent didn't exist, it was created just now
326 * and its ID is now in e->e_id. Make sure the current
327 * entry gets added under the new parent ID.
329 if ( eid != e->e_id ) {
330 eidummy.bei_id = e->e_id;
334 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
336 snprintf( text->bv_val, text->bv_len,
337 "next_id failed: %s (%d)",
338 db_strerror(rc), rc );
339 Debug( LDAP_DEBUG_ANY,
340 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
343 rc = bdb_dn2id_add( op, tid, ei, e );
345 snprintf( text->bv_val, text->bv_len,
346 "dn2id_add failed: %s (%d)",
347 db_strerror(rc), rc );
348 Debug( LDAP_DEBUG_ANY,
349 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
351 if ( nholes == nhmax - 1 ) {
352 if ( holes == hbuf ) {
353 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
354 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
356 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
360 ber_dupbv( &holes[nholes].dn, &ndn );
361 holes[nholes++].id = e->e_id;
363 } else if ( !hole ) {
366 e->e_id = ei->bei_id;
368 for ( i=0; i<nholes; i++) {
369 if ( holes[i].id == e->e_id ) {
371 free(holes[i].dn.bv_val);
372 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
376 } else if ( holes[i].id > e->e_id ) {
390 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
392 if ( slapMode & SLAP_TOOL_QUICK ) {
397 ir = bdb_tool_index_rec;
398 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
400 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
401 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
402 &a->a_desc->ad_tags, ir );
406 bdb_tool_ix_id = e->e_id;
408 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
409 for ( i=1; i<slap_tool_thread_max; i++ )
410 bdb_tool_index_threads[i] = LDAP_BUSY;
411 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond );
412 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
413 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
416 for ( i=1; i<slap_tool_thread_max; i++ ) {
417 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
418 ldap_pvt_thread_yield();
422 if ( bdb_tool_index_threads[i] )
423 return bdb_tool_index_threads[i];
427 return bdb_index_entry_add( op, txn, e );
431 ID bdb_tool_entry_put(
434 struct berval *text )
437 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
442 assert( be != NULL );
443 assert( slapMode & SLAP_TOOL_MODE );
445 assert( text != NULL );
446 assert( text->bv_val != NULL );
447 assert( text->bv_val[0] == '\0' ); /* overconservative? */
449 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
450 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
452 if (! (slapMode & SLAP_TOOL_QUICK)) {
453 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
454 bdb->bi_db_opflags );
456 snprintf( text->bv_val, text->bv_len,
457 "txn_begin failed: %s (%d)",
458 db_strerror(rc), rc );
459 Debug( LDAP_DEBUG_ANY,
460 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
461 text->bv_val, 0, 0 );
468 op.o_tmpmemctx = NULL;
469 op.o_tmpmfuncs = &ch_mfuncs;
471 /* add dn2id indices */
472 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
478 rc = bdb_id2entry_add( be, tid, e );
480 snprintf( text->bv_val, text->bv_len,
481 "id2entry_add failed: %s (%d)",
482 db_strerror(rc), rc );
483 Debug( LDAP_DEBUG_ANY,
484 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
485 text->bv_val, 0, 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 );
503 if ( !( slapMode & SLAP_TOOL_QUICK )) {
504 rc = TXN_COMMIT( tid, 0 );
506 snprintf( text->bv_val, text->bv_len,
507 "txn_commit failed: %s (%d)",
508 db_strerror(rc), rc );
509 Debug( LDAP_DEBUG_ANY,
510 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
511 text->bv_val, 0, 0 );
517 if ( !( slapMode & SLAP_TOOL_QUICK )) {
519 snprintf( text->bv_val, text->bv_len,
520 "txn_aborted! %s (%d)",
521 db_strerror(rc), rc );
522 Debug( LDAP_DEBUG_ANY,
523 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
524 text->bv_val, 0, 0 );
532 int bdb_tool_entry_reindex(
536 struct bdb_info *bi = (struct bdb_info *) be->be_private;
543 Debug( LDAP_DEBUG_ARGS,
544 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
547 /* No indexes configured, nothing to do. Could return an
548 * error here to shortcut things.
554 /* Get the first attribute to index */
555 if (bi->bi_linear_index && !index_nattrs) {
556 index_nattrs = bi->bi_nattrs - 1;
560 e = bdb_tool_entry_get( be, id );
563 Debug( LDAP_DEBUG_ANY,
564 LDAP_XSTRING(bdb_tool_entry_reindex)
565 ": could not locate id=%ld\n",
570 if (! (slapMode & SLAP_TOOL_QUICK)) {
571 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
573 Debug( LDAP_DEBUG_ANY,
574 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
575 "txn_begin failed: %s (%d)\n",
576 db_strerror(rc), rc, 0 );
582 * just (re)add them for now
583 * assume that some other routine (not yet implemented)
584 * will zap index databases
588 Debug( LDAP_DEBUG_TRACE,
589 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
590 (long) id, e->e_dn, 0 );
594 op.o_tmpmemctx = NULL;
595 op.o_tmpmfuncs = &ch_mfuncs;
597 rc = bdb_tool_index_add( &op, tid, e );
601 if (! (slapMode & SLAP_TOOL_QUICK)) {
602 rc = TXN_COMMIT( tid, 0 );
604 Debug( LDAP_DEBUG_ANY,
605 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
606 ": txn_commit failed: %s (%d)\n",
607 db_strerror(rc), rc, 0 );
613 if (! (slapMode & SLAP_TOOL_QUICK)) {
615 Debug( LDAP_DEBUG_ANY,
616 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
617 ": txn_aborted! %s (%d)\n",
618 db_strerror(rc), rc, 0 );
622 bdb_entry_release( &op, e, 0 );
627 ID bdb_tool_entry_modify(
630 struct berval *text )
633 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
638 assert( be != NULL );
639 assert( slapMode & SLAP_TOOL_MODE );
641 assert( text != NULL );
642 assert( text->bv_val != NULL );
643 assert( text->bv_val[0] == '\0' ); /* overconservative? */
645 assert ( e->e_id != NOID );
647 Debug( LDAP_DEBUG_TRACE,
648 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
649 (long) e->e_id, e->e_dn, 0 );
651 if (! (slapMode & SLAP_TOOL_QUICK)) {
652 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
653 bdb->bi_db_opflags );
655 snprintf( text->bv_val, text->bv_len,
656 "txn_begin failed: %s (%d)",
657 db_strerror(rc), rc );
658 Debug( LDAP_DEBUG_ANY,
659 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
660 text->bv_val, 0, 0 );
667 op.o_tmpmemctx = NULL;
668 op.o_tmpmfuncs = &ch_mfuncs;
671 rc = bdb_id2entry_update( be, tid, e );
673 snprintf( text->bv_val, text->bv_len,
674 "id2entry_add failed: %s (%d)",
675 db_strerror(rc), rc );
676 Debug( LDAP_DEBUG_ANY,
677 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
678 text->bv_val, 0, 0 );
683 /* FIXME: this is bogus, we don't have the old values to delete
684 * from the index because the given entry has already been modified.
686 rc = bdb_index_entry_del( &op, tid, e );
688 snprintf( text->bv_val, text->bv_len,
689 "index_entry_del failed: %s (%d)",
690 db_strerror(rc), rc );
691 Debug( LDAP_DEBUG_ANY,
692 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
693 text->bv_val, 0, 0 );
698 rc = bdb_index_entry_add( &op, tid, e );
700 snprintf( text->bv_val, text->bv_len,
701 "index_entry_add failed: %s (%d)",
702 db_strerror(rc), rc );
703 Debug( LDAP_DEBUG_ANY,
704 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
705 text->bv_val, 0, 0 );
711 if (! (slapMode & SLAP_TOOL_QUICK)) {
712 rc = TXN_COMMIT( tid, 0 );
714 snprintf( text->bv_val, text->bv_len,
715 "txn_commit failed: %s (%d)",
716 db_strerror(rc), rc );
717 Debug( LDAP_DEBUG_ANY,
718 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
719 "%s\n", text->bv_val, 0, 0 );
725 if (! (slapMode & SLAP_TOOL_QUICK)) {
727 snprintf( text->bv_val, text->bv_len,
728 "txn_aborted! %s (%d)",
729 db_strerror(rc), rc );
730 Debug( LDAP_DEBUG_ANY,
731 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
732 text->bv_val, 0, 0 );
740 #ifdef BDB_TOOL_IDL_CACHING
742 bdb_tool_idl_cmp( const void *v1, const void *v2 )
744 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
747 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
748 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
752 bdb_tool_idl_flush_one( void *v1, void *arg )
754 bdb_tool_idl_cache *ic = v1;
756 struct bdb_info *bdb = bdb_tool_info;
757 bdb_tool_idl_cache_entry *ice;
763 /* Freshly allocated, ignore it */
764 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
768 rc = db->cursor( db, NULL, &curs, 0 );
775 bv2DBT( &ic->kstr, &key );
777 data.size = data.ulen = sizeof( ID );
778 data.flags = DB_DBT_USERMEM;
781 rc = curs->c_get( curs, &key, &data, DB_SET );
782 /* If key already exists and we're writing a range... */
783 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
784 /* If it's not currently a range, must delete old info */
787 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
788 curs->c_del( curs, 0 );
791 /* Store range marker */
792 curs->c_put( curs, &key, &data, DB_KEYFIRST );
796 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
799 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
802 curs->c_del( curs, 0 );
804 BDB_ID2DISK( ic->last, &nid );
805 curs->c_put( curs, &key, &data, DB_KEYLAST );
807 } else if ( rc && rc != DB_NOTFOUND ) {
809 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
810 /* range, didn't exist before */
812 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
814 BDB_ID2DISK( ic->first, &nid );
815 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
817 BDB_ID2DISK( ic->last, &nid );
818 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
827 /* Just a normal write */
829 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
834 end = ic->count & (IDBLOCK-1);
838 for ( i=0; i<end; i++ ) {
839 if ( !ice->ids[i] ) continue;
840 BDB_ID2DISK( ice->ids[i], &nid );
841 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
843 if ( rc == DB_KEYEXIST ) {
857 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
858 ic->tail->next = bdb_tool_idl_free_list;
859 bdb_tool_idl_free_list = ic->head;
860 bdb->bi_idl_cache_size -= n;
861 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
864 if ( ic != db->app_private ) {
867 ic->head = ic->tail = NULL;
869 curs->c_close( curs );
874 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
876 Avlnode *root = db->app_private;
879 db->app_private = ic;
880 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
881 avl_free( root, NULL );
882 db->app_private = NULL;
889 bdb_tool_idl_flush( BackendDB *be )
891 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
896 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
897 db = bdb->bi_databases[i]->bdi_db;
898 if ( !db->app_private ) continue;
899 rc = bdb_tool_idl_flush_db( db, NULL );
904 bdb->bi_idl_cache_size = 0;
909 int bdb_tool_idl_add(
916 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
917 bdb_tool_idl_cache *ic, itmp;
918 bdb_tool_idl_cache_entry *ice;
921 if ( !bdb->bi_idl_cache_max_size )
922 return bdb_idl_insert_key( be, db, txn, key, id );
924 DBT2bv( key, &itmp.kstr );
926 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
928 /* No entry yet, create one */
935 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
936 ic->kstr.bv_len = itmp.kstr.bv_len;
937 ic->kstr.bv_val = (char *)(ic+1);
938 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
939 ic->head = ic->tail = NULL;
942 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
945 /* load existing key count here */
946 rc = db->cursor( db, NULL, &curs, 0 );
949 data.ulen = sizeof( ID );
950 data.flags = DB_DBT_USERMEM;
952 rc = curs->c_get( curs, key, &data, DB_SET );
955 ic->count = BDB_IDL_DB_SIZE+1;
959 curs->c_count( curs, &count, 0 );
961 BDB_DISK2ID( &nid, &ic->first );
964 curs->c_close( curs );
966 /* are we a range already? */
967 if ( ic->count > BDB_IDL_DB_SIZE ) {
970 /* Are we at the limit, and converting to a range? */
971 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
973 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
976 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
977 ic->tail->next = bdb_tool_idl_free_list;
978 bdb_tool_idl_free_list = ic->head;
979 bdb->bi_idl_cache_size -= n;
980 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
982 ic->head = ic->tail = NULL;
987 /* No free block, create that too */
988 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
990 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
991 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
992 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
993 rc = bdb_tool_idl_flush_db( db, ic );
996 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
998 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1000 bdb->bi_idl_cache_size++;
1001 if ( bdb_tool_idl_free_list ) {
1002 ice = bdb_tool_idl_free_list;
1003 bdb_tool_idl_free_list = ice->next;
1005 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1007 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1009 memset( ice, 0, sizeof( *ice ));
1013 ic->tail->next = ice;
1020 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1028 bdb_tool_index_task( void *ctx, void *ptr )
1030 int base = *(int *)ptr;
1034 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1035 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond,
1036 &bdb_tool_index_mutex );
1037 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1038 if ( slapd_shutdown )
1041 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1042 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );