1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2009 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 #ifdef BDB_TOOL_IDL_CACHING
46 #define bdb_tool_idl_cmp BDB_SYMBOL(tool_idl_cmp)
47 #define bdb_tool_idl_flush_one BDB_SYMBOL(tool_idl_flush_one)
48 #define bdb_tool_idl_flush BDB_SYMBOL(tool_idl_flush)
50 static int bdb_tool_idl_flush( BackendDB *be );
54 typedef struct bdb_tool_idl_cache_entry {
55 struct bdb_tool_idl_cache_entry *next;
57 } bdb_tool_idl_cache_entry;
59 typedef struct bdb_tool_idl_cache {
61 bdb_tool_idl_cache_entry *head, *tail;
66 static bdb_tool_idl_cache_entry *bdb_tool_idl_free_list;
67 #endif /* BDB_TOOL_IDL_CACHING */
69 static ID bdb_tool_ix_id;
70 static Operation *bdb_tool_ix_op;
71 static int *bdb_tool_index_threads, bdb_tool_index_tcount;
72 static void *bdb_tool_index_rec;
73 static struct bdb_info *bdb_tool_info;
74 static ldap_pvt_thread_mutex_t bdb_tool_index_mutex;
75 static ldap_pvt_thread_cond_t bdb_tool_index_cond_main;
76 static ldap_pvt_thread_cond_t bdb_tool_index_cond_work;
78 static ldap_pvt_thread_mutex_t bdb_tool_trickle_mutex;
79 static ldap_pvt_thread_cond_t bdb_tool_trickle_cond;
81 static void * bdb_tool_index_task( void *ctx, void *ptr );
82 static void * bdb_tool_trickle_task( void *ctx, void *ptr );
84 int bdb_tool_entry_open(
85 BackendDB *be, int mode )
87 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
89 /* initialize key and data thangs */
92 key.flags = DB_DBT_USERMEM;
94 key.size = key.ulen = sizeof( nid );
95 data.flags = DB_DBT_USERMEM;
98 int rc = bdb->bi_id2entry->bdi_db->cursor(
99 bdb->bi_id2entry->bdi_db, bdb->bi_cache.c_txn, &cursor,
100 bdb->bi_db_opflags );
106 /* Set up for threaded slapindex */
107 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
108 if ( !bdb_tool_info ) {
109 ldap_pvt_thread_mutex_init( &bdb_tool_trickle_mutex );
110 ldap_pvt_thread_cond_init( &bdb_tool_trickle_cond );
111 ldap_pvt_thread_pool_submit( &connection_pool, bdb_tool_trickle_task, bdb->bi_dbenv );
113 ldap_pvt_thread_mutex_init( &bdb_tool_index_mutex );
114 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_main );
115 ldap_pvt_thread_cond_init( &bdb_tool_index_cond_work );
116 if ( bdb->bi_nattrs ) {
118 bdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
119 bdb_tool_index_rec = ch_malloc( bdb->bi_nattrs * sizeof( IndexRec ));
120 bdb_tool_index_tcount = slap_tool_thread_max - 1;
121 for (i=1; i<slap_tool_thread_max; i++) {
122 int *ptr = ch_malloc( sizeof( int ));
124 ldap_pvt_thread_pool_submit( &connection_pool,
125 bdb_tool_index_task, ptr );
135 int bdb_tool_entry_close(
138 if ( bdb_tool_info ) {
140 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
141 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
142 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
143 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
144 bdb_tool_index_tcount = slap_tool_thread_max - 1;
145 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
146 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
150 ch_free( eh.bv.bv_val );
155 cursor->c_close( cursor );
159 #ifdef BDB_TOOL_IDL_CACHING
160 bdb_tool_idl_flush( be );
165 fprintf( stderr, "Error, entries missing!\n");
166 for (i=0; i<nholes; i++) {
167 fprintf(stderr, " entry %ld: %s\n",
168 holes[i].id, holes[i].dn.bv_val);
176 ID bdb_tool_entry_next(
181 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
183 assert( be != NULL );
184 assert( slapMode & SLAP_TOOL_MODE );
185 assert( bdb != NULL );
188 data.ulen = data.dlen = sizeof( ehbuf );
190 data.flags |= DB_DBT_PARTIAL;
191 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
194 /* If we're doing linear indexing and there are more attrs to
195 * index, and we're at the end of the database, start over.
197 if ( index_nattrs && rc == DB_NOTFOUND ) {
198 /* optional - do a checkpoint here? */
199 bdb_attr_info_free( bdb->bi_attrs[0] );
200 bdb->bi_attrs[0] = bdb->bi_attrs[index_nattrs];
202 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
211 BDB_DISK2ID( key.data, &id );
216 ID bdb_tool_dn2id_get(
223 EntryInfo *ei = NULL;
226 if ( BER_BVISEMPTY(dn) )
231 op.o_tmpmemctx = NULL;
232 op.o_tmpmfuncs = &ch_mfuncs;
234 rc = bdb_cache_find_ndn( &op, 0, dn, &ei );
235 if ( ei ) bdb_cache_entryinfo_unlock( ei );
236 if ( rc == DB_NOTFOUND )
242 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
248 assert( be != NULL );
249 assert( slapMode & SLAP_TOOL_MODE );
251 if ( id != previd ) {
252 data.ulen = data.dlen = sizeof( ehbuf );
254 data.flags |= DB_DBT_PARTIAL;
256 BDB_ID2DISK( id, &nid );
257 rc = cursor->c_get( cursor, &key, &data, DB_SET );
263 eh.bv.bv_val = ehbuf;
264 eh.bv.bv_len = data.size;
265 rc = entry_header( &eh );
266 eoff = eh.data - eh.bv.bv_val;
271 data.flags &= ~DB_DBT_PARTIAL;
273 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
274 if ( rc != DB_BUFFER_SMALL ) goto done;
276 /* Allocate a block and retrieve the data */
277 eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + data.size;
278 eh.bv.bv_val = ch_realloc( eh.bv.bv_val, eh.bv.bv_len );
279 eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
281 data.ulen = data.size;
283 /* Skip past already parsed nattr/nvals */
286 rc = cursor->c_get( cursor, &key, &data, DB_CURRENT );
289 #ifdef SLAP_ZONE_ALLOC
290 /* FIXME: will add ctx later */
291 rc = entry_decode( &eh, &e, NULL );
293 rc = entry_decode( &eh, &e );
296 if( rc == LDAP_SUCCESS ) {
299 if ( slapMode & SLAP_TOOL_READONLY ) {
300 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
301 EntryInfo *ei = NULL;
307 op.o_tmpmemctx = NULL;
308 op.o_tmpmfuncs = &ch_mfuncs;
310 rc = bdb_cache_find_parent( &op, bdb->bi_cache.c_txn, 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, &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 ) {
404 free(holes[i].dn.bv_val);
405 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
409 } else if ( holes[i].id > e->e_id ) {
423 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
425 if ( !bdb->bi_nattrs )
428 if ( slapMode & SLAP_TOOL_QUICK ) {
433 ir = bdb_tool_index_rec;
434 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
436 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
437 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
438 &a->a_desc->ad_tags, ir );
442 bdb_tool_ix_id = e->e_id;
444 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
445 /* Wait for all threads to be ready */
446 while ( bdb_tool_index_tcount ) {
447 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
448 &bdb_tool_index_mutex );
450 for ( i=1; i<slap_tool_thread_max; i++ )
451 bdb_tool_index_threads[i] = LDAP_BUSY;
452 bdb_tool_index_tcount = slap_tool_thread_max - 1;
453 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
454 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
455 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
458 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
459 for ( i=1; i<slap_tool_thread_max; i++ ) {
460 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
461 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
462 &bdb_tool_index_mutex );
466 if ( bdb_tool_index_threads[i] ) {
467 rc = bdb_tool_index_threads[i];
471 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
474 return bdb_index_entry_add( op, txn, e );
478 ID bdb_tool_entry_put(
481 struct berval *text )
484 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
489 assert( be != NULL );
490 assert( slapMode & SLAP_TOOL_MODE );
492 assert( text != NULL );
493 assert( text->bv_val != NULL );
494 assert( text->bv_val[0] == '\0' ); /* overconservative? */
496 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
497 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
499 if (! (slapMode & SLAP_TOOL_QUICK)) {
500 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
501 bdb->bi_db_opflags );
503 snprintf( text->bv_val, text->bv_len,
504 "txn_begin failed: %s (%d)",
505 db_strerror(rc), rc );
506 Debug( LDAP_DEBUG_ANY,
507 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
508 text->bv_val, 0, 0 );
515 op.o_tmpmemctx = NULL;
516 op.o_tmpmfuncs = &ch_mfuncs;
518 /* add dn2id indices */
519 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
524 if (( slapMode & SLAP_TOOL_QUICK ) && (( e->e_id & 0xfff ) == 0xfff )) {
525 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
528 if ( !bdb->bi_linear_index )
529 rc = bdb_tool_index_add( &op, tid, e );
531 snprintf( text->bv_val, text->bv_len,
532 "index_entry_add failed: %s (%d)",
533 rc == LDAP_OTHER ? "Internal error" :
534 db_strerror(rc), rc );
535 Debug( LDAP_DEBUG_ANY,
536 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
537 text->bv_val, 0, 0 );
542 rc = bdb_id2entry_add( be, tid, e );
544 snprintf( text->bv_val, text->bv_len,
545 "id2entry_add failed: %s (%d)",
546 db_strerror(rc), rc );
547 Debug( LDAP_DEBUG_ANY,
548 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
549 text->bv_val, 0, 0 );
555 if ( !( slapMode & SLAP_TOOL_QUICK )) {
556 rc = TXN_COMMIT( tid, 0 );
558 snprintf( text->bv_val, text->bv_len,
559 "txn_commit failed: %s (%d)",
560 db_strerror(rc), rc );
561 Debug( LDAP_DEBUG_ANY,
562 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
563 text->bv_val, 0, 0 );
569 if ( !( slapMode & SLAP_TOOL_QUICK )) {
571 snprintf( text->bv_val, text->bv_len,
572 "txn_aborted! %s (%d)",
573 rc == LDAP_OTHER ? "Internal error" :
574 db_strerror(rc), rc );
575 Debug( LDAP_DEBUG_ANY,
576 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
577 text->bv_val, 0, 0 );
585 int bdb_tool_entry_reindex(
588 AttributeDescription **adv )
590 struct bdb_info *bi = (struct bdb_info *) be->be_private;
597 Debug( LDAP_DEBUG_ARGS,
598 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
601 /* No indexes configured, nothing to do. Could return an
602 * error here to shortcut things.
608 /* Check for explicit list of attrs to index */
612 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
614 for ( n = 0; adv[n]; n++ ) ;
617 for ( i = 0; i < n; i++ ) {
618 AttributeDescription *ad = adv[i];
619 for ( j = i-1; j>=0; j--) {
620 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
627 for ( i = 0; adv[i]; i++ ) {
628 if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
629 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
630 if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
631 AttrInfo *ai = bi->bi_attrs[i];
632 bi->bi_attrs[i] = bi->bi_attrs[j];
633 bi->bi_attrs[j] = ai;
637 if ( j == bi->bi_nattrs ) {
638 Debug( LDAP_DEBUG_ANY,
639 LDAP_XSTRING(bdb_tool_entry_reindex)
640 ": no index configured for %s\n",
641 adv[i]->ad_cname.bv_val, 0, 0 );
649 /* Get the first attribute to index */
650 if (bi->bi_linear_index && !index_nattrs) {
651 index_nattrs = bi->bi_nattrs - 1;
655 e = bdb_tool_entry_get( be, id );
658 Debug( LDAP_DEBUG_ANY,
659 LDAP_XSTRING(bdb_tool_entry_reindex)
660 ": could not locate id=%ld\n",
665 if (! (slapMode & SLAP_TOOL_QUICK)) {
666 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
668 Debug( LDAP_DEBUG_ANY,
669 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
670 "txn_begin failed: %s (%d)\n",
671 db_strerror(rc), rc, 0 );
677 * just (re)add them for now
678 * assume that some other routine (not yet implemented)
679 * will zap index databases
683 Debug( LDAP_DEBUG_TRACE,
684 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
685 (long) id, e->e_dn, 0 );
689 op.o_tmpmemctx = NULL;
690 op.o_tmpmfuncs = &ch_mfuncs;
692 rc = bdb_tool_index_add( &op, tid, e );
696 if (! (slapMode & SLAP_TOOL_QUICK)) {
697 rc = TXN_COMMIT( tid, 0 );
699 Debug( LDAP_DEBUG_ANY,
700 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
701 ": txn_commit failed: %s (%d)\n",
702 db_strerror(rc), rc, 0 );
708 if (! (slapMode & SLAP_TOOL_QUICK)) {
710 Debug( LDAP_DEBUG_ANY,
711 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
712 ": txn_aborted! %s (%d)\n",
713 db_strerror(rc), rc, 0 );
717 bdb_entry_release( &op, e, 0 );
722 ID bdb_tool_entry_modify(
725 struct berval *text )
728 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
733 assert( be != NULL );
734 assert( slapMode & SLAP_TOOL_MODE );
736 assert( text != NULL );
737 assert( text->bv_val != NULL );
738 assert( text->bv_val[0] == '\0' ); /* overconservative? */
740 assert ( e->e_id != NOID );
742 Debug( LDAP_DEBUG_TRACE,
743 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
744 (long) e->e_id, e->e_dn, 0 );
746 if (! (slapMode & SLAP_TOOL_QUICK)) {
748 cursor->c_close( cursor );
751 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
752 bdb->bi_db_opflags );
754 snprintf( text->bv_val, text->bv_len,
755 "txn_begin failed: %s (%d)",
756 db_strerror(rc), rc );
757 Debug( LDAP_DEBUG_ANY,
758 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
759 text->bv_val, 0, 0 );
766 op.o_tmpmemctx = NULL;
767 op.o_tmpmfuncs = &ch_mfuncs;
770 rc = bdb_id2entry_update( be, tid, e );
772 snprintf( text->bv_val, text->bv_len,
773 "id2entry_add failed: %s (%d)",
774 db_strerror(rc), rc );
775 Debug( LDAP_DEBUG_ANY,
776 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
777 text->bv_val, 0, 0 );
783 if (! (slapMode & SLAP_TOOL_QUICK)) {
784 rc = TXN_COMMIT( tid, 0 );
786 snprintf( text->bv_val, text->bv_len,
787 "txn_commit failed: %s (%d)",
788 db_strerror(rc), rc );
789 Debug( LDAP_DEBUG_ANY,
790 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
791 "%s\n", text->bv_val, 0, 0 );
797 if (! (slapMode & SLAP_TOOL_QUICK)) {
799 snprintf( text->bv_val, text->bv_len,
800 "txn_aborted! %s (%d)",
801 db_strerror(rc), rc );
802 Debug( LDAP_DEBUG_ANY,
803 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
804 text->bv_val, 0, 0 );
812 #ifdef BDB_TOOL_IDL_CACHING
814 bdb_tool_idl_cmp( const void *v1, const void *v2 )
816 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
819 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
820 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
824 bdb_tool_idl_flush_one( void *v1, void *arg )
826 bdb_tool_idl_cache *ic = v1;
828 struct bdb_info *bdb = bdb_tool_info;
829 bdb_tool_idl_cache_entry *ice;
835 /* Freshly allocated, ignore it */
836 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
840 rc = db->cursor( db, NULL, &curs, 0 );
847 bv2DBT( &ic->kstr, &key );
849 data.size = data.ulen = sizeof( ID );
850 data.flags = DB_DBT_USERMEM;
853 rc = curs->c_get( curs, &key, &data, DB_SET );
854 /* If key already exists and we're writing a range... */
855 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
856 /* If it's not currently a range, must delete old info */
859 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
860 curs->c_del( curs, 0 );
863 /* Store range marker */
864 curs->c_put( curs, &key, &data, DB_KEYFIRST );
868 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
871 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
874 curs->c_del( curs, 0 );
876 BDB_ID2DISK( ic->last, &nid );
877 curs->c_put( curs, &key, &data, DB_KEYLAST );
879 } else if ( rc && rc != DB_NOTFOUND ) {
881 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
882 /* range, didn't exist before */
884 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
886 BDB_ID2DISK( ic->first, &nid );
887 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
889 BDB_ID2DISK( ic->last, &nid );
890 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
899 /* Just a normal write */
901 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
906 end = ic->count & (IDBLOCK-1);
910 for ( i=0; i<end; i++ ) {
911 if ( !ice->ids[i] ) continue;
912 BDB_ID2DISK( ice->ids[i], &nid );
913 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
915 if ( rc == DB_KEYEXIST ) {
929 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
930 ic->tail->next = bdb_tool_idl_free_list;
931 bdb_tool_idl_free_list = ic->head;
932 bdb->bi_idl_cache_size -= n;
933 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
936 if ( ic != db->app_private ) {
939 ic->head = ic->tail = NULL;
941 curs->c_close( curs );
946 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
948 Avlnode *root = db->app_private;
951 db->app_private = ic;
952 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
953 avl_free( root, NULL );
954 db->app_private = NULL;
961 bdb_tool_idl_flush( BackendDB *be )
963 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
968 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
969 db = bdb->bi_databases[i]->bdi_db;
970 if ( !db->app_private ) continue;
971 rc = bdb_tool_idl_flush_db( db, NULL );
976 bdb->bi_idl_cache_size = 0;
981 int bdb_tool_idl_add(
988 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
989 bdb_tool_idl_cache *ic, itmp;
990 bdb_tool_idl_cache_entry *ice;
993 if ( !bdb->bi_idl_cache_max_size )
994 return bdb_idl_insert_key( be, db, txn, key, id );
996 DBT2bv( key, &itmp.kstr );
998 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
1000 /* No entry yet, create one */
1007 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1008 ic->kstr.bv_len = itmp.kstr.bv_len;
1009 ic->kstr.bv_val = (char *)(ic+1);
1010 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1011 ic->head = ic->tail = NULL;
1014 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1017 /* load existing key count here */
1018 rc = db->cursor( db, NULL, &curs, 0 );
1019 if ( rc ) return rc;
1021 data.ulen = sizeof( ID );
1022 data.flags = DB_DBT_USERMEM;
1024 rc = curs->c_get( curs, key, &data, DB_SET );
1027 ic->count = BDB_IDL_DB_SIZE+1;
1031 curs->c_count( curs, &count, 0 );
1033 BDB_DISK2ID( &nid, &ic->first );
1036 curs->c_close( curs );
1038 /* are we a range already? */
1039 if ( ic->count > BDB_IDL_DB_SIZE ) {
1042 /* Are we at the limit, and converting to a range? */
1043 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1045 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1048 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1049 ic->tail->next = bdb_tool_idl_free_list;
1050 bdb_tool_idl_free_list = ic->head;
1051 bdb->bi_idl_cache_size -= n;
1052 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1054 ic->head = ic->tail = NULL;
1059 /* No free block, create that too */
1060 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1062 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1063 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1064 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1065 rc = bdb_tool_idl_flush_db( db, ic );
1068 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1070 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1072 bdb->bi_idl_cache_size++;
1073 if ( bdb_tool_idl_free_list ) {
1074 ice = bdb_tool_idl_free_list;
1075 bdb_tool_idl_free_list = ice->next;
1077 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1079 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1081 memset( ice, 0, sizeof( *ice ));
1085 ic->tail->next = ice;
1092 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1100 bdb_tool_trickle_task( void *ctx, void *ptr )
1105 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1107 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1108 &bdb_tool_trickle_mutex );
1109 if ( slapd_shutdown )
1111 env->memp_trickle( env, 30, &wrote );
1113 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1119 bdb_tool_index_task( void *ctx, void *ptr )
1121 int base = *(int *)ptr;
1125 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1126 bdb_tool_index_tcount--;
1127 if ( !bdb_tool_index_tcount )
1128 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1129 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1130 &bdb_tool_index_mutex );
1131 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1132 if ( slapd_shutdown )
1135 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1136 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );