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>
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 Avlnode *index_attrs, index_dummy;
41 int bdb_tool_entry_open(
42 BackendDB *be, int mode )
44 /* initialize key and data thangs */
47 key.flags = DB_DBT_REALLOC;
48 data.flags = DB_DBT_REALLOC;
53 int bdb_tool_entry_close(
68 cursor->c_close( cursor );
74 fprintf( stderr, "Error, entries missing!\n");
75 for (i=0; i<nholes; i++) {
76 fprintf(stderr, " entry %ld: %s\n",
77 holes[i].id, holes[i].dn.bv_val);
85 static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
87 ID bdb_tool_entry_next(
92 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
95 assert( slapMode & SLAP_TOOL_MODE );
96 assert( bdb != NULL );
100 rc = bdb->bi_id2entry->bdi_db->cursor(
101 bdb->bi_id2entry->bdi_db, NULL, &cursor,
102 bdb->bi_db_opflags );
108 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
111 /* If we're doing linear indexing and there are more attrs to
112 * index, and we're at the end of the database, start over.
114 if ( bdb->bi_attrs == &index_dummy ) {
115 if ( index_attrs && rc == DB_NOTFOUND ) {
116 /* optional - do a checkpoint here? */
117 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
118 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
121 bdb->bi_attrs = NULL;
129 if( data.data == NULL ) {
133 AC_MEMCPY( &id, key.data, key.size );
137 ID bdb_tool_dn2id_get(
142 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
143 DB *db = bdb->bi_dn2id->bdi_db;
149 key.size = dn->bv_len + 2;
150 key.data = ch_malloc( key.size );
151 ((char*)key.data)[0] = DN_BASE_PREFIX;
152 AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
156 data.ulen = sizeof(ID);
157 data.flags = DB_DBT_USERMEM;
159 rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
162 Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
163 db_strerror( rc ), rc, 0 );
171 int bdb_tool_id2entry_get(
177 return bdb_id2entry( be, NULL, id, e );
180 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
186 assert( be != NULL );
187 assert( slapMode & SLAP_TOOL_MODE );
188 assert( data.data != NULL );
191 DBT2bv( &data, &bv );
193 rc = entry_decode( &bv, &e );
195 if( rc == LDAP_SUCCESS ) {
200 EntryInfo *ei = NULL;
204 op.o_tmpmemctx = NULL;
205 op.o_tmpmfuncs = &ch_mfuncs;
207 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
208 if ( rc == LDAP_SUCCESS )
215 static int bdb_tool_next_id(
222 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
223 struct berval dn = e->e_name;
224 struct berval ndn = e->e_nname;
225 struct berval pdn, npdn;
226 EntryInfo *ei = NULL;
229 if (ndn.bv_len == 0) return 0;
231 rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
232 if ( ei ) bdb_cache_entryinfo_unlock( ei );
233 if ( rc == DB_NOTFOUND ) {
234 if ( !be_issuffix( op->o_bd, &ndn ) ) {
235 dnParent( &dn, &pdn );
236 dnParent( &ndn, &npdn );
239 rc = bdb_tool_next_id( op, tid, e, text, 1 );
246 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
248 snprintf( text->bv_val, text->bv_len,
249 "next_id failed: %s (%d)",
250 db_strerror(rc), rc );
251 Debug( LDAP_DEBUG_ANY,
252 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
255 rc = bdb_dn2id_add( op, tid, ei, e );
257 snprintf( text->bv_val, text->bv_len,
258 "dn2id_add failed: %s (%d)",
259 db_strerror(rc), rc );
260 Debug( LDAP_DEBUG_ANY,
261 "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
263 if ( nholes == nhmax - 1 ) {
264 if ( holes == hbuf ) {
265 holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
266 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
268 holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
272 ber_dupbv( &holes[nholes].dn, &ndn );
273 holes[nholes++].id = e->e_id;
275 } else if ( !hole ) {
278 e->e_id = ei->bei_id;
280 for ( i=0; i<nholes; i++) {
281 if ( holes[i].id == e->e_id ) {
283 free(holes[i].dn.bv_val);
284 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
288 } else if ( holes[i].id > e->e_id ) {
296 ID bdb_tool_entry_put(
299 struct berval *text )
302 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
306 assert( be != NULL );
307 assert( slapMode & SLAP_TOOL_MODE );
310 assert( text->bv_val );
311 assert( text->bv_val[0] == '\0' ); /* overconservative? */
313 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
314 (long) e->e_id, e->e_dn, 0 );
316 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
317 bdb->bi_db_opflags );
319 snprintf( text->bv_val, text->bv_len,
320 "txn_begin failed: %s (%d)",
321 db_strerror(rc), rc );
322 Debug( LDAP_DEBUG_ANY,
323 "=> bdb_tool_entry_put: %s\n",
324 text->bv_val, 0, 0 );
329 op.o_tmpmemctx = NULL;
330 op.o_tmpmfuncs = &ch_mfuncs;
332 /* add dn2id indices */
333 rc = bdb_tool_next_id( &op, tid, e, text, 0 );
339 rc = bdb_id2entry_add( be, tid, e );
341 snprintf( text->bv_val, text->bv_len,
342 "id2entry_add failed: %s (%d)",
343 db_strerror(rc), rc );
344 Debug( LDAP_DEBUG_ANY,
345 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
349 if ( !bdb->bi_linear_index )
350 rc = bdb_index_entry_add( &op, tid, e );
352 snprintf( text->bv_val, text->bv_len,
353 "index_entry_add failed: %s (%d)",
354 db_strerror(rc), rc );
355 Debug( LDAP_DEBUG_ANY,
356 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
362 rc = TXN_COMMIT( tid, 0 );
364 snprintf( text->bv_val, text->bv_len,
365 "txn_commit failed: %s (%d)",
366 db_strerror(rc), rc );
367 Debug( LDAP_DEBUG_ANY,
368 "=> bdb_tool_entry_put: %s\n",
369 text->bv_val, 0, 0 );
375 snprintf( text->bv_val, text->bv_len,
376 "txn_aborted! %s (%d)",
377 db_strerror(rc), rc );
378 Debug( LDAP_DEBUG_ANY,
379 "=> bdb_tool_entry_put: %s\n",
380 text->bv_val, 0, 0 );
387 int bdb_tool_entry_reindex(
391 struct bdb_info *bi = (struct bdb_info *) be->be_private;
397 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
400 /* No indexes configured, nothing to do. Could return an
401 * error here to shortcut things.
407 /* Get the first attribute to index */
408 if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
409 index_attrs = bi->bi_attrs;
410 bi->bi_attrs = &index_dummy;
411 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
414 e = bdb_tool_entry_get( be, id );
417 Debug( LDAP_DEBUG_ANY,
418 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
423 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
425 Debug( LDAP_DEBUG_ANY,
426 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
427 db_strerror(rc), rc, 0 );
432 * just (re)add them for now
433 * assume that some other routine (not yet implemented)
434 * will zap index databases
438 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
439 (long) id, e->e_dn, 0 );
442 op.o_tmpmemctx = NULL;
443 op.o_tmpmfuncs = &ch_mfuncs;
445 #if 0 /* ndef BDB_HIER */
446 /* add dn2id indices */
447 rc = bdb_dn2id_add( &op, tid, NULL, e );
448 if( rc != 0 && rc != DB_KEYEXIST ) {
449 Debug( LDAP_DEBUG_ANY,
450 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
451 db_strerror(rc), rc, 0 );
456 rc = bdb_index_entry_add( &op, tid, e );
460 rc = TXN_COMMIT( tid, 0 );
462 Debug( LDAP_DEBUG_ANY,
463 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
464 db_strerror(rc), rc, 0 );
470 Debug( LDAP_DEBUG_ANY,
471 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
472 db_strerror(rc), rc, 0 );
475 bdb_entry_release( &op, e, 0 );
480 ID bdb_tool_entry_modify(
483 struct berval *text )
486 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
490 assert( be != NULL );
491 assert( slapMode & SLAP_TOOL_MODE );
494 assert( text->bv_val );
495 assert( text->bv_val[0] == '\0' ); /* overconservative? */
497 assert ( e->e_id != NOID );
498 assert ( e->e_id != 0 );
500 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
501 (long) e->e_id, e->e_dn, 0 );
503 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
504 bdb->bi_db_opflags );
506 snprintf( text->bv_val, text->bv_len,
507 "txn_begin failed: %s (%d)",
508 db_strerror(rc), rc );
509 Debug( LDAP_DEBUG_ANY,
510 "=> bdb_tool_entry_put: %s\n",
511 text->bv_val, 0, 0 );
516 op.o_tmpmemctx = NULL;
517 op.o_tmpmfuncs = &ch_mfuncs;
520 rc = bdb_id2entry_update( be, tid, e );
522 snprintf( text->bv_val, text->bv_len,
523 "id2entry_add failed: %s (%d)",
524 db_strerror(rc), rc );
525 Debug( LDAP_DEBUG_ANY,
526 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
530 rc = bdb_index_entry_del( &op, tid, e );
532 snprintf( text->bv_val, text->bv_len,
533 "index_entry_del failed: %s (%d)",
534 db_strerror(rc), rc );
535 Debug( LDAP_DEBUG_ANY,
536 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
540 rc = bdb_index_entry_add( &op, tid, e );
542 snprintf( text->bv_val, text->bv_len,
543 "index_entry_add failed: %s (%d)",
544 db_strerror(rc), rc );
545 Debug( LDAP_DEBUG_ANY,
546 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
552 rc = TXN_COMMIT( tid, 0 );
554 snprintf( text->bv_val, text->bv_len,
555 "txn_commit failed: %s (%d)",
556 db_strerror(rc), rc );
557 Debug( LDAP_DEBUG_ANY,
558 "=> bdb_tool_entry_put: %s\n",
559 text->bv_val, 0, 0 );
565 snprintf( text->bv_val, text->bv_len,
566 "txn_aborted! %s (%d)",
567 db_strerror(rc), rc );
568 Debug( LDAP_DEBUG_ANY,
569 "=> bdb_tool_entry_put: %s\n",
570 text->bv_val, 0, 0 );