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 static int mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn );
31 typedef struct mdb_tool_idl_cache_entry {
32 struct mdb_tool_idl_cache_entry *next;
34 } mdb_tool_idl_cache_entry;
36 typedef struct mdb_tool_idl_cache {
38 mdb_tool_idl_cache_entry *head, *tail;
44 #define WAS_FOUND 0x01
45 #define WAS_RANGE 0x02
47 static mdb_tool_idl_cache_entry *mdb_tool_idl_free_list;
48 static Avlnode *mdb_tool_roots[MDB_INDICES];
50 static MDB_txn *txn = NULL, *txi = NULL;
51 static MDB_cursor *cursor = NULL, *idcursor = NULL;
52 static MDB_val key, data;
53 static ID previd = NOID;
55 typedef struct dn_id {
60 #define HOLE_SIZE 4096
61 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
62 static unsigned nhmax = HOLE_SIZE;
63 static unsigned nholes;
65 static struct berval *tool_base;
66 static int tool_scope;
67 static Filter *tool_filter;
68 static Entry *tool_next_entry;
71 static ID mdb_tool_ix_id;
72 static Operation *mdb_tool_ix_op;
73 static int *mdb_tool_index_threads, mdb_tool_index_tcount;
74 static void *mdb_tool_index_rec;
75 static struct mdb_info *mdb_tool_info;
76 static ldap_pvt_thread_mutex_t mdb_tool_index_mutex;
77 static ldap_pvt_thread_cond_t mdb_tool_index_cond_main;
78 static ldap_pvt_thread_cond_t mdb_tool_index_cond_work;
79 static void * mdb_tool_index_task( void *ctx, void *ptr );
82 static int mdb_writes, mdb_writes_per_commit;
85 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep );
87 int mdb_tool_entry_open(
88 BackendDB *be, int mode )
90 /* In Quick mode, commit once per 1000 entries */
92 if ( slapMode & SLAP_TOOL_QUICK )
93 mdb_writes_per_commit = 1000;
95 mdb_writes_per_commit = 1;
98 /* Set up for threaded slapindex */
99 if (( slapMode & (SLAP_TOOL_QUICK|SLAP_TOOL_READONLY)) == SLAP_TOOL_QUICK ) {
100 if ( !mdb_tool_info ) {
101 ldap_pvt_thread_mutex_init( &mdb_tool_index_mutex );
102 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_main );
103 ldap_pvt_thread_cond_init( &mdb_tool_index_cond_work );
104 if ( mdb->bi_nattrs ) {
106 mdb_tool_index_threads = ch_malloc( slap_tool_thread_max * sizeof( int ));
107 mdb_tool_index_rec = ch_malloc( mdb->bi_nattrs * sizeof( IndexRec ));
108 mdb_tool_index_tcount = slap_tool_thread_max - 1;
109 for (i=1; i<slap_tool_thread_max; i++) {
110 int *ptr = ch_malloc( sizeof( int ));
112 ldap_pvt_thread_pool_submit( &connection_pool,
113 mdb_tool_index_task, ptr );
124 int mdb_tool_entry_close(
128 if ( mdb_tool_info ) {
130 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
132 /* There might still be some threads starting */
133 while ( mdb_tool_index_tcount ) {
134 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
135 &mdb_tool_index_mutex );
138 mdb_tool_index_tcount = slap_tool_thread_max - 1;
139 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
141 /* Make sure all threads are stopped */
142 while ( mdb_tool_index_tcount ) {
143 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
144 &mdb_tool_index_mutex );
146 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
148 mdb_tool_info = NULL;
150 ch_free( mdb_tool_index_threads );
151 ch_free( mdb_tool_index_rec );
152 mdb_tool_index_tcount = slap_tool_thread_max - 1;
157 mdb_cursor_close( idcursor );
161 mdb_cursor_close( cursor );
164 mdb_tool_idl_flush( be, txn );
166 if ( mdb_txn_commit( txn ))
173 fprintf( stderr, "Error, entries missing!\n");
174 for (i=0; i<nholes; i++) {
175 fprintf(stderr, " entry %ld: %s\n",
176 holes[i].id, holes[i].dn.bv_val);
186 mdb_tool_entry_first_x(
196 return mdb_tool_entry_next( be );
199 ID mdb_tool_entry_next(
204 struct mdb_info *mdb;
206 assert( be != NULL );
207 assert( slapMode & SLAP_TOOL_MODE );
209 mdb = (struct mdb_info *) be->be_private;
210 assert( mdb != NULL );
213 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &txn );
216 rc = mdb_cursor_open( txn, mdb->mi_id2entry, &cursor );
218 mdb_txn_abort( txn );
224 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT );
230 previd = *(ID *)key.mv_data;
233 if ( tool_filter || tool_base ) {
234 static Operation op = {0};
235 static Opheader ohdr = {0};
239 op.o_tmpmemctx = NULL;
240 op.o_tmpmfuncs = &ch_mfuncs;
242 if ( tool_next_entry ) {
243 mdb_entry_release( &op, tool_next_entry, 0 );
244 tool_next_entry = NULL;
247 rc = mdb_tool_entry_get_int( be, id, &tool_next_entry );
248 if ( rc == LDAP_NO_SUCH_OBJECT ) {
252 assert( tool_next_entry != NULL );
254 if ( tool_filter && test_filter( NULL, tool_next_entry, tool_filter ) != LDAP_COMPARE_TRUE )
256 mdb_entry_release( &op, tool_next_entry, 0 );
257 tool_next_entry = NULL;
265 ID mdb_tool_dn2id_get(
270 struct mdb_info *mdb;
276 if ( BER_BVISEMPTY(dn) )
279 mdb = (struct mdb_info *) be->be_private;
282 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, (slapMode & SLAP_TOOL_READONLY) != 0 ?
283 MDB_RDONLY : 0, &txn );
290 op.o_tmpmemctx = NULL;
291 op.o_tmpmfuncs = &ch_mfuncs;
293 rc = mdb_dn2id( &op, txn, dn, &id, NULL, NULL );
294 if ( rc == MDB_NOTFOUND )
301 mdb_tool_entry_get_int( BackendDB *be, ID id, Entry **ep )
307 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
310 assert( be != NULL );
311 assert( slapMode & SLAP_TOOL_MODE );
313 if ( ( tool_filter || tool_base ) && id == previd && tool_next_entry != NULL ) {
314 *ep = tool_next_entry;
315 tool_next_entry = NULL;
319 if ( id != previd ) {
320 key.mv_size = sizeof(ID);
322 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
331 op.o_tmpmemctx = NULL;
332 op.o_tmpmfuncs = &ch_mfuncs;
333 if ( slapMode & SLAP_TOOL_READONLY ) {
334 rc = mdb_id2name( &op, txn, &idcursor, id, &dn, &ndn );
337 mdb_entry_return( &op, e );
341 if ( tool_base != NULL ) {
342 if ( !dnIsSuffixScope( &ndn, tool_base, tool_scope ) ) {
343 ch_free( dn.bv_val );
344 ch_free( ndn.bv_val );
345 rc = LDAP_NO_SUCH_OBJECT;
349 rc = mdb_entry_decode( &op, &data, &e );
351 if ( !BER_BVISNULL( &dn )) {
355 e->e_name.bv_val = NULL;
356 e->e_nname.bv_val = NULL;
368 mdb_tool_entry_get( BackendDB *be, ID id )
372 (void)mdb_tool_entry_get_int( be, id, &e );
376 static int mdb_tool_next_id(
383 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
384 struct berval dn = e->e_name;
385 struct berval ndn = e->e_nname;
386 struct berval pdn, npdn, nmatched;
390 if (ndn.bv_len == 0) {
395 rc = mdb_dn2id( op, tid, &ndn, &id, NULL, &nmatched );
396 if ( rc == MDB_NOTFOUND ) {
397 if ( !be_issuffix( op->o_bd, &ndn ) ) {
399 dnParent( &ndn, &npdn );
400 if ( nmatched.bv_len != npdn.bv_len ) {
401 dnParent( &dn, &pdn );
404 rc = mdb_tool_next_id( op, tid, e, text, 1 );
410 /* If parent didn't exist, it was created just now
411 * and its ID is now in e->e_id. Make sure the current
412 * entry gets added under the new parent ID.
414 if ( eid != e->e_id ) {
421 rc = mdb_next_id( op->o_bd, tid, &e->e_id );
423 snprintf( text->bv_val, text->bv_len,
424 "next_id failed: %s (%d)",
425 mdb_strerror(rc), rc );
426 Debug( LDAP_DEBUG_ANY,
427 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
430 rc = mdb_dn2id_add( op, tid, pid, e );
432 snprintf( text->bv_val, text->bv_len,
433 "dn2id_add failed: %s (%d)",
434 mdb_strerror(rc), rc );
435 Debug( LDAP_DEBUG_ANY,
436 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
439 if ( nholes == nhmax - 1 ) {
440 if ( holes == hbuf ) {
441 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
442 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
444 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
448 ber_dupbv( &holes[nholes].dn, &ndn );
449 holes[nholes++].id = e->e_id;
450 key.mv_size = sizeof(ID);
451 key.mv_data = &e->e_id;
454 rc = mdb_put( tid, mdb->mi_id2entry, &key, &data, MDB_NOOVERWRITE );
455 if ( rc == MDB_KEYEXIST )
458 snprintf( text->bv_val, text->bv_len,
459 "dummy id2entry add failed: %s (%d)",
460 mdb_strerror(rc), rc );
461 Debug( LDAP_DEBUG_ANY,
462 "=> mdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
465 } else if ( !hole ) {
470 for ( i=0; i<nholes; i++) {
471 if ( holes[i].id == e->e_id ) {
472 free(holes[i].dn.bv_val);
473 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
477 } else if ( holes[i].id > e->e_id ) {
491 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
493 if ( !mdb->mi_nattrs )
497 if ( slapMode & SLAP_TOOL_QUICK ) {
502 ir = mdb_tool_index_rec;
503 memset(ir, 0, mdb->bi_nattrs * sizeof( IndexRec ));
505 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
506 rc = mdb_index_recset( mdb, a, a->a_desc->ad_type,
507 &a->a_desc->ad_tags, ir );
511 mdb_tool_ix_id = e->e_id;
513 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
514 /* Wait for all threads to be ready */
515 while ( mdb_tool_index_tcount ) {
516 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
517 &mdb_tool_index_mutex );
519 for ( i=1; i<slap_tool_thread_max; i++ )
520 mdb_tool_index_threads[i] = LDAP_BUSY;
521 mdb_tool_index_tcount = slap_tool_thread_max - 1;
522 ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );
523 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
524 rc = mdb_index_recrun( op, mdb, ir, e->e_id, 0 );
527 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
528 for ( i=1; i<slap_tool_thread_max; i++ ) {
529 if ( mdb_tool_index_threads[i] == LDAP_BUSY ) {
530 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
531 &mdb_tool_index_mutex );
535 if ( mdb_tool_index_threads[i] ) {
536 rc = mdb_tool_index_threads[i];
540 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
545 return mdb_index_entry_add( op, txn, e );
549 ID mdb_tool_entry_put(
552 struct berval *text )
555 struct mdb_info *mdb;
559 assert( be != NULL );
560 assert( slapMode & SLAP_TOOL_MODE );
562 assert( text != NULL );
563 assert( text->bv_val != NULL );
564 assert( text->bv_val[0] == '\0' ); /* overconservative? */
566 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_tool_entry_put)
567 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
569 mdb = (struct mdb_info *) be->be_private;
572 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
574 snprintf( text->bv_val, text->bv_len,
575 "txn_begin failed: %s (%d)",
576 mdb_strerror(rc), rc );
577 Debug( LDAP_DEBUG_ANY,
578 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
579 text->bv_val, 0, 0 );
586 op.o_tmpmemctx = NULL;
587 op.o_tmpmfuncs = &ch_mfuncs;
589 /* add dn2id indices */
590 rc = mdb_tool_next_id( &op, txn, e, text, 0 );
595 rc = mdb_tool_index_add( &op, txn, e );
597 snprintf( text->bv_val, text->bv_len,
598 "index_entry_add failed: err=%d", rc );
599 Debug( LDAP_DEBUG_ANY,
600 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
601 text->bv_val, 0, 0 );
607 rc = mdb_id2entry_add( &op, txn, e );
609 snprintf( text->bv_val, text->bv_len,
610 "id2entry_add failed: err=%d", rc );
611 Debug( LDAP_DEBUG_ANY,
612 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
613 text->bv_val, 0, 0 );
620 if ( mdb_writes >= mdb_writes_per_commit ) {
621 mdb_tool_idl_flush( be, txn );
622 rc = mdb_txn_commit( txn );
626 snprintf( text->bv_val, text->bv_len,
627 "txn_commit failed: %s (%d)",
628 mdb_strerror(rc), rc );
629 Debug( LDAP_DEBUG_ANY,
630 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
631 text->bv_val, 0, 0 );
637 mdb_txn_abort( txn );
639 snprintf( text->bv_val, text->bv_len,
640 "txn_aborted! %s (%d)",
641 rc == LDAP_OTHER ? "Internal error" :
642 mdb_strerror(rc), rc );
643 Debug( LDAP_DEBUG_ANY,
644 "=> " LDAP_XSTRING(mdb_tool_entry_put) ": %s\n",
645 text->bv_val, 0, 0 );
652 int mdb_tool_entry_reindex(
655 AttributeDescription **adv )
657 struct mdb_info *mi = (struct mdb_info *) be->be_private;
663 Debug( LDAP_DEBUG_ARGS,
664 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld )\n",
666 assert( tool_base == NULL );
667 assert( tool_filter == NULL );
669 /* No indexes configured, nothing to do. Could return an
670 * error here to shortcut things.
676 /* Check for explicit list of attrs to index */
680 if ( mi->mi_attrs[0]->ai_desc != adv[0] ) {
682 for ( n = 0; adv[n]; n++ ) ;
685 for ( i = 0; i < n; i++ ) {
686 AttributeDescription *ad = adv[i];
687 for ( j = i-1; j>=0; j--) {
688 if ( SLAP_PTRCMP( adv[j], ad ) <= 0 ) break;
695 for ( i = 0; adv[i]; i++ ) {
696 if ( mi->mi_attrs[i]->ai_desc != adv[i] ) {
697 for ( j = i+1; j < mi->mi_nattrs; j++ ) {
698 if ( mi->mi_attrs[j]->ai_desc == adv[i] ) {
699 AttrInfo *ai = mi->mi_attrs[i];
700 mi->mi_attrs[i] = mi->mi_attrs[j];
701 mi->mi_attrs[j] = ai;
705 if ( j == mi->mi_nattrs ) {
706 Debug( LDAP_DEBUG_ANY,
707 LDAP_XSTRING(mdb_tool_entry_reindex)
708 ": no index configured for %s\n",
709 adv[i]->ad_cname.bv_val, 0, 0 );
717 if ( slapMode & SLAP_TRUNCATE_MODE ) {
719 for ( i=0; i < mi->mi_nattrs; i++ ) {
720 rc = mdb_drop( txn, mi->mi_attrs[i]->ai_dbi, 0 );
722 Debug( LDAP_DEBUG_ANY,
723 LDAP_XSTRING(mdb_tool_entry_reindex)
724 ": (Truncate) mdb_drop(%s) failed: %s (%d)\n",
725 mi->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
726 mdb_strerror(rc), rc );
730 slapMode ^= SLAP_TRUNCATE_MODE;
733 e = mdb_tool_entry_get( be, id );
736 Debug( LDAP_DEBUG_ANY,
737 LDAP_XSTRING(mdb_tool_entry_reindex)
738 ": could not locate id=%ld\n",
744 rc = mdb_txn_begin( mi->mi_dbenv, NULL, 0, &txi );
746 Debug( LDAP_DEBUG_ANY,
747 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) ": "
748 "txn_begin failed: %s (%d)\n",
749 mdb_strerror(rc), rc, 0 );
755 * just (re)add them for now
756 * assume that some other routine (not yet implemented)
757 * will zap index databases
761 Debug( LDAP_DEBUG_TRACE,
762 "=> " LDAP_XSTRING(mdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
763 (long) id, e->e_dn, 0 );
767 op.o_tmpmemctx = NULL;
768 op.o_tmpmfuncs = &ch_mfuncs;
770 rc = mdb_tool_index_add( &op, txi, e );
775 if ( mdb_writes >= mdb_writes_per_commit ) {
776 mdb_tool_idl_flush( be, txi );
777 rc = mdb_txn_commit( txi );
779 Debug( LDAP_DEBUG_ANY,
780 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
781 ": txn_commit failed: %s (%d)\n",
782 mdb_strerror(rc), rc, 0 );
789 mdb_txn_abort( txi );
790 Debug( LDAP_DEBUG_ANY,
791 "=> " LDAP_XSTRING(mdb_tool_entry_reindex)
792 ": txn_aborted! err=%d\n",
797 mdb_entry_release( &op, e, 0 );
802 ID mdb_tool_entry_modify(
805 struct berval *text )
808 struct mdb_info *mdb;
813 assert( be != NULL );
814 assert( slapMode & SLAP_TOOL_MODE );
816 assert( text != NULL );
817 assert( text->bv_val != NULL );
818 assert( text->bv_val[0] == '\0' ); /* overconservative? */
820 assert ( e->e_id != NOID );
822 Debug( LDAP_DEBUG_TRACE,
823 "=> " LDAP_XSTRING(mdb_tool_entry_modify) "( %ld, \"%s\" )\n",
824 (long) e->e_id, e->e_dn, 0 );
826 mdb = (struct mdb_info *) be->be_private;
829 mdb_cursor_close( cursor );
832 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &tid );
834 snprintf( text->bv_val, text->bv_len,
835 "txn_begin failed: %s (%d)",
836 mdb_strerror(rc), rc );
837 Debug( LDAP_DEBUG_ANY,
838 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
839 text->bv_val, 0, 0 );
845 op.o_tmpmemctx = NULL;
846 op.o_tmpmfuncs = &ch_mfuncs;
849 rc = mdb_id2entry_update( &op, tid, e );
851 snprintf( text->bv_val, text->bv_len,
852 "id2entry_update failed: err=%d", rc );
853 Debug( LDAP_DEBUG_ANY,
854 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
855 text->bv_val, 0, 0 );
861 rc = mdb_txn_commit( tid );
863 snprintf( text->bv_val, text->bv_len,
864 "txn_commit failed: %s (%d)",
865 mdb_strerror(rc), rc );
866 Debug( LDAP_DEBUG_ANY,
867 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": "
868 "%s\n", text->bv_val, 0, 0 );
873 mdb_txn_abort( tid );
874 snprintf( text->bv_val, text->bv_len,
875 "txn_aborted! %s (%d)",
876 mdb_strerror(rc), rc );
877 Debug( LDAP_DEBUG_ANY,
878 "=> " LDAP_XSTRING(mdb_tool_entry_modify) ": %s\n",
879 text->bv_val, 0, 0 );
888 mdb_tool_index_task( void *ctx, void *ptr )
890 int base = *(int *)ptr;
894 ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );
895 mdb_tool_index_tcount--;
896 if ( !mdb_tool_index_tcount )
897 ldap_pvt_thread_cond_signal( &mdb_tool_index_cond_main );
898 ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_work,
899 &mdb_tool_index_mutex );
900 if ( slapd_shutdown ) {
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_mutex_unlock( &mdb_tool_index_mutex );
907 ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );
909 mdb_tool_index_threads[base] = mdb_index_recrun( mdb_tool_ix_op,
910 mdb_tool_info, mdb_tool_index_rec, mdb_tool_ix_id, base );
918 mdb_tool_idl_cmp( const void *v1, const void *v2 )
920 const mdb_tool_idl_cache *c1 = v1, *c2 = v2;
923 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
924 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
928 mdb_tool_idl_flush_one( MDB_cursor *mc, mdb_tool_idl_cache *ic )
930 mdb_tool_idl_cache_entry *ice;
931 MDB_val key, data[2];
935 /* Freshly allocated, ignore it */
936 if ( !ic->head && ic->count <= MDB_IDL_DB_SIZE ) {
940 key.mv_data = ic->kstr.bv_val;
941 key.mv_size = ic->kstr.bv_len;
943 if ( ic->count > MDB_IDL_DB_SIZE ) {
944 while ( ic->flags & WAS_FOUND ) {
945 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
947 /* FIXME: find out why this happens */
951 if ( ic->flags & WAS_RANGE ) {
953 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
956 rc = mdb_cursor_get( mc, &key, data, MDB_NEXT_DUP );
959 data[0].mv_data = &ic->last;
960 rc = mdb_cursor_put( mc, &key, data, MDB_CURRENT );
962 /* Delete old data, replace with range */
963 ic->first = *(ID *)data[0].mv_data;
964 mdb_cursor_del( mc, MDB_NODUPDATA );
968 if ( !(ic->flags & WAS_RANGE)) {
969 /* range, didn't exist before */
971 data[0].mv_size = sizeof(ID);
972 data[0].mv_data = &nid;
973 rc = mdb_cursor_put( mc, &key, data, 0 );
975 data[0].mv_data = &ic->first;
976 rc = mdb_cursor_put( mc, &key, data, 0 );
978 data[0].mv_data = &ic->last;
979 rc = mdb_cursor_put( mc, &key, data, 0 );
990 data[0].mv_size = sizeof(ID);
993 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) {
998 end = ic->count & (IDBLOCK-1);
1002 data[1].mv_size = end - i;
1003 data[0].mv_data = &ice->ids[i];
1005 rc = mdb_cursor_put( mc, &key, data, MDB_NODUPDATA|MDB_APPEND|MDB_MULTIPLE );
1007 if ( rc == MDB_KEYEXIST ) {
1016 ic->tail->next = mdb_tool_idl_free_list;
1017 mdb_tool_idl_free_list = ic->head;
1025 mdb_tool_idl_flush_db( MDB_txn *txn, MDB_dbi dbi, Avlnode *root )
1030 mdb_cursor_open( txn, dbi, &mc );
1031 root = tavl_end( root, TAVL_DIR_LEFT );
1033 rc = mdb_tool_idl_flush_one( mc, root->avl_data );
1036 } while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
1037 mdb_cursor_close( mc );
1043 mdb_tool_idl_flush( BackendDB *be, MDB_txn *txn )
1045 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
1049 for ( i=MDB_NDB; i < mdb->mi_nattrs+MDB_NDB; i++ ) {
1050 if ( !mdb_tool_roots[i] ) continue;
1051 rc = mdb_tool_idl_flush_db( txn, i, mdb_tool_roots[i] );
1052 tavl_free(mdb_tool_roots[i], NULL);
1053 mdb_tool_roots[i] = NULL;
1060 int mdb_tool_idl_add(
1062 struct berval *keys,
1066 mdb_tool_idl_cache *ic, itmp;
1067 mdb_tool_idl_cache_entry *ice;
1070 dbi = mdb_cursor_dbi(mc);
1071 for (i=0; keys[i].bv_val; i++) {
1072 itmp.kstr = keys[i];
1073 ic = tavl_find( (Avlnode *)mdb_tool_roots[dbi], &itmp, mdb_tool_idl_cmp );
1075 /* No entry yet, create one */
1081 ic = ch_malloc( sizeof( mdb_tool_idl_cache ) + itmp.kstr.bv_len );
1082 ic->kstr.bv_len = itmp.kstr.bv_len;
1083 ic->kstr.bv_val = (char *)(ic+1);
1084 memcpy( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
1085 ic->head = ic->tail = NULL;
1090 tavl_insert( (Avlnode **)&mdb_tool_roots[dbi], ic, mdb_tool_idl_cmp,
1093 /* load existing key count here */
1094 key.mv_size = keys[i].bv_len;
1095 key.mv_data = keys[i].bv_val;
1096 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
1098 ic->flags |= WAS_FOUND;
1099 nid = *(ID *)data.mv_data;
1101 ic->count = MDB_IDL_DB_SIZE+1;
1102 ic->flags |= WAS_RANGE;
1106 mdb_cursor_count( mc, &count );
1109 ic->offset = count & (IDBLOCK-1);
1113 /* are we a range already? */
1114 if ( ic->count > MDB_IDL_DB_SIZE ) {
1117 /* Are we at the limit, and converting to a range? */
1118 } else if ( ic->count == MDB_IDL_DB_SIZE ) {
1120 for ( ice = ic->head, n=0; ice; ice = ice->next, n++ )
1123 ic->tail->next = mdb_tool_idl_free_list;
1124 mdb_tool_idl_free_list = ic->head;
1126 ic->head = ic->tail = NULL;
1131 /* No free block, create that too */
1132 lcount = ic->count & (IDBLOCK-1);
1133 if ( !ic->tail || lcount == 0) {
1135 if ( mdb_tool_idl_free_list ) {
1136 ice = mdb_tool_idl_free_list;
1137 mdb_tool_idl_free_list = ice->next;
1140 ice = ch_malloc( sizeof( mdb_tool_idl_cache_entry ));
1146 ic->tail->next = ice;
1153 if (!lcount || ice->ids[lcount-1] != id)
1154 ice->ids[lcount] = id;