1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2014 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;
30 static ID nid, previd = NOID;
31 static char ehbuf[16];
33 typedef struct dn_id {
38 #define HOLE_SIZE 4096
39 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
40 static unsigned nhmax = HOLE_SIZE;
41 static unsigned nholes;
43 static int index_nattrs;
45 static struct berval *tool_base;
46 static int tool_scope;
47 static Filter *tool_filter;
48 static Entry *tool_next_entry;
50 #ifdef BDB_TOOL_IDL_CACHING
51 #define bdb_tool_idl_cmp BDB_SYMBOL(tool_idl_cmp)
52 #define bdb_tool_idl_flush_one BDB_SYMBOL(tool_idl_flush_one)
53 #define bdb_tool_idl_flush BDB_SYMBOL(tool_idl_flush)
55 static int bdb_tool_idl_flush( BackendDB *be );
59 typedef struct bdb_tool_idl_cache_entry {
60 struct bdb_tool_idl_cache_entry *next;
62 } bdb_tool_idl_cache_entry;
64 typedef struct bdb_tool_idl_cache {
66 bdb_tool_idl_cache_entry *head, *tail;
71 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
72 #endif /* BDB_TOOL_IDL_CACHING */
74 static ID bdb_tool_ix_id;
75 static Operation *bdb_tool_ix_op;
76 static int *bdb_tool_index_threads, bdb_tool_index_tcount;
77 static void *bdb_tool_index_rec;
78 static struct bdb_info *bdb_tool_info;
79 static ldap_pvt_thread_mutex_t bdb_tool_index_mutex;
80 static ldap_pvt_thread_cond_t bdb_tool_index_cond_main;
81 static ldap_pvt_thread_cond_t bdb_tool_index_cond_work;
83 #if DB_VERSION_FULL >= 0x04060000
86 /* Seems to slow things down too much in BDB 4.5 */
91 static ldap_pvt_thread_mutex_t bdb_tool_trickle_mutex;
92 static ldap_pvt_thread_cond_t bdb_tool_trickle_cond;
93 static ldap_pvt_thread_cond_t bdb_tool_trickle_cond_end;
95 static void * bdb_tool_trickle_task( void *ctx, void *ptr );
96 static int bdb_tool_trickle_active;
99 static void * bdb_tool_index_task( void *ctx, void *ptr );
102 bdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
104 static int bdb_tool_threads;
106 int bdb_tool_entry_open(
107 BackendDB *be, int mode )
109 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
111 /* initialize key and data thangs */
114 key.flags = DB_DBT_USERMEM;
116 key.size = key.ulen = sizeof( nid );
117 data.flags = DB_DBT_USERMEM;
119 if (cursor == NULL) {
120 int rc = bdb->bi_id2entry->bdi_db->cursor(
121 bdb->bi_id2entry->bdi_db, bdb->bi_cache.c_txn, &cursor,
122 bdb->bi_db_opflags );
128 /* Set up for threaded slapindex */
129 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
130 if ( !bdb_tool_info ) {
132 ldap_pvt_thread_mutex_init( &bdb_tool_trickle_mutex );
133 ldap_pvt_thread_cond_init( &bdb_tool_trickle_cond );
134 ldap_pvt_thread_cond_init( &bdb_tool_trickle_cond_end );
135 ldap_pvt_thread_pool_submit( &connection_pool, bdb_tool_trickle_task, bdb->bi_dbenv );
138 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
139 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
140 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
141 if ( bdb->bi_nattrs ) {
143 bdb_tool_threads = slap_tool_thread_max - 1;
144 if ( bdb_tool_threads > 1 ) {
145 bdb_tool_index_threads = ch_malloc( bdb_tool_threads * sizeof( int ));
146 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
147 bdb_tool_index_tcount = bdb_tool_threads - 1;
148 for (i=1; i<bdb_tool_threads; i++) {
149 int *ptr = ch_malloc( sizeof( int ));
151 ldap_pvt_thread_pool_submit( &connection_pool,
152 bdb_tool_index_task, ptr );
163 int bdb_tool_entry_close(
166 if ( bdb_tool_info ) {
169 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
171 /* trickle thread may not have started yet */
172 while ( !bdb_tool_trickle_active )
173 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond_end,
174 &bdb_tool_trickle_mutex );
176 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
177 while ( bdb_tool_trickle_active )
178 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond_end,
179 &bdb_tool_trickle_mutex );
180 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
182 if ( bdb_tool_threads > 1 ) {
183 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
185 /* There might still be some threads starting */
186 while ( bdb_tool_index_tcount > 0 ) {
187 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
188 &bdb_tool_index_mutex );
191 bdb_tool_index_tcount = bdb_tool_threads - 1;
192 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
194 /* Make sure all threads are stopped */
195 while ( bdb_tool_index_tcount > 0 ) {
196 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
197 &bdb_tool_index_mutex );
199 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
201 ch_free( bdb_tool_index_threads );
202 ch_free( bdb_tool_index_rec );
203 bdb_tool_index_tcount = bdb_tool_threads - 1;
205 bdb_tool_info = NULL;
210 ch_free( eh.bv.bv_val );
215 cursor->c_close( cursor );
219 #ifdef BDB_TOOL_IDL_CACHING
220 bdb_tool_idl_flush( be );
225 fprintf( stderr, "Error, entries missing!\n");
226 for (i=0; i<nholes; i++) {
227 fprintf(stderr, " entry %ld: %s\n",
228 holes[i].id, holes[i].dn.bv_val);
237 bdb_tool_entry_first_x(
247 return bdb_tool_entry_next( be );
250 ID bdb_tool_entry_next(
255 struct bdb_info *bdb;
257 assert( be != NULL );
258 assert( slapMode & SLAP_TOOL_MODE );
260 bdb = (struct bdb_info *) be->be_private;
261 assert( bdb != NULL );
265 data.ulen = data.dlen = sizeof( ehbuf );
267 data.flags |= DB_DBT_PARTIAL;
268 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
271 /* If we're doing linear indexing and there are more attrs to
272 * index, and we're at the end of the database, start over.
274 if ( index_nattrs && rc == DB_NOTFOUND ) {
275 /* optional - do a checkpoint here? */
276 bdb_attr_info_free( bdb->bi_attrs[0] );
277 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
279 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
288 BDB_DISK2ID( key.data, &id );
291 if ( tool_filter || tool_base ) {
292 static Operation op = {0};
293 static Opheader ohdr = {0};
297 op.o_tmpmemctx = NULL;
298 op.o_tmpmfuncs = &ch_mfuncs;
300 if ( tool_next_entry ) {
301 bdb_entry_release( &op, tool_next_entry, 0 );
302 tool_next_entry = NULL;
305 rc = bdb_tool_entry_get_int( be, id, &tool_next_entry );
306 if ( rc == LDAP_NO_SUCH_OBJECT ) {
310 assert( tool_next_entry != NULL );
313 /* TODO: needed until BDB_HIER is handled accordingly
314 * in bdb_tool_entry_get_int() */
315 if ( tool_base && !dnIsSuffixScope( &tool_next_entry->e_nname, tool_base, tool_scope ) )
317 bdb_entry_release( &op, tool_next_entry, 0 );
318 tool_next_entry = NULL;
323 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
325 bdb_entry_release( &op, tool_next_entry, 0 );
326 tool_next_entry = NULL;
334 ID bdb_tool_dn2id_get(
341 EntryInfo *ei = NULL;
344 if ( BER_BVISEMPTY(dn) )
349 op.o_tmpmemctx = NULL;
350 op.o_tmpmfuncs = &ch_mfuncs;
352 rc = bdb_cache_find_ndn( &op, 0, dn, &ei );
353 if ( ei ) bdb_cache_entryinfo_unlock( ei );
354 if ( rc == DB_NOTFOUND )
361 bdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
367 assert( be != NULL );
368 assert( slapMode & SLAP_TOOL_MODE );
370 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
371 *ep = tool_next_entry;
372 tool_next_entry = NULL;
376 if ( id != previd ) {
377 data.ulen = data.dlen = sizeof( ehbuf );
379 data.flags |= DB_DBT_PARTIAL;
381 BDB_ID2DISK( id, &nid );
382 rc = cursor->c_get( cursor, &key, &data, DB_SET );
391 eh.bv.bv_val = ehbuf;
392 eh.bv.bv_len = data.size;
393 rc = entry_header( &eh );
394 eoff = eh.data - eh.bv.bv_val;
402 data.flags &= ~DB_DBT_PARTIAL;
404 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
405 if ( rc != DB_BUFFER_SMALL ) {
410 /* Allocate a block and retrieve the data */
411 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
412 eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
413 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
415 data.ulen = data.size;
417 /* Skip past already parsed nattr/nvals */
420 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
427 /* TODO: handle BDB_HIER accordingly */
428 if ( tool_base != NULL ) {
430 entry_decode_dn( &eh, NULL, &ndn );
432 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
433 return LDAP_NO_SUCH_OBJECT;
438 #ifdef SLAP_ZONE_ALLOC
439 /* FIXME: will add ctx later */
440 rc = entry_decode( &eh, &e, NULL );
442 rc = entry_decode( &eh, &e );
445 if( rc == LDAP_SUCCESS ) {
448 if ( slapMode & SLAP_TOOL_READONLY ) {
449 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
450 EntryInfo *ei = NULL;
456 op.o_tmpmemctx = NULL;
457 op.o_tmpmfuncs = &ch_mfuncs;
459 rc = bdb_cache_find_parent( &op, bdb->bi_cache.c_txn, id, &ei );
460 if ( rc == LDAP_SUCCESS ) {
461 bdb_cache_entryinfo_unlock( ei );
480 bdb_tool_entry_get( BackendDB *be, ID id )
484 (void)bdb_tool_entry_get_int( be, id, &e );
488 static int bdb_tool_next_id(
495 struct berval dn = e->e_name;
496 struct berval ndn = e->e_nname;
497 struct berval pdn, npdn;
498 EntryInfo *ei = NULL, eidummy;
501 if (ndn.bv_len == 0) {
506 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
507 if ( ei ) bdb_cache_entryinfo_unlock( ei );
508 if ( rc == DB_NOTFOUND ) {
509 if ( !be_issuffix( op->o_bd, &ndn ) ) {
511 dnParent( &dn, &pdn );
512 dnParent( &ndn, &npdn );
515 rc = bdb_tool_next_id( op, tid, e, text, 1 );
521 /* If parent didn't exist, it was created just now
522 * and its ID is now in e->e_id. Make sure the current
523 * entry gets added under the new parent ID.
525 if ( eid != e->e_id ) {
526 eidummy.bei_id = e->e_id;
530 rc = bdb_next_id( op->o_bd, &e->e_id );
532 snprintf( text->bv_val, text->bv_len,
533 "next_id failed: %s (%d)",
534 db_strerror(rc), rc );
535 Debug( LDAP_DEBUG_ANY,
536 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
539 rc = bdb_dn2id_add( op, tid, ei, e );
541 snprintf( text->bv_val, text->bv_len,
542 "dn2id_add failed: %s (%d)",
543 db_strerror(rc), rc );
544 Debug( LDAP_DEBUG_ANY,
545 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
547 if ( nholes == nhmax - 1 ) {
548 if ( holes == hbuf ) {
549 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
550 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
552 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
556 ber_dupbv( &holes[nholes].dn, &ndn );
557 holes[nholes++].id = e->e_id;
559 } else if ( !hole ) {
562 e->e_id = ei->bei_id;
564 for ( i=0; i<nholes; i++) {
565 if ( holes[i].id == e->e_id ) {
566 free(holes[i].dn.bv_val);
567 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
571 } else if ( holes[i].id > e->e_id ) {
585 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
587 if ( !bdb->bi_nattrs )
590 if ( bdb_tool_threads > 1 ) {
595 ir = bdb_tool_index_rec;
596 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
598 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
599 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
600 &a->a_desc->ad_tags, ir );
604 bdb_tool_ix_id = e->e_id;
606 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
607 /* Wait for all threads to be ready */
608 while ( bdb_tool_index_tcount > 0 ) {
609 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
610 &bdb_tool_index_mutex );
612 for ( i=1; i<bdb_tool_threads; i++ )
613 bdb_tool_index_threads[i] = LDAP_BUSY;
614 bdb_tool_index_tcount = bdb_tool_threads - 1;
615 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
616 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
617 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
620 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
621 for ( i=1; i<bdb_tool_threads; i++ ) {
622 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
623 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
624 &bdb_tool_index_mutex );
628 if ( bdb_tool_index_threads[i] ) {
629 rc = bdb_tool_index_threads[i];
633 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
636 return bdb_index_entry_add( op, txn, e );
640 ID bdb_tool_entry_put(
643 struct berval *text )
646 struct bdb_info *bdb;
651 assert( be != NULL );
652 assert( slapMode & SLAP_TOOL_MODE );
654 assert( text != NULL );
655 assert( text->bv_val != NULL );
656 assert( text->bv_val[0] == '\0' ); /* overconservative? */
658 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
659 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
661 bdb = (struct bdb_info *) be->be_private;
663 if (! (slapMode & SLAP_TOOL_QUICK)) {
664 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
665 bdb->bi_db_opflags );
667 snprintf( text->bv_val, text->bv_len,
668 "txn_begin failed: %s (%d)",
669 db_strerror(rc), rc );
670 Debug( LDAP_DEBUG_ANY,
671 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
672 text->bv_val, 0, 0 );
675 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_tool_entry_put) ": txn id: %x\n",
676 tid->id(tid), 0, 0 );
681 op.o_tmpmemctx = NULL;
682 op.o_tmpmfuncs = &ch_mfuncs;
684 /* add dn2id indices */
685 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
691 if (( slapMode & SLAP_TOOL_QUICK ) && (( e->e_id & 0xfff ) == 0xfff )) {
692 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
696 if ( !bdb->bi_linear_index )
697 rc = bdb_tool_index_add( &op, tid, e );
699 snprintf( text->bv_val, text->bv_len,
700 "index_entry_add failed: %s (%d)",
701 rc == LDAP_OTHER ? "Internal error" :
702 db_strerror(rc), rc );
703 Debug( LDAP_DEBUG_ANY,
704 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
705 text->bv_val, 0, 0 );
710 rc = bdb_id2entry_add( be, tid, e );
712 snprintf( text->bv_val, text->bv_len,
713 "id2entry_add failed: %s (%d)",
714 db_strerror(rc), rc );
715 Debug( LDAP_DEBUG_ANY,
716 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
717 text->bv_val, 0, 0 );
723 if ( !( slapMode & SLAP_TOOL_QUICK )) {
724 rc = TXN_COMMIT( tid, 0 );
726 snprintf( text->bv_val, text->bv_len,
727 "txn_commit failed: %s (%d)",
728 db_strerror(rc), rc );
729 Debug( LDAP_DEBUG_ANY,
730 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
731 text->bv_val, 0, 0 );
737 if ( !( slapMode & SLAP_TOOL_QUICK )) {
739 snprintf( text->bv_val, text->bv_len,
740 "txn_aborted! %s (%d)",
741 rc == LDAP_OTHER ? "Internal error" :
742 db_strerror(rc), rc );
743 Debug( LDAP_DEBUG_ANY,
744 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
745 text->bv_val, 0, 0 );
753 int bdb_tool_entry_reindex(
756 AttributeDescription **adv )
758 struct bdb_info *bi = (struct bdb_info *) be->be_private;
765 Debug( LDAP_DEBUG_ARGS,
766 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
768 assert( tool_base == NULL );
769 assert( tool_filter == NULL );
771 /* No indexes configured, nothing to do. Could return an
772 * error here to shortcut things.
778 /* Check for explicit list of attrs to index */
782 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
784 for ( n = 0; adv[n]; n++ ) ;
787 for ( i = 0; i < n; i++ ) {
788 AttributeDescription *ad = adv[i];
789 for ( j = i-1; j>=0; j--) {
790 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
797 for ( i = 0; adv[i]; i++ ) {
798 if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
799 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
800 if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
801 AttrInfo *ai = bi->bi_attrs[i];
802 bi->bi_attrs[i] = bi->bi_attrs[j];
803 bi->bi_attrs[j] = ai;
807 if ( j == bi->bi_nattrs ) {
808 Debug( LDAP_DEBUG_ANY,
809 LDAP_XSTRING(bdb_tool_entry_reindex)
810 ": no index configured for %s\n",
811 adv[i]->ad_cname.bv_val, 0, 0 );
819 /* Get the first attribute to index */
820 if (bi->bi_linear_index && !index_nattrs) {
821 index_nattrs = bi->bi_nattrs - 1;
825 e = bdb_tool_entry_get( be, id );
828 Debug( LDAP_DEBUG_ANY,
829 LDAP_XSTRING(bdb_tool_entry_reindex)
830 ": could not locate id=%ld\n",
835 if (! (slapMode & SLAP_TOOL_QUICK)) {
836 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
838 Debug( LDAP_DEBUG_ANY,
839 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
840 "txn_begin failed: %s (%d)\n",
841 db_strerror(rc), rc, 0 );
844 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_tool_entry_reindex) ": txn id: %x\n",
845 tid->id(tid), 0, 0 );
849 * just (re)add them for now
850 * assume that some other routine (not yet implemented)
851 * will zap index databases
855 Debug( LDAP_DEBUG_TRACE,
856 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
857 (long) id, e->e_dn, 0 );
861 op.o_tmpmemctx = NULL;
862 op.o_tmpmfuncs = &ch_mfuncs;
864 rc = bdb_tool_index_add( &op, tid, e );
868 if (! (slapMode & SLAP_TOOL_QUICK)) {
869 rc = TXN_COMMIT( tid, 0 );
871 Debug( LDAP_DEBUG_ANY,
872 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
873 ": txn_commit failed: %s (%d)\n",
874 db_strerror(rc), rc, 0 );
880 if (! (slapMode & SLAP_TOOL_QUICK)) {
882 Debug( LDAP_DEBUG_ANY,
883 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
884 ": txn_aborted! %s (%d)\n",
885 db_strerror(rc), rc, 0 );
889 bdb_entry_release( &op, e, 0 );
894 ID bdb_tool_entry_modify(
897 struct berval *text )
900 struct bdb_info *bdb;
905 assert( be != NULL );
906 assert( slapMode & SLAP_TOOL_MODE );
908 assert( text != NULL );
909 assert( text->bv_val != NULL );
910 assert( text->bv_val[0] == '\0' ); /* overconservative? */
912 assert ( e->e_id != NOID );
914 Debug( LDAP_DEBUG_TRACE,
915 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
916 (long) e->e_id, e->e_dn, 0 );
918 bdb = (struct bdb_info *) be->be_private;
920 if (! (slapMode & SLAP_TOOL_QUICK)) {
922 cursor->c_close( cursor );
925 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
926 bdb->bi_db_opflags );
928 snprintf( text->bv_val, text->bv_len,
929 "txn_begin failed: %s (%d)",
930 db_strerror(rc), rc );
931 Debug( LDAP_DEBUG_ANY,
932 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
933 text->bv_val, 0, 0 );
936 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_tool_entry_modify) ": txn id: %x\n",
937 tid->id(tid), 0, 0 );
942 op.o_tmpmemctx = NULL;
943 op.o_tmpmfuncs = &ch_mfuncs;
946 rc = bdb_id2entry_update( be, tid, e );
948 snprintf( text->bv_val, text->bv_len,
949 "id2entry_add failed: %s (%d)",
950 db_strerror(rc), rc );
951 Debug( LDAP_DEBUG_ANY,
952 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
953 text->bv_val, 0, 0 );
959 if (! (slapMode & SLAP_TOOL_QUICK)) {
960 rc = TXN_COMMIT( tid, 0 );
962 snprintf( text->bv_val, text->bv_len,
963 "txn_commit failed: %s (%d)",
964 db_strerror(rc), rc );
965 Debug( LDAP_DEBUG_ANY,
966 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
967 "%s\n", text->bv_val, 0, 0 );
973 if (! (slapMode & SLAP_TOOL_QUICK)) {
975 snprintf( text->bv_val, text->bv_len,
976 "txn_aborted! %s (%d)",
977 db_strerror(rc), rc );
978 Debug( LDAP_DEBUG_ANY,
979 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
980 text->bv_val, 0, 0 );
988 #ifdef BDB_TOOL_IDL_CACHING
990 bdb_tool_idl_cmp( const void *v1, const void *v2 )
992 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
995 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
996 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
1000 bdb_tool_idl_flush_one( void *v1, void *arg )
1002 bdb_tool_idl_cache *ic = v1;
1004 struct bdb_info *bdb = bdb_tool_info;
1005 bdb_tool_idl_cache_entry *ice;
1011 /* Freshly allocated, ignore it */
1012 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
1016 rc = db->cursor( db, NULL, &curs, 0 );
1023 bv2DBT( &ic->kstr, &key );
1025 data.size = data.ulen = sizeof( ID );
1026 data.flags = DB_DBT_USERMEM;
1029 rc = curs->c_get( curs, &key, &data, DB_SET );
1030 /* If key already exists and we're writing a range... */
1031 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
1032 /* If it's not currently a range, must delete old info */
1035 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
1036 curs->c_del( curs, 0 );
1039 /* Store range marker */
1040 curs->c_put( curs, &key, &data, DB_KEYFIRST );
1044 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
1047 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
1050 curs->c_del( curs, 0 );
1052 BDB_ID2DISK( ic->last, &nid );
1053 curs->c_put( curs, &key, &data, DB_KEYLAST );
1055 } else if ( rc && rc != DB_NOTFOUND ) {
1057 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
1058 /* range, didn't exist before */
1060 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1062 BDB_ID2DISK( ic->first, &nid );
1063 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1065 BDB_ID2DISK( ic->last, &nid );
1066 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1075 /* Just a normal write */
1077 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1082 end = ic->count & (IDBLOCK-1);
1086 for ( i=0; i<end; i++ ) {
1087 if ( !ice->ids[i] ) continue;
1088 BDB_ID2DISK( ice->ids[i], &nid );
1089 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
1091 if ( rc == DB_KEYEXIST ) {
1105 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1106 ic->tail->next = bdb_tool_idl_free_list;
1107 bdb_tool_idl_free_list = ic->head;
1108 bdb->bi_idl_cache_size -= n;
1109 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1112 if ( ic != db->app_private ) {
1115 ic->head = ic->tail = NULL;
1117 curs->c_close( curs );
1122 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
1124 Avlnode *root = db->app_private;
1127 db->app_private = ic;
1128 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
1129 avl_free( root, NULL );
1130 db->app_private = NULL;
1137 bdb_tool_idl_flush( BackendDB *be )
1139 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1144 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
1145 db = bdb->bi_databases[i]->bdi_db;
1146 if ( !db->app_private ) continue;
1147 rc = bdb_tool_idl_flush_db( db, NULL );
1152 bdb->bi_idl_cache_size = 0;
1157 int bdb_tool_idl_add(
1164 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1165 bdb_tool_idl_cache *ic, itmp;
1166 bdb_tool_idl_cache_entry *ice;
1169 if ( !bdb->bi_idl_cache_max_size )
1170 return bdb_idl_insert_key( be, db, txn, key, id );
1172 DBT2bv( key, &itmp.kstr );
1174 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
1176 /* No entry yet, create one */
1183 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1184 ic->kstr.bv_len = itmp.kstr.bv_len;
1185 ic->kstr.bv_val = (char *)(ic+1);
1186 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1187 ic->head = ic->tail = NULL;
1190 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1193 /* load existing key count here */
1194 rc = db->cursor( db, NULL, &curs, 0 );
1195 if ( rc ) return rc;
1197 data.ulen = sizeof( ID );
1198 data.flags = DB_DBT_USERMEM;
1200 rc = curs->c_get( curs, key, &data, DB_SET );
1203 ic->count = BDB_IDL_DB_SIZE+1;
1207 curs->c_count( curs, &count, 0 );
1209 BDB_DISK2ID( &nid, &ic->first );
1212 curs->c_close( curs );
1214 /* are we a range already? */
1215 if ( ic->count > BDB_IDL_DB_SIZE ) {
1218 /* Are we at the limit, and converting to a range? */
1219 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1221 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1224 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1225 ic->tail->next = bdb_tool_idl_free_list;
1226 bdb_tool_idl_free_list = ic->head;
1227 bdb->bi_idl_cache_size -= n;
1228 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1230 ic->head = ic->tail = NULL;
1235 /* No free block, create that too */
1236 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1238 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1239 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1240 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1241 rc = bdb_tool_idl_flush_db( db, ic );
1244 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1246 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1248 bdb->bi_idl_cache_size++;
1249 if ( bdb_tool_idl_free_list ) {
1250 ice = bdb_tool_idl_free_list;
1251 bdb_tool_idl_free_list = ice->next;
1253 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1255 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1257 memset( ice, 0, sizeof( *ice ));
1261 ic->tail->next = ice;
1268 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1277 bdb_tool_trickle_task( void *ctx, void *ptr )
1282 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1283 bdb_tool_trickle_active = 1;
1284 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond_end );
1286 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1287 &bdb_tool_trickle_mutex );
1288 if ( slapd_shutdown )
1290 env->memp_trickle( env, 30, &wrote );
1292 bdb_tool_trickle_active = 0;
1293 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond_end );
1294 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1301 bdb_tool_index_task( void *ctx, void *ptr )
1303 int base = *(int *)ptr;
1307 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1308 bdb_tool_index_tcount--;
1309 if ( !bdb_tool_index_tcount )
1310 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1311 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1312 &bdb_tool_index_mutex );
1313 if ( slapd_shutdown ) {
1314 bdb_tool_index_tcount--;
1315 if ( !bdb_tool_index_tcount )
1316 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1317 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1320 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1322 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1323 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );