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>
27 static DBC *cursor = NULL;
29 static EntryHeader eh;
32 typedef struct dn_id {
37 #define HOLE_SIZE 4096
38 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
39 static unsigned nhmax = HOLE_SIZE;
40 static unsigned nholes;
42 static int index_nattrs;
44 #ifdef BDB_TOOL_IDL_CACHING
45 #define bdb_tool_idl_cmp BDB_SYMBOL(tool_idl_cmp)
46 #define bdb_tool_idl_flush_one BDB_SYMBOL(tool_idl_flush_one)
47 #define bdb_tool_idl_flush BDB_SYMBOL(tool_idl_flush)
49 static int bdb_tool_idl_flush( BackendDB *be );
53 typedef struct bdb_tool_idl_cache_entry {
54 struct bdb_tool_idl_cache_entry *next;
56 } bdb_tool_idl_cache_entry;
58 typedef struct bdb_tool_idl_cache {
60 bdb_tool_idl_cache_entry *head, *tail;
65 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
66 #endif /* BDB_TOOL_IDL_CACHING */
68 static ID bdb_tool_ix_id;
69 static Operation *bdb_tool_ix_op;
70 static int *bdb_tool_index_threads, bdb_tool_index_tcount;
71 static void *bdb_tool_index_rec;
72 static struct bdb_info *bdb_tool_info;
73 static ldap_pvt_thread_mutex_t bdb_tool_index_mutex;
74 static ldap_pvt_thread_cond_t bdb_tool_index_cond_main;
75 static ldap_pvt_thread_cond_t bdb_tool_index_cond_work;
77 static void * bdb_tool_index_task( void *ctx, void *ptr );
79 int bdb_tool_entry_open(
80 BackendDB *be, int mode )
82 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
84 /* initialize key and data thangs */
87 key.flags = DB_DBT_REALLOC;
88 data.flags = DB_DBT_USERMEM;
91 int rc = bdb->bi_id2entry->bdi_db->cursor(
92 bdb->bi_id2entry->bdi_db, NULL, &cursor,
99 /* Set up for threaded slapindex */
100 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK
101 && bdb->bi_nattrs ) {
102 if ( !bdb_tool_info ) {
104 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
105 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
106 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
107 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
108 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
109 bdb_tool_index_tcount = slap_tool_thread_max - 1;
110 for (i=1; i<slap_tool_thread_max; i++) {
111 int *ptr = ch_malloc( sizeof( int ));
113 ldap_pvt_thread_pool_submit( &connection_pool,
114 bdb_tool_index_task, ptr );
123 int bdb_tool_entry_close(
126 if ( bdb_tool_info ) {
128 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
129 bdb_tool_index_tcount = slap_tool_thread_max - 1;
130 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
131 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
139 ch_free( eh.bv.bv_val );
144 cursor->c_close( cursor );
148 #ifdef BDB_TOOL_IDL_CACHING
149 bdb_tool_idl_flush( be );
154 fprintf( stderr, "Error, entries missing!\n");
155 for (i=0; i<nholes; i++) {
156 fprintf(stderr, " entry %ld: %s\n",
157 holes[i].id, holes[i].dn.bv_val);
165 ID bdb_tool_entry_next(
170 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
173 assert( be != NULL );
174 assert( slapMode & SLAP_TOOL_MODE );
175 assert( bdb != NULL );
178 data.ulen = data.dlen = sizeof( buf );
180 data.flags |= DB_DBT_PARTIAL;
181 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
184 /* If we're doing linear indexing and there are more attrs to
185 * index, and we're at the end of the database, start over.
187 if ( index_nattrs && rc == DB_NOTFOUND ) {
188 /* optional - do a checkpoint here? */
189 bdb_attr_info_free( bdb->bi_attrs[0] );
190 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
192 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
203 eh.bv.bv_len = data.size;
204 rc = entry_header( &eh );
205 eoff = eh.data - eh.bv.bv_val;
211 BDB_DISK2ID( key.data, &id );
215 ID bdb_tool_dn2id_get(
222 EntryInfo *ei = NULL;
225 if ( BER_BVISEMPTY(dn) )
230 op.o_tmpmemctx = NULL;
231 op.o_tmpmfuncs = &ch_mfuncs;
233 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
234 if ( ei ) bdb_cache_entryinfo_unlock( ei );
235 if ( rc == DB_NOTFOUND )
241 int bdb_tool_id2entry_get(
247 int rc = bdb_id2entry( be, NULL, 0, id, e );
249 if ( rc == DB_NOTFOUND && id == 0 ) {
250 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
251 struct berval gluebv = BER_BVC("glue");
252 dummy->e_name.bv_val = ch_strdup( "" );
253 dummy->e_nname.bv_val = ch_strdup( "" );
254 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
255 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
263 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
268 assert( be != NULL );
269 assert( slapMode & SLAP_TOOL_MODE );
272 data.flags ^= DB_DBT_PARTIAL;
274 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
275 if ( rc != DB_BUFFER_SMALL ) goto leave;
277 /* Allocate a block and retrieve the data */
278 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
279 eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
280 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
282 data.ulen = data.size;
284 /* Skip past already parsed nattr/nvals */
287 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
288 if ( rc ) goto leave;
290 #ifdef SLAP_ZONE_ALLOC
291 /* FIXME: will add ctx later */
292 rc = entry_decode( &eh, &e, NULL );
294 rc = entry_decode( &eh, &e );
297 if( rc == LDAP_SUCCESS ) {
300 if ( slapMode & SLAP_TOOL_READONLY ) {
301 EntryInfo *ei = NULL;
307 op.o_tmpmemctx = NULL;
308 op.o_tmpmfuncs = &ch_mfuncs;
310 rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
311 if ( rc == LDAP_SUCCESS ) {
312 bdb_cache_entryinfo_unlock( ei );
326 static int bdb_tool_next_id(
333 struct berval dn = e->e_name;
334 struct berval ndn = e->e_nname;
335 struct berval pdn, npdn;
336 EntryInfo *ei = NULL, eidummy;
339 if (ndn.bv_len == 0) {
344 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
345 if ( ei ) bdb_cache_entryinfo_unlock( ei );
346 if ( rc == DB_NOTFOUND ) {
347 if ( !be_issuffix( op->o_bd, &ndn ) ) {
349 dnParent( &dn, &pdn );
350 dnParent( &ndn, &npdn );
353 rc = bdb_tool_next_id( op, tid, e, text, 1 );
359 /* If parent didn't exist, it was created just now
360 * and its ID is now in e->e_id. Make sure the current
361 * entry gets added under the new parent ID.
363 if ( eid != e->e_id ) {
364 eidummy.bei_id = e->e_id;
368 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
370 snprintf( text->bv_val, text->bv_len,
371 "next_id failed: %s (%d)",
372 db_strerror(rc), rc );
373 Debug( LDAP_DEBUG_ANY,
374 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
377 rc = bdb_dn2id_add( op, tid, ei, e );
379 snprintf( text->bv_val, text->bv_len,
380 "dn2id_add failed: %s (%d)",
381 db_strerror(rc), rc );
382 Debug( LDAP_DEBUG_ANY,
383 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
385 if ( nholes == nhmax - 1 ) {
386 if ( holes == hbuf ) {
387 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
388 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
390 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
394 ber_dupbv( &holes[nholes].dn, &ndn );
395 holes[nholes++].id = e->e_id;
397 } else if ( !hole ) {
400 e->e_id = ei->bei_id;
402 for ( i=0; i<nholes; i++) {
403 if ( holes[i].id == e->e_id ) {
405 free(holes[i].dn.bv_val);
406 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
410 } else if ( holes[i].id > e->e_id ) {
424 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
426 if ( !bdb->bi_nattrs )
429 if ( slapMode & SLAP_TOOL_QUICK ) {
434 ir = bdb_tool_index_rec;
435 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
437 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
438 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
439 &a->a_desc->ad_tags, ir );
443 bdb_tool_ix_id = e->e_id;
445 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
446 /* Wait for all threads to be ready */
447 while ( bdb_tool_index_tcount ) {
448 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
449 &bdb_tool_index_mutex );
451 for ( i=1; i<slap_tool_thread_max; i++ )
452 bdb_tool_index_threads[i] = LDAP_BUSY;
453 bdb_tool_index_tcount = slap_tool_thread_max - 1;
454 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
455 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
456 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
459 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
460 for ( i=1; i<slap_tool_thread_max; i++ ) {
461 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
462 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
463 &bdb_tool_index_mutex );
467 if ( bdb_tool_index_threads[i] ) {
468 rc = bdb_tool_index_threads[i];
472 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
475 return bdb_index_entry_add( op, txn, e );
479 ID bdb_tool_entry_put(
482 struct berval *text )
485 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
490 assert( be != NULL );
491 assert( slapMode & SLAP_TOOL_MODE );
493 assert( text != NULL );
494 assert( text->bv_val != NULL );
495 assert( text->bv_val[0] == '\0' ); /* overconservative? */
497 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
498 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
500 if (! (slapMode & SLAP_TOOL_QUICK)) {
501 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
502 bdb->bi_db_opflags );
504 snprintf( text->bv_val, text->bv_len,
505 "txn_begin 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 );
516 op.o_tmpmemctx = NULL;
517 op.o_tmpmfuncs = &ch_mfuncs;
519 /* add dn2id indices */
520 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
525 if ( !bdb->bi_linear_index )
526 rc = bdb_tool_index_add( &op, tid, e );
528 snprintf( text->bv_val, text->bv_len,
529 "index_entry_add failed: %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 );
538 rc = bdb_id2entry_add( be, tid, e );
540 snprintf( text->bv_val, text->bv_len,
541 "id2entry_add failed: %s (%d)",
542 db_strerror(rc), rc );
543 Debug( LDAP_DEBUG_ANY,
544 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
545 text->bv_val, 0, 0 );
551 if ( !( slapMode & SLAP_TOOL_QUICK )) {
552 rc = TXN_COMMIT( tid, 0 );
554 snprintf( text->bv_val, text->bv_len,
555 "txn_commit failed: %s (%d)",
556 db_strerror(rc), rc );
557 Debug( LDAP_DEBUG_ANY,
558 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
559 text->bv_val, 0, 0 );
565 if ( !( slapMode & SLAP_TOOL_QUICK )) {
567 snprintf( text->bv_val, text->bv_len,
568 "txn_aborted! %s (%d)",
569 db_strerror(rc), rc );
570 Debug( LDAP_DEBUG_ANY,
571 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
572 text->bv_val, 0, 0 );
580 int bdb_tool_entry_reindex(
583 AttributeDescription **adv )
585 struct bdb_info *bi = (struct bdb_info *) be->be_private;
592 Debug( LDAP_DEBUG_ARGS,
593 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
596 /* No indexes configured, nothing to do. Could return an
597 * error here to shortcut things.
603 /* Check for explicit list of attrs to index */
607 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
609 for ( n = 0; adv[n]; n++ ) ;
612 for ( i = 0; i < n; i++ ) {
613 AttributeDescription *ad = adv[i];
614 for ( j = i-1; j>=0; j--) {
615 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
622 for ( i = 0; adv[i]; i++ ) {
623 if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
624 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
625 if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
626 AttrInfo *ai = bi->bi_attrs[i];
627 bi->bi_attrs[i] = bi->bi_attrs[j];
628 bi->bi_attrs[j] = ai;
632 if ( j == bi->bi_nattrs ) {
633 Debug( LDAP_DEBUG_ANY,
634 LDAP_XSTRING(bdb_tool_entry_reindex)
635 ": no index configured for %s\n",
636 adv[i]->ad_cname.bv_val, 0, 0 );
644 /* Get the first attribute to index */
645 if (bi->bi_linear_index && !index_nattrs) {
646 index_nattrs = bi->bi_nattrs - 1;
650 e = bdb_tool_entry_get( be, id );
653 Debug( LDAP_DEBUG_ANY,
654 LDAP_XSTRING(bdb_tool_entry_reindex)
655 ": could not locate id=%ld\n",
660 if (! (slapMode & SLAP_TOOL_QUICK)) {
661 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
663 Debug( LDAP_DEBUG_ANY,
664 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
665 "txn_begin failed: %s (%d)\n",
666 db_strerror(rc), rc, 0 );
672 * just (re)add them for now
673 * assume that some other routine (not yet implemented)
674 * will zap index databases
678 Debug( LDAP_DEBUG_TRACE,
679 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
680 (long) id, e->e_dn, 0 );
684 op.o_tmpmemctx = NULL;
685 op.o_tmpmfuncs = &ch_mfuncs;
687 rc = bdb_tool_index_add( &op, tid, e );
691 if (! (slapMode & SLAP_TOOL_QUICK)) {
692 rc = TXN_COMMIT( tid, 0 );
694 Debug( LDAP_DEBUG_ANY,
695 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
696 ": txn_commit failed: %s (%d)\n",
697 db_strerror(rc), rc, 0 );
703 if (! (slapMode & SLAP_TOOL_QUICK)) {
705 Debug( LDAP_DEBUG_ANY,
706 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
707 ": txn_aborted! %s (%d)\n",
708 db_strerror(rc), rc, 0 );
712 bdb_entry_release( &op, e, 0 );
717 ID bdb_tool_entry_modify(
720 struct berval *text )
723 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
728 assert( be != NULL );
729 assert( slapMode & SLAP_TOOL_MODE );
731 assert( text != NULL );
732 assert( text->bv_val != NULL );
733 assert( text->bv_val[0] == '\0' ); /* overconservative? */
735 assert ( e->e_id != NOID );
737 Debug( LDAP_DEBUG_TRACE,
738 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
739 (long) e->e_id, e->e_dn, 0 );
741 if (! (slapMode & SLAP_TOOL_QUICK)) {
742 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
743 bdb->bi_db_opflags );
745 snprintf( text->bv_val, text->bv_len,
746 "txn_begin failed: %s (%d)",
747 db_strerror(rc), rc );
748 Debug( LDAP_DEBUG_ANY,
749 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
750 text->bv_val, 0, 0 );
757 op.o_tmpmemctx = NULL;
758 op.o_tmpmfuncs = &ch_mfuncs;
761 rc = bdb_id2entry_update( be, tid, e );
763 snprintf( text->bv_val, text->bv_len,
764 "id2entry_add failed: %s (%d)",
765 db_strerror(rc), rc );
766 Debug( LDAP_DEBUG_ANY,
767 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
768 text->bv_val, 0, 0 );
774 if (! (slapMode & SLAP_TOOL_QUICK)) {
775 rc = TXN_COMMIT( tid, 0 );
777 snprintf( text->bv_val, text->bv_len,
778 "txn_commit failed: %s (%d)",
779 db_strerror(rc), rc );
780 Debug( LDAP_DEBUG_ANY,
781 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
782 "%s\n", text->bv_val, 0, 0 );
788 if (! (slapMode & SLAP_TOOL_QUICK)) {
790 snprintf( text->bv_val, text->bv_len,
791 "txn_aborted! %s (%d)",
792 db_strerror(rc), rc );
793 Debug( LDAP_DEBUG_ANY,
794 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
795 text->bv_val, 0, 0 );
803 #ifdef BDB_TOOL_IDL_CACHING
805 bdb_tool_idl_cmp( const void *v1, const void *v2 )
807 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
810 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
811 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
815 bdb_tool_idl_flush_one( void *v1, void *arg )
817 bdb_tool_idl_cache *ic = v1;
819 struct bdb_info *bdb = bdb_tool_info;
820 bdb_tool_idl_cache_entry *ice;
826 /* Freshly allocated, ignore it */
827 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
831 rc = db->cursor( db, NULL, &curs, 0 );
838 bv2DBT( &ic->kstr, &key );
840 data.size = data.ulen = sizeof( ID );
841 data.flags = DB_DBT_USERMEM;
844 rc = curs->c_get( curs, &key, &data, DB_SET );
845 /* If key already exists and we're writing a range... */
846 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
847 /* If it's not currently a range, must delete old info */
850 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
851 curs->c_del( curs, 0 );
854 /* Store range marker */
855 curs->c_put( curs, &key, &data, DB_KEYFIRST );
859 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
862 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
865 curs->c_del( curs, 0 );
867 BDB_ID2DISK( ic->last, &nid );
868 curs->c_put( curs, &key, &data, DB_KEYLAST );
870 } else if ( rc && rc != DB_NOTFOUND ) {
872 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
873 /* range, didn't exist before */
875 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
877 BDB_ID2DISK( ic->first, &nid );
878 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
880 BDB_ID2DISK( ic->last, &nid );
881 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
890 /* Just a normal write */
892 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
897 end = ic->count & (IDBLOCK-1);
901 for ( i=0; i<end; i++ ) {
902 if ( !ice->ids[i] ) continue;
903 BDB_ID2DISK( ice->ids[i], &nid );
904 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
906 if ( rc == DB_KEYEXIST ) {
920 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
921 ic->tail->next = bdb_tool_idl_free_list;
922 bdb_tool_idl_free_list = ic->head;
923 bdb->bi_idl_cache_size -= n;
924 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
927 if ( ic != db->app_private ) {
930 ic->head = ic->tail = NULL;
932 curs->c_close( curs );
937 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
939 Avlnode *root = db->app_private;
942 db->app_private = ic;
943 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
944 avl_free( root, NULL );
945 db->app_private = NULL;
952 bdb_tool_idl_flush( BackendDB *be )
954 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
959 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
960 db = bdb->bi_databases[i]->bdi_db;
961 if ( !db->app_private ) continue;
962 rc = bdb_tool_idl_flush_db( db, NULL );
967 bdb->bi_idl_cache_size = 0;
972 int bdb_tool_idl_add(
979 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
980 bdb_tool_idl_cache *ic, itmp;
981 bdb_tool_idl_cache_entry *ice;
984 if ( !bdb->bi_idl_cache_max_size )
985 return bdb_idl_insert_key( be, db, txn, key, id );
987 DBT2bv( key, &itmp.kstr );
989 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
991 /* No entry yet, create one */
998 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
999 ic->kstr.bv_len = itmp.kstr.bv_len;
1000 ic->kstr.bv_val = (char *)(ic+1);
1001 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1002 ic->head = ic->tail = NULL;
1005 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1008 /* load existing key count here */
1009 rc = db->cursor( db, NULL, &curs, 0 );
1010 if ( rc ) return rc;
1012 data.ulen = sizeof( ID );
1013 data.flags = DB_DBT_USERMEM;
1015 rc = curs->c_get( curs, key, &data, DB_SET );
1018 ic->count = BDB_IDL_DB_SIZE+1;
1022 curs->c_count( curs, &count, 0 );
1024 BDB_DISK2ID( &nid, &ic->first );
1027 curs->c_close( curs );
1029 /* are we a range already? */
1030 if ( ic->count > BDB_IDL_DB_SIZE ) {
1033 /* Are we at the limit, and converting to a range? */
1034 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1036 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1039 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1040 ic->tail->next = bdb_tool_idl_free_list;
1041 bdb_tool_idl_free_list = ic->head;
1042 bdb->bi_idl_cache_size -= n;
1043 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1045 ic->head = ic->tail = NULL;
1050 /* No free block, create that too */
1051 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1053 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1054 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1055 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1056 rc = bdb_tool_idl_flush_db( db, ic );
1059 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1061 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1063 bdb->bi_idl_cache_size++;
1064 if ( bdb_tool_idl_free_list ) {
1065 ice = bdb_tool_idl_free_list;
1066 bdb_tool_idl_free_list = ice->next;
1068 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1070 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1072 memset( ice, 0, sizeof( *ice ));
1076 ic->tail->next = ice;
1083 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1091 bdb_tool_index_task( void *ctx, void *ptr )
1093 int base = *(int *)ptr;
1097 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1098 bdb_tool_index_tcount--;
1099 if ( !bdb_tool_index_tcount )
1100 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1101 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1102 &bdb_tool_index_mutex );
1103 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1104 if ( slapd_shutdown )
1107 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1108 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );