]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Some fixes for BDB_IDL_MULTI. Experimental back-hdb code.
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 static DBC *cursor = NULL;
16 static DBT key, data;
17
18 int bdb_tool_entry_open(
19         BackendDB *be, int mode )
20 {
21         /* initialize key and data thangs */
22         DBTzero( &key );
23         DBTzero( &data );
24         key.flags = DB_DBT_REALLOC;
25         data.flags = DB_DBT_REALLOC;
26
27         return 0;
28 }
29
30 int bdb_tool_entry_close(
31         BackendDB *be )
32 {
33         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
34
35         assert( be != NULL );
36
37         if( key.data ) {
38                 ch_free( key.data );
39                 key.data = NULL;
40         }
41         if( data.data ) {
42                 ch_free( data.data );
43                 data.data = NULL;
44         }
45
46         if( cursor ) {
47                 cursor->c_close( cursor );
48                 cursor = NULL;
49         }
50
51         return 0;
52 }
53
54 ID bdb_tool_entry_next(
55         BackendDB *be )
56 {
57         int rc;
58         ID id;
59         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
60
61         assert( be != NULL );
62         assert( slapMode & SLAP_TOOL_MODE );
63         assert( bdb != NULL );
64         
65         if (cursor == NULL) {
66                 rc = bdb->bi_id2entry->bdi_db->cursor(
67                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
68                         bdb->bi_db_opflags );
69                 if( rc != 0 ) {
70                         return NOID;
71                 }
72         }
73
74         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
75
76         if( rc != 0 ) {
77                 return NOID;
78         }
79
80         if( data.data == NULL ) {
81                 return NOID;
82         }
83
84         AC_MEMCPY( &id, key.data, key.size );
85         return id;
86 }
87
88 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
89 {
90         int rc;
91         Entry *e;
92         struct berval bv;
93
94         assert( be != NULL );
95         assert( slapMode & SLAP_TOOL_MODE );
96         assert( data.data != NULL );
97
98         DBT2bv( &data, &bv );
99
100         rc = entry_decode( &bv, &e );
101
102         if( rc == LDAP_SUCCESS ) {
103                 e->e_id = id;
104         }
105
106 #ifdef BDB_HIER
107         bdb_fix_dn(be, id, e);
108 #endif
109
110         return e;
111 }
112
113 ID bdb_tool_entry_put(
114         BackendDB *be,
115         Entry *e )
116 {
117         int rc;
118         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
119         DB_TXN *tid = NULL;
120         char *pdn;
121
122         assert( be != NULL );
123         assert( slapMode & SLAP_TOOL_MODE );
124
125         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
126                 (long) e->e_id, e->e_dn, 0 );
127
128         if( bdb->bi_txn ) {
129                 rc = txn_begin( bdb->bi_dbenv, NULL, &tid, 
130                         bdb->bi_db_opflags );
131                 if( rc != 0 ) {
132                         Debug( LDAP_DEBUG_ANY,
133                                 "=> bdb_tool_entry_put: txn_begin failed: %s (%d)\n",
134                                 db_strerror(rc), rc, 0 );
135                         return NOID;
136                 }
137         }
138
139         rc = bdb_next_id( be, tid, &e->e_id );
140         if( rc != 0 ) {
141                 Debug( LDAP_DEBUG_ANY,
142                         "=> bdb_tool_entry_put: next_id failed: %s (%d)\n",
143                         db_strerror(rc), rc, 0 );
144                 goto done;
145         }
146
147         /* add dn2id indices */
148         pdn = dn_parent( be, e->e_ndn );
149         rc = bdb_dn2id_add( be, tid, pdn, e );
150         if( pdn ) free( pdn );
151         if( rc != 0 ) {
152                 Debug( LDAP_DEBUG_ANY,
153                         "=> bdb_tool_entry_put: dn2id_add failed: %s (%d)\n",
154                         db_strerror(rc), rc, 0 );
155                 goto done;
156         }
157
158         /* id2entry index */
159         rc = bdb_id2entry_add( be, tid, e );
160         if( rc != 0 ) {
161                 Debug( LDAP_DEBUG_ANY,
162                         "=> bdb_tool_entry_put: id2entry_add failed: %s (%d)\n",
163                         db_strerror(rc), rc, 0 );
164                 goto done;
165         }
166
167         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
168         if( rc != 0 ) {
169                 Debug( LDAP_DEBUG_ANY,
170                         "=> bdb_tool_entry_put: index_entry_add failed: %s (%d)\n",
171                         db_strerror(rc), rc, 0 );
172                 goto done;
173         }
174
175 done:
176         if( bdb->bi_txn ) {
177                 if( rc == 0 ) {
178                         rc = txn_commit( tid, 0 );
179                         if( rc != 0 ) {
180                                 Debug( LDAP_DEBUG_ANY,
181                                         "=> bdb_tool_entry_put: txn_commit failed: %s (%d)\n",
182                                         db_strerror(rc), rc, 0 );
183                                 e->e_id = NOID;
184                         }
185
186                 } else {
187                         txn_abort( tid );
188                         Debug( LDAP_DEBUG_ANY,
189                                 "=> bdb_tool_entry_put: txn_aborted! %s (%d)\n",
190                                 db_strerror(rc), rc, 0 );
191                         e->e_id = NOID;
192                 }
193         }
194
195         return e->e_id;
196 }
197
198 int bdb_tool_entry_reindex(
199         BackendDB *be,
200         ID id )
201 {
202         struct bdb_info *bi = (struct bdb_info *) be->be_private;
203         int rc;
204         Entry *e;
205         DB_TXN *tid = NULL;
206         char *pdn;
207
208         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
209                 (long) id, 0, 0 );
210
211         e = bdb_tool_entry_get( be, id );
212
213         if( e == NULL ) {
214                 Debug( LDAP_DEBUG_ANY,
215                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
216                         (long) id, 0, 0 );
217                 return -1;
218         }
219
220         if( bi->bi_txn ) {
221                 rc = txn_begin( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
222                 if( rc != 0 ) {
223                         Debug( LDAP_DEBUG_ANY,
224                                 "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
225                                 db_strerror(rc), rc, 0 );
226                         goto done;
227                 }
228         }
229         
230         /*
231          * just (re)add them for now
232          * assume that some other routine (not yet implemented)
233          * will zap index databases
234          *
235          */
236
237         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
238                 (long) id, e->e_dn, 0 );
239
240         /* add dn2id indices */
241         pdn = dn_parent( be, e->e_ndn );
242         rc = bdb_dn2id_add( be, tid, pdn, e );
243         if( pdn ) free( pdn );
244         if( rc != 0 && rc != DB_KEYEXIST ) {
245                 Debug( LDAP_DEBUG_ANY,
246                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
247                         db_strerror(rc), rc, 0 );
248                 goto done;
249         }
250
251         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
252
253 done:
254         if( bi->bi_txn ) {
255                 if( rc == 0 ) {
256                         rc = txn_commit( tid, 0 );
257                         if( rc != 0 ) {
258                                 Debug( LDAP_DEBUG_ANY,
259                                         "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
260                                         db_strerror(rc), rc, 0 );
261                                 e->e_id = NOID;
262                         }
263
264                 } else {
265                         txn_abort( tid );
266                         Debug( LDAP_DEBUG_ANY,
267                                 "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
268                                 db_strerror(rc), rc, 0 );
269                         e->e_id = NOID;
270                 }
271         }
272
273         return rc;
274 }