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 = (struct bdb_info *) be->be_private;
250 assert( be != NULL );
251 assert( slapMode & SLAP_TOOL_MODE );
252 assert( bdb != NULL );
256 data.ulen = data.dlen = sizeof( ehbuf );
258 data.flags |= DB_DBT_PARTIAL;
259 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
262 /* If we're doing linear indexing and there are more attrs to
263 * index, and we're at the end of the database, start over.
265 if ( index_nattrs && rc == DB_NOTFOUND ) {
266 /* optional - do a checkpoint here? */
267 bdb_attr_info_free( bdb->bi_attrs[0] );
268 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
270 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
279 BDB_DISK2ID( key.data, &id );
282 if ( tool_filter || tool_base ) {
283 static Operation op = {0};
284 static Opheader ohdr = {0};
288 op.o_tmpmemctx = NULL;
289 op.o_tmpmfuncs = &ch_mfuncs;
291 if ( tool_next_entry ) {
292 bdb_entry_release( &op, tool_next_entry, 0 );
293 tool_next_entry = NULL;
296 rc = bdb_tool_entry_get_int( be, id, &tool_next_entry );
297 if ( rc == LDAP_NO_SUCH_OBJECT ) {
301 assert( tool_next_entry != NULL );
304 /* TODO: needed until BDB_HIER is handled accordingly
305 * in bdb_tool_entry_get_int() */
306 if ( tool_base && !dnIsSuffixScope( &tool_next_entry->e_nname, tool_base, tool_scope ) )
308 bdb_entry_release( &op, tool_next_entry, 0 );
309 tool_next_entry = NULL;
314 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
316 bdb_entry_release( &op, tool_next_entry, 0 );
317 tool_next_entry = NULL;
325 ID bdb_tool_dn2id_get(
332 EntryInfo *ei = NULL;
335 if ( BER_BVISEMPTY(dn) )
340 op.o_tmpmemctx = NULL;
341 op.o_tmpmfuncs = &ch_mfuncs;
343 rc = bdb_cache_find_ndn( &op, 0, dn, &ei );
344 if ( ei ) bdb_cache_entryinfo_unlock( ei );
345 if ( rc == DB_NOTFOUND )
352 bdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
358 assert( be != NULL );
359 assert( slapMode & SLAP_TOOL_MODE );
361 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
362 *ep = tool_next_entry;
363 tool_next_entry = NULL;
367 if ( id != previd ) {
368 data.ulen = data.dlen = sizeof( ehbuf );
370 data.flags |= DB_DBT_PARTIAL;
372 BDB_ID2DISK( id, &nid );
373 rc = cursor->c_get( cursor, &key, &data, DB_SET );
382 eh.bv.bv_val = ehbuf;
383 eh.bv.bv_len = data.size;
384 rc = entry_header( &eh );
385 eoff = eh.data - eh.bv.bv_val;
393 data.flags &= ~DB_DBT_PARTIAL;
395 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
396 if ( rc != DB_BUFFER_SMALL ) {
401 /* Allocate a block and retrieve the data */
402 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
403 eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
404 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
406 data.ulen = data.size;
408 /* Skip past already parsed nattr/nvals */
411 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
418 /* TODO: handle BDB_HIER accordingly */
419 if ( tool_base != NULL ) {
421 entry_decode_dn( &eh, NULL, &ndn );
423 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
424 return LDAP_NO_SUCH_OBJECT;
429 #ifdef SLAP_ZONE_ALLOC
430 /* FIXME: will add ctx later */
431 rc = entry_decode( &eh, &e, NULL );
433 rc = entry_decode( &eh, &e );
436 if( rc == LDAP_SUCCESS ) {
439 if ( slapMode & SLAP_TOOL_READONLY ) {
440 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
441 EntryInfo *ei = NULL;
447 op.o_tmpmemctx = NULL;
448 op.o_tmpmfuncs = &ch_mfuncs;
450 rc = bdb_cache_find_parent( &op, bdb->bi_cache.c_txn, id, &ei );
451 if ( rc == LDAP_SUCCESS ) {
452 bdb_cache_entryinfo_unlock( ei );
471 bdb_tool_entry_get( BackendDB *be, ID id )
475 (void)bdb_tool_entry_get_int( be, id, &e );
479 static int bdb_tool_next_id(
486 struct berval dn = e->e_name;
487 struct berval ndn = e->e_nname;
488 struct berval pdn, npdn;
489 EntryInfo *ei = NULL, eidummy;
492 if (ndn.bv_len == 0) {
497 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
498 if ( ei ) bdb_cache_entryinfo_unlock( ei );
499 if ( rc == DB_NOTFOUND ) {
500 if ( !be_issuffix( op->o_bd, &ndn ) ) {
502 dnParent( &dn, &pdn );
503 dnParent( &ndn, &npdn );
506 rc = bdb_tool_next_id( op, tid, e, text, 1 );
512 /* If parent didn't exist, it was created just now
513 * and its ID is now in e->e_id. Make sure the current
514 * entry gets added under the new parent ID.
516 if ( eid != e->e_id ) {
517 eidummy.bei_id = e->e_id;
521 rc = bdb_next_id( op->o_bd, &e->e_id );
523 snprintf( text->bv_val, text->bv_len,
524 "next_id failed: %s (%d)",
525 db_strerror(rc), rc );
526 Debug( LDAP_DEBUG_ANY,
527 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
530 rc = bdb_dn2id_add( op, tid, ei, e );
532 snprintf( text->bv_val, text->bv_len,
533 "dn2id_add 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 );
538 if ( nholes == nhmax - 1 ) {
539 if ( holes == hbuf ) {
540 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
541 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
543 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
547 ber_dupbv( &holes[nholes].dn, &ndn );
548 holes[nholes++].id = e->e_id;
550 } else if ( !hole ) {
553 e->e_id = ei->bei_id;
555 for ( i=0; i<nholes; i++) {
556 if ( holes[i].id == e->e_id ) {
557 free(holes[i].dn.bv_val);
558 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
562 } else if ( holes[i].id > e->e_id ) {
576 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
578 if ( !bdb->bi_nattrs )
581 if ( slapMode & SLAP_TOOL_QUICK ) {
586 ir = bdb_tool_index_rec;
587 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
589 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
590 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
591 &a->a_desc->ad_tags, ir );
595 bdb_tool_ix_id = e->e_id;
597 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
598 /* Wait for all threads to be ready */
599 while ( bdb_tool_index_tcount ) {
600 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
601 &bdb_tool_index_mutex );
603 for ( i=1; i<slap_tool_thread_max; i++ )
604 bdb_tool_index_threads[i] = LDAP_BUSY;
605 bdb_tool_index_tcount = slap_tool_thread_max - 1;
606 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
607 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
608 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
611 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
612 for ( i=1; i<slap_tool_thread_max; i++ ) {
613 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
614 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
615 &bdb_tool_index_mutex );
619 if ( bdb_tool_index_threads[i] ) {
620 rc = bdb_tool_index_threads[i];
624 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
627 return bdb_index_entry_add( op, txn, e );
631 ID bdb_tool_entry_put(
634 struct berval *text )
637 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
642 assert( be != NULL );
643 assert( slapMode & SLAP_TOOL_MODE );
645 assert( text != NULL );
646 assert( text->bv_val != NULL );
647 assert( text->bv_val[0] == '\0' ); /* overconservative? */
649 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
650 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
652 if (! (slapMode & SLAP_TOOL_QUICK)) {
653 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
654 bdb->bi_db_opflags );
656 snprintf( text->bv_val, text->bv_len,
657 "txn_begin failed: %s (%d)",
658 db_strerror(rc), rc );
659 Debug( LDAP_DEBUG_ANY,
660 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
661 text->bv_val, 0, 0 );
668 op.o_tmpmemctx = NULL;
669 op.o_tmpmfuncs = &ch_mfuncs;
671 /* add dn2id indices */
672 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
678 if (( slapMode & SLAP_TOOL_QUICK ) && (( e->e_id & 0xfff ) == 0xfff )) {
679 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
683 if ( !bdb->bi_linear_index )
684 rc = bdb_tool_index_add( &op, tid, e );
686 snprintf( text->bv_val, text->bv_len,
687 "index_entry_add failed: %s (%d)",
688 rc == LDAP_OTHER ? "Internal error" :
689 db_strerror(rc), rc );
690 Debug( LDAP_DEBUG_ANY,
691 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
692 text->bv_val, 0, 0 );
697 rc = bdb_id2entry_add( be, tid, e );
699 snprintf( text->bv_val, text->bv_len,
700 "id2entry_add failed: %s (%d)",
701 db_strerror(rc), rc );
702 Debug( LDAP_DEBUG_ANY,
703 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
704 text->bv_val, 0, 0 );
710 if ( !( slapMode & SLAP_TOOL_QUICK )) {
711 rc = TXN_COMMIT( tid, 0 );
713 snprintf( text->bv_val, text->bv_len,
714 "txn_commit failed: %s (%d)",
715 db_strerror(rc), rc );
716 Debug( LDAP_DEBUG_ANY,
717 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
718 text->bv_val, 0, 0 );
724 if ( !( slapMode & SLAP_TOOL_QUICK )) {
726 snprintf( text->bv_val, text->bv_len,
727 "txn_aborted! %s (%d)",
728 rc == LDAP_OTHER ? "Internal error" :
729 db_strerror(rc), rc );
730 Debug( LDAP_DEBUG_ANY,
731 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
732 text->bv_val, 0, 0 );
740 int bdb_tool_entry_reindex(
743 AttributeDescription **adv )
745 struct bdb_info *bi = (struct bdb_info *) be->be_private;
752 Debug( LDAP_DEBUG_ARGS,
753 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
755 assert( tool_base == NULL );
756 assert( tool_filter == NULL );
758 /* No indexes configured, nothing to do. Could return an
759 * error here to shortcut things.
765 /* Check for explicit list of attrs to index */
769 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
771 for ( n = 0; adv[n]; n++ ) ;
774 for ( i = 0; i < n; i++ ) {
775 AttributeDescription *ad = adv[i];
776 for ( j = i-1; j>=0; j--) {
777 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
784 for ( i = 0; adv[i]; i++ ) {
785 if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
786 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
787 if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
788 AttrInfo *ai = bi->bi_attrs[i];
789 bi->bi_attrs[i] = bi->bi_attrs[j];
790 bi->bi_attrs[j] = ai;
794 if ( j == bi->bi_nattrs ) {
795 Debug( LDAP_DEBUG_ANY,
796 LDAP_XSTRING(bdb_tool_entry_reindex)
797 ": no index configured for %s\n",
798 adv[i]->ad_cname.bv_val, 0, 0 );
806 /* Get the first attribute to index */
807 if (bi->bi_linear_index && !index_nattrs) {
808 index_nattrs = bi->bi_nattrs - 1;
812 e = bdb_tool_entry_get( be, id );
815 Debug( LDAP_DEBUG_ANY,
816 LDAP_XSTRING(bdb_tool_entry_reindex)
817 ": could not locate id=%ld\n",
822 if (! (slapMode & SLAP_TOOL_QUICK)) {
823 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
825 Debug( LDAP_DEBUG_ANY,
826 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
827 "txn_begin failed: %s (%d)\n",
828 db_strerror(rc), rc, 0 );
834 * just (re)add them for now
835 * assume that some other routine (not yet implemented)
836 * will zap index databases
840 Debug( LDAP_DEBUG_TRACE,
841 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
842 (long) id, e->e_dn, 0 );
846 op.o_tmpmemctx = NULL;
847 op.o_tmpmfuncs = &ch_mfuncs;
849 rc = bdb_tool_index_add( &op, tid, e );
853 if (! (slapMode & SLAP_TOOL_QUICK)) {
854 rc = TXN_COMMIT( tid, 0 );
856 Debug( LDAP_DEBUG_ANY,
857 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
858 ": txn_commit failed: %s (%d)\n",
859 db_strerror(rc), rc, 0 );
865 if (! (slapMode & SLAP_TOOL_QUICK)) {
867 Debug( LDAP_DEBUG_ANY,
868 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
869 ": txn_aborted! %s (%d)\n",
870 db_strerror(rc), rc, 0 );
874 bdb_entry_release( &op, e, 0 );
879 ID bdb_tool_entry_modify(
882 struct berval *text )
885 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
890 assert( be != NULL );
891 assert( slapMode & SLAP_TOOL_MODE );
893 assert( text != NULL );
894 assert( text->bv_val != NULL );
895 assert( text->bv_val[0] == '\0' ); /* overconservative? */
897 assert ( e->e_id != NOID );
899 Debug( LDAP_DEBUG_TRACE,
900 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
901 (long) e->e_id, e->e_dn, 0 );
903 if (! (slapMode & SLAP_TOOL_QUICK)) {
905 cursor->c_close( cursor );
908 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
909 bdb->bi_db_opflags );
911 snprintf( text->bv_val, text->bv_len,
912 "txn_begin failed: %s (%d)",
913 db_strerror(rc), rc );
914 Debug( LDAP_DEBUG_ANY,
915 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
916 text->bv_val, 0, 0 );
923 op.o_tmpmemctx = NULL;
924 op.o_tmpmfuncs = &ch_mfuncs;
927 rc = bdb_id2entry_update( be, tid, e );
929 snprintf( text->bv_val, text->bv_len,
930 "id2entry_add failed: %s (%d)",
931 db_strerror(rc), rc );
932 Debug( LDAP_DEBUG_ANY,
933 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
934 text->bv_val, 0, 0 );
940 if (! (slapMode & SLAP_TOOL_QUICK)) {
941 rc = TXN_COMMIT( tid, 0 );
943 snprintf( text->bv_val, text->bv_len,
944 "txn_commit failed: %s (%d)",
945 db_strerror(rc), rc );
946 Debug( LDAP_DEBUG_ANY,
947 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
948 "%s\n", text->bv_val, 0, 0 );
954 if (! (slapMode & SLAP_TOOL_QUICK)) {
956 snprintf( text->bv_val, text->bv_len,
957 "txn_aborted! %s (%d)",
958 db_strerror(rc), rc );
959 Debug( LDAP_DEBUG_ANY,
960 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
961 text->bv_val, 0, 0 );
969 #ifdef BDB_TOOL_IDL_CACHING
971 bdb_tool_idl_cmp( const void *v1, const void *v2 )
973 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
976 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
977 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
981 bdb_tool_idl_flush_one( void *v1, void *arg )
983 bdb_tool_idl_cache *ic = v1;
985 struct bdb_info *bdb = bdb_tool_info;
986 bdb_tool_idl_cache_entry *ice;
992 /* Freshly allocated, ignore it */
993 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
997 rc = db->cursor( db, NULL, &curs, 0 );
1004 bv2DBT( &ic->kstr, &key );
1006 data.size = data.ulen = sizeof( ID );
1007 data.flags = DB_DBT_USERMEM;
1010 rc = curs->c_get( curs, &key, &data, DB_SET );
1011 /* If key already exists and we're writing a range... */
1012 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
1013 /* If it's not currently a range, must delete old info */
1016 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
1017 curs->c_del( curs, 0 );
1020 /* Store range marker */
1021 curs->c_put( curs, &key, &data, DB_KEYFIRST );
1025 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
1028 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
1031 curs->c_del( curs, 0 );
1033 BDB_ID2DISK( ic->last, &nid );
1034 curs->c_put( curs, &key, &data, DB_KEYLAST );
1036 } else if ( rc && rc != DB_NOTFOUND ) {
1038 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
1039 /* range, didn't exist before */
1041 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1043 BDB_ID2DISK( ic->first, &nid );
1044 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1046 BDB_ID2DISK( ic->last, &nid );
1047 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
1056 /* Just a normal write */
1058 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1063 end = ic->count & (IDBLOCK-1);
1067 for ( i=0; i<end; i++ ) {
1068 if ( !ice->ids[i] ) continue;
1069 BDB_ID2DISK( ice->ids[i], &nid );
1070 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
1072 if ( rc == DB_KEYEXIST ) {
1086 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1087 ic->tail->next = bdb_tool_idl_free_list;
1088 bdb_tool_idl_free_list = ic->head;
1089 bdb->bi_idl_cache_size -= n;
1090 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1093 if ( ic != db->app_private ) {
1096 ic->head = ic->tail = NULL;
1098 curs->c_close( curs );
1103 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
1105 Avlnode *root = db->app_private;
1108 db->app_private = ic;
1109 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
1110 avl_free( root, NULL );
1111 db->app_private = NULL;
1118 bdb_tool_idl_flush( BackendDB *be )
1120 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1125 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
1126 db = bdb->bi_databases[i]->bdi_db;
1127 if ( !db->app_private ) continue;
1128 rc = bdb_tool_idl_flush_db( db, NULL );
1133 bdb->bi_idl_cache_size = 0;
1138 int bdb_tool_idl_add(
1145 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
1146 bdb_tool_idl_cache *ic, itmp;
1147 bdb_tool_idl_cache_entry *ice;
1150 if ( !bdb->bi_idl_cache_max_size )
1151 return bdb_idl_insert_key( be, db, txn, key, id );
1153 DBT2bv( key, &itmp.kstr );
1155 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
1157 /* No entry yet, create one */
1164 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1165 ic->kstr.bv_len = itmp.kstr.bv_len;
1166 ic->kstr.bv_val = (char *)(ic+1);
1167 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1168 ic->head = ic->tail = NULL;
1171 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1174 /* load existing key count here */
1175 rc = db->cursor( db, NULL, &curs, 0 );
1176 if ( rc ) return rc;
1178 data.ulen = sizeof( ID );
1179 data.flags = DB_DBT_USERMEM;
1181 rc = curs->c_get( curs, key, &data, DB_SET );
1184 ic->count = BDB_IDL_DB_SIZE+1;
1188 curs->c_count( curs, &count, 0 );
1190 BDB_DISK2ID( &nid, &ic->first );
1193 curs->c_close( curs );
1195 /* are we a range already? */
1196 if ( ic->count > BDB_IDL_DB_SIZE ) {
1199 /* Are we at the limit, and converting to a range? */
1200 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1202 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1205 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1206 ic->tail->next = bdb_tool_idl_free_list;
1207 bdb_tool_idl_free_list = ic->head;
1208 bdb->bi_idl_cache_size -= n;
1209 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1211 ic->head = ic->tail = NULL;
1216 /* No free block, create that too */
1217 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1219 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1220 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1221 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1222 rc = bdb_tool_idl_flush_db( db, ic );
1225 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1227 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1229 bdb->bi_idl_cache_size++;
1230 if ( bdb_tool_idl_free_list ) {
1231 ice = bdb_tool_idl_free_list;
1232 bdb_tool_idl_free_list = ice->next;
1234 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1236 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1238 memset( ice, 0, sizeof( *ice ));
1242 ic->tail->next = ice;
1249 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1258 bdb_tool_trickle_task( void *ctx, void *ptr )
1263 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1264 bdb_tool_trickle_active = 1;
1265 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond_end );
1267 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1268 &bdb_tool_trickle_mutex );
1269 if ( slapd_shutdown )
1271 env->memp_trickle( env, 30, &wrote );
1273 bdb_tool_trickle_active = 0;
1274 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond_end );
1275 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1282 bdb_tool_index_task( void *ctx, void *ptr )
1284 int base = *(int *)ptr;
1288 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1289 bdb_tool_index_tcount--;
1290 if ( !bdb_tool_index_tcount )
1291 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1292 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1293 &bdb_tool_index_mutex );
1294 if ( slapd_shutdown ) {
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_mutex_unlock( &bdb_tool_index_mutex );
1301 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1303 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1304 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );