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_main;
72 static ldap_pvt_thread_cond_t bdb_tool_index_cond_work;
74 static void * bdb_tool_index_task( void *ctx, void *ptr );
76 int bdb_tool_entry_open(
77 BackendDB *be, int mode )
79 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
81 /* initialize key and data thangs */
84 key.flags = DB_DBT_REALLOC;
85 data.flags = DB_DBT_REALLOC;
88 int rc = bdb->bi_id2entry->bdi_db->cursor(
89 bdb->bi_id2entry->bdi_db, NULL, &cursor,
96 /* Set up for threaded slapindex */
97 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK
99 if ( !bdb_tool_info ) {
101 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
102 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
103 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
104 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
105 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
106 bdb_tool_index_tcount = slap_tool_thread_max - 1;
107 for (i=1; i<slap_tool_thread_max; i++) {
108 int *ptr = ch_malloc( sizeof( int ));
110 ldap_pvt_thread_pool_submit( &connection_pool,
111 bdb_tool_index_task, ptr );
120 int bdb_tool_entry_close(
123 if ( bdb_tool_info ) {
125 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
126 bdb_tool_index_tcount = slap_tool_thread_max - 1;
127 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
128 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
136 ch_free( data.data );
141 cursor->c_close( cursor );
145 #ifdef BDB_TOOL_IDL_CACHING
146 bdb_tool_idl_flush( be );
151 fprintf( stderr, "Error, entries missing!\n");
152 for (i=0; i<nholes; i++) {
153 fprintf(stderr, " entry %ld: %s\n",
154 holes[i].id, holes[i].dn.bv_val);
162 ID bdb_tool_entry_next(
167 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
169 assert( be != NULL );
170 assert( slapMode & SLAP_TOOL_MODE );
171 assert( bdb != NULL );
173 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
176 /* If we're doing linear indexing and there are more attrs to
177 * index, and we're at the end of the database, start over.
179 if ( index_nattrs && rc == DB_NOTFOUND ) {
180 /* optional - do a checkpoint here? */
181 bdb_attr_info_free( bdb->bi_attrs[0] );
182 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
184 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
193 if( data.data == NULL ) {
197 BDB_DISK2ID( key.data, &id );
201 ID bdb_tool_dn2id_get(
208 EntryInfo *ei = NULL;
211 if ( BER_BVISEMPTY(dn) )
216 op.o_tmpmemctx = NULL;
217 op.o_tmpmfuncs = &ch_mfuncs;
219 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
220 if ( ei ) bdb_cache_entryinfo_unlock( ei );
221 if ( rc == DB_NOTFOUND )
227 int bdb_tool_id2entry_get(
233 int rc = bdb_id2entry( be, NULL, 0, id, e );
235 if ( rc == DB_NOTFOUND && id == 0 ) {
236 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
237 struct berval gluebv = BER_BVC("glue");
238 dummy->e_name.bv_val = ch_strdup( "" );
239 dummy->e_nname.bv_val = ch_strdup( "" );
240 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
241 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
249 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
255 assert( be != NULL );
256 assert( slapMode & SLAP_TOOL_MODE );
257 assert( data.data != NULL );
259 DBT2bv( &data, &bv );
261 #ifdef SLAP_ZONE_ALLOC
262 /* FIXME: will add ctx later */
263 rc = entry_decode( &bv, &e, NULL );
265 rc = entry_decode( &bv, &e );
268 if( rc == LDAP_SUCCESS ) {
272 if ( slapMode & SLAP_TOOL_READONLY ) {
273 EntryInfo *ei = NULL;
279 op.o_tmpmemctx = NULL;
280 op.o_tmpmfuncs = &ch_mfuncs;
282 rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
283 if ( rc == LDAP_SUCCESS ) {
284 bdb_cache_entryinfo_unlock( ei );
296 static int bdb_tool_next_id(
303 struct berval dn = e->e_name;
304 struct berval ndn = e->e_nname;
305 struct berval pdn, npdn;
306 EntryInfo *ei = NULL, eidummy;
309 if (ndn.bv_len == 0) {
314 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
315 if ( ei ) bdb_cache_entryinfo_unlock( ei );
316 if ( rc == DB_NOTFOUND ) {
317 if ( !be_issuffix( op->o_bd, &ndn ) ) {
319 dnParent( &dn, &pdn );
320 dnParent( &ndn, &npdn );
323 rc = bdb_tool_next_id( op, tid, e, text, 1 );
329 /* If parent didn't exist, it was created just now
330 * and its ID is now in e->e_id. Make sure the current
331 * entry gets added under the new parent ID.
333 if ( eid != e->e_id ) {
334 eidummy.bei_id = e->e_id;
338 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
340 snprintf( text->bv_val, text->bv_len,
341 "next_id failed: %s (%d)",
342 db_strerror(rc), rc );
343 Debug( LDAP_DEBUG_ANY,
344 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
347 rc = bdb_dn2id_add( op, tid, ei, e );
349 snprintf( text->bv_val, text->bv_len,
350 "dn2id_add failed: %s (%d)",
351 db_strerror(rc), rc );
352 Debug( LDAP_DEBUG_ANY,
353 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
355 if ( nholes == nhmax - 1 ) {
356 if ( holes == hbuf ) {
357 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
358 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
360 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
364 ber_dupbv( &holes[nholes].dn, &ndn );
365 holes[nholes++].id = e->e_id;
367 } else if ( !hole ) {
370 e->e_id = ei->bei_id;
372 for ( i=0; i<nholes; i++) {
373 if ( holes[i].id == e->e_id ) {
375 free(holes[i].dn.bv_val);
376 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
380 } else if ( holes[i].id > e->e_id ) {
394 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
396 if ( slapMode & SLAP_TOOL_QUICK ) {
401 ir = bdb_tool_index_rec;
402 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
404 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
405 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
406 &a->a_desc->ad_tags, ir );
410 bdb_tool_ix_id = e->e_id;
412 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
413 /* Wait for all threads to be ready */
414 while ( bdb_tool_index_tcount ) {
415 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
416 &bdb_tool_index_mutex );
418 for ( i=1; i<slap_tool_thread_max; i++ )
419 bdb_tool_index_threads[i] = LDAP_BUSY;
420 bdb_tool_index_tcount = slap_tool_thread_max - 1;
421 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
422 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
423 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
426 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
427 for ( i=1; i<slap_tool_thread_max; i++ ) {
428 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
429 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
430 &bdb_tool_index_mutex );
434 if ( bdb_tool_index_threads[i] ) {
435 rc = bdb_tool_index_threads[i];
439 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
442 return bdb_index_entry_add( op, txn, e );
446 ID bdb_tool_entry_put(
449 struct berval *text )
452 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
457 assert( be != NULL );
458 assert( slapMode & SLAP_TOOL_MODE );
460 assert( text != NULL );
461 assert( text->bv_val != NULL );
462 assert( text->bv_val[0] == '\0' ); /* overconservative? */
464 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
465 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
467 if (! (slapMode & SLAP_TOOL_QUICK)) {
468 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
469 bdb->bi_db_opflags );
471 snprintf( text->bv_val, text->bv_len,
472 "txn_begin failed: %s (%d)",
473 db_strerror(rc), rc );
474 Debug( LDAP_DEBUG_ANY,
475 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
476 text->bv_val, 0, 0 );
483 op.o_tmpmemctx = NULL;
484 op.o_tmpmfuncs = &ch_mfuncs;
486 /* add dn2id indices */
487 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
492 if ( !bdb->bi_linear_index )
493 rc = bdb_tool_index_add( &op, tid, e );
495 snprintf( text->bv_val, text->bv_len,
496 "index_entry_add failed: %s (%d)",
497 db_strerror(rc), rc );
498 Debug( LDAP_DEBUG_ANY,
499 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
500 text->bv_val, 0, 0 );
505 rc = bdb_id2entry_add( be, tid, e );
507 snprintf( text->bv_val, text->bv_len,
508 "id2entry_add failed: %s (%d)",
509 db_strerror(rc), rc );
510 Debug( LDAP_DEBUG_ANY,
511 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
512 text->bv_val, 0, 0 );
518 if ( !( slapMode & SLAP_TOOL_QUICK )) {
519 rc = TXN_COMMIT( tid, 0 );
521 snprintf( text->bv_val, text->bv_len,
522 "txn_commit failed: %s (%d)",
523 db_strerror(rc), rc );
524 Debug( LDAP_DEBUG_ANY,
525 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
526 text->bv_val, 0, 0 );
532 if ( !( slapMode & SLAP_TOOL_QUICK )) {
534 snprintf( text->bv_val, text->bv_len,
535 "txn_aborted! %s (%d)",
536 db_strerror(rc), rc );
537 Debug( LDAP_DEBUG_ANY,
538 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
539 text->bv_val, 0, 0 );
547 int bdb_tool_entry_reindex(
551 struct bdb_info *bi = (struct bdb_info *) be->be_private;
558 Debug( LDAP_DEBUG_ARGS,
559 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
562 /* No indexes configured, nothing to do. Could return an
563 * error here to shortcut things.
569 /* Get the first attribute to index */
570 if (bi->bi_linear_index && !index_nattrs) {
571 index_nattrs = bi->bi_nattrs - 1;
575 e = bdb_tool_entry_get( be, id );
578 Debug( LDAP_DEBUG_ANY,
579 LDAP_XSTRING(bdb_tool_entry_reindex)
580 ": could not locate id=%ld\n",
585 if (! (slapMode & SLAP_TOOL_QUICK)) {
586 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
588 Debug( LDAP_DEBUG_ANY,
589 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
590 "txn_begin failed: %s (%d)\n",
591 db_strerror(rc), rc, 0 );
597 * just (re)add them for now
598 * assume that some other routine (not yet implemented)
599 * will zap index databases
603 Debug( LDAP_DEBUG_TRACE,
604 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
605 (long) id, e->e_dn, 0 );
609 op.o_tmpmemctx = NULL;
610 op.o_tmpmfuncs = &ch_mfuncs;
612 rc = bdb_tool_index_add( &op, tid, e );
616 if (! (slapMode & SLAP_TOOL_QUICK)) {
617 rc = TXN_COMMIT( tid, 0 );
619 Debug( LDAP_DEBUG_ANY,
620 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
621 ": txn_commit failed: %s (%d)\n",
622 db_strerror(rc), rc, 0 );
628 if (! (slapMode & SLAP_TOOL_QUICK)) {
630 Debug( LDAP_DEBUG_ANY,
631 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
632 ": txn_aborted! %s (%d)\n",
633 db_strerror(rc), rc, 0 );
637 bdb_entry_release( &op, e, 0 );
642 ID bdb_tool_entry_modify(
645 struct berval *text )
648 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
653 assert( be != NULL );
654 assert( slapMode & SLAP_TOOL_MODE );
656 assert( text != NULL );
657 assert( text->bv_val != NULL );
658 assert( text->bv_val[0] == '\0' ); /* overconservative? */
660 assert ( e->e_id != NOID );
662 Debug( LDAP_DEBUG_TRACE,
663 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
664 (long) e->e_id, e->e_dn, 0 );
666 if (! (slapMode & SLAP_TOOL_QUICK)) {
667 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
668 bdb->bi_db_opflags );
670 snprintf( text->bv_val, text->bv_len,
671 "txn_begin failed: %s (%d)",
672 db_strerror(rc), rc );
673 Debug( LDAP_DEBUG_ANY,
674 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
675 text->bv_val, 0, 0 );
682 op.o_tmpmemctx = NULL;
683 op.o_tmpmfuncs = &ch_mfuncs;
686 rc = bdb_id2entry_update( be, tid, e );
688 snprintf( text->bv_val, text->bv_len,
689 "id2entry_add 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 );
699 if (! (slapMode & SLAP_TOOL_QUICK)) {
700 rc = TXN_COMMIT( tid, 0 );
702 snprintf( text->bv_val, text->bv_len,
703 "txn_commit failed: %s (%d)",
704 db_strerror(rc), rc );
705 Debug( LDAP_DEBUG_ANY,
706 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
707 "%s\n", text->bv_val, 0, 0 );
713 if (! (slapMode & SLAP_TOOL_QUICK)) {
715 snprintf( text->bv_val, text->bv_len,
716 "txn_aborted! %s (%d)",
717 db_strerror(rc), rc );
718 Debug( LDAP_DEBUG_ANY,
719 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
720 text->bv_val, 0, 0 );
728 #ifdef BDB_TOOL_IDL_CACHING
730 bdb_tool_idl_cmp( const void *v1, const void *v2 )
732 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
735 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
736 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
740 bdb_tool_idl_flush_one( void *v1, void *arg )
742 bdb_tool_idl_cache *ic = v1;
744 struct bdb_info *bdb = bdb_tool_info;
745 bdb_tool_idl_cache_entry *ice;
751 /* Freshly allocated, ignore it */
752 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
756 rc = db->cursor( db, NULL, &curs, 0 );
763 bv2DBT( &ic->kstr, &key );
765 data.size = data.ulen = sizeof( ID );
766 data.flags = DB_DBT_USERMEM;
769 rc = curs->c_get( curs, &key, &data, DB_SET );
770 /* If key already exists and we're writing a range... */
771 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
772 /* If it's not currently a range, must delete old info */
775 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
776 curs->c_del( curs, 0 );
779 /* Store range marker */
780 curs->c_put( curs, &key, &data, DB_KEYFIRST );
784 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
787 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
790 curs->c_del( curs, 0 );
792 BDB_ID2DISK( ic->last, &nid );
793 curs->c_put( curs, &key, &data, DB_KEYLAST );
795 } else if ( rc && rc != DB_NOTFOUND ) {
797 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
798 /* range, didn't exist before */
800 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
802 BDB_ID2DISK( ic->first, &nid );
803 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
805 BDB_ID2DISK( ic->last, &nid );
806 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
815 /* Just a normal write */
817 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
822 end = ic->count & (IDBLOCK-1);
826 for ( i=0; i<end; i++ ) {
827 if ( !ice->ids[i] ) continue;
828 BDB_ID2DISK( ice->ids[i], &nid );
829 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
831 if ( rc == DB_KEYEXIST ) {
845 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
846 ic->tail->next = bdb_tool_idl_free_list;
847 bdb_tool_idl_free_list = ic->head;
848 bdb->bi_idl_cache_size -= n;
849 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
852 if ( ic != db->app_private ) {
855 ic->head = ic->tail = NULL;
857 curs->c_close( curs );
862 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
864 Avlnode *root = db->app_private;
867 db->app_private = ic;
868 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
869 avl_free( root, NULL );
870 db->app_private = NULL;
877 bdb_tool_idl_flush( BackendDB *be )
879 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
884 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
885 db = bdb->bi_databases[i]->bdi_db;
886 if ( !db->app_private ) continue;
887 rc = bdb_tool_idl_flush_db( db, NULL );
892 bdb->bi_idl_cache_size = 0;
897 int bdb_tool_idl_add(
904 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
905 bdb_tool_idl_cache *ic, itmp;
906 bdb_tool_idl_cache_entry *ice;
909 if ( !bdb->bi_idl_cache_max_size )
910 return bdb_idl_insert_key( be, db, txn, key, id );
912 DBT2bv( key, &itmp.kstr );
914 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
916 /* No entry yet, create one */
923 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
924 ic->kstr.bv_len = itmp.kstr.bv_len;
925 ic->kstr.bv_val = (char *)(ic+1);
926 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
927 ic->head = ic->tail = NULL;
930 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
933 /* load existing key count here */
934 rc = db->cursor( db, NULL, &curs, 0 );
937 data.ulen = sizeof( ID );
938 data.flags = DB_DBT_USERMEM;
940 rc = curs->c_get( curs, key, &data, DB_SET );
943 ic->count = BDB_IDL_DB_SIZE+1;
947 curs->c_count( curs, &count, 0 );
949 BDB_DISK2ID( &nid, &ic->first );
952 curs->c_close( curs );
954 /* are we a range already? */
955 if ( ic->count > BDB_IDL_DB_SIZE ) {
958 /* Are we at the limit, and converting to a range? */
959 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
961 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
964 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
965 ic->tail->next = bdb_tool_idl_free_list;
966 bdb_tool_idl_free_list = ic->head;
967 bdb->bi_idl_cache_size -= n;
968 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
970 ic->head = ic->tail = NULL;
975 /* No free block, create that too */
976 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
978 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
979 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
980 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
981 rc = bdb_tool_idl_flush_db( db, ic );
984 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
986 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
988 bdb->bi_idl_cache_size++;
989 if ( bdb_tool_idl_free_list ) {
990 ice = bdb_tool_idl_free_list;
991 bdb_tool_idl_free_list = ice->next;
993 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
995 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
997 memset( ice, 0, sizeof( *ice ));
1001 ic->tail->next = ice;
1008 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1016 bdb_tool_index_task( void *ctx, void *ptr )
1018 int base = *(int *)ptr;
1022 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1023 bdb_tool_index_tcount--;
1024 if ( !bdb_tool_index_tcount )
1025 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1026 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1027 &bdb_tool_index_mutex );
1028 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1029 if ( slapd_shutdown )
1032 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1033 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );