]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
0f4dcfcfd6667514a68cff700e938aa28d29cb00
[openldap] / servers / slapd / back-bdb / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #define AVL_INTERNAL
23 #include "back-bdb.h"
24
25 static DBC *cursor = NULL;
26 static DBT key, data;
27
28 typedef struct dn_id {
29         ID id;
30         struct berval dn;
31 } dn_id;
32
33 #define HOLE_SIZE       4096
34 static dn_id hbuf[HOLE_SIZE], *holes = hbuf;
35 static unsigned nhmax = HOLE_SIZE;
36 static unsigned nholes;
37
38 Avlnode *index_attrs, index_dummy;
39
40 int bdb_tool_entry_open(
41         BackendDB *be, int mode )
42 {
43         /* initialize key and data thangs */
44         DBTzero( &key );
45         DBTzero( &data );
46         key.flags = DB_DBT_REALLOC;
47         data.flags = DB_DBT_REALLOC;
48
49         return 0;
50 }
51
52 int bdb_tool_entry_close(
53         BackendDB *be )
54 {
55         assert( be != NULL );
56
57         if( key.data ) {
58                 ch_free( key.data );
59                 key.data = NULL;
60         }
61         if( data.data ) {
62                 ch_free( data.data );
63                 data.data = NULL;
64         }
65
66         if( cursor ) {
67                 cursor->c_close( cursor );
68                 cursor = NULL;
69         }
70
71         if( nholes ) {
72                 unsigned i;
73                 fprintf( stderr, "Error, entries missing!\n");
74                 for (i=0; i<nholes; i++) {
75                         fprintf(stderr, "  entry %ld: %s\n",
76                                 holes[i].id, holes[i].dn.bv_val);
77                 }
78                 return -1;
79         }
80                         
81         return 0;
82 }
83
84 static int bdb_reindex_cmp(const void *a, const void *b) { return 0; }
85
86 ID bdb_tool_entry_next(
87         BackendDB *be )
88 {
89         int rc;
90         ID id;
91         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
92
93         assert( be != NULL );
94         assert( slapMode & SLAP_TOOL_MODE );
95         assert( bdb != NULL );
96         
97         /* Initialization */
98         if (cursor == NULL) {
99                 rc = bdb->bi_id2entry->bdi_db->cursor(
100                         bdb->bi_id2entry->bdi_db, NULL, &cursor,
101                         bdb->bi_db_opflags );
102                 if( rc != 0 ) {
103                         return NOID;
104                 }
105         }
106
107         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
108
109         if( rc != 0 ) {
110                 /* If we're doing linear indexing and there are more attrs to
111                  * index, and we're at the end of the database, start over.
112                  */
113                 if ( bdb->bi_attrs == &index_dummy ) {
114                         if ( index_attrs && rc == DB_NOTFOUND ) {
115                                 /* optional - do a checkpoint here? */
116                                 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
117                                 rc = cursor->c_get( cursor, &key, &data, DB_FIRST );
118                         }
119                         if ( rc ) {
120                                 bdb->bi_attrs = NULL;
121                                 return NOID;
122                         }
123                 } else {
124                         return NOID;
125                 }
126         }
127
128         if( data.data == NULL ) {
129                 return NOID;
130         }
131
132         AC_MEMCPY( &id, key.data, key.size );
133         return id;
134 }
135
136 ID bdb_tool_dn2id_get(
137         Backend *be,
138         struct berval *dn
139 )
140 {
141         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
142         DB *db = bdb->bi_dn2id->bdi_db;
143         int rc;
144         DBT     key, data;
145         ID      id;
146
147         DBTzero( &key );
148         key.size = dn->bv_len + 2;
149         key.data = ch_malloc( key.size );
150         ((char*)key.data)[0] = DN_BASE_PREFIX;
151         AC_MEMCPY( &((char*)key.data)[1], dn->bv_val, key.size - 1 );
152
153         DBTzero( &data );
154         data.data = &id;
155         data.ulen = sizeof(ID);
156         data.flags = DB_DBT_USERMEM;
157
158         rc = db->get( db, NULL, &key, &data, bdb->bi_db_opflags );
159
160     if( rc != 0 ) {
161                 Debug( LDAP_DEBUG_TRACE, "bdb_tool_dn2id_get: get failed: %s (%d)\n",
162                                 db_strerror( rc ), rc, 0 );
163                 id = NOID;
164         }
165
166         ch_free( key.data );
167         return id;
168 }
169
170 int bdb_tool_id2entry_get(
171         Backend *be,
172         ID id,
173         Entry **e
174 )
175 {
176         return bdb_id2entry( be, NULL, id, e );
177 }
178
179 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
180 {
181         int rc;
182         Entry *e = NULL;
183         struct berval bv;
184
185         assert( be != NULL );
186         assert( slapMode & SLAP_TOOL_MODE );
187         assert( data.data != NULL );
188
189 #ifndef BDB_HIER
190         DBT2bv( &data, &bv );
191
192         rc = entry_decode( &bv, &e );
193
194         if( rc == LDAP_SUCCESS ) {
195                 e->e_id = id;
196         }
197 #else
198         {
199                 EntryInfo *ei = NULL;
200                 Operation op = {0};
201
202                 op.o_bd = be;
203                 op.o_tmpmemctx = NULL;
204                 op.o_tmpmfuncs = &ch_mfuncs;
205
206                 rc = bdb_cache_find_id( &op, NULL, id, &ei, 0, 0, NULL );
207                 if ( rc == LDAP_SUCCESS )
208                         e = ei->bei_e;
209         }
210 #endif
211         return e;
212 }
213
214 static int bdb_tool_next_id(
215         Operation *op,
216         DB_TXN *tid,
217         Entry *e,
218         struct berval *text,
219         int hole )
220 {
221         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
222         struct berval dn = e->e_name;
223         struct berval ndn = e->e_nname;
224         struct berval pdn, npdn;
225         EntryInfo *ei = NULL;
226         int rc;
227
228         if (ndn.bv_len == 0) return 0;
229
230         rc = bdb_cache_find_ndn( op, tid, &ndn, &ei );
231         if ( ei ) bdb_cache_entryinfo_unlock( ei );
232         if ( rc == DB_NOTFOUND ) {
233                 if ( !be_issuffix( op->o_bd, &ndn ) ) {
234                         dnParent( &dn, &pdn );
235                         dnParent( &ndn, &npdn );
236                         e->e_name = pdn;
237                         e->e_nname = npdn;
238                         rc = bdb_tool_next_id( op, tid, e, text, 1 );
239                         e->e_name = dn;
240                         e->e_nname = ndn;
241                         if ( rc ) {
242                                 return rc;
243                         }
244                 }
245                 rc = bdb_next_id( op->o_bd, tid, &e->e_id );
246                 if ( rc ) {
247                         snprintf( text->bv_val, text->bv_len,
248                                 "next_id failed: %s (%d)",
249                                 db_strerror(rc), rc );
250                 Debug( LDAP_DEBUG_ANY,
251                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
252                         return rc;
253                 }
254                 rc = bdb_dn2id_add( op, tid, ei, e );
255                 if ( rc ) {
256                         snprintf( text->bv_val, text->bv_len, 
257                                 "dn2id_add failed: %s (%d)",
258                                 db_strerror(rc), rc );
259                 Debug( LDAP_DEBUG_ANY,
260                         "=> bdb_tool_next_id: %s\n", text->bv_val, 0, 0 );
261                 } else if ( hole ) {
262                         if ( nholes == nhmax - 1 ) {
263                                 if ( holes == hbuf ) {
264                                         holes = ch_malloc( nhmax * sizeof(dn_id) * 2 );
265                                         AC_MEMCPY( holes, hbuf, sizeof(hbuf) );
266                                 } else {
267                                         holes = ch_realloc( holes, nhmax * sizeof(dn_id) * 2 );
268                                 }
269                                 nhmax *= 2;
270                         }
271                         ber_dupbv( &holes[nholes].dn, &ndn );
272                         holes[nholes++].id = e->e_id;
273                 }
274         } else if ( !hole ) {
275                 unsigned i;
276
277                 e->e_id = ei->bei_id;
278
279                 for ( i=0; i<nholes; i++) {
280                         if ( holes[i].id == e->e_id ) {
281                                 int j;
282                                 free(holes[i].dn.bv_val);
283                                 for (j=i;j<nholes;j++) holes[j] = holes[j+1];
284                                 holes[j].id = 0;
285                                 nholes--;
286                                 break;
287                         } else if ( holes[i].id > e->e_id ) {
288                                 break;
289                         }
290                 }
291         }
292         return rc;
293 }
294
295 ID bdb_tool_entry_put(
296         BackendDB *be,
297         Entry *e,
298         struct berval *text )
299 {
300         int rc;
301         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
302         DB_TXN *tid = NULL;
303         Operation op = {0};
304
305         assert( be != NULL );
306         assert( slapMode & SLAP_TOOL_MODE );
307
308         assert( text );
309         assert( text->bv_val );
310         assert( text->bv_val[0] == '\0' );      /* overconservative? */
311
312         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
313                 (long) e->e_id, e->e_dn, 0 );
314
315         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
316                 bdb->bi_db_opflags );
317         if( rc != 0 ) {
318                 snprintf( text->bv_val, text->bv_len,
319                         "txn_begin failed: %s (%d)",
320                         db_strerror(rc), rc );
321                 Debug( LDAP_DEBUG_ANY,
322                         "=> bdb_tool_entry_put: %s\n",
323                          text->bv_val, 0, 0 );
324                 return NOID;
325         }
326
327         op.o_bd = be;
328         op.o_tmpmemctx = NULL;
329         op.o_tmpmfuncs = &ch_mfuncs;
330
331         /* add dn2id indices */
332         rc = bdb_tool_next_id( &op, tid, e, text, 0 );
333         if( rc != 0 ) {
334                 goto done;
335         }
336
337         /* id2entry index */
338         rc = bdb_id2entry_add( be, tid, e );
339         if( rc != 0 ) {
340                 snprintf( text->bv_val, text->bv_len,
341                                 "id2entry_add failed: %s (%d)",
342                                 db_strerror(rc), rc );
343                 Debug( LDAP_DEBUG_ANY,
344                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
345                 goto done;
346         }
347
348         if ( !bdb->bi_linear_index )
349                 rc = bdb_index_entry_add( &op, tid, e );
350         if( rc != 0 ) {
351                 snprintf( text->bv_val, text->bv_len,
352                                 "index_entry_add failed: %s (%d)",
353                                 db_strerror(rc), rc );
354                 Debug( LDAP_DEBUG_ANY,
355                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
356                 goto done;
357         }
358
359 done:
360         if( rc == 0 ) {
361                 rc = TXN_COMMIT( tid, 0 );
362                 if( rc != 0 ) {
363                         snprintf( text->bv_val, text->bv_len,
364                                         "txn_commit failed: %s (%d)",
365                                         db_strerror(rc), rc );
366                         Debug( LDAP_DEBUG_ANY,
367                                 "=> bdb_tool_entry_put: %s\n",
368                                 text->bv_val, 0, 0 );
369                         e->e_id = NOID;
370                 }
371
372         } else {
373                 TXN_ABORT( tid );
374                 snprintf( text->bv_val, text->bv_len,
375                         "txn_aborted! %s (%d)",
376                         db_strerror(rc), rc );
377                 Debug( LDAP_DEBUG_ANY,
378                         "=> bdb_tool_entry_put: %s\n",
379                         text->bv_val, 0, 0 );
380                 e->e_id = NOID;
381         }
382
383         return e->e_id;
384 }
385
386 int bdb_tool_entry_reindex(
387         BackendDB *be,
388         ID id )
389 {
390         struct bdb_info *bi = (struct bdb_info *) be->be_private;
391         int rc;
392         Entry *e;
393         DB_TXN *tid = NULL;
394         Operation op = {0};
395
396         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
397                 (long) id, 0, 0 );
398
399         /* No indexes configured, nothing to do. Could return an
400          * error here to shortcut things.
401          */
402         if (!bi->bi_attrs) {
403                 return 0;
404         }
405
406         /* Get the first attribute to index */
407         if (bi->bi_linear_index && !index_attrs && bi->bi_attrs != &index_dummy) {
408                 index_attrs = bi->bi_attrs;
409                 bi->bi_attrs = &index_dummy;
410                 index_dummy.avl_data = avl_delete(&index_attrs, NULL, bdb_reindex_cmp);
411         }
412
413         e = bdb_tool_entry_get( be, id );
414
415         if( e == NULL ) {
416                 Debug( LDAP_DEBUG_ANY,
417                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
418                         (long) id, 0, 0 );
419                 return -1;
420         }
421
422         rc = TXN_BEGIN( bi->bi_dbenv, NULL, &tid, bi->bi_db_opflags );
423         if( rc != 0 ) {
424                 Debug( LDAP_DEBUG_ANY,
425                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
426                         db_strerror(rc), rc, 0 );
427                 goto done;
428         }
429         
430         /*
431          * just (re)add them for now
432          * assume that some other routine (not yet implemented)
433          * will zap index databases
434          *
435          */
436
437         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
438                 (long) id, e->e_dn, 0 );
439
440         op.o_bd = be;
441         op.o_tmpmemctx = NULL;
442         op.o_tmpmfuncs = &ch_mfuncs;
443
444 #if 0 /* ndef BDB_HIER */
445         /* add dn2id indices */
446         rc = bdb_dn2id_add( &op, tid, NULL, e );
447         if( rc != 0 && rc != DB_KEYEXIST ) {
448                 Debug( LDAP_DEBUG_ANY,
449                         "=> bdb_tool_entry_reindex: dn2id_add failed: %s (%d)\n",
450                         db_strerror(rc), rc, 0 );
451                 goto done;
452         }
453 #endif
454
455         rc = bdb_index_entry_add( &op, tid, e );
456
457 done:
458         if( rc == 0 ) {
459                 rc = TXN_COMMIT( tid, 0 );
460                 if( rc != 0 ) {
461                         Debug( LDAP_DEBUG_ANY,
462                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
463                                 db_strerror(rc), rc, 0 );
464                         e->e_id = NOID;
465                 }
466
467         } else {
468                 TXN_ABORT( tid );
469                 Debug( LDAP_DEBUG_ANY,
470                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
471                         db_strerror(rc), rc, 0 );
472                 e->e_id = NOID;
473         }
474         bdb_entry_release( &op, e, 0 );
475
476         return rc;
477 }
478
479 ID bdb_tool_entry_modify(
480         BackendDB *be,
481         Entry *e,
482         struct berval *text )
483 {
484         int rc;
485         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
486         DB_TXN *tid = NULL;
487         Operation op = {0};
488
489         assert( be != NULL );
490         assert( slapMode & SLAP_TOOL_MODE );
491
492         assert( text );
493         assert( text->bv_val );
494         assert( text->bv_val[0] == '\0' );      /* overconservative? */
495
496         assert ( e->e_id != NOID );
497         assert ( e->e_id != 0 );
498
499         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
500                 (long) e->e_id, e->e_dn, 0 );
501
502         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &tid, 
503                 bdb->bi_db_opflags );
504         if( rc != 0 ) {
505                 snprintf( text->bv_val, text->bv_len,
506                         "txn_begin failed: %s (%d)",
507                         db_strerror(rc), rc );
508                 Debug( LDAP_DEBUG_ANY,
509                         "=> bdb_tool_entry_put: %s\n",
510                          text->bv_val, 0, 0 );
511                 return NOID;
512         }
513
514         op.o_bd = be;
515         op.o_tmpmemctx = NULL;
516         op.o_tmpmfuncs = &ch_mfuncs;
517
518         /* id2entry index */
519         rc = bdb_id2entry_update( be, tid, e );
520         if( rc != 0 ) {
521                 snprintf( text->bv_val, text->bv_len,
522                                 "id2entry_add failed: %s (%d)",
523                                 db_strerror(rc), rc );
524                 Debug( LDAP_DEBUG_ANY,
525                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
526                 goto done;
527         }
528
529         rc = bdb_index_entry_del( &op, tid, e );
530         if( rc != 0 ) {
531                 snprintf( text->bv_val, text->bv_len,
532                                 "index_entry_del failed: %s (%d)",
533                                 db_strerror(rc), rc );
534                 Debug( LDAP_DEBUG_ANY,
535                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
536                 goto done;
537         }
538
539         rc = bdb_index_entry_add( &op, tid, e );
540         if( rc != 0 ) {
541                 snprintf( text->bv_val, text->bv_len,
542                                 "index_entry_add failed: %s (%d)",
543                                 db_strerror(rc), rc );
544                 Debug( LDAP_DEBUG_ANY,
545                         "=> bdb_tool_entry_put: %s\n", text->bv_val, 0, 0 );
546                 goto done;
547         }
548
549 done:
550         if( rc == 0 ) {
551                 rc = TXN_COMMIT( tid, 0 );
552                 if( rc != 0 ) {
553                         snprintf( text->bv_val, text->bv_len,
554                                         "txn_commit failed: %s (%d)",
555                                         db_strerror(rc), rc );
556                         Debug( LDAP_DEBUG_ANY,
557                                 "=> bdb_tool_entry_put: %s\n",
558                                 text->bv_val, 0, 0 );
559                         e->e_id = NOID;
560                 }
561
562         } else {
563                 TXN_ABORT( tid );
564                 snprintf( text->bv_val, text->bv_len,
565                         "txn_aborted! %s (%d)",
566                         db_strerror(rc), rc );
567                 Debug( LDAP_DEBUG_ANY,
568                         "=> bdb_tool_entry_put: %s\n",
569                         text->bv_val, 0, 0 );
570                 e->e_id = NOID;
571         }
572
573         return e->e_id;
574 }