]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
Change - bdb_tool_entry_put always "sorts" entries:
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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         assert( be != NULL );
34
35         if( key.data ) {
36                 ch_free( key.data );
37                 key.data = NULL;
38         }
39         if( data.data ) {
40                 ch_free( data.data );
41                 data.data = NULL;
42         }
43
44         if( cursor ) {
45                 cursor->c_close( cursor );
46                 cursor = NULL;
47         }
48
49         return 0;
50 }
51
52 ID bdb_tool_entry_next(
53         BackendDB *be )
54 {
55         int rc;
56         ID id;
57         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
58
59         assert( be != NULL );
60         assert( slapMode & SLAP_TOOL_MODE );
61         assert( bdb != NULL );
62         
63         if (cursor == NULL) {
64                 rc = bdb->bi_id2entry->bdi_db->cursor(
65                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
66                         bdb->bi_db_opflags );
67                 if( rc != 0 ) {
68                         return NOID;
69                 }
70         }
71
72         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
73
74         if( rc != 0 ) {
75                 return NOID;
76         }
77
78         if( data.data == NULL ) {
79                 return NOID;
80         }
81
82         AC_MEMCPY( &id, key.data, key.size );
83         return id;
84 }
85
86 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
87 {
88         int rc;
89         Entry *e;
90         struct berval bv;
91
92         assert( be != NULL );
93         assert( slapMode & SLAP_TOOL_MODE );
94         assert( data.data != NULL );
95
96         DBT2bv( &data, &bv );
97
98         rc = entry_decode( &bv, &e );
99
100         if( rc == LDAP_SUCCESS ) {
101                 e->e_id = id;
102         }
103
104 #ifdef BDB_HIER
105         bdb_fix_dn(be, id, e);
106 #endif
107
108         return e;
109 }
110
111 int bdb_tool_next_id(
112         BackendDB *be,
113         DB_TXN *tid,
114         Entry *e,
115         struct berval *text )
116 {
117         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
118         struct berval dn = e->e_nname;
119         struct berval pdn;
120         int rc;
121
122         rc = bdb_dn2id( be, tid, &dn, &e->e_id, 0 );
123         if ( rc == DB_NOTFOUND ) {
124                 if ( be_issuffix( be, &dn ) ) {
125                         pdn = slap_empty_bv;
126                 } else {
127                         dnParent( &dn, &pdn );
128                         e->e_nname = pdn;
129                         rc = bdb_tool_next_id( be, tid, e, text );
130                         if ( rc ) {
131                                 return rc;
132                         }
133                 }
134                 rc = bdb_next_id( be, tid, &e->e_id );
135                 if ( rc ) {
136                         snprintf( text->bv_val, text->bv_len,
137                                 "next_id failed: %s (%d)",
138                                 db_strerror(rc), rc );
139 #ifdef NEW_LOGGING
140                 LDAP_LOG ( TOOLS, ERR, 
141                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
142 #else
143                 Debug( LDAP_DEBUG_ANY,
144                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
145 #endif
146                         return rc;
147                 }
148                 e->e_nname = dn;
149                 rc = bdb_dn2id_add( be, tid, &pdn, e );
150                 if ( rc ) {
151                         snprintf( text->bv_val, text->bv_len, 
152                                 "dn2id_add failed: %s (%d)",
153                                 db_strerror(rc), rc );
154 #ifdef NEW_LOGGING
155                 LDAP_LOG ( TOOLS, ERR, 
156                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
157 #else
158                 Debug( LDAP_DEBUG_ANY,
159                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
160 #endif
161                 }
162         }
163         return rc;
164 }
165
166 ID bdb_tool_entry_put(
167         BackendDB *be,
168         Entry *e,
169         struct berval *text )
170 {
171         int rc;
172         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
173         DB_TXN *tid = NULL;
174         struct berval pdn;
175
176         assert( be != NULL );
177         assert( slapMode & SLAP_TOOL_MODE );
178
179         assert( text );
180         assert( text->bv_val );
181         assert( text->bv_val[0] == '\0' );      /* overconservative? */
182
183 #ifdef NEW_LOGGING
184         LDAP_LOG ( TOOLS, ARGS, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
185                 (long) e->e_id, e->e_dn, 0 );
186 #else
187         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
188                 (long) e->e_id, e->e_dn, 0 );
189 #endif
190
191         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
192                 bdb->bi_db_opflags );
193         if( rc != 0 ) {
194                 snprintf( text->bv_val, text->bv_len,
195                         "txn_begin failed: %s (%d)",
196                         db_strerror(rc), rc );
197 #ifdef NEW_LOGGING
198         LDAP_LOG ( TOOLS, ERR, "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
199 #else
200                 Debug( LDAP_DEBUG_ANY,
201                         "=> bdb_tool_entry_put: %s\n",
202                          text->bv_val, 0, 0 );
203 #endif
204                 return NOID;
205         }
206
207         /* add dn2id indices */
208         rc = bdb_tool_next_id( be, tid, e, text );
209         if( rc != 0 ) {
210                 goto done;
211         }
212
213         /* id2entry index */
214         rc = bdb_id2entry_add( be, tid, e );
215         if( rc != 0 ) {
216                 snprintf( text->bv_val, text->bv_len,
217                                 "id2entry_add failed: %s (%d)",
218                                 db_strerror(rc), rc );
219 #ifdef NEW_LOGGING
220                 LDAP_LOG ( TOOLS, ERR, 
221                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
222 #else
223                 Debug( LDAP_DEBUG_ANY,
224                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
225 #endif
226                 goto done;
227         }
228
229         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
230         if( rc != 0 ) {
231                 snprintf( text->bv_val, text->bv_len,
232                                 "index_entry_add failed: %s (%d)",
233                                 db_strerror(rc), rc );
234 #ifdef NEW_LOGGING
235                 LDAP_LOG ( TOOLS, ERR, 
236                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
237 #else
238                 Debug( LDAP_DEBUG_ANY,
239                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
240 #endif
241                 goto done;
242         }
243
244 done:
245         if( rc == 0 ) {
246                 rc = TXN_COMMIT( tid, 0 );
247                 if( rc != 0 ) {
248                         snprintf( text->bv_val, text->bv_len,
249                                         "txn_commit failed: %s (%d)",
250                                         db_strerror(rc), rc );
251 #ifdef NEW_LOGGING
252                         LDAP_LOG ( TOOLS, ERR, 
253                                 "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
254 #else
255                         Debug( LDAP_DEBUG_ANY,
256                                 "=> bdb_tool_entry_put: %s\n",
257                                 text->bv_val, 0, 0 );
258 #endif
259                         e->e_id = NOID;
260                 }
261
262         } else {
263                 TXN_ABORT( tid );
264                 snprintf( text->bv_val, text->bv_len,
265                         "txn_aborted! %s (%d)",
266                         db_strerror(rc), rc );
267 #ifdef NEW_LOGGING
268                 LDAP_LOG ( TOOLS, ERR, 
269                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
270 #else
271                 Debug( LDAP_DEBUG_ANY,
272                         "=> bdb_tool_entry_put: %s\n",
273                         text->bv_val, 0, 0 );
274 #endif
275                 e->e_id = NOID;
276         }
277
278         return e->e_id;
279 }
280
281 int bdb_tool_entry_reindex(
282         BackendDB *be,
283         ID id )
284 {
285         struct bdb_info *bi = (struct bdb_info *) be->be_private;
286         int rc;
287         Entry *e;
288         DB_TXN *tid = NULL;
289         struct berval pdn;
290
291 #ifdef NEW_LOGGING
292         LDAP_LOG ( TOOLS, ARGS, 
293                 "=> bdb_tool_entry_reindex( %ld )\n", (long) id, 0, 0 );
294 #else
295         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
296                 (long) id, 0, 0 );
297 #endif
298
299         e = bdb_tool_entry_get( be, id );
300
301         if( e == NULL ) {
302 #ifdef NEW_LOGGING
303                 LDAP_LOG ( TOOLS, DETAIL1, 
304                         "bdb_tool_entry_reindex:: could not locate id=%ld\n", 
305                         (long) id, 0, 0 );
306 #else
307                 Debug( LDAP_DEBUG_ANY,
308                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
309                         (long) id, 0, 0 );
310 #endif
311                 return -1;
312         }
313
314         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
315         if( rc != 0 ) {
316 #ifdef NEW_LOGGING
317                 LDAP_LOG ( TOOLS, ERR, 
318                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n", 
319                         db_strerror(rc), rc, 0 );
320 #else
321                 Debug( LDAP_DEBUG_ANY,
322                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
323                         db_strerror(rc), rc, 0 );
324 #endif
325                 goto done;
326         }
327         
328         /*
329          * just (re)add them for now
330          * assume that some other routine (not yet implemented)
331          * will zap index databases
332          *
333          */
334
335 #ifdef NEW_LOGGING
336         LDAP_LOG ( TOOLS, ERR, 
337                 "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n", (long) id, e->e_dn, 0 );
338 #else
339         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
340                 (long) id, e->e_dn, 0 );
341 #endif
342
343         /* add dn2id indices */
344         if ( be_issuffix( be, &e->e_nname ) ) {
345                 pdn = slap_empty_bv;
346         } else {
347                 dnParent( &e->e_nname, &pdn );
348         }
349         rc = bdb_dn2id_add( be, tid, &pdn, e );
350         if( rc != 0 && rc != DB_KEYEXIST ) {
351 #ifdef NEW_LOGGING
352                 LDAP_LOG ( TOOLS, ERR, 
353                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n", 
354                         db_strerror(rc), rc, 0 );
355 #else
356                 Debug( LDAP_DEBUG_ANY,
357                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
358                         db_strerror(rc), rc, 0 );
359 #endif
360                 goto done;
361         }
362
363         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
364
365 done:
366         if( rc == 0 ) {
367                 rc = TXN_COMMIT( tid, 0 );
368                 if( rc != 0 ) {
369 #ifdef NEW_LOGGING
370                         LDAP_LOG ( TOOLS, ERR, 
371                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n", 
372                                 db_strerror(rc), rc, 0 );
373 #else
374                         Debug( LDAP_DEBUG_ANY,
375                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
376                                 db_strerror(rc), rc, 0 );
377 #endif
378                         e->e_id = NOID;
379                 }
380
381         } else {
382                 TXN_ABORT( tid );
383 #ifdef NEW_LOGGING
384                 LDAP_LOG ( TOOLS, DETAIL1, 
385                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n", 
386                         db_strerror(rc), rc, 0 );
387 #else
388                 Debug( LDAP_DEBUG_ANY,
389                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
390                         db_strerror(rc), rc, 0 );
391 #endif
392                 e->e_id = NOID;
393         }
394
395         return rc;
396 }