]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/tools.c
BDB_INDEX code does no harm (but no good yet, not used by filters yet).
[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         int rc;
22         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
23
24         assert( be != NULL );
25         assert( bdb != NULL );
26         
27         rc = bdb->bi_id2entry->bdi_db->cursor(
28                 bdb->bi_id2entry->bdi_db, NULL, &cursor, 0 );
29         if( rc != 0 ) {
30                 return NOID;
31         }
32
33         /* initialize key and data thangs */
34         DBTzero( &key );
35         DBTzero( &data );
36         key.flags = DB_DBT_REALLOC;
37         data.flags = DB_DBT_REALLOC;
38
39         return 0;
40 }
41
42 int bdb_tool_entry_close(
43         BackendDB *be )
44 {
45         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
46
47         assert( be != NULL );
48
49         if( key.data ) {
50                 ch_free( key.data );
51                 key.data = NULL;
52         }
53         if( data.data ) {
54                 ch_free( data.data );
55                 data.data = NULL;
56         }
57
58         if( cursor ) {
59                 cursor->c_close( cursor );
60                 cursor = NULL;
61         }
62
63         return 0;
64 }
65
66 ID bdb_tool_entry_next(
67         BackendDB *be )
68 {
69         int rc;
70         ID id;
71
72         assert( be != NULL );
73         assert( slapMode & SLAP_TOOL_MODE );
74         assert( cursor != NULL );
75
76         rc = cursor->c_get( cursor, &key, &data, DB_NEXT );
77
78         if( rc != 0 ) {
79                 return NOID;
80         }
81
82         if( data.data == NULL ) {
83                 return NOID;
84         }
85
86         AC_MEMCPY( &id, key.data, key.size );
87         return id;
88 }
89
90 Entry* bdb_tool_entry_get( BackendDB *be, ID id )
91 {
92         int rc;
93         Entry *e;
94         struct berval bv;
95
96         assert( be != NULL );
97         assert( slapMode & SLAP_TOOL_MODE );
98         assert( data.data != NULL );
99
100         DBT2bv( &data, &bv );
101
102         rc = entry_decode( &bv, &e );
103
104         if( rc == LDAP_SUCCESS ) {
105                 e->e_id = id;
106         }
107
108         return e;
109 }
110
111 ID bdb_tool_entry_put(
112         BackendDB *be,
113         Entry *e )
114 {
115         int rc;
116         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
117         DB_TXN *tid;
118
119         assert( be != NULL );
120         assert( slapMode & SLAP_TOOL_MODE );
121
122         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_put( %ld, \"%s\" )\n",
123                 e->e_id, e->e_dn, 0 );
124
125         rc = txn_begin( bdb->bi_dbenv, NULL, &tid, 0 );
126         if( rc != 0 ) {
127                 Debug( LDAP_DEBUG_ANY,
128                         "=> bdb_tool_entry_put: txn_begin failed: %s (%d)\n",
129                         db_strerror(rc), rc, 0 );
130                 return NOID;
131         }
132
133         rc = bdb_next_id( be, tid, &e->e_id );
134         if( rc != 0 ) {
135                 Debug( LDAP_DEBUG_ANY,
136                         "=> bdb_tool_entry_put: next_id failed: %s (%d)\n",
137                         db_strerror(rc), rc, 0 );
138                 goto done;
139         }
140
141         /* add dn2id indices */
142         rc = bdb_dn2id_add( be, tid, e->e_ndn, e->e_id );
143         if( rc != 0 ) {
144                 Debug( LDAP_DEBUG_ANY,
145                         "=> bdb_tool_entry_put: dn2id_add failed: %s (%d)\n",
146                         db_strerror(rc), rc, 0 );
147                 goto done;
148         }
149
150         /* id2entry index */
151         rc = bdb_id2entry_add( be, tid, e );
152         if( rc != 0 ) {
153                 Debug( LDAP_DEBUG_ANY,
154                         "=> bdb_tool_entry_put: id2entry_add failed: %s (%d)\n",
155                         db_strerror(rc), rc, 0 );
156                 goto done;
157         }
158
159         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
160         if( rc != 0 ) {
161                 Debug( LDAP_DEBUG_ANY,
162                         "=> bdb_tool_entry_put: index_entry_add failed: %s (%d)\n",
163                         db_strerror(rc), rc, 0 );
164                 goto done;
165         }
166
167 done:
168         if( rc == 0 ) {
169                 rc = txn_commit( tid, 0 );
170                 if( rc != 0 ) {
171                         Debug( LDAP_DEBUG_ANY,
172                                 "=> bdb_tool_entry_put: txn_commit failed: %s (%d)\n",
173                                 db_strerror(rc), rc, 0 );
174                         e->e_id = NOID;
175                 }
176
177         } else {
178                 txn_abort( tid );
179                 Debug( LDAP_DEBUG_ANY,
180                         "=> bdb_tool_entry_put: txn_aborted! %s (%d)\n",
181                         db_strerror(rc), rc, 0 );
182                 e->e_id = NOID;
183         }
184
185         return e->e_id;
186 }
187
188 int bdb_tool_entry_reindex(
189         BackendDB *be,
190         ID id )
191 {
192         struct bdb_info *bi = (struct bdb_info *) be->be_private;
193         int rc;
194         Entry *e;
195         DB_TXN *tid = NULL;
196
197         Debug( LDAP_DEBUG_ARGS, "=> bdb_tool_entry_reindex( %ld )\n",
198                 (long) id, 0, 0 );
199
200         e = bdb_tool_entry_get( be, id );
201
202         if( e == NULL ) {
203                 Debug( LDAP_DEBUG_ANY,
204                         "bdb_tool_entry_reindex:: could not locate id=%ld\n",
205                         (long) id, 0, 0 );
206                 return -1;
207         }
208
209         rc = txn_begin( bi->bi_dbenv, NULL, &tid, 0 );
210         if( rc != 0 ) {
211                 Debug( LDAP_DEBUG_ANY,
212                         "=> bdb_tool_entry_reindex: txn_begin failed: %s (%d)\n",
213                         db_strerror(rc), rc, 0 );
214                 goto done;
215         }
216         
217         /*
218          * just (re)add them for now
219          * assume that some other routine (not yet implemented)
220          * will zap index databases
221          *
222          */
223
224         Debug( LDAP_DEBUG_TRACE, "=> bdb_tool_entry_reindex( %ld, \"%s\" )\n",
225                 id, e->e_dn, 0 );
226
227         rc = bdb_index_entry_add( be, tid, e, e->e_attrs );
228
229         if( rc == 0 ) {
230                 rc = txn_commit( tid, 0 );
231                 if( rc != 0 ) {
232                         Debug( LDAP_DEBUG_ANY,
233                                 "=> bdb_tool_entry_reindex: txn_commit failed: %s (%d)\n",
234                                 db_strerror(rc), rc, 0 );
235                         e->e_id = NOID;
236                 }
237
238         } else {
239                 txn_abort( tid );
240                 Debug( LDAP_DEBUG_ANY,
241                         "=> bdb_tool_entry_reindex: txn_aborted! %s (%d)\n",
242                         db_strerror(rc), rc, 0 );
243                 e->e_id = NOID;
244         }
245
246 done:
247         entry_free( e );
248         return rc;
249 }