1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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>
26 static DBC *cursor = NULL;
29 typedef struct dn_id {
34 #define HOLE_SIZE 4096
35 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
36 static unsigned nhmax = HOLE_SIZE;
37 static unsigned nholes;
39 static Avlnode *index_attrs, index_dummy;
41 #define bdb_tool_idl_cmp BDB_SYMBOL(tool_idl_cmp)
42 #define bdb_tool_idl_flush_one BDB_SYMBOL(tool_idl_flush_one)
43 #define bdb_tool_idl_flush BDB_SYMBOL(tool_idl_flush)
45 static int bdb_tool_idl_flush( BackendDB *be );
49 static ID *bdb_tool_idls;
50 static int bdb_tool_idl_next;
52 int bdb_tool_entry_open(
53 BackendDB *be, int mode )
55 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
57 /* initialize key and data thangs */
60 key.flags = DB_DBT_REALLOC;
61 data.flags = DB_DBT_REALLOC;
64 int rc = bdb->bi_id2entry->bdi_db->cursor(
65 bdb->bi_id2entry->bdi_db, NULL, &cursor,
72 /* Get a block of memory for the IDL cache */
73 if ( !(slapMode & SLAP_TOOL_READONLY ) && bdb->bi_idl_cache_max_size )
74 bdb_tool_idls = ch_malloc( bdb->bi_idl_cache_max_size *
75 sizeof( ID ) * IDBLOCK );
80 int bdb_tool_entry_close(
95 cursor->c_close( cursor );
99 bdb_tool_idl_flush( be );
101 free( bdb_tool_idls );
102 bdb_tool_idls = NULL;
103 bdb_tool_idl_next = 0;
107 fprintf( stderr, "Error, entries missing!\n");
108 for (i=0; i<nholes; i++) {
109 fprintf(stderr, " entry %ld: %s\n",
110 holes[i].id, holes[i].dn.bv_val);
118 static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
120 ID bdb_tool_entry_next(
125 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
127 assert( be != NULL );
128 assert( slapMode & SLAP_TOOL_MODE );
129 assert( bdb != NULL );
131 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
134 /* If we're doing linear indexing and there are more attrs to
135 * index, and we're at the end of the database, start over.
137 if ( bdb->bi_attrs == &index_dummy ) {
138 if ( index_attrs && rc == DB_NOTFOUND ) {
139 /* optional - do a checkpoint here? */
140 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
141 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
144 bdb->bi_attrs = NULL;
152 if( data.data == NULL ) {
156 BDB_DISK2ID( key.data, &id );
160 ID bdb_tool_dn2id_get(
167 EntryInfo *ei = NULL;
170 if ( BER_BVISEMPTY(dn) )
175 op.o_tmpmemctx = NULL;
176 op.o_tmpmfuncs = &ch_mfuncs;
178 rc = bdb_cache_find_ndn( &op, NULL, dn, &ei );
179 if ( ei ) bdb_cache_entryinfo_unlock( ei );
180 if ( rc == DB_NOTFOUND )
186 int bdb_tool_id2entry_get(
192 int rc = bdb_id2entry( be, NULL, 0, id, e );
194 if ( rc == DB_NOTFOUND && id == 0 ) {
195 Entry *dummy = ch_calloc( 1, sizeof(Entry) );
196 struct berval gluebv = BER_BVC("glue");
197 dummy->e_name.bv_val = ch_strdup( "" );
198 dummy->e_nname.bv_val = ch_strdup( "" );
199 attr_merge_one( dummy, slap_schema.si_ad_objectClass, &gluebv, NULL );
200 attr_merge_one( dummy, slap_schema.si_ad_structuralObjectClass,
208 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
214 assert( be != NULL );
215 assert( slapMode & SLAP_TOOL_MODE );
216 assert( data.data != NULL );
218 DBT2bv( &data, &bv );
220 #ifdef SLAP_ZONE_ALLOC
221 /* FIXME: will add ctx later */
222 rc = entry_decode( &bv, &e, NULL );
224 rc = entry_decode( &bv, &e );
227 if( rc == LDAP_SUCCESS ) {
231 if ( slapMode & SLAP_TOOL_READONLY ) {
232 EntryInfo *ei = NULL;
238 op.o_tmpmemctx = NULL;
239 op.o_tmpmfuncs = &ch_mfuncs;
241 rc = bdb_cache_find_parent( &op, NULL, cursor->locker, id, &ei );
242 if ( rc == LDAP_SUCCESS ) {
243 bdb_cache_entryinfo_unlock( ei );
255 static int bdb_tool_next_id(
262 struct berval dn = e->e_name;
263 struct berval ndn = e->e_nname;
264 struct berval pdn, npdn;
265 EntryInfo *ei = NULL, eidummy;
268 if (ndn.bv_len == 0) {
273 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
274 if ( ei ) bdb_cache_entryinfo_unlock( ei );
275 if ( rc == DB_NOTFOUND ) {
276 if ( !be_issuffix( op->o_bd, &ndn ) ) {
278 dnParent( &dn, &pdn );
279 dnParent( &ndn, &npdn );
282 rc = bdb_tool_next_id( op, tid, e, text, 1 );
288 /* If parent didn't exist, it was created just now
289 * and its ID is now in e->e_id. Make sure the current
290 * entry gets added under the new parent ID.
292 if ( eid != e->e_id ) {
293 eidummy.bei_id = e->e_id;
297 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
299 snprintf( text->bv_val, text->bv_len,
300 "next_id failed: %s (%d)",
301 db_strerror(rc), rc );
302 Debug( LDAP_DEBUG_ANY,
303 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
306 rc = bdb_dn2id_add( op, tid, ei, e );
308 snprintf( text->bv_val, text->bv_len,
309 "dn2id_add failed: %s (%d)",
310 db_strerror(rc), rc );
311 Debug( LDAP_DEBUG_ANY,
312 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
314 if ( nholes == nhmax - 1 ) {
315 if ( holes == hbuf ) {
316 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
317 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
319 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
323 ber_dupbv( &holes[nholes].dn, &ndn );
324 holes[nholes++].id = e->e_id;
326 } else if ( !hole ) {
329 e->e_id = ei->bei_id;
331 for ( i=0; i<nholes; i++) {
332 if ( holes[i].id == e->e_id ) {
334 free(holes[i].dn.bv_val);
335 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
339 } else if ( holes[i].id > e->e_id ) {
347 ID bdb_tool_entry_put(
350 struct berval *text )
353 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
358 assert( be != NULL );
359 assert( slapMode & SLAP_TOOL_MODE );
361 assert( text != NULL );
362 assert( text->bv_val != NULL );
363 assert( text->bv_val[0] == '\0' ); /* overconservative? */
365 Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_tool_entry_put)
366 "( %ld, \"%s\" )\n", (long) e->e_id, e->e_dn, 0 );
368 if (! (slapMode & SLAP_TOOL_QUICK)) {
369 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
370 bdb->bi_db_opflags );
372 snprintf( text->bv_val, text->bv_len,
373 "txn_begin failed: %s (%d)",
374 db_strerror(rc), rc );
375 Debug( LDAP_DEBUG_ANY,
376 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
377 text->bv_val, 0, 0 );
384 op.o_tmpmemctx = NULL;
385 op.o_tmpmfuncs = &ch_mfuncs;
387 /* add dn2id indices */
388 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
394 rc = bdb_id2entry_add( be, tid, e );
396 snprintf( text->bv_val, text->bv_len,
397 "id2entry_add failed: %s (%d)",
398 db_strerror(rc), rc );
399 Debug( LDAP_DEBUG_ANY,
400 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
401 text->bv_val, 0, 0 );
405 if ( !bdb->bi_linear_index )
406 rc = bdb_index_entry_add( &op, tid, e );
408 snprintf( text->bv_val, text->bv_len,
409 "index_entry_add failed: %s (%d)",
410 db_strerror(rc), rc );
411 Debug( LDAP_DEBUG_ANY,
412 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
413 text->bv_val, 0, 0 );
419 if ( !( slapMode & SLAP_TOOL_QUICK )) {
420 rc = TXN_COMMIT( tid, 0 );
422 snprintf( text->bv_val, text->bv_len,
423 "txn_commit failed: %s (%d)",
424 db_strerror(rc), rc );
425 Debug( LDAP_DEBUG_ANY,
426 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
427 text->bv_val, 0, 0 );
433 if ( !( slapMode & SLAP_TOOL_QUICK )) {
435 snprintf( text->bv_val, text->bv_len,
436 "txn_aborted! %s (%d)",
437 db_strerror(rc), rc );
438 Debug( LDAP_DEBUG_ANY,
439 "=> " LDAP_XSTRING(bdb_tool_entry_put) ": %s\n",
440 text->bv_val, 0, 0 );
448 int bdb_tool_entry_reindex(
452 struct bdb_info *bi = (struct bdb_info *) be->be_private;
459 Debug( LDAP_DEBUG_ARGS,
460 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld )\n",
463 /* No indexes configured, nothing to do. Could return an
464 * error here to shortcut things.
470 /* Get the first attribute to index */
471 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
472 index_attrs = bi->bi_attrs;
473 bi->bi_attrs = &index_dummy;
474 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
477 e = bdb_tool_entry_get( be, id );
480 Debug( LDAP_DEBUG_ANY,
481 LDAP_XSTRING(bdb_tool_entry_reindex)
482 ": could not locate id=%ld\n",
487 if (! (slapMode & SLAP_TOOL_QUICK)) {
488 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
490 Debug( LDAP_DEBUG_ANY,
491 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) ": "
492 "txn_begin failed: %s (%d)\n",
493 db_strerror(rc), rc, 0 );
499 * just (re)add them for now
500 * assume that some other routine (not yet implemented)
501 * will zap index databases
505 Debug( LDAP_DEBUG_TRACE,
506 "=> " LDAP_XSTRING(bdb_tool_entry_reindex) "( %ld, \"%s\" )\n",
507 (long) id, e->e_dn, 0 );
511 op.o_tmpmemctx = NULL;
512 op.o_tmpmfuncs = &ch_mfuncs;
514 rc = bdb_index_entry_add( &op, tid, e );
518 if (! (slapMode & SLAP_TOOL_QUICK)) {
519 rc = TXN_COMMIT( tid, 0 );
521 Debug( LDAP_DEBUG_ANY,
522 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
523 ": txn_commit failed: %s (%d)\n",
524 db_strerror(rc), rc, 0 );
530 if (! (slapMode & SLAP_TOOL_QUICK)) {
532 Debug( LDAP_DEBUG_ANY,
533 "=> " LDAP_XSTRING(bdb_tool_entry_reindex)
534 ": txn_aborted! %s (%d)\n",
535 db_strerror(rc), rc, 0 );
539 bdb_entry_release( &op, e, 0 );
544 ID bdb_tool_entry_modify(
547 struct berval *text )
550 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
555 assert( be != NULL );
556 assert( slapMode & SLAP_TOOL_MODE );
558 assert( text != NULL );
559 assert( text->bv_val != NULL );
560 assert( text->bv_val[0] == '\0' ); /* overconservative? */
562 assert ( e->e_id != NOID );
564 Debug( LDAP_DEBUG_TRACE,
565 "=> " LDAP_XSTRING(bdb_tool_entry_modify) "( %ld, \"%s\" )\n",
566 (long) e->e_id, e->e_dn, 0 );
568 if (! (slapMode & SLAP_TOOL_QUICK)) {
569 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
570 bdb->bi_db_opflags );
572 snprintf( text->bv_val, text->bv_len,
573 "txn_begin failed: %s (%d)",
574 db_strerror(rc), rc );
575 Debug( LDAP_DEBUG_ANY,
576 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
577 text->bv_val, 0, 0 );
584 op.o_tmpmemctx = NULL;
585 op.o_tmpmfuncs = &ch_mfuncs;
588 rc = bdb_id2entry_update( be, tid, e );
590 snprintf( text->bv_val, text->bv_len,
591 "id2entry_add failed: %s (%d)",
592 db_strerror(rc), rc );
593 Debug( LDAP_DEBUG_ANY,
594 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
595 text->bv_val, 0, 0 );
600 /* FIXME: this is bogus, we don't have the old values to delete
601 * from the index because the given entry has already been modified.
603 rc = bdb_index_entry_del( &op, tid, e );
605 snprintf( text->bv_val, text->bv_len,
606 "index_entry_del failed: %s (%d)",
607 db_strerror(rc), rc );
608 Debug( LDAP_DEBUG_ANY,
609 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
610 text->bv_val, 0, 0 );
615 rc = bdb_index_entry_add( &op, tid, e );
617 snprintf( text->bv_val, text->bv_len,
618 "index_entry_add failed: %s (%d)",
619 db_strerror(rc), rc );
620 Debug( LDAP_DEBUG_ANY,
621 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
622 text->bv_val, 0, 0 );
628 if (! (slapMode & SLAP_TOOL_QUICK)) {
629 rc = TXN_COMMIT( tid, 0 );
631 snprintf( text->bv_val, text->bv_len,
632 "txn_commit failed: %s (%d)",
633 db_strerror(rc), rc );
634 Debug( LDAP_DEBUG_ANY,
635 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": "
636 "%s\n", text->bv_val, 0, 0 );
642 if (! (slapMode & SLAP_TOOL_QUICK)) {
644 snprintf( text->bv_val, text->bv_len,
645 "txn_aborted! %s (%d)",
646 db_strerror(rc), rc );
647 Debug( LDAP_DEBUG_ANY,
648 "=> " LDAP_XSTRING(bdb_tool_entry_modify) ": %s\n",
649 text->bv_val, 0, 0 );
658 typedef struct bdb_tool_idl_cache {
661 int idls[BDB_IDL_DB_SIZE / IDBLOCK];
663 } bdb_tool_idl_cache;
666 bdb_tool_idl_cmp( const void *v1, const void *v2 )
668 const bdb_tool_idl_cache *c1 = v1, *c2 = v2;
671 if (( rc = c1->kstr.bv_len - c2->kstr.bv_len )) return rc;
672 return memcmp( c1->kstr.bv_val, c2->kstr.bv_val, c1->kstr.bv_len );
676 bdb_tool_idl_flush_one( void *v1, void *arg )
678 bdb_tool_idl_cache *ic = v1;
688 rc = db->cursor( db, NULL, &curs, 0 );
695 bv2DBT( &ic->kstr, &key );
697 data.size = data.ulen = sizeof( ID );
698 data.flags = DB_DBT_USERMEM;
701 rc = curs->c_get( curs, &key, &data, DB_SET );
702 /* If key already exists and we're writing a range... */
703 if ( rc == 0 && ic->idn == -1 ) {
704 /* If it's not currently a range, must delete old info */
707 while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 )
708 curs->c_del( curs, 0 );
711 /* Store range marker */
712 curs->c_put( curs, &key, &data, DB_KEYFIRST );
716 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
719 rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP );
722 curs->c_del( curs, 0 );
724 BDB_ID2DISK( ic->last, &nid );
725 curs->c_put( curs, &key, &data, DB_KEYLAST );
727 } else if ( rc && rc != DB_NOTFOUND ) {
729 } else if ( ic->idn == -1 ) {
730 /* range, didn't exist before */
732 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
734 BDB_ID2DISK( ic->first, &nid );
735 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
737 BDB_ID2DISK( ic->last, &nid );
738 rc = curs->c_put( curs, &key, &data, DB_KEYLAST );
747 /* Just a normal write */
749 for ( n=0; n<=ic->idn; n++ ) {
750 ID *ids = bdb_tool_idls + ic->idls[n];
751 int end = ( n < ic->idn ) ? IDBLOCK : (ic->count & (IDBLOCK-1));
752 for ( i=0; i<end; i++ ) {
753 if ( !ids[i] ) continue;
754 BDB_ID2DISK( ids[i], &nid );
755 rc = curs->c_put( curs, &key, &data, DB_NODUPDATA );
757 if ( rc == DB_KEYEXIST ) {
772 curs->c_close( curs );
777 bdb_tool_idl_flush( BackendDB *be )
779 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
784 for ( i=BDB_NDB; i < bdb->bi_ndatabases; i++ ) {
785 db = bdb->bi_databases[i]->bdi_db;
786 if ( !db->app_private ) continue;
787 rc = avl_apply( db->app_private, bdb_tool_idl_flush_one, db, -1,
789 avl_free( db->app_private, NULL );
790 db->app_private = NULL;
796 bdb->bi_idl_cache_size = 0;
797 bdb_tool_idl_next = 0;
802 int bdb_tool_idl_add(
809 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
810 bdb_tool_idl_cache *ic, itmp;
815 if ( !bdb->bi_idl_cache_max_size )
816 return bdb_idl_insert_key( be, db, txn, key, id );
819 root = db->app_private;
821 DBT2bv( key, &itmp.kstr );
823 ic = avl_find( root, &itmp, bdb_tool_idl_cmp );
825 /* No entry yet, create one */
832 ic = ch_malloc( sizeof( bdb_tool_idl_cache ) + itmp.kstr.bv_len );
833 ic->kstr.bv_len = itmp.kstr.bv_len;
834 ic->kstr.bv_val = (char *)(ic+1);
835 AC_MEMCPY( ic->kstr.bv_val, itmp.kstr.bv_val, ic->kstr.bv_len );
839 avl_insert( &root, ic, bdb_tool_idl_cmp, avl_dup_error );
840 db->app_private = root;
842 /* load existing key count here */
843 rc = db->cursor( db, NULL, &curs, 0 );
846 data.ulen = sizeof( ID );
847 data.flags = DB_DBT_USERMEM;
849 rc = curs->c_get( curs, key, &data, DB_SET );
852 ic->count = BDB_IDL_DB_SIZE+1;
856 curs->c_count( curs, &count, 0 );
858 BDB_DISK2ID( &nid, &ic->first );
861 curs->c_close( curs );
863 /* are we a range already? */
864 if ( ic->count > BDB_IDL_DB_SIZE ) {
867 /* Are we at the limit, and converting to a range? */
868 } else if ( ic->count == BDB_IDL_DB_SIZE ) {
874 /* No free block, create that too */
875 if ( ic->idn == -1 || ( ic->count & (IDBLOCK-1)) == 0) {
876 bdb->bi_idl_cache_size++;
877 if ( bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
878 rc = bdb_tool_idl_flush( be );
883 ic->idls[++ic->idn] = bdb_tool_idl_next;
884 ids = bdb_tool_idls + bdb_tool_idl_next;
885 bdb_tool_idl_next += IDBLOCK;
887 memset( ids, 0, IDBLOCK * sizeof( ID ));
891 ids = bdb_tool_idls + ic->idls[ic->idn];
892 ids[ ic->count & (IDBLOCK-1) ] = id;