1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
27 static DBC *cursor = NULL;
29 static EntryHeader eh;
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, NULL, &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, NULL, 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 EntryInfo *ei = NULL;
306 op.o_tmpmemctx = NULL;
307 op.o_tmpmfuncs = &ch_mfuncs;
309 rc = bdb_cache_find_parent( &op, NULL, CURSOR_GETLOCKER(cursor), id, &ei );
310 if ( rc == LDAP_SUCCESS ) {
311 bdb_cache_entryinfo_unlock( ei );
325 static int bdb_tool_next_id(
332 struct berval dn = e->e_name;
333 struct berval ndn = e->e_nname;
334 struct berval pdn, npdn;
335 EntryInfo *ei = NULL, eidummy;
338 if (ndn.bv_len == 0) {
343 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
344 if ( ei ) bdb_cache_entryinfo_unlock( ei );
345 if ( rc == DB_NOTFOUND ) {
346 if ( !be_issuffix( op->o_bd, &ndn ) ) {
348 dnParent( &dn, &pdn );
349 dnParent( &ndn, &npdn );
352 rc = bdb_tool_next_id( op, tid, e, text, 1 );
358 /* If parent didn't exist, it was created just now
359 * and its ID is now in e->e_id. Make sure the current
360 * entry gets added under the new parent ID.
362 if ( eid != e->e_id ) {
363 eidummy.bei_id = e->e_id;
367 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
369 snprintf( text->bv_val, text->bv_len,
370 "next_id failed: %s (%d)",
371 db_strerror(rc), rc );
372 Debug( LDAP_DEBUG_ANY,
373 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
376 rc = bdb_dn2id_add( op, tid, ei, e );
378 snprintf( text->bv_val, text->bv_len,
379 "dn2id_add failed: %s (%d)",
380 db_strerror(rc), rc );
381 Debug( LDAP_DEBUG_ANY,
382 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
384 if ( nholes == nhmax - 1 ) {
385 if ( holes == hbuf ) {
386 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
387 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
389 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
393 ber_dupbv( &holes[nholes].dn, &ndn );
394 holes[nholes++].id = e->e_id;
396 } else if ( !hole ) {
399 e->e_id = ei->bei_id;
401 for ( i=0; i<nholes; i++) {
402 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_mutex_lock( &bdb_tool_trickle_mutex );
526 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
527 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
530 if ( !bdb->bi_linear_index )
531 rc = bdb_tool_index_add( &op, tid, e );
533 snprintf( text->bv_val, text->bv_len,
534 "index_entry_add failed: %s (%d)",
535 db_strerror(rc), rc );
536 Debug( LDAP_DEBUG_ANY,
537 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
538 text->bv_val, 0, 0 );
543 rc = bdb_id2entry_add( be, tid, e );
545 snprintf( text->bv_val, text->bv_len,
546 "id2entry_add failed: %s (%d)",
547 db_strerror(rc), rc );
548 Debug( LDAP_DEBUG_ANY,
549 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
550 text->bv_val, 0, 0 );
556 if ( !( slapMode & SLAP_TOOL_QUICK )) {
557 rc = TXN_COMMIT( tid, 0 );
559 snprintf( text->bv_val, text->bv_len,
560 "txn_commit failed: %s (%d)",
561 db_strerror(rc), rc );
562 Debug( LDAP_DEBUG_ANY,
563 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
564 text->bv_val, 0, 0 );
570 if ( !( slapMode & SLAP_TOOL_QUICK )) {
572 snprintf( text->bv_val, text->bv_len,
573 "txn_aborted! %s (%d)",
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)) {
747 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
748 bdb->bi_db_opflags );
750 snprintf( text->bv_val, text->bv_len,
751 "txn_begin failed: %s (%d)",
752 db_strerror(rc), rc );
753 Debug( LDAP_DEBUG_ANY,
754 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
755 text->bv_val, 0, 0 );
762 op.o_tmpmemctx = NULL;
763 op.o_tmpmfuncs = &ch_mfuncs;
766 rc = bdb_id2entry_update( be, tid, e );
768 snprintf( text->bv_val, text->bv_len,
769 "id2entry_add failed: %s (%d)",
770 db_strerror(rc), rc );
771 Debug( LDAP_DEBUG_ANY,
772 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
773 text->bv_val, 0, 0 );
779 if (! (slapMode & SLAP_TOOL_QUICK)) {
780 rc = TXN_COMMIT( tid, 0 );
782 snprintf( text->bv_val, text->bv_len,
783 "txn_commit failed: %s (%d)",
784 db_strerror(rc), rc );
785 Debug( LDAP_DEBUG_ANY,
786 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
787 "%s\n", text->bv_val, 0, 0 );
793 if (! (slapMode & SLAP_TOOL_QUICK)) {
795 snprintf( text->bv_val, text->bv_len,
796 "txn_aborted! %s (%d)",
797 db_strerror(rc), rc );
798 Debug( LDAP_DEBUG_ANY,
799 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
800 text->bv_val, 0, 0 );
808 #ifdef BDB_TOOL_IDL_CACHING
810 bdb_tool_idl_cmp( const void *v1, const void *v2 )
812 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
815 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
816 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
820 bdb_tool_idl_flush_one( void *v1, void *arg )
822 bdb_tool_idl_cache *ic = v1;
824 struct bdb_info *bdb = bdb_tool_info;
825 bdb_tool_idl_cache_entry *ice;
831 /* Freshly allocated, ignore it */
832 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
836 rc = db->cursor( db, NULL, &curs, 0 );
843 bv2DBT( &ic->kstr, &key );
845 data.size = data.ulen = sizeof( ID );
846 data.flags = DB_DBT_USERMEM;
849 rc = curs->c_get( curs, &key, &data, DB_SET );
850 /* If key already exists and we're writing a range... */
851 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
852 /* If it's not currently a range, must delete old info */
855 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
856 curs->c_del( curs, 0 );
859 /* Store range marker */
860 curs->c_put( curs, &key, &data, DB_KEYFIRST );
864 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
867 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
870 curs->c_del( curs, 0 );
872 BDB_ID2DISK( ic->last, &nid );
873 curs->c_put( curs, &key, &data, DB_KEYLAST );
875 } else if ( rc && rc != DB_NOTFOUND ) {
877 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
878 /* range, didn't exist before */
880 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
882 BDB_ID2DISK( ic->first, &nid );
883 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
885 BDB_ID2DISK( ic->last, &nid );
886 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
895 /* Just a normal write */
897 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
902 end = ic->count & (IDBLOCK-1);
906 for ( i=0; i<end; i++ ) {
907 if ( !ice->ids[i] ) continue;
908 BDB_ID2DISK( ice->ids[i], &nid );
909 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
911 if ( rc == DB_KEYEXIST ) {
925 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
926 ic->tail->next = bdb_tool_idl_free_list;
927 bdb_tool_idl_free_list = ic->head;
928 bdb->bi_idl_cache_size -= n;
929 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
932 if ( ic != db->app_private ) {
935 ic->head = ic->tail = NULL;
937 curs->c_close( curs );
942 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
944 Avlnode *root = db->app_private;
947 db->app_private = ic;
948 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
949 avl_free( root, NULL );
950 db->app_private = NULL;
957 bdb_tool_idl_flush( BackendDB *be )
959 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
964 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
965 db = bdb->bi_databases[i]->bdi_db;
966 if ( !db->app_private ) continue;
967 rc = bdb_tool_idl_flush_db( db, NULL );
972 bdb->bi_idl_cache_size = 0;
977 int bdb_tool_idl_add(
984 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
985 bdb_tool_idl_cache *ic, itmp;
986 bdb_tool_idl_cache_entry *ice;
989 if ( !bdb->bi_idl_cache_max_size )
990 return bdb_idl_insert_key( be, db, txn, key, id );
992 DBT2bv( key, &itmp.kstr );
994 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
996 /* No entry yet, create one */
1003 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1004 ic->kstr.bv_len = itmp.kstr.bv_len;
1005 ic->kstr.bv_val = (char *)(ic+1);
1006 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1007 ic->head = ic->tail = NULL;
1010 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1013 /* load existing key count here */
1014 rc = db->cursor( db, NULL, &curs, 0 );
1015 if ( rc ) return rc;
1017 data.ulen = sizeof( ID );
1018 data.flags = DB_DBT_USERMEM;
1020 rc = curs->c_get( curs, key, &data, DB_SET );
1023 ic->count = BDB_IDL_DB_SIZE+1;
1027 curs->c_count( curs, &count, 0 );
1029 BDB_DISK2ID( &nid, &ic->first );
1032 curs->c_close( curs );
1034 /* are we a range already? */
1035 if ( ic->count > BDB_IDL_DB_SIZE ) {
1038 /* Are we at the limit, and converting to a range? */
1039 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1041 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1044 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1045 ic->tail->next = bdb_tool_idl_free_list;
1046 bdb_tool_idl_free_list = ic->head;
1047 bdb->bi_idl_cache_size -= n;
1048 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1050 ic->head = ic->tail = NULL;
1055 /* No free block, create that too */
1056 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1058 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1059 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1060 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1061 rc = bdb_tool_idl_flush_db( db, ic );
1064 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1066 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1068 bdb->bi_idl_cache_size++;
1069 if ( bdb_tool_idl_free_list ) {
1070 ice = bdb_tool_idl_free_list;
1071 bdb_tool_idl_free_list = ice->next;
1073 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1075 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1077 memset( ice, 0, sizeof( *ice ));
1081 ic->tail->next = ice;
1088 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1096 bdb_tool_trickle_task( void *ctx, void *ptr )
1101 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1103 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1104 &bdb_tool_trickle_mutex );
1105 if ( slapd_shutdown )
1107 env->memp_trickle( env, 30, &wrote );
1109 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1115 bdb_tool_index_task( void *ctx, void *ptr )
1117 int base = *(int *)ptr;
1121 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1122 bdb_tool_index_tcount--;
1123 if ( !bdb_tool_index_tcount )
1124 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1125 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1126 &bdb_tool_index_mutex );
1127 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1128 if ( slapd_shutdown )
1131 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1132 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );