1 /* tools.c - tools for slap tools */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
15 static DBC *cursor = NULL;
18 int bdb_tool_entry_open(
19 BackendDB *be, int mode )
21 /* initialize key and data thangs */
24 key.flags = DB_DBT_REALLOC;
25 data.flags = DB_DBT_REALLOC;
30 int bdb_tool_entry_close(
33 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
47 cursor->c_close( cursor );
54 ID bdb_tool_entry_next(
59 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
62 assert( slapMode & SLAP_TOOL_MODE );
63 assert( bdb != NULL );
66 rc = bdb->bi_id2entry->bdi_db->cursor(
67 bdb->bi_id2entry->bdi_db, NULL, &cursor,
74 rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
80 if( data.data == NULL ) {
84 AC_MEMCPY( &id, key.data, key.size );
88 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
95 assert( slapMode & SLAP_TOOL_MODE );
96 assert( data.data != NULL );
100 rc = entry_decode( &bv, &e );
102 if( rc == LDAP_SUCCESS ) {
107 bdb_fix_dn(be, id, e);
113 ID bdb_tool_entry_put(
118 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
122 assert( be != NULL );
123 assert( slapMode & SLAP_TOOL_MODE );
125 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
126 (long) e->e_id, e->e_dn, 0 );
129 rc = txn_begin( bdb->bi_dbenv, NULL, &tid,
130 bdb->bi_db_opflags );
132 Debug( LDAP_DEBUG_ANY,
133 "=> bdb_tool_entry_put: txn_begin failed: %s (%d)\n",
134 db_strerror(rc), rc, 0 );
139 rc = bdb_next_id( be, tid, &e->e_id );
141 Debug( LDAP_DEBUG_ANY,
142 "=> bdb_tool_entry_put: next_id failed: %s (%d)\n",
143 db_strerror(rc), rc, 0 );
147 /* add dn2id indices */
148 pdn = dn_parent( be, e->e_ndn );
149 rc = bdb_dn2id_add( be, tid, pdn, e );
151 Debug( LDAP_DEBUG_ANY,
152 "=> bdb_tool_entry_put: dn2id_add failed: %s (%d)\n",
153 db_strerror(rc), rc, 0 );
158 rc = bdb_id2entry_add( be, tid, e );
160 Debug( LDAP_DEBUG_ANY,
161 "=> bdb_tool_entry_put: id2entry_add failed: %s (%d)\n",
162 db_strerror(rc), rc, 0 );
166 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
168 Debug( LDAP_DEBUG_ANY,
169 "=> bdb_tool_entry_put: index_entry_add failed: %s (%d)\n",
170 db_strerror(rc), rc, 0 );
177 rc = txn_commit( tid, 0 );
179 Debug( LDAP_DEBUG_ANY,
180 "=> bdb_tool_entry_put: txn_commit failed: %s (%d)\n",
181 db_strerror(rc), rc, 0 );
187 Debug( LDAP_DEBUG_ANY,
188 "=> bdb_tool_entry_put: txn_aborted! %s (%d)\n",
189 db_strerror(rc), rc, 0 );
197 int bdb_tool_entry_reindex(
201 struct bdb_info *bi = (struct bdb_info *) be->be_private;
207 Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
210 e = bdb_tool_entry_get( be, id );
213 Debug( LDAP_DEBUG_ANY,
214 "bdb_tool_entry_reindex:: could not locate id=%ld\n",
220 rc = txn_begin( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
222 Debug( LDAP_DEBUG_ANY,
223 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
224 db_strerror(rc), rc, 0 );
230 * just (re)add them for now
231 * assume that some other routine (not yet implemented)
232 * will zap index databases
236 Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
237 (long) id, e->e_dn, 0 );
239 /* add dn2id indices */
240 pdn = dn_parent( be, e->e_ndn );
241 rc = bdb_dn2id_add( be, tid, pdn, e );
242 if( rc != 0 && rc != DB_KEYEXIST ) {
243 Debug( LDAP_DEBUG_ANY,
244 "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
245 db_strerror(rc), rc, 0 );
249 rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
254 rc = txn_commit( tid, 0 );
256 Debug( LDAP_DEBUG_ANY,
257 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
258 db_strerror(rc), rc, 0 );
264 Debug( LDAP_DEBUG_ANY,
265 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
266 db_strerror(rc), rc, 0 );