1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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 int bdb_tool_entry_open(
105 BackendDB *be, int mode )
107 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
109 /* initialize key and data thangs */
112 key.flags = DB_DBT_USERMEM;
114 key.size = key.ulen = sizeof( nid );
115 data.flags = DB_DBT_USERMEM;
117 if (cursor == NULL) {
118 int rc = bdb->bi_id2entry->bdi_db->cursor(
119 bdb->bi_id2entry->bdi_db, bdb->bi_cache.c_txn, &cursor,
120 bdb->bi_db_opflags );
126 /* Set up for threaded slapindex */
127 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
128 if ( !bdb_tool_info ) {
130 ldap_pvt_thread_mutex_init( &bdb_tool_trickle_mutex );
131 ldap_pvt_thread_cond_init( &bdb_tool_trickle_cond );
132 ldap_pvt_thread_cond_init( &bdb_tool_trickle_cond_end );
133 ldap_pvt_thread_pool_submit( &connection_pool, bdb_tool_trickle_task, bdb->bi_dbenv );
136 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
137 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
138 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
139 if ( bdb->bi_nattrs ) {
141 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
142 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
143 bdb_tool_index_tcount = slap_tool_thread_max - 1;
144 for (i=1; i<slap_tool_thread_max; i++) {
145 int *ptr = ch_malloc( sizeof( int ));
147 ldap_pvt_thread_pool_submit( &connection_pool,
148 bdb_tool_index_task, ptr );
158 int bdb_tool_entry_close(
161 if ( bdb_tool_info ) {
164 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
166 /* trickle thread may not have started yet */
167 while ( !bdb_tool_trickle_active )
168 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond_end,
169 &bdb_tool_trickle_mutex );
171 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
172 while ( bdb_tool_trickle_active )
173 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond_end,
174 &bdb_tool_trickle_mutex );
175 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
177 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
179 /* There might still be some threads starting */
180 while ( bdb_tool_index_tcount ) {
181 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
182 &bdb_tool_index_mutex );
185 bdb_tool_index_tcount = slap_tool_thread_max - 1;
186 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
188 /* Make sure all threads are stopped */
189 while ( bdb_tool_index_tcount ) {
190 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
191 &bdb_tool_index_mutex );
193 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
195 bdb_tool_info = NULL;
197 ch_free( bdb_tool_index_threads );
198 ch_free( bdb_tool_index_rec );
199 bdb_tool_index_tcount = slap_tool_thread_max - 1;
203 ch_free( eh.bv.bv_val );
208 cursor->c_close( cursor );
212 #ifdef BDB_TOOL_IDL_CACHING
213 bdb_tool_idl_flush( be );
218 fprintf( stderr, "Error, entries missing!\n");
219 for (i=0; i<nholes; i++) {
220 fprintf(stderr, " entry %ld: %s\n",
221 holes[i].id, holes[i].dn.bv_val);
230 bdb_tool_entry_first_x(
240 return bdb_tool_entry_next( be );
243 ID bdb_tool_entry_next(
248 struct bdb_info *bdb;
250 assert( be != NULL );
251 assert( slapMode & SLAP_TOOL_MODE );
253 bdb = (struct bdb_info *) be->be_private;
254 assert( bdb != NULL );
258 data.ulen = data.dlen = sizeof( ehbuf );
260 data.flags |= DB_DBT_PARTIAL;
261 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
264 /* If we're doing linear indexing and there are more attrs to
265 * index, and we're at the end of the database, start over.
267 if ( index_nattrs && rc == DB_NOTFOUND ) {
268 /* optional - do a checkpoint here? */
269 bdb_attr_info_free( bdb->bi_attrs[0] );
270 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
272 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
281 BDB_DISK2ID( key.data, &id );
284 if ( tool_filter || tool_base ) {
285 static Operation op = {0};
286 static Opheader ohdr = {0};
290 op.o_tmpmemctx = NULL;
291 op.o_tmpmfuncs = &ch_mfuncs;
293 if ( tool_next_entry ) {
294 bdb_entry_release( &op, tool_next_entry, 0 );
295 tool_next_entry = NULL;
298 rc = bdb_tool_entry_get_int( be, id, &tool_next_entry );
299 if ( rc == LDAP_NO_SUCH_OBJECT ) {
303 assert( tool_next_entry != NULL );
306 /* TODO: needed until BDB_HIER is handled accordingly
307 * in bdb_tool_entry_get_int() */
308 if ( tool_base && !dnIsSuffixScope( &tool_next_entry->e_nname, tool_base, tool_scope ) )
310 bdb_entry_release( &op, tool_next_entry, 0 );
311 tool_next_entry = NULL;
316 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
318 bdb_entry_release( &op, tool_next_entry, 0 );
319 tool_next_entry = NULL;
327 ID bdb_tool_dn2id_get(
334 EntryInfo *ei = NULL;
337 if ( BER_BVISEMPTY(dn) )
342 op.o_tmpmemctx = NULL;
343 op.o_tmpmfuncs = &ch_mfuncs;
345 rc = bdb_cache_find_ndn( &op, 0, dn, &ei );
346 if ( ei ) bdb_cache_entryinfo_unlock( ei );
347 if ( rc == DB_NOTFOUND )
354 bdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
360 assert( be != NULL );
361 assert( slapMode & SLAP_TOOL_MODE );
363 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
364 *ep = tool_next_entry;
365 tool_next_entry = NULL;
369 if ( id != previd ) {
370 data.ulen = data.dlen = sizeof( ehbuf );
372 data.flags |= DB_DBT_PARTIAL;
374 BDB_ID2DISK( id, &nid );
375 rc = cursor->c_get( cursor, &key, &data, DB_SET );
384 eh.bv.bv_val = ehbuf;
385 eh.bv.bv_len = data.size;
386 rc = entry_header( &eh );
387 eoff = eh.data - eh.bv.bv_val;
395 data.flags &= ~DB_DBT_PARTIAL;
397 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
398 if ( rc != DB_BUFFER_SMALL ) {
403 /* Allocate a block and retrieve the data */
404 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
405 eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
406 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
408 data.ulen = data.size;
410 /* Skip past already parsed nattr/nvals */
413 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
420 /* TODO: handle BDB_HIER accordingly */
421 if ( tool_base != NULL ) {
423 entry_decode_dn( &eh, NULL, &ndn );
425 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
426 return LDAP_NO_SUCH_OBJECT;
431 #ifdef SLAP_ZONE_ALLOC
432 /* FIXME: will add ctx later */
433 rc = entry_decode( &eh, &e, NULL );
435 rc = entry_decode( &eh, &e );
438 if( rc == LDAP_SUCCESS ) {
441 if ( slapMode & SLAP_TOOL_READONLY ) {
442 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
443 EntryInfo *ei = NULL;
449 op.o_tmpmemctx = NULL;
450 op.o_tmpmfuncs = &ch_mfuncs;
452 rc = bdb_cache_find_parent( &op, bdb->bi_cache.c_txn, id, &ei );
453 if ( rc == LDAP_SUCCESS ) {
454 bdb_cache_entryinfo_unlock( ei );
473 bdb_tool_entry_get( BackendDB *be, ID id )
477 (void)bdb_tool_entry_get_int( be, id, &e );
481 static int bdb_tool_next_id(
488 struct berval dn = e->e_name;
489 struct berval ndn = e->e_nname;
490 struct berval pdn, npdn;
491 EntryInfo *ei = NULL, eidummy;
494 if (ndn.bv_len == 0) {
499 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
500 if ( ei ) bdb_cache_entryinfo_unlock( ei );
501 if ( rc == DB_NOTFOUND ) {
502 if ( !be_issuffix( op->o_bd, &ndn ) ) {
504 dnParent( &dn, &pdn );
505 dnParent( &ndn, &npdn );
508 rc = bdb_tool_next_id( op, tid, e, text, 1 );
514 /* If parent didn't exist, it was created just now
515 * and its ID is now in e->e_id. Make sure the current
516 * entry gets added under the new parent ID.
518 if ( eid != e->e_id ) {
519 eidummy.bei_id = e->e_id;
523 rc = bdb_next_id( op->o_bd, &e->e_id );
525 snprintf( text->bv_val, text->bv_len,
526 "next_id failed: %s (%d)",
527 db_strerror(rc), rc );
528 Debug( LDAP_DEBUG_ANY,
529 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
532 rc = bdb_dn2id_add( op, tid, ei, e );
534 snprintf( text->bv_val, text->bv_len,
535 "dn2id_add failed: %s (%d)",
536 db_strerror(rc), rc );
537 Debug( LDAP_DEBUG_ANY,
538 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
540 if ( nholes == nhmax - 1 ) {
541 if ( holes == hbuf ) {
542 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
543 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
545 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
549 ber_dupbv( &holes[nholes].dn, &ndn );
550 holes[nholes++].id = e->e_id;
552 } else if ( !hole ) {
555 e->e_id = ei->bei_id;
557 for ( i=0; i<nholes; i++) {
558 if ( holes[i].id == e->e_id ) {
559 free(holes[i].dn.bv_val);
560 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
564 } else if ( holes[i].id > e->e_id ) {
578 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
580 if ( !bdb->bi_nattrs )
583 if ( slapMode & SLAP_TOOL_QUICK ) {
588 ir = bdb_tool_index_rec;
589 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
591 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
592 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
593 &a->a_desc->ad_tags, ir );
597 bdb_tool_ix_id = e->e_id;
599 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
600 /* Wait for all threads to be ready */
601 while ( bdb_tool_index_tcount ) {
602 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
603 &bdb_tool_index_mutex );
605 for ( i=1; i<slap_tool_thread_max; i++ )
606 bdb_tool_index_threads[i] = LDAP_BUSY;
607 bdb_tool_index_tcount = slap_tool_thread_max - 1;
608 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
609 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
610 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
613 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
614 for ( i=1; i<slap_tool_thread_max; i++ ) {
615 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
616 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
617 &bdb_tool_index_mutex );
621 if ( bdb_tool_index_threads[i] ) {
622 rc = bdb_tool_index_threads[i];
626 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
629 return bdb_index_entry_add( op, txn, e );
633 ID bdb_tool_entry_put(
636 struct berval *text )
639 struct bdb_info *bdb;
644 assert( be != NULL );
645 assert( slapMode & SLAP_TOOL_MODE );
647 assert( text != NULL );
648 assert( text->bv_val != NULL );
649 assert( text->bv_val[0] == '\0' ); /* overconservative? */
651 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
652 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
654 bdb = (struct bdb_info *) be->be_private;
656 if (! (slapMode & SLAP_TOOL_QUICK)) {
657 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
658 bdb->bi_db_opflags );
660 snprintf( text->bv_val, text->bv_len,
661 "txn_begin failed: %s (%d)",
662 db_strerror(rc), rc );
663 Debug( LDAP_DEBUG_ANY,
664 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
665 text->bv_val, 0, 0 );
672 op.o_tmpmemctx = NULL;
673 op.o_tmpmfuncs = &ch_mfuncs;
675 /* add dn2id indices */
676 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
682 if (( slapMode & SLAP_TOOL_QUICK ) && (( e->e_id & 0xfff ) == 0xfff )) {
683 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
687 if ( !bdb->bi_linear_index )
688 rc = bdb_tool_index_add( &op, tid, e );
690 snprintf( text->bv_val, text->bv_len,
691 "index_entry_add failed: %s (%d)",
692 rc == LDAP_OTHER ? "Internal error" :
693 db_strerror(rc), rc );
694 Debug( LDAP_DEBUG_ANY,
695 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
696 text->bv_val, 0, 0 );
701 rc = bdb_id2entry_add( be, tid, e );
703 snprintf( text->bv_val, text->bv_len,
704 "id2entry_add failed: %s (%d)",
705 db_strerror(rc), rc );
706 Debug( LDAP_DEBUG_ANY,
707 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
708 text->bv_val, 0, 0 );
714 if ( !( slapMode & SLAP_TOOL_QUICK )) {
715 rc = TXN_COMMIT( tid, 0 );
717 snprintf( text->bv_val, text->bv_len,
718 "txn_commit failed: %s (%d)",
719 db_strerror(rc), rc );
720 Debug( LDAP_DEBUG_ANY,
721 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
722 text->bv_val, 0, 0 );
728 if ( !( slapMode & SLAP_TOOL_QUICK )) {
730 snprintf( text->bv_val, text->bv_len,
731 "txn_aborted! %s (%d)",
732 rc == LDAP_OTHER ? "Internal error" :
733 db_strerror(rc), rc );
734 Debug( LDAP_DEBUG_ANY,
735 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
736 text->bv_val, 0, 0 );
744 int bdb_tool_entry_reindex(
747 AttributeDescription **adv )
749 struct bdb_info *bi = (struct bdb_info *) be->be_private;
756 Debug( LDAP_DEBUG_ARGS,
757 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
759 assert( tool_base == NULL );
760 assert( tool_filter == NULL );
762 /* No indexes configured, nothing to do. Could return an
763 * error here to shortcut things.
769 /* Check for explicit list of attrs to index */
773 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
775 for ( n = 0; adv[n]; n++ ) ;
778 for ( i = 0; i < n; i++ ) {
779 AttributeDescription *ad = adv[i];
780 for ( j = i-1; j>=0; j--) {
781 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
788 for ( i = 0; adv[i]; i++ ) {
789 if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
790 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
791 if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
792 AttrInfo *ai = bi->bi_attrs[i];
793 bi->bi_attrs[i] = bi->bi_attrs[j];
794 bi->bi_attrs[j] = ai;
798 if ( j == bi->bi_nattrs ) {
799 Debug( LDAP_DEBUG_ANY,
800 LDAP_XSTRING(bdb_tool_entry_reindex)
801 ": no index configured for %s\n",
802 adv[i]->ad_cname.bv_val, 0, 0 );
810 /* Get the first attribute to index */
811 if (bi->bi_linear_index && !index_nattrs) {
812 index_nattrs = bi->bi_nattrs - 1;
816 e = bdb_tool_entry_get( be, id );
819 Debug( LDAP_DEBUG_ANY,
820 LDAP_XSTRING(bdb_tool_entry_reindex)
821 ": could not locate id=%ld\n",
826 if (! (slapMode & SLAP_TOOL_QUICK)) {
827 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
829 Debug( LDAP_DEBUG_ANY,
830 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
831 "txn_begin failed: %s (%d)\n",
832 db_strerror(rc), rc, 0 );
838 * just (re)add them for now
839 * assume that some other routine (not yet implemented)
840 * will zap index databases
844 Debug( LDAP_DEBUG_TRACE,
845 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
846 (long) id, e->e_dn, 0 );
850 op.o_tmpmemctx = NULL;
851 op.o_tmpmfuncs = &ch_mfuncs;
853 rc = bdb_tool_index_add( &op, tid, e );
857 if (! (slapMode & SLAP_TOOL_QUICK)) {
858 rc = TXN_COMMIT( tid, 0 );
860 Debug( LDAP_DEBUG_ANY,
861 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
862 ": txn_commit failed: %s (%d)\n",
863 db_strerror(rc), rc, 0 );
869 if (! (slapMode & SLAP_TOOL_QUICK)) {
871 Debug( LDAP_DEBUG_ANY,
872 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
873 ": txn_aborted! %s (%d)\n",
874 db_strerror(rc), rc, 0 );
878 bdb_entry_release( &op, e, 0 );
883 ID bdb_tool_entry_modify(
886 struct berval *text )
889 struct bdb_info *bdb;
894 assert( be != NULL );
895 assert( slapMode & SLAP_TOOL_MODE );
897 assert( text != NULL );
898 assert( text->bv_val != NULL );
899 assert( text->bv_val[0] == '\0' ); /* overconservative? */
901 assert ( e->e_id != NOID );
903 Debug( LDAP_DEBUG_TRACE,
904 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
905 (long) e->e_id, e->e_dn, 0 );
907 bdb = (struct bdb_info *) be->be_private;
909 if (! (slapMode & SLAP_TOOL_QUICK)) {
911 cursor->c_close( cursor );
914 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
915 bdb->bi_db_opflags );
917 snprintf( text->bv_val, text->bv_len,
918 "txn_begin failed: %s (%d)",
919 db_strerror(rc), rc );
920 Debug( LDAP_DEBUG_ANY,
921 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
922 text->bv_val, 0, 0 );
929 op.o_tmpmemctx = NULL;
930 op.o_tmpmfuncs = &ch_mfuncs;
933 rc = bdb_id2entry_update( be, tid, e );
935 snprintf( text->bv_val, text->bv_len,
936 "id2entry_add failed: %s (%d)",
937 db_strerror(rc), rc );
938 Debug( LDAP_DEBUG_ANY,
939 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
940 text->bv_val, 0, 0 );
946 if (! (slapMode & SLAP_TOOL_QUICK)) {
947 rc = TXN_COMMIT( tid, 0 );
949 snprintf( text->bv_val, text->bv_len,
950 "txn_commit failed: %s (%d)",
951 db_strerror(rc), rc );
952 Debug( LDAP_DEBUG_ANY,
953 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
954 "%s\n", text->bv_val, 0, 0 );
960 if (! (slapMode & SLAP_TOOL_QUICK)) {
962 snprintf( text->bv_val, text->bv_len,
963 "txn_aborted! %s (%d)",
964 db_strerror(rc), rc );
965 Debug( LDAP_DEBUG_ANY,
966 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
967 text->bv_val, 0, 0 );
975 #ifdef BDB_TOOL_IDL_CACHING
977 bdb_tool_idl_cmp( const void *v1, const void *v2 )
979 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
982 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
983 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
987 bdb_tool_idl_flush_one( void *v1, void *arg )
989 bdb_tool_idl_cache *ic = v1;
991 struct bdb_info *bdb = bdb_tool_info;
992 bdb_tool_idl_cache_entry *ice;
998 /* Freshly allocated, ignore it */
999 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
1003 rc = db->cursor( db, NULL, &curs, 0 );
1010 bv2DBT( &ic->kstr, &key );
1012 data.size = data.ulen = sizeof( ID );
1013 data.flags = DB_DBT_USERMEM;
1016 rc = curs->c_get( curs, &key, &data, DB_SET );
1017 /* If key already exists and we're writing a range... */
1018 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
1019 /* If it's not currently a range, must delete old info */
1022 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
1023 curs->c_del( curs, 0 );
1026 /* Store range marker */
1027 curs->c_put( curs, &key, &data, DB_KEYFIRST );
1031 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
1034 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
1037 curs->c_del( curs, 0 );
1039 BDB_ID2DISK( ic->last, &nid );
1040 curs->c_put( curs, &key, &data, DB_KEYLAST );
1042 } else if ( rc && rc != DB_NOTFOUND ) {
1044 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
1045 /* range, didn't exist before */
1047 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1049 BDB_ID2DISK( ic->first, &nid );
1050 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1052 BDB_ID2DISK( ic->last, &nid );
1053 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1062 /* Just a normal write */
1064 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1069 end = ic->count & (IDBLOCK-1);
1073 for ( i=0; i<end; i++ ) {
1074 if ( !ice->ids[i] ) continue;
1075 BDB_ID2DISK( ice->ids[i], &nid );
1076 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
1078 if ( rc == DB_KEYEXIST ) {
1092 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1093 ic->tail->next = bdb_tool_idl_free_list;
1094 bdb_tool_idl_free_list = ic->head;
1095 bdb->bi_idl_cache_size -= n;
1096 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1099 if ( ic != db->app_private ) {
1102 ic->head = ic->tail = NULL;
1104 curs->c_close( curs );
1109 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
1111 Avlnode *root = db->app_private;
1114 db->app_private = ic;
1115 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
1116 avl_free( root, NULL );
1117 db->app_private = NULL;
1124 bdb_tool_idl_flush( BackendDB *be )
1126 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1131 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
1132 db = bdb->bi_databases[i]->bdi_db;
1133 if ( !db->app_private ) continue;
1134 rc = bdb_tool_idl_flush_db( db, NULL );
1139 bdb->bi_idl_cache_size = 0;
1144 int bdb_tool_idl_add(
1151 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1152 bdb_tool_idl_cache *ic, itmp;
1153 bdb_tool_idl_cache_entry *ice;
1156 if ( !bdb->bi_idl_cache_max_size )
1157 return bdb_idl_insert_key( be, db, txn, key, id );
1159 DBT2bv( key, &itmp.kstr );
1161 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
1163 /* No entry yet, create one */
1170 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1171 ic->kstr.bv_len = itmp.kstr.bv_len;
1172 ic->kstr.bv_val = (char *)(ic+1);
1173 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1174 ic->head = ic->tail = NULL;
1177 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1180 /* load existing key count here */
1181 rc = db->cursor( db, NULL, &curs, 0 );
1182 if ( rc ) return rc;
1184 data.ulen = sizeof( ID );
1185 data.flags = DB_DBT_USERMEM;
1187 rc = curs->c_get( curs, key, &data, DB_SET );
1190 ic->count = BDB_IDL_DB_SIZE+1;
1194 curs->c_count( curs, &count, 0 );
1196 BDB_DISK2ID( &nid, &ic->first );
1199 curs->c_close( curs );
1201 /* are we a range already? */
1202 if ( ic->count > BDB_IDL_DB_SIZE ) {
1205 /* Are we at the limit, and converting to a range? */
1206 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1208 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1211 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1212 ic->tail->next = bdb_tool_idl_free_list;
1213 bdb_tool_idl_free_list = ic->head;
1214 bdb->bi_idl_cache_size -= n;
1215 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1217 ic->head = ic->tail = NULL;
1222 /* No free block, create that too */
1223 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1225 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1226 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1227 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1228 rc = bdb_tool_idl_flush_db( db, ic );
1231 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1233 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1235 bdb->bi_idl_cache_size++;
1236 if ( bdb_tool_idl_free_list ) {
1237 ice = bdb_tool_idl_free_list;
1238 bdb_tool_idl_free_list = ice->next;
1240 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1242 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1244 memset( ice, 0, sizeof( *ice ));
1248 ic->tail->next = ice;
1255 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1264 bdb_tool_trickle_task( void *ctx, void *ptr )
1269 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1270 bdb_tool_trickle_active = 1;
1271 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond_end );
1273 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1274 &bdb_tool_trickle_mutex );
1275 if ( slapd_shutdown )
1277 env->memp_trickle( env, 30, &wrote );
1279 bdb_tool_trickle_active = 0;
1280 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond_end );
1281 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1288 bdb_tool_index_task( void *ctx, void *ptr )
1290 int base = *(int *)ptr;
1294 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1295 bdb_tool_index_tcount--;
1296 if ( !bdb_tool_index_tcount )
1297 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1298 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1299 &bdb_tool_index_mutex );
1300 if ( slapd_shutdown ) {
1301 bdb_tool_index_tcount--;
1302 if ( !bdb_tool_index_tcount )
1303 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1304 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1307 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1309 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1310 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );