1 /* tools.c - tools for slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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>
25 static DBC *cursor = NULL;
28 typedef struct dn_id {
33 #define HOLE_SIZE 4096
34 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
35 static unsigned nhmax = HOLE_SIZE;
36 static unsigned nholes;
38 Avlnode *index_attrs, index_dummy;
40 int bdb_tool_entry_open(
41 BackendDB *be, int mode )
43 /* initialize key and data thangs */
46 key.flags = DB_DBT_REALLOC;
47 data.flags = DB_DBT_REALLOC;
52 int bdb_tool_entry_close(
67 cursor->c_close( cursor );
73 fprintf( stderr, "Error, entries missing!\n");
74 for (i=0; i<nholes; i++) {
75 fprintf(stderr, " entry %ld: %s\n",
76 holes[i].id, holes[i].dn.bv_val);
84 static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
86 ID bdb_tool_entry_next(
91 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
94 assert( slapMode & SLAP_TOOL_MODE );
95 assert( bdb != NULL );
99 rc = bdb->bi_id2entry->bdi_db->cursor(
100 bdb->bi_id2entry->bdi_db, NULL, &cursor,
101 bdb->bi_db_opflags );
107 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
110 /* If we're doing linear indexing and there are more attrs to
111 * index, and we're at the end of the database, start over.
113 if ( bdb->bi_attrs == &index_dummy ) {
114 if ( index_attrs && rc == DB_NOTFOUND ) {
115 /* optional - do a checkpoint here? */
116 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
117 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
120 bdb->bi_attrs = NULL;
128 if( data.data == NULL ) {
132 AC_MEMCPY( &id, key.data, key.size );
136 ID bdb_tool_dn2id_get(
141 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
142 DB *db = bdb->bi_dn2id->bdi_db;
148 key.size = dn->bv_len + 2;
149 key.data = ch_malloc( key.size );
150 ((char*)key.data)[0] = DN_BASE_PREFIX;
151 AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
155 data.ulen = sizeof(ID);
156 data.flags = DB_DBT_USERMEM;
158 rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
161 Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
162 db_strerror( rc ), rc, 0 );
170 int bdb_tool_id2entry_get(
176 return bdb_id2entry( be, NULL, id, e );
179 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
185 assert( be != NULL );
186 assert( slapMode & SLAP_TOOL_MODE );
187 assert( data.data != NULL );
190 DBT2bv( &data, &bv );
192 rc = entry_decode( &bv, &e );
194 if( rc == LDAP_SUCCESS ) {
199 EntryInfo *ei = NULL;
203 op.o_tmpmemctx = NULL;
204 op.o_tmpmfuncs = &ch_mfuncs;
206 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
207 if ( rc == LDAP_SUCCESS )
214 static int bdb_tool_next_id(
221 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
222 struct berval dn = e->e_name;
223 struct berval ndn = e->e_nname;
224 struct berval pdn, npdn;
225 EntryInfo *ei = NULL;
228 if (ndn.bv_len == 0) return 0;
230 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
231 if ( ei ) bdb_cache_entryinfo_unlock( ei );
232 if ( rc == DB_NOTFOUND ) {
233 if ( !be_issuffix( op->o_bd, &ndn ) ) {
234 dnParent( &dn, &pdn );
235 dnParent( &ndn, &npdn );
238 rc = bdb_tool_next_id( op, tid, e, text, 1 );
245 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
247 snprintf( text->bv_val, text->bv_len,
248 "next_id failed: %s (%d)",
249 db_strerror(rc), rc );
250 Debug( LDAP_DEBUG_ANY,
251 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
254 rc = bdb_dn2id_add( op, tid, ei, e );
256 snprintf( text->bv_val, text->bv_len,
257 "dn2id_add failed: %s (%d)",
258 db_strerror(rc), rc );
259 Debug( LDAP_DEBUG_ANY,
260 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
262 if ( nholes == nhmax - 1 ) {
263 if ( holes == hbuf ) {
264 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
265 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
267 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
271 ber_dupbv( &holes[nholes].dn, &ndn );
272 holes[nholes++].id = e->e_id;
274 } else if ( !hole ) {
277 e->e_id = ei->bei_id;
279 for ( i=0; i<nholes; i++) {
280 if ( holes[i].id == e->e_id ) {
282 free(holes[i].dn.bv_val);
283 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
287 } else if ( holes[i].id > e->e_id ) {
295 ID bdb_tool_entry_put(
298 struct berval *text )
301 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
305 assert( be != NULL );
306 assert( slapMode & SLAP_TOOL_MODE );
309 assert( text->bv_val );
310 assert( text->bv_val[0] == '\0' ); /* overconservative? */
312 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
313 (long) e->e_id, e->e_dn, 0 );
315 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
316 bdb->bi_db_opflags );
318 snprintf( text->bv_val, text->bv_len,
319 "txn_begin failed: %s (%d)",
320 db_strerror(rc), rc );
321 Debug( LDAP_DEBUG_ANY,
322 "=> bdb_tool_entry_put: %s\n",
323 text->bv_val, 0, 0 );
328 op.o_tmpmemctx = NULL;
329 op.o_tmpmfuncs = &ch_mfuncs;
331 /* add dn2id indices */
332 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
338 rc = bdb_id2entry_add( be, tid, e );
340 snprintf( text->bv_val, text->bv_len,
341 "id2entry_add failed: %s (%d)",
342 db_strerror(rc), rc );
343 Debug( LDAP_DEBUG_ANY,
344 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
348 if ( !bdb->bi_linear_index )
349 rc = bdb_index_entry_add( &op, tid, e );
351 snprintf( text->bv_val, text->bv_len,
352 "index_entry_add failed: %s (%d)",
353 db_strerror(rc), rc );
354 Debug( LDAP_DEBUG_ANY,
355 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
361 rc = TXN_COMMIT( tid, 0 );
363 snprintf( text->bv_val, text->bv_len,
364 "txn_commit failed: %s (%d)",
365 db_strerror(rc), rc );
366 Debug( LDAP_DEBUG_ANY,
367 "=> bdb_tool_entry_put: %s\n",
368 text->bv_val, 0, 0 );
374 snprintf( text->bv_val, text->bv_len,
375 "txn_aborted! %s (%d)",
376 db_strerror(rc), rc );
377 Debug( LDAP_DEBUG_ANY,
378 "=> bdb_tool_entry_put: %s\n",
379 text->bv_val, 0, 0 );
386 int bdb_tool_entry_reindex(
390 struct bdb_info *bi = (struct bdb_info *) be->be_private;
396 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
399 /* No indexes configured, nothing to do. Could return an
400 * error here to shortcut things.
406 /* Get the first attribute to index */
407 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
408 index_attrs = bi->bi_attrs;
409 bi->bi_attrs = &index_dummy;
410 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
413 e = bdb_tool_entry_get( be, id );
416 Debug( LDAP_DEBUG_ANY,
417 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
422 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
424 Debug( LDAP_DEBUG_ANY,
425 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
426 db_strerror(rc), rc, 0 );
431 * just (re)add them for now
432 * assume that some other routine (not yet implemented)
433 * will zap index databases
437 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
438 (long) id, e->e_dn, 0 );
441 op.o_tmpmemctx = NULL;
442 op.o_tmpmfuncs = &ch_mfuncs;
444 #if 0 /* ndef BDB_HIER */
445 /* add dn2id indices */
446 rc = bdb_dn2id_add( &op, tid, NULL, e );
447 if( rc != 0 && rc != DB_KEYEXIST ) {
448 Debug( LDAP_DEBUG_ANY,
449 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
450 db_strerror(rc), rc, 0 );
455 rc = bdb_index_entry_add( &op, tid, e );
459 rc = TXN_COMMIT( tid, 0 );
461 Debug( LDAP_DEBUG_ANY,
462 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
463 db_strerror(rc), rc, 0 );
469 Debug( LDAP_DEBUG_ANY,
470 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
471 db_strerror(rc), rc, 0 );
474 bdb_entry_release( &op, e, 0 );
479 ID bdb_tool_entry_modify(
482 struct berval *text )
485 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
489 assert( be != NULL );
490 assert( slapMode & SLAP_TOOL_MODE );
493 assert( text->bv_val );
494 assert( text->bv_val[0] == '\0' ); /* overconservative? */
496 assert ( e->e_id != NOID );
497 assert ( e->e_id != 0 );
499 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
500 (long) e->e_id, e->e_dn, 0 );
502 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
503 bdb->bi_db_opflags );
505 snprintf( text->bv_val, text->bv_len,
506 "txn_begin failed: %s (%d)",
507 db_strerror(rc), rc );
508 Debug( LDAP_DEBUG_ANY,
509 "=> bdb_tool_entry_put: %s\n",
510 text->bv_val, 0, 0 );
515 op.o_tmpmemctx = NULL;
516 op.o_tmpmfuncs = &ch_mfuncs;
519 rc = bdb_id2entry_update( be, tid, e );
521 snprintf( text->bv_val, text->bv_len,
522 "id2entry_add failed: %s (%d)",
523 db_strerror(rc), rc );
524 Debug( LDAP_DEBUG_ANY,
525 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
529 rc = bdb_index_entry_del( &op, tid, e );
531 snprintf( text->bv_val, text->bv_len,
532 "index_entry_del failed: %s (%d)",
533 db_strerror(rc), rc );
534 Debug( LDAP_DEBUG_ANY,
535 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
539 rc = bdb_index_entry_add( &op, tid, e );
541 snprintf( text->bv_val, text->bv_len,
542 "index_entry_add failed: %s (%d)",
543 db_strerror(rc), rc );
544 Debug( LDAP_DEBUG_ANY,
545 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
551 rc = TXN_COMMIT( tid, 0 );
553 snprintf( text->bv_val, text->bv_len,
554 "txn_commit failed: %s (%d)",
555 db_strerror(rc), rc );
556 Debug( LDAP_DEBUG_ANY,
557 "=> bdb_tool_entry_put: %s\n",
558 text->bv_val, 0, 0 );
564 snprintf( text->bv_val, text->bv_len,
565 "txn_aborted! %s (%d)",
566 db_strerror(rc), rc );
567 Debug( LDAP_DEBUG_ANY,
568 "=> bdb_tool_entry_put: %s\n",
569 text->bv_val, 0, 0 );