1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 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 ) {
405 free(holes[i].dn.bv_val);
406 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
410 } else if ( holes[i].id > e->e_id ) {
424 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
426 if ( !bdb->bi_nattrs )
429 if ( slapMode & SLAP_TOOL_QUICK ) {
434 ir = bdb_tool_index_rec;
435 memset(ir, 0, bdb->bi_nattrs * sizeof( IndexRec ));
437 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
438 rc = bdb_index_recset( bdb, a, a->a_desc->ad_type,
439 &a->a_desc->ad_tags, ir );
443 bdb_tool_ix_id = e->e_id;
445 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
446 /* Wait for all threads to be ready */
447 while ( bdb_tool_index_tcount ) {
448 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
449 &bdb_tool_index_mutex );
451 for ( i=1; i<slap_tool_thread_max; i++ )
452 bdb_tool_index_threads[i] = LDAP_BUSY;
453 bdb_tool_index_tcount = slap_tool_thread_max - 1;
454 ldap_pvt_thread_cond_broadcast( &bdb_tool_index_cond_work );
455 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
456 rc = bdb_index_recrun( op, bdb, ir, e->e_id, 0 );
459 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
460 for ( i=1; i<slap_tool_thread_max; i++ ) {
461 if ( bdb_tool_index_threads[i] == LDAP_BUSY ) {
462 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_main,
463 &bdb_tool_index_mutex );
467 if ( bdb_tool_index_threads[i] ) {
468 rc = bdb_tool_index_threads[i];
472 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
475 return bdb_index_entry_add( op, txn, e );
479 ID bdb_tool_entry_put(
482 struct berval *text )
485 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
490 assert( be != NULL );
491 assert( slapMode & SLAP_TOOL_MODE );
493 assert( text != NULL );
494 assert( text->bv_val != NULL );
495 assert( text->bv_val[0] == '\0' ); /* overconservative? */
497 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
498 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
500 if (! (slapMode & SLAP_TOOL_QUICK)) {
501 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
502 bdb->bi_db_opflags );
504 snprintf( text->bv_val, text->bv_len,
505 "txn_begin failed: %s (%d)",
506 db_strerror(rc), rc );
507 Debug( LDAP_DEBUG_ANY,
508 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
509 text->bv_val, 0, 0 );
516 op.o_tmpmemctx = NULL;
517 op.o_tmpmfuncs = &ch_mfuncs;
519 /* add dn2id indices */
520 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
525 if (( slapMode & SLAP_TOOL_QUICK ) && (( e->e_id & 0xfff ) == 0xfff )) {
526 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
527 ldap_pvt_thread_cond_signal( &bdb_tool_trickle_cond );
528 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
531 if ( !bdb->bi_linear_index )
532 rc = bdb_tool_index_add( &op, tid, e );
534 snprintf( text->bv_val, text->bv_len,
535 "index_entry_add failed: %s (%d)",
536 rc == LDAP_OTHER ? "Internal error" :
537 db_strerror(rc), rc );
538 Debug( LDAP_DEBUG_ANY,
539 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
540 text->bv_val, 0, 0 );
545 rc = bdb_id2entry_add( be, tid, e );
547 snprintf( text->bv_val, text->bv_len,
548 "id2entry_add failed: %s (%d)",
549 db_strerror(rc), rc );
550 Debug( LDAP_DEBUG_ANY,
551 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
552 text->bv_val, 0, 0 );
558 if ( !( slapMode & SLAP_TOOL_QUICK )) {
559 rc = TXN_COMMIT( tid, 0 );
561 snprintf( text->bv_val, text->bv_len,
562 "txn_commit failed: %s (%d)",
563 db_strerror(rc), rc );
564 Debug( LDAP_DEBUG_ANY,
565 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
566 text->bv_val, 0, 0 );
572 if ( !( slapMode & SLAP_TOOL_QUICK )) {
574 snprintf( text->bv_val, text->bv_len,
575 "txn_aborted! %s (%d)",
576 rc == LDAP_OTHER ? "Internal error" :
577 db_strerror(rc), rc );
578 Debug( LDAP_DEBUG_ANY,
579 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
580 text->bv_val, 0, 0 );
588 int bdb_tool_entry_reindex(
591 AttributeDescription **adv )
593 struct bdb_info *bi = (struct bdb_info *) be->be_private;
600 Debug( LDAP_DEBUG_ARGS,
601 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
604 /* No indexes configured, nothing to do. Could return an
605 * error here to shortcut things.
611 /* Check for explicit list of attrs to index */
615 if ( bi->bi_attrs[0]->ai_desc != adv[0] ) {
617 for ( n = 0; adv[n]; n++ ) ;
620 for ( i = 0; i < n; i++ ) {
621 AttributeDescription *ad = adv[i];
622 for ( j = i-1; j>=0; j--) {
623 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
630 for ( i = 0; adv[i]; i++ ) {
631 if ( bi->bi_attrs[i]->ai_desc != adv[i] ) {
632 for ( j = i+1; j < bi->bi_nattrs; j++ ) {
633 if ( bi->bi_attrs[j]->ai_desc == adv[i] ) {
634 AttrInfo *ai = bi->bi_attrs[i];
635 bi->bi_attrs[i] = bi->bi_attrs[j];
636 bi->bi_attrs[j] = ai;
640 if ( j == bi->bi_nattrs ) {
641 Debug( LDAP_DEBUG_ANY,
642 LDAP_XSTRING(bdb_tool_entry_reindex)
643 ": no index configured for %s\n",
644 adv[i]->ad_cname.bv_val, 0, 0 );
652 /* Get the first attribute to index */
653 if (bi->bi_linear_index && !index_nattrs) {
654 index_nattrs = bi->bi_nattrs - 1;
658 e = bdb_tool_entry_get( be, id );
661 Debug( LDAP_DEBUG_ANY,
662 LDAP_XSTRING(bdb_tool_entry_reindex)
663 ": could not locate id=%ld\n",
668 if (! (slapMode & SLAP_TOOL_QUICK)) {
669 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
671 Debug( LDAP_DEBUG_ANY,
672 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
673 "txn_begin failed: %s (%d)\n",
674 db_strerror(rc), rc, 0 );
680 * just (re)add them for now
681 * assume that some other routine (not yet implemented)
682 * will zap index databases
686 Debug( LDAP_DEBUG_TRACE,
687 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
688 (long) id, e->e_dn, 0 );
692 op.o_tmpmemctx = NULL;
693 op.o_tmpmfuncs = &ch_mfuncs;
695 rc = bdb_tool_index_add( &op, tid, e );
699 if (! (slapMode & SLAP_TOOL_QUICK)) {
700 rc = TXN_COMMIT( tid, 0 );
702 Debug( LDAP_DEBUG_ANY,
703 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
704 ": txn_commit failed: %s (%d)\n",
705 db_strerror(rc), rc, 0 );
711 if (! (slapMode & SLAP_TOOL_QUICK)) {
713 Debug( LDAP_DEBUG_ANY,
714 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
715 ": txn_aborted! %s (%d)\n",
716 db_strerror(rc), rc, 0 );
720 bdb_entry_release( &op, e, 0 );
725 ID bdb_tool_entry_modify(
728 struct berval *text )
731 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
736 assert( be != NULL );
737 assert( slapMode & SLAP_TOOL_MODE );
739 assert( text != NULL );
740 assert( text->bv_val != NULL );
741 assert( text->bv_val[0] == '\0' ); /* overconservative? */
743 assert ( e->e_id != NOID );
745 Debug( LDAP_DEBUG_TRACE,
746 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
747 (long) e->e_id, e->e_dn, 0 );
749 if (! (slapMode & SLAP_TOOL_QUICK)) {
751 cursor->c_close( cursor );
754 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
755 bdb->bi_db_opflags );
757 snprintf( text->bv_val, text->bv_len,
758 "txn_begin failed: %s (%d)",
759 db_strerror(rc), rc );
760 Debug( LDAP_DEBUG_ANY,
761 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
762 text->bv_val, 0, 0 );
769 op.o_tmpmemctx = NULL;
770 op.o_tmpmfuncs = &ch_mfuncs;
773 rc = bdb_id2entry_update( be, tid, e );
775 snprintf( text->bv_val, text->bv_len,
776 "id2entry_add failed: %s (%d)",
777 db_strerror(rc), rc );
778 Debug( LDAP_DEBUG_ANY,
779 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
780 text->bv_val, 0, 0 );
786 if (! (slapMode & SLAP_TOOL_QUICK)) {
787 rc = TXN_COMMIT( tid, 0 );
789 snprintf( text->bv_val, text->bv_len,
790 "txn_commit failed: %s (%d)",
791 db_strerror(rc), rc );
792 Debug( LDAP_DEBUG_ANY,
793 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
794 "%s\n", text->bv_val, 0, 0 );
800 if (! (slapMode & SLAP_TOOL_QUICK)) {
802 snprintf( text->bv_val, text->bv_len,
803 "txn_aborted! %s (%d)",
804 db_strerror(rc), rc );
805 Debug( LDAP_DEBUG_ANY,
806 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
807 text->bv_val, 0, 0 );
815 #ifdef BDB_TOOL_IDL_CACHING
817 bdb_tool_idl_cmp( const void *v1, const void *v2 )
819 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
822 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
823 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
827 bdb_tool_idl_flush_one( void *v1, void *arg )
829 bdb_tool_idl_cache *ic = v1;
831 struct bdb_info *bdb = bdb_tool_info;
832 bdb_tool_idl_cache_entry *ice;
838 /* Freshly allocated, ignore it */
839 if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) {
843 rc = db->cursor( db, NULL, &curs, 0 );
850 bv2DBT( &ic->kstr, &key );
852 data.size = data.ulen = sizeof( ID );
853 data.flags = DB_DBT_USERMEM;
856 rc = curs->c_get( curs, &key, &data, DB_SET );
857 /* If key already exists and we're writing a range... */
858 if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) {
859 /* If it's not currently a range, must delete old info */
862 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
863 curs->c_del( curs, 0 );
866 /* Store range marker */
867 curs->c_put( curs, &key, &data, DB_KEYFIRST );
871 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
874 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
877 curs->c_del( curs, 0 );
879 BDB_ID2DISK( ic->last, &nid );
880 curs->c_put( curs, &key, &data, DB_KEYLAST );
882 } else if ( rc && rc != DB_NOTFOUND ) {
884 } else if ( ic->count > BDB_IDL_DB_SIZE ) {
885 /* range, didn't exist before */
887 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
889 BDB_ID2DISK( ic->first, &nid );
890 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
892 BDB_ID2DISK( ic->last, &nid );
893 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
902 /* Just a normal write */
904 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
909 end = ic->count & (IDBLOCK-1);
913 for ( i=0; i<end; i++ ) {
914 if ( !ice->ids[i] ) continue;
915 BDB_ID2DISK( ice->ids[i], &nid );
916 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
918 if ( rc == DB_KEYEXIST ) {
932 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
933 ic->tail->next = bdb_tool_idl_free_list;
934 bdb_tool_idl_free_list = ic->head;
935 bdb->bi_idl_cache_size -= n;
936 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
939 if ( ic != db->app_private ) {
942 ic->head = ic->tail = NULL;
944 curs->c_close( curs );
949 bdb_tool_idl_flush_db( DB *db, bdb_tool_idl_cache *ic )
951 Avlnode *root = db->app_private;
954 db->app_private = ic;
955 rc = avl_apply( root, bdb_tool_idl_flush_one, db, -1, AVL_INORDER );
956 avl_free( root, NULL );
957 db->app_private = NULL;
964 bdb_tool_idl_flush( BackendDB *be )
966 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
971 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
972 db = bdb->bi_databases[i]->bdi_db;
973 if ( !db->app_private ) continue;
974 rc = bdb_tool_idl_flush_db( db, NULL );
979 bdb->bi_idl_cache_size = 0;
984 int bdb_tool_idl_add(
991 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
992 bdb_tool_idl_cache *ic, itmp;
993 bdb_tool_idl_cache_entry *ice;
996 if ( !bdb->bi_idl_cache_max_size )
997 return bdb_idl_insert_key( be, db, txn, key, id );
999 DBT2bv( key, &itmp.kstr );
1001 ic = avl_find( (Avlnode *)db->app_private, &itmp, bdb_tool_idl_cmp );
1003 /* No entry yet, create one */
1010 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
1011 ic->kstr.bv_len = itmp.kstr.bv_len;
1012 ic->kstr.bv_val = (char *)(ic+1);
1013 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1014 ic->head = ic->tail = NULL;
1017 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1020 /* load existing key count here */
1021 rc = db->cursor( db, NULL, &curs, 0 );
1022 if ( rc ) return rc;
1024 data.ulen = sizeof( ID );
1025 data.flags = DB_DBT_USERMEM;
1027 rc = curs->c_get( curs, key, &data, DB_SET );
1030 ic->count = BDB_IDL_DB_SIZE+1;
1034 curs->c_count( curs, &count, 0 );
1036 BDB_DISK2ID( &nid, &ic->first );
1039 curs->c_close( curs );
1041 /* are we a range already? */
1042 if ( ic->count > BDB_IDL_DB_SIZE ) {
1045 /* Are we at the limit, and converting to a range? */
1046 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
1048 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1051 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1052 ic->tail->next = bdb_tool_idl_free_list;
1053 bdb_tool_idl_free_list = ic->head;
1054 bdb->bi_idl_cache_size -= n;
1055 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1057 ic->head = ic->tail = NULL;
1062 /* No free block, create that too */
1063 if ( !ic->tail || ( ic->count & (IDBLOCK-1)) == 0) {
1065 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1066 if ( bdb->bi_idl_cache_size >= bdb->bi_idl_cache_max_size ) {
1067 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1068 rc = bdb_tool_idl_flush_db( db, ic );
1071 avl_insert( (Avlnode **)&db->app_private, ic, bdb_tool_idl_cmp,
1073 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
1075 bdb->bi_idl_cache_size++;
1076 if ( bdb_tool_idl_free_list ) {
1077 ice = bdb_tool_idl_free_list;
1078 bdb_tool_idl_free_list = ice->next;
1080 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
1082 ice = ch_malloc( sizeof( bdb_tool_idl_cache_entry ));
1084 memset( ice, 0, sizeof( *ice ));
1088 ic->tail->next = ice;
1095 ice->ids[ ic->count & (IDBLOCK-1) ] = id;
1103 bdb_tool_trickle_task( void *ctx, void *ptr )
1108 ldap_pvt_thread_mutex_lock( &bdb_tool_trickle_mutex );
1110 ldap_pvt_thread_cond_wait( &bdb_tool_trickle_cond,
1111 &bdb_tool_trickle_mutex );
1112 if ( slapd_shutdown )
1114 env->memp_trickle( env, 30, &wrote );
1116 ldap_pvt_thread_mutex_unlock( &bdb_tool_trickle_mutex );
1122 bdb_tool_index_task( void *ctx, void *ptr )
1124 int base = *(int *)ptr;
1128 ldap_pvt_thread_mutex_lock( &bdb_tool_index_mutex );
1129 bdb_tool_index_tcount--;
1130 if ( !bdb_tool_index_tcount )
1131 ldap_pvt_thread_cond_signal( &bdb_tool_index_cond_main );
1132 ldap_pvt_thread_cond_wait( &bdb_tool_index_cond_work,
1133 &bdb_tool_index_mutex );
1134 ldap_pvt_thread_mutex_unlock( &bdb_tool_index_mutex );
1135 if ( slapd_shutdown )
1138 bdb_tool_index_threads[base] = bdb_index_recrun( bdb_tool_ix_op,
1139 bdb_tool_info, bdb_tool_index_rec, bdb_tool_ix_id, base );