1 /* tools.c - tools for slap tools */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 static DBC *cursor = NULL;
19 typedef struct dn_id {
24 #define HOLE_SIZE 4096
25 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
26 static unsigned nhmax = HOLE_SIZE;
27 static unsigned nholes;
29 int bdb_tool_entry_open(
30 BackendDB *be, int mode )
32 /* initialize key and data thangs */
35 key.flags = DB_DBT_REALLOC;
36 data.flags = DB_DBT_REALLOC;
41 int bdb_tool_entry_close(
56 cursor->c_close( cursor );
62 fprintf( stderr, "Error, entries missing!\n");
63 for (i=0; i<nholes; i++) {
64 fprintf(stderr, " entry %ld: %s\n",
65 holes[i].id, holes[i].dn.bv_val);
73 ID bdb_tool_entry_next(
78 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
81 assert( slapMode & SLAP_TOOL_MODE );
82 assert( bdb != NULL );
85 rc = bdb->bi_id2entry->bdi_db->cursor(
86 bdb->bi_id2entry->bdi_db, NULL, &cursor,
93 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
99 if( data.data == NULL ) {
103 AC_MEMCPY( &id, key.data, key.size );
107 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
113 assert( be != NULL );
114 assert( slapMode & SLAP_TOOL_MODE );
115 assert( data.data != NULL );
118 DBT2bv( &data, &bv );
120 rc = entry_decode( &bv, &e );
122 if( rc == LDAP_SUCCESS ) {
127 EntryInfo *ei = NULL;
131 op.o_tmpmemctx = NULL;
132 op.o_tmpmfuncs = &ch_mfuncs;
134 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
135 if ( rc == LDAP_SUCCESS )
142 static int bdb_tool_next_id(
150 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
151 struct berval dn = e->e_nname;
153 EntryInfo *ei = NULL;
156 rc = bdb_cache_find_ndn( op, tid, &dn, &ei, locker );
157 if ( ei ) bdb_cache_entryinfo_unlock( ei );
158 if ( rc == DB_NOTFOUND ) {
159 if ( be_issuffix( op->o_bd, &dn ) ) {
162 dnParent( &dn, &pdn );
164 rc = bdb_tool_next_id( op, tid, e, text, 1, locker );
169 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
171 snprintf( text->bv_val, text->bv_len,
172 "next_id failed: %s (%d)",
173 db_strerror(rc), rc );
175 LDAP_LOG ( TOOLS, ERR,
176 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
178 Debug( LDAP_DEBUG_ANY,
179 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
184 rc = bdb_dn2id_add( op, tid, ei, e );
186 snprintf( text->bv_val, text->bv_len,
187 "dn2id_add failed: %s (%d)",
188 db_strerror(rc), rc );
190 LDAP_LOG ( TOOLS, ERR,
191 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
193 Debug( LDAP_DEBUG_ANY,
194 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
197 if ( nholes == nhmax - 1 ) {
198 if ( holes == hbuf ) {
199 holes = ch_malloc( nhmax * sizeof(ID) * 2 );
200 AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
202 holes = ch_realloc( holes, nhmax * sizeof(ID) * 2 );
206 ber_dupbv( &holes[nholes].dn, &dn );
207 holes[nholes++].id = e->e_id;
209 } else if ( !hole ) {
212 for ( i=0; i<nholes; i++) {
213 if ( holes[i].id == e->e_id ) {
215 free(holes[i].dn.bv_val);
216 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
220 } else if ( holes[i].id > e->e_id ) {
228 ID bdb_tool_entry_put(
231 struct berval *text )
234 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
239 assert( be != NULL );
240 assert( slapMode & SLAP_TOOL_MODE );
243 assert( text->bv_val );
244 assert( text->bv_val[0] == '\0' ); /* overconservative? */
247 LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
248 (long) e->e_id, e->e_dn, 0 );
250 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
251 (long) e->e_id, e->e_dn, 0 );
254 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid,
255 bdb->bi_db_opflags );
257 snprintf( text->bv_val, text->bv_len,
258 "txn_begin failed: %s (%d)",
259 db_strerror(rc), rc );
261 LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
263 Debug( LDAP_DEBUG_ANY,
264 "=> bdb_tool_entry_put: %s\n",
265 text->bv_val, 0, 0 );
271 op.o_tmpmemctx = NULL;
272 op.o_tmpmfuncs = &ch_mfuncs;
274 locker = TXN_ID( tid );
275 /* add dn2id indices */
276 rc = bdb_tool_next_id( &op, tid, e, text, 0, locker );
282 rc = bdb_id2entry_add( be, tid, e );
284 snprintf( text->bv_val, text->bv_len,
285 "id2entry_add failed: %s (%d)",
286 db_strerror(rc), rc );
288 LDAP_LOG ( TOOLS, ERR,
289 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
291 Debug( LDAP_DEBUG_ANY,
292 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
297 rc = bdb_index_entry_add( &op, tid, e );
299 snprintf( text->bv_val, text->bv_len,
300 "index_entry_add failed: %s (%d)",
301 db_strerror(rc), rc );
303 LDAP_LOG ( TOOLS, ERR,
304 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
306 Debug( LDAP_DEBUG_ANY,
307 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
314 rc = TXN_COMMIT( tid, 0 );
316 snprintf( text->bv_val, text->bv_len,
317 "txn_commit failed: %s (%d)",
318 db_strerror(rc), rc );
320 LDAP_LOG ( TOOLS, ERR,
321 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
323 Debug( LDAP_DEBUG_ANY,
324 "=> bdb_tool_entry_put: %s\n",
325 text->bv_val, 0, 0 );
332 snprintf( text->bv_val, text->bv_len,
333 "txn_aborted! %s (%d)",
334 db_strerror(rc), rc );
336 LDAP_LOG ( TOOLS, ERR,
337 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
339 Debug( LDAP_DEBUG_ANY,
340 "=> bdb_tool_entry_put: %s\n",
341 text->bv_val, 0, 0 );
349 int bdb_tool_entry_reindex(
353 struct bdb_info *bi = (struct bdb_info *) be->be_private;
360 LDAP_LOG ( TOOLS, ARGS,
361 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
363 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
367 e = bdb_tool_entry_get( be, id );
371 LDAP_LOG ( TOOLS, DETAIL1,
372 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
375 Debug( LDAP_DEBUG_ANY,
376 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
382 rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
385 LDAP_LOG ( TOOLS, ERR,
386 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
387 db_strerror(rc), rc, 0 );
389 Debug( LDAP_DEBUG_ANY,
390 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
391 db_strerror(rc), rc, 0 );
397 * just (re)add them for now
398 * assume that some other routine (not yet implemented)
399 * will zap index databases
404 LDAP_LOG ( TOOLS, ERR,
405 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
407 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
408 (long) id, e->e_dn, 0 );
412 op.o_tmpmemctx = NULL;
413 op.o_tmpmfuncs = &ch_mfuncs;
416 /* add dn2id indices */
417 rc = bdb_dn2id_add( &op, tid, NULL, e );
418 if( rc != 0 && rc != DB_KEYEXIST ) {
420 LDAP_LOG ( TOOLS, ERR,
421 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
422 db_strerror(rc), rc, 0 );
424 Debug( LDAP_DEBUG_ANY,
425 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
426 db_strerror(rc), rc, 0 );
432 rc = bdb_index_entry_add( &op, tid, e );
436 rc = TXN_COMMIT( tid, 0 );
439 LDAP_LOG ( TOOLS, ERR,
440 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
441 db_strerror(rc), rc, 0 );
443 Debug( LDAP_DEBUG_ANY,
444 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
445 db_strerror(rc), rc, 0 );
453 LDAP_LOG ( TOOLS, DETAIL1,
454 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
455 db_strerror(rc), rc, 0 );
457 Debug( LDAP_DEBUG_ANY,
458 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
459 db_strerror(rc), rc, 0 );
463 bdb_entry_release( &op, e, 0 );