1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 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 #ifdef MDB_TOOL_IDL_CACHING
28 static int mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn );
32 typedef struct mdb_tool_idl_cache_entry {
33 struct mdb_tool_idl_cache_entry *next;
35 } mdb_tool_idl_cache_entry;
37 typedef struct mdb_tool_idl_cache {
39 mdb_tool_idl_cache_entry *head, *tail;
45 #define WAS_FOUND 0x01
46 #define WAS_RANGE 0x02
48 static mdb_tool_idl_cache_entry *mdb_tool_idl_free_list;
49 static Avlnode *mdb_tool_roots[MDB_INDICES];
51 #define MDB_TOOL_IDL_FLUSH(be, txn) mdb_tool_idl_flush(be, txn)
53 #define MDB_TOOL_IDL_FLUSH(be, txn)
54 #endif /* MDB_TOOL_IDL_CACHING */
56 static MDB_txn *txn = NULL, *txi = NULL;
57 static MDB_cursor *cursor = NULL, *idcursor = NULL;
58 static MDB_val key, data;
59 static ID previd = NOID;
61 typedef struct dn_id {
66 #define HOLE_SIZE 4096
67 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
68 static unsigned nhmax = HOLE_SIZE;
69 static unsigned nholes;
71 static struct berval *tool_base;
72 static int tool_scope;
73 static Filter *tool_filter;
74 static Entry *tool_next_entry;
77 static ID mdb_tool_ix_id;
78 static Operation *mdb_tool_ix_op;
79 static int *mdb_tool_index_threads, mdb_tool_index_tcount;
80 static void *mdb_tool_index_rec;
81 static struct mdb_info *mdb_tool_info;
82 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
83 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
84 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
85 static void * mdb_tool_index_task( void *ctx, void *ptr );
88 static int mdb_writes, mdb_writes_per_commit;
91 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
93 int mdb_tool_entry_open(
94 BackendDB *be, int mode )
96 /* In Quick mode, commit once per 1000 entries */
98 if ( slapMode & SLAP_TOOL_QUICK )
99 mdb_writes_per_commit = 1000;
101 mdb_writes_per_commit = 1;
104 /* Set up for threaded slapindex */
105 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
106 if ( !mdb_tool_info ) {
107 ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
108 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
109 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
110 if ( mdb->bi_nattrs ) {
112 mdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
113 mdb_tool_index_rec = ch_malloc( mdb->bi_nattrs * sizeof( IndexRec ));
114 mdb_tool_index_tcount = slap_tool_thread_max - 1;
115 for (i=1; i<slap_tool_thread_max; i++) {
116 int *ptr = ch_malloc( sizeof( int ));
118 ldap_pvt_thread_pool_submit( &connection_pool,
119 mdb_tool_index_task, ptr );
130 int mdb_tool_entry_close(
134 if ( mdb_tool_info ) {
136 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
138 /* There might still be some threads starting */
139 while ( mdb_tool_index_tcount ) {
140 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
141 &mdb_tool_index_mutex );
144 mdb_tool_index_tcount = slap_tool_thread_max - 1;
145 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
147 /* Make sure all threads are stopped */
148 while ( mdb_tool_index_tcount ) {
149 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
150 &mdb_tool_index_mutex );
152 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
154 mdb_tool_info = NULL;
156 ch_free( mdb_tool_index_threads );
157 ch_free( mdb_tool_index_rec );
158 mdb_tool_index_tcount = slap_tool_thread_max - 1;
163 mdb_cursor_close( idcursor );
167 mdb_cursor_close( cursor );
170 MDB_TOOL_IDL_FLUSH( be, txn );
172 if ( mdb_txn_commit( txn ))
179 fprintf( stderr, "Error, entries missing!\n");
180 for (i=0; i<nholes; i++) {
181 fprintf(stderr, " entry %ld: %s\n",
182 holes[i].id, holes[i].dn.bv_val);
192 mdb_tool_entry_first_x(
202 return mdb_tool_entry_next( be );
205 ID mdb_tool_entry_next(
210 struct mdb_info *mdb;
212 assert( be != NULL );
213 assert( slapMode & SLAP_TOOL_MODE );
215 mdb = (struct mdb_info *) be->be_private;
216 assert( mdb != NULL );
219 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
222 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
224 mdb_txn_abort( txn );
230 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
236 previd = *(ID *)key.mv_data;
239 if ( tool_filter || tool_base ) {
240 static Operation op = {0};
241 static Opheader ohdr = {0};
245 op.o_tmpmemctx = NULL;
246 op.o_tmpmfuncs = &ch_mfuncs;
248 if ( tool_next_entry ) {
249 mdb_entry_release( &op, tool_next_entry, 0 );
250 tool_next_entry = NULL;
253 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
254 if ( rc == LDAP_NO_SUCH_OBJECT ) {
258 assert( tool_next_entry != NULL );
260 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
262 mdb_entry_release( &op, tool_next_entry, 0 );
263 tool_next_entry = NULL;
271 ID mdb_tool_dn2id_get(
276 struct mdb_info *mdb;
282 if ( BER_BVISEMPTY(dn) )
285 mdb = (struct mdb_info *) be->be_private;
288 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
289 MDB_RDONLY : 0, &txn );
296 op.o_tmpmemctx = NULL;
297 op.o_tmpmfuncs = &ch_mfuncs;
299 rc = mdb_dn2id( &op, txn, dn, &id, NULL, NULL );
300 if ( rc == MDB_NOTFOUND )
307 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
313 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
316 assert( be != NULL );
317 assert( slapMode & SLAP_TOOL_MODE );
319 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
320 *ep = tool_next_entry;
321 tool_next_entry = NULL;
325 if ( id != previd ) {
326 key.mv_size = sizeof(ID);
328 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
337 op.o_tmpmemctx = NULL;
338 op.o_tmpmfuncs = &ch_mfuncs;
339 if ( slapMode & SLAP_TOOL_READONLY ) {
340 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
343 mdb_entry_return( &op, e );
347 if ( tool_base != NULL ) {
348 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
349 ch_free( dn.bv_val );
350 ch_free( ndn.bv_val );
351 rc = LDAP_NO_SUCH_OBJECT;
355 rc = mdb_entry_decode( &op, &data, &e );
357 if ( !BER_BVISNULL( &dn )) {
361 e->e_name.bv_val = NULL;
362 e->e_nname.bv_val = NULL;
374 mdb_tool_entry_get( BackendDB *be, ID id )
378 (void)mdb_tool_entry_get_int( be, id, &e );
382 static int mdb_tool_next_id(
389 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
390 struct berval dn = e->e_name;
391 struct berval ndn = e->e_nname;
392 struct berval pdn, npdn, nmatched;
396 if (ndn.bv_len == 0) {
401 rc = mdb_dn2id( op, tid, &ndn, &id, NULL, &nmatched );
402 if ( rc == MDB_NOTFOUND ) {
403 if ( !be_issuffix( op->o_bd, &ndn ) ) {
405 dnParent( &ndn, &npdn );
406 if ( nmatched.bv_len != npdn.bv_len ) {
407 dnParent( &dn, &pdn );
410 rc = mdb_tool_next_id( op, tid, e, text, 1 );
416 /* If parent didn't exist, it was created just now
417 * and its ID is now in e->e_id. Make sure the current
418 * entry gets added under the new parent ID.
420 if ( eid != e->e_id ) {
427 rc = mdb_next_id( op->o_bd, tid, &e->e_id );
429 snprintf( text->bv_val, text->bv_len,
430 "next_id failed: %s (%d)",
431 mdb_strerror(rc), rc );
432 Debug( LDAP_DEBUG_ANY,
433 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
436 rc = mdb_dn2id_add( op, tid, pid, e );
438 snprintf( text->bv_val, text->bv_len,
439 "dn2id_add failed: %s (%d)",
440 mdb_strerror(rc), rc );
441 Debug( LDAP_DEBUG_ANY,
442 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
445 if ( nholes == nhmax - 1 ) {
446 if ( holes == hbuf ) {
447 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
448 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
450 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
454 ber_dupbv( &holes[nholes].dn, &ndn );
455 holes[nholes++].id = e->e_id;
456 key.mv_size = sizeof(ID);
457 key.mv_data = &e->e_id;
460 rc = mdb_put( tid, mdb->mi_id2entry, &key, &data, MDB_NOOVERWRITE );
461 if ( rc == MDB_KEYEXIST )
464 snprintf( text->bv_val, text->bv_len,
465 "dummy id2entry add failed: %s (%d)",
466 mdb_strerror(rc), rc );
467 Debug( LDAP_DEBUG_ANY,
468 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
471 } else if ( !hole ) {
476 for ( i=0; i<nholes; i++) {
477 if ( holes[i].id == e->e_id ) {
478 free(holes[i].dn.bv_val);
479 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
483 } else if ( holes[i].id > e->e_id ) {
497 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
499 if ( !mdb->mi_nattrs )
503 if ( slapMode & SLAP_TOOL_QUICK ) {
508 ir = mdb_tool_index_rec;
509 memset(ir, 0, mdb->bi_nattrs * sizeof( IndexRec ));
511 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
512 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
513 &a->a_desc->ad_tags, ir );
517 mdb_tool_ix_id = e->e_id;
519 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
520 /* Wait for all threads to be ready */
521 while ( mdb_tool_index_tcount ) {
522 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
523 &mdb_tool_index_mutex );
525 for ( i=1; i<slap_tool_thread_max; i++ )
526 mdb_tool_index_threads[i] = LDAP_BUSY;
527 mdb_tool_index_tcount = slap_tool_thread_max - 1;
528 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
529 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
530 rc = mdb_index_recrun( op, mdb, ir, e->e_id, 0 );
533 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
534 for ( i=1; i<slap_tool_thread_max; i++ ) {
535 if ( mdb_tool_index_threads[i] == LDAP_BUSY ) {
536 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
537 &mdb_tool_index_mutex );
541 if ( mdb_tool_index_threads[i] ) {
542 rc = mdb_tool_index_threads[i];
546 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
551 return mdb_index_entry_add( op, txn, e );
555 ID mdb_tool_entry_put(
558 struct berval *text )
561 struct mdb_info *mdb;
565 assert( be != NULL );
566 assert( slapMode & SLAP_TOOL_MODE );
568 assert( text != NULL );
569 assert( text->bv_val != NULL );
570 assert( text->bv_val[0] == '\0' ); /* overconservative? */
572 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
573 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
575 mdb = (struct mdb_info *) be->be_private;
578 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
580 snprintf( text->bv_val, text->bv_len,
581 "txn_begin failed: %s (%d)",
582 mdb_strerror(rc), rc );
583 Debug( LDAP_DEBUG_ANY,
584 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
585 text->bv_val, 0, 0 );
592 op.o_tmpmemctx = NULL;
593 op.o_tmpmfuncs = &ch_mfuncs;
595 /* add dn2id indices */
596 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
601 rc = mdb_tool_index_add( &op, txn, e );
603 snprintf( text->bv_val, text->bv_len,
604 "index_entry_add failed: err=%d", rc );
605 Debug( LDAP_DEBUG_ANY,
606 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
607 text->bv_val, 0, 0 );
613 rc = mdb_id2entry_add( &op, txn, e );
615 snprintf( text->bv_val, text->bv_len,
616 "id2entry_add failed: err=%d", rc );
617 Debug( LDAP_DEBUG_ANY,
618 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
619 text->bv_val, 0, 0 );
626 if ( mdb_writes >= mdb_writes_per_commit ) {
627 MDB_TOOL_IDL_FLUSH( be, txn );
628 rc = mdb_txn_commit( txn );
632 snprintf( text->bv_val, text->bv_len,
633 "txn_commit failed: %s (%d)",
634 mdb_strerror(rc), rc );
635 Debug( LDAP_DEBUG_ANY,
636 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
637 text->bv_val, 0, 0 );
643 mdb_txn_abort( txn );
645 snprintf( text->bv_val, text->bv_len,
646 "txn_aborted! %s (%d)",
647 rc == LDAP_OTHER ? "Internal error" :
648 mdb_strerror(rc), rc );
649 Debug( LDAP_DEBUG_ANY,
650 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
651 text->bv_val, 0, 0 );
658 int mdb_tool_entry_reindex(
661 AttributeDescription **adv )
663 struct mdb_info *mi = (struct mdb_info *) be->be_private;
669 Debug( LDAP_DEBUG_ARGS,
670 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
672 assert( tool_base == NULL );
673 assert( tool_filter == NULL );
675 /* No indexes configured, nothing to do. Could return an
676 * error here to shortcut things.
682 /* Check for explicit list of attrs to index */
686 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
688 for ( n = 0; adv[n]; n++ ) ;
691 for ( i = 0; i < n; i++ ) {
692 AttributeDescription *ad = adv[i];
693 for ( j = i-1; j>=0; j--) {
694 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
701 for ( i = 0; adv[i]; i++ ) {
702 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
703 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
704 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
705 AttrInfo *ai = mi->mi_attrs[i];
706 mi->mi_attrs[i] = mi->mi_attrs[j];
707 mi->mi_attrs[j] = ai;
711 if ( j == mi->mi_nattrs ) {
712 Debug( LDAP_DEBUG_ANY,
713 LDAP_XSTRING(mdb_tool_entry_reindex)
714 ": no index configured for %s\n",
715 adv[i]->ad_cname.bv_val, 0, 0 );
723 if ( slapMode & SLAP_TRUNCATE_MODE ) {
725 for ( i=0; i < mi->mi_nattrs; i++ ) {
726 rc = mdb_drop( txn, mi->mi_attrs[i]->ai_dbi, 0 );
728 Debug( LDAP_DEBUG_ANY,
729 LDAP_XSTRING(mdb_tool_entry_reindex)
730 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
731 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
732 mdb_strerror(rc), rc );
736 slapMode ^= SLAP_TRUNCATE_MODE;
739 e = mdb_tool_entry_get( be, id );
742 Debug( LDAP_DEBUG_ANY,
743 LDAP_XSTRING(mdb_tool_entry_reindex)
744 ": could not locate id=%ld\n",
750 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
752 Debug( LDAP_DEBUG_ANY,
753 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
754 "txn_begin failed: %s (%d)\n",
755 mdb_strerror(rc), rc, 0 );
761 * just (re)add them for now
762 * assume that some other routine (not yet implemented)
763 * will zap index databases
767 Debug( LDAP_DEBUG_TRACE,
768 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
769 (long) id, e->e_dn, 0 );
773 op.o_tmpmemctx = NULL;
774 op.o_tmpmfuncs = &ch_mfuncs;
776 rc = mdb_tool_index_add( &op, txi, e );
781 if ( mdb_writes >= mdb_writes_per_commit ) {
782 MDB_TOOL_IDL_FLUSH( be, txi );
783 rc = mdb_txn_commit( txi );
785 Debug( LDAP_DEBUG_ANY,
786 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
787 ": txn_commit failed: %s (%d)\n",
788 mdb_strerror(rc), rc, 0 );
795 mdb_txn_abort( txi );
796 Debug( LDAP_DEBUG_ANY,
797 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
798 ": txn_aborted! err=%d\n",
803 mdb_entry_release( &op, e, 0 );
808 ID mdb_tool_entry_modify(
811 struct berval *text )
814 struct mdb_info *mdb;
819 assert( be != NULL );
820 assert( slapMode & SLAP_TOOL_MODE );
822 assert( text != NULL );
823 assert( text->bv_val != NULL );
824 assert( text->bv_val[0] == '\0' ); /* overconservative? */
826 assert ( e->e_id != NOID );
828 Debug( LDAP_DEBUG_TRACE,
829 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
830 (long) e->e_id, e->e_dn, 0 );
832 mdb = (struct mdb_info *) be->be_private;
835 mdb_cursor_close( cursor );
838 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &tid );
840 snprintf( text->bv_val, text->bv_len,
841 "txn_begin failed: %s (%d)",
842 mdb_strerror(rc), rc );
843 Debug( LDAP_DEBUG_ANY,
844 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
845 text->bv_val, 0, 0 );
851 op.o_tmpmemctx = NULL;
852 op.o_tmpmfuncs = &ch_mfuncs;
855 rc = mdb_id2entry_update( &op, tid, e );
857 snprintf( text->bv_val, text->bv_len,
858 "id2entry_update failed: err=%d", rc );
859 Debug( LDAP_DEBUG_ANY,
860 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
861 text->bv_val, 0, 0 );
867 rc = mdb_txn_commit( tid );
869 snprintf( text->bv_val, text->bv_len,
870 "txn_commit failed: %s (%d)",
871 mdb_strerror(rc), rc );
872 Debug( LDAP_DEBUG_ANY,
873 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
874 "%s\n", text->bv_val, 0, 0 );
879 mdb_txn_abort( tid );
880 snprintf( text->bv_val, text->bv_len,
881 "txn_aborted! %s (%d)",
882 mdb_strerror(rc), rc );
883 Debug( LDAP_DEBUG_ANY,
884 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
885 text->bv_val, 0, 0 );
894 mdb_tool_index_task( void *ctx, void *ptr )
896 int base = *(int *)ptr;
900 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
901 mdb_tool_index_tcount--;
902 if ( !mdb_tool_index_tcount )
903 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
904 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
905 &mdb_tool_index_mutex );
906 if ( slapd_shutdown ) {
907 mdb_tool_index_tcount--;
908 if ( !mdb_tool_index_tcount )
909 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
910 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
913 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
915 mdb_tool_index_threads[base] = mdb_index_recrun( mdb_tool_ix_op,
916 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
923 #ifdef MDB_TOOL_IDL_CACHING
925 mdb_tool_idl_cmp( const void *v1, const void *v2 )
927 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
930 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
931 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
935 mdb_tool_idl_flush_one( MDB_cursor *mc, mdb_tool_idl_cache *ic )
937 mdb_tool_idl_cache_entry *ice;
938 MDB_val key, data[2];
942 /* Freshly allocated, ignore it */
943 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
947 key.mv_data = ic->kstr.bv_val;
948 key.mv_size = ic->kstr.bv_len;
950 if ( ic->count > MDB_IDL_DB_SIZE ) {
951 while ( ic->flags & WAS_FOUND ) {
952 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
954 /* FIXME: find out why this happens */
958 if ( ic->flags & WAS_RANGE ) {
960 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
963 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
966 data[0].mv_data = &ic->last;
967 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
969 /* Delete old data, replace with range */
970 ic->first = *(ID *)data[0].mv_data;
971 mdb_cursor_del( mc, MDB_NODUPDATA );
975 if ( !(ic->flags & WAS_RANGE)) {
976 /* range, didn't exist before */
978 data[0].mv_size = sizeof(ID);
979 data[0].mv_data = &nid;
980 rc = mdb_cursor_put( mc, &key, data, 0 );
982 data[0].mv_data = &ic->first;
983 rc = mdb_cursor_put( mc, &key, data, 0 );
985 data[0].mv_data = &ic->last;
986 rc = mdb_cursor_put( mc, &key, data, 0 );
997 data[0].mv_size = sizeof(ID);
1000 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
1005 end = ic->count & (IDBLOCK-1);
1009 data[1].mv_size = end - i;
1010 data[0].mv_data = &ice->ids[i];
1012 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1014 if ( rc == MDB_KEYEXIST ) {
1023 ic->tail->next = mdb_tool_idl_free_list;
1024 mdb_tool_idl_free_list = ic->head;
1032 mdb_tool_idl_flush_db( MDB_txn *txn, MDB_dbi dbi, Avlnode *root )
1037 mdb_cursor_open( txn, dbi, &mc );
1038 root = tavl_end( root, TAVL_DIR_LEFT );
1040 rc = mdb_tool_idl_flush_one( mc, root->avl_data );
1043 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1044 mdb_cursor_close( mc );
1050 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1052 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1054 unsigned int i, dbi;
1056 for ( i=0; i < mdb->mi_nattrs; i++ ) {
1057 dbi = mdb->mi_attrs[i]->ai_dbi;
1058 if ( !mdb_tool_roots[dbi] ) continue;
1059 rc = mdb_tool_idl_flush_db( txn, dbi, mdb_tool_roots[dbi] );
1060 tavl_free(mdb_tool_roots[dbi], NULL);
1061 mdb_tool_roots[dbi] = NULL;
1068 int mdb_tool_idl_add(
1070 struct berval *keys,
1074 mdb_tool_idl_cache *ic, itmp;
1075 mdb_tool_idl_cache_entry *ice;
1078 dbi = mdb_cursor_dbi(mc);
1079 for (i=0; keys[i].bv_val; i++) {
1080 itmp.kstr = keys[i];
1081 ic = tavl_find( (Avlnode *)mdb_tool_roots[dbi], &itmp, mdb_tool_idl_cmp );
1083 /* No entry yet, create one */
1089 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len );
1090 ic->kstr.bv_len = itmp.kstr.bv_len;
1091 ic->kstr.bv_val = (char *)(ic+1);
1092 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1093 ic->head = ic->tail = NULL;
1098 tavl_insert( (Avlnode **)&mdb_tool_roots[dbi], ic, mdb_tool_idl_cmp,
1101 /* load existing key count here */
1102 key.mv_size = keys[i].bv_len;
1103 key.mv_data = keys[i].bv_val;
1104 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1106 ic->flags |= WAS_FOUND;
1107 nid = *(ID *)data.mv_data;
1109 ic->count = MDB_IDL_DB_SIZE+1;
1110 ic->flags |= WAS_RANGE;
1114 mdb_cursor_count( mc, &count );
1117 ic->offset = count & (IDBLOCK-1);
1121 /* are we a range already? */
1122 if ( ic->count > MDB_IDL_DB_SIZE ) {
1125 /* Are we at the limit, and converting to a range? */
1126 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1128 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1131 ic->tail->next = mdb_tool_idl_free_list;
1132 mdb_tool_idl_free_list = ic->head;
1134 ic->head = ic->tail = NULL;
1139 /* No free block, create that too */
1140 lcount = ic->count & (IDBLOCK-1);
1141 if ( !ic->tail || lcount == 0) {
1143 if ( mdb_tool_idl_free_list ) {
1144 ice = mdb_tool_idl_free_list;
1145 mdb_tool_idl_free_list = ice->next;
1148 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1154 ic->tail->next = ice;
1158 ice->ids[lcount-1] = 0;
1163 if (!lcount || ice->ids[lcount-1] != id)
1164 ice->ids[lcount] = id;
1170 #endif /* MDB_TOOL_IDL_CACHING */