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