]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Delete CDB (no transactions) support
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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         struct berval *text )
117 {
118         int rc;
119         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
120         DB_TXN *tid = NULL;
121         struct berval pdn;
122
123         assert( be != NULL );
124         assert( slapMode & SLAP_TOOL_MODE );
125
126         assert( text );
127         assert( text->bv_val );
128         assert( text->bv_val[0] == '\0' );
129
130         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
131                 (long) e->e_id, e->e_dn, 0 );
132
133         rc = txn_begin( bdb->bi_dbenv, NULL, &tid, 
134                 bdb->bi_db_opflags );
135         if( rc != 0 ) {
136                 snprintf( text->bv_val, text->bv_len,
137                         "txn_begin failed: %s (%d)",
138                         db_strerror(rc), rc );
139                 Debug( LDAP_DEBUG_ANY,
140                         "=> bdb_tool_entry_put: %s\n",
141                          text->bv_val, 0, 0 );
142                 return NOID;
143         }
144
145         rc = bdb_next_id( be, tid, &e->e_id );
146         if( rc != 0 ) {
147                 snprintf( text->bv_val, text->bv_len,
148                                 "next_id failed: %s (%d)",
149                                 db_strerror(rc), rc );
150                 Debug( LDAP_DEBUG_ANY,
151                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
152                 goto done;
153         }
154
155         /* add dn2id indices */
156         if ( be_issuffix( be, &e->e_nname ) ) {
157                 pdn = slap_empty_bv;
158         } else {
159                 dnParent( &e->e_nname, &pdn );
160         }
161         rc = bdb_dn2id_add( be, tid, &pdn, e );
162         if( rc != 0 ) {
163                 snprintf( text->bv_val, text->bv_len, 
164                                 "dn2id_add failed: %s (%d)",
165                                 db_strerror(rc), rc );
166                 Debug( LDAP_DEBUG_ANY,
167                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
168                 goto done;
169         }
170
171         /* id2entry index */
172         rc = bdb_id2entry_add( be, tid, e );
173         if( rc != 0 ) {
174                 snprintf( text->bv_val, text->bv_len,
175                                 "id2entry_add failed: %s (%d)",
176                                 db_strerror(rc), rc );
177                 Debug( LDAP_DEBUG_ANY,
178                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
179                 goto done;
180         }
181
182         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
183         if( rc != 0 ) {
184                 snprintf( text->bv_val, text->bv_len,
185                                 "index_entry_add failed: %s (%d)",
186                                 db_strerror(rc), rc );
187                 Debug( LDAP_DEBUG_ANY,
188                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
189                 goto done;
190         }
191
192 done:
193         if( rc == 0 ) {
194                 rc = txn_commit( tid, 0 );
195                 if( rc != 0 ) {
196                         snprintf( text->bv_val, text->bv_len,
197                                         "txn_commit failed: %s (%d)",
198                                         db_strerror(rc), rc );
199                         Debug( LDAP_DEBUG_ANY,
200                                 "=> bdb_tool_entry_put: %s\n",
201                                 text->bv_val, 0, 0 );
202                         e->e_id = NOID;
203                 }
204
205         } else {
206                 txn_abort( tid );
207                 snprintf( text->bv_val, text->bv_len,
208                         "txn_aborted! %s (%d)",
209                         db_strerror(rc), rc );
210                 Debug( LDAP_DEBUG_ANY,
211                         "=> bdb_tool_entry_put: %s\n",
212                         text->bv_val, 0, 0 );
213                 e->e_id = NOID;
214         }
215
216         return e->e_id;
217 }
218
219 int bdb_tool_entry_reindex(
220         BackendDB *be,
221         ID id )
222 {
223         struct bdb_info *bi = (struct bdb_info *) be->be_private;
224         int rc;
225         Entry *e;
226         DB_TXN *tid = NULL;
227         struct berval pdn;
228
229         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
230                 (long) id, 0, 0 );
231
232         e = bdb_tool_entry_get( be, id );
233
234         if( e == NULL ) {
235                 Debug( LDAP_DEBUG_ANY,
236                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
237                         (long) id, 0, 0 );
238                 return -1;
239         }
240
241         rc = txn_begin( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
242         if( rc != 0 ) {
243                 Debug( LDAP_DEBUG_ANY,
244                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
245                         db_strerror(rc), rc, 0 );
246                 goto done;
247         }
248         
249         /*
250          * just (re)add them for now
251          * assume that some other routine (not yet implemented)
252          * will zap index databases
253          *
254          */
255
256         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
257                 (long) id, e->e_dn, 0 );
258
259         /* add dn2id indices */
260         if ( be_issuffix( be, &e->e_nname ) ) {
261                 pdn = slap_empty_bv;
262         } else {
263                 dnParent( &e->e_nname, &pdn );
264         }
265         rc = bdb_dn2id_add( be, tid, &pdn, e );
266         if( rc != 0 && rc != DB_KEYEXIST ) {
267                 Debug( LDAP_DEBUG_ANY,
268                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
269                         db_strerror(rc), rc, 0 );
270                 goto done;
271         }
272
273         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
274
275 done:
276         if( rc == 0 ) {
277                 rc = txn_commit( tid, 0 );
278                 if( rc != 0 ) {
279                         Debug( LDAP_DEBUG_ANY,
280                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
281                                 db_strerror(rc), rc, 0 );
282                         e->e_id = NOID;
283                 }
284
285         } else {
286                 txn_abort( tid );
287                 Debug( LDAP_DEBUG_ANY,
288                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
289                         db_strerror(rc), rc, 0 );
290                 e->e_id = NOID;
291         }
292
293         return rc;
294 }