Debian Codegraph @

servers/slapd/back-wt/dn2id.c

raw
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2002-2024 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 /* ACKNOWLEDGEMENTS:
17 * This work was developed by HAMANO Tsukasa <[email protected]>
18 * based on back-bdb for inclusion in OpenLDAP Software.
19 * WiredTiger is a product of MongoDB Inc.
20 */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include "back-wt.h"
26 #include "slap-config.h"
27 #include "idl.h"
28
29 static char *
30 mkrevdn(struct berval src){
31 char *dst, *p;
32 char *rdn;
33 size_t rdn_len;
34
35 p = dst = ch_malloc(src.bv_len + 2);
36 while(src.bv_len){
37 rdn = ber_bvrchr( &src, ',' );
38 if (rdn) {
39 rdn_len = src.bv_len;
40 src.bv_len = rdn - src.bv_val;
41 rdn_len -= src.bv_len + 1;
42 rdn++;
43 }else{
44 /* first rdn */
45 rdn_len = src.bv_len;
46 rdn = src.bv_val;
47 src.bv_len = 0;
48 }
49 memcpy( p, rdn, rdn_len );
50 p += rdn_len;
51 *p++ = ',';
52 }
53 *p = '\0';
54 return dst;
55 }
56
57 int
58 wt_dn2id_add(
59 Operation *op,
60 wt_ctx *wc,
61 ID pid,
62 Entry *e)
63 {
64 struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
65 int rc;
66 WT_SESSION *session = wc->session;
67 WT_CURSOR *cursor = wc->dn2id_w;
68 char *revdn = NULL;
69
70 Debug( LDAP_DEBUG_TRACE, "=> wt_dn2id_add 0x%lx: \"%s\"\n",
71 e->e_id, e->e_ndn );
72 assert( e->e_id != NOID );
73
74 /* make reverse dn */
75 revdn = mkrevdn(e->e_nname);
76
77 if(!cursor){
78 rc = session->open_cursor(session, WT_TABLE_DN2ID, NULL,
79 "overwrite=false", &cursor);
80 if(rc){
81 Debug( LDAP_DEBUG_ANY,
82 "wt_dn2id_add: open_cursor failed: %s (%d)\n",
83 wiredtiger_strerror(rc), rc );
84 goto done;
85 }
86 wc->dn2id_w = cursor;
87 }
88 cursor->set_key(cursor, revdn);
89 cursor->set_value(cursor, e->e_ndn, e->e_id, pid);
90 rc = cursor->insert(cursor);
91 if(rc){
92 Debug( LDAP_DEBUG_ANY,
93 "wt_dn2id_add: insert failed: %s (%d)\n",
94 wiredtiger_strerror(rc), rc );
95 goto done;
96 }
97
98 if (wi->wi_flags & WT_USE_IDLCACHE) {
99 wt_idlcache_clear(op, wc, &e->e_nname);
100 }
101
102 done:
103 if(revdn){
104 ch_free(revdn);
105 }
106
107 #ifdef WT_CURSOR_CACHE
108 if(cursor){
109 cursor->reset(cursor);
110 }
111 #else
112 if(cursor){
113 cursor->close(cursor);
114 wc->dn2id_w = NULL;
115 }
116 #endif
117
118 Debug( LDAP_DEBUG_TRACE, "<= wt_dn2id_add 0x%lx: %d\n", e->e_id, rc );
119 return rc;
120 }
121
122 int
123 wt_dn2id_delete(
124 Operation *op,
125 wt_ctx *wc,
126 struct berval *ndn)
127 {
128 struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
129 int rc = 0;
130 WT_SESSION *session = wc->session;
131 WT_CURSOR *cursor = wc->dn2id_w;
132 char *revdn = NULL;
133
134 Debug( LDAP_DEBUG_TRACE, "=> wt_dn2id_delete %s\n", ndn->bv_val );
135
136 /* make reverse dn */
137 revdn = mkrevdn(*ndn);
138
139 if(!cursor){
140 rc = session->open_cursor(session, WT_TABLE_DN2ID, NULL,
141 "overwrite=false", &cursor);
142 if ( rc ) {
143 Debug( LDAP_DEBUG_ANY,
144 "wt_dn2id_delete: open_cursor failed: %s (%d)\n",
145 wiredtiger_strerror(rc), rc );
146 goto done;
147 }
148 wc->dn2id_w = cursor;
149 }
150
151 cursor->set_key(cursor, revdn);
152 rc = cursor->remove(cursor);
153 if ( rc ) {
154 Debug( LDAP_DEBUG_ANY,
155 "wt_dn2id_delete: remove failed: %s (%d)\n",
156 wiredtiger_strerror(rc), rc );
157 goto done;
158 }
159
160 if (wi->wi_flags & WT_USE_IDLCACHE) {
161 wt_idlcache_clear(op, wc, ndn);
162 }
163
164 Debug( LDAP_DEBUG_TRACE,
165 "<= wt_dn2id_delete %s: %d\n", ndn->bv_val, rc );
166 done:
167 if(revdn){
168 ch_free(revdn);
169 }
170
171 #ifdef WT_CURSOR_CACHE
172 if(cursor){
173 cursor->reset(cursor);
174 }
175 #else
176 if(cursor){
177 cursor->close(cursor);
178 wc->dn2id_w = NULL;
179 }
180 #endif
181 return rc;
182 }
183
184 int
185 wt_dn2id(
186 Operation *op,
187 wt_ctx *wc,
188 struct berval *ndn,
189 ID *id)
190 {
191 WT_SESSION *session = wc->session;
192 WT_CURSOR *cursor = wc->dn2id_ndn;
193 int rc = LDAP_SUCCESS;
194
195 Debug( LDAP_DEBUG_TRACE, "=> wt_dn2id(\"%s\")\n", ndn->bv_val );
196
197 if ( ndn->bv_len == 0 ) {
198 *id = 0;
199 goto done;
200 }
201
202 if(!cursor){
203 rc = session->open_cursor(session, WT_INDEX_NDN
204 "(id)",
205 NULL, NULL, &cursor);
206 if( rc ){
207 Debug( LDAP_DEBUG_ANY,
208 "wt_dn2id: cursor open failed: %s (%d)\n",
209 wiredtiger_strerror(rc), rc );
210 goto done;
211 }
212 wc->dn2id_ndn = cursor;
213 }
214
215 cursor->set_key(cursor, ndn->bv_val);
216 rc = cursor->search(cursor);
217 switch( rc ){
218 case 0:
219 break;
220 case WT_NOTFOUND:
221 goto done;
222 default:
223 Debug( LDAP_DEBUG_ANY,
224 "wt_dn2id: search failed: %s (%d)\n",
225 wiredtiger_strerror(rc), rc );
226 goto done;
227 }
228 rc = cursor->get_value(cursor, id);
229 if( rc ){
230 Debug( LDAP_DEBUG_ANY,
231 "wt_dn2id: get_value failed: %s (%d)\n",
232 wiredtiger_strerror(rc), rc );
233 goto done;
234 }
235
236 done:
237
238 #ifdef WT_CURSOR_CACHE
239 if(cursor){
240 cursor->reset(cursor);
241 }
242 #else
243 if(cursor){
244 cursor->close(cursor);
245 wc->dn2id_ndn = NULL;
246 }
247 #endif
248
249 if( rc ) {
250 Debug( LDAP_DEBUG_TRACE, "<= wt_dn2id: get failed: %s (%d)\n",
251 wiredtiger_strerror(rc), rc );
252 } else {
253 Debug( LDAP_DEBUG_TRACE, "<= wt_dn2id: got id=0x%lx\n",
254 *id );
255 }
256
257 return rc;
258 }
259
260 int
261 wt_dn2id_has_children(
262 Operation *op,
263 wt_ctx *wc,
264 ID id )
265 {
266 WT_SESSION *session = wc->session;
267 WT_CURSOR *cursor = wc->index_pid;
268 int rc;
269 uint64_t key = id;
270
271 if(!cursor){
272 rc = session->open_cursor(session, WT_INDEX_PID,
273 NULL, NULL, &cursor);
274 if( rc ){
275 Debug( LDAP_DEBUG_ANY,
276 "wt_dn2id_has_children: cursor open failed: %s (%d)\n",
277 wiredtiger_strerror(rc), rc );
278 goto done;
279 }
280 wc->index_pid = cursor;
281 }
282
283 cursor->set_key(cursor, key);
284 rc = cursor->search(cursor);
285
286 done:
287
288 #ifdef WT_CURSOR_CACHE
289 if(cursor){
290 cursor->reset(cursor);
291 }
292 #else
293 if(cursor){
294 cursor->close(cursor);
295 wc->index_pid = NULL;
296 }
297 #endif
298
299 return rc;
300 }
301
302 int
303 wt_dn2idl_db(
304 Operation *op,
305 wt_ctx *wc,
306 struct berval *ndn,
307 Entry *e,
308 ID *ids,
309 ID *stack)
310 {
311 WT_SESSION *session = wc->session;
312 WT_CURSOR *cursor = wc->dn2id;
313 int rc;
314 char *revdn = NULL;
315 size_t revdn_len;
316 char *key;
317 ID id, pid;
318
319 Debug( LDAP_DEBUG_TRACE,
320 "=> wt_dn2idl(\"%s\")\n",
321 ndn->bv_val );
322
323 revdn = mkrevdn(*ndn);
324 revdn_len = strlen(revdn);
325
326 if ( !cursor ) {
327 rc = session->open_cursor(session, WT_TABLE_DN2ID"(id, pid)",
328 NULL, NULL, &cursor);
329 if( rc ){
330 Debug( LDAP_DEBUG_ANY,
331 "wt_dn2idl: cursor open failed: %s (%d)\n",
332 wiredtiger_strerror(rc), rc );
333 goto done;
334 }
335 wc->dn2id = cursor;
336 }
337 cursor->set_key(cursor, revdn);
338 rc = cursor->search(cursor);
339 if( rc ){
340 Debug( LDAP_DEBUG_ANY,
341 "wt_dn2idl: search failed: %s (%d)\n",
342 wiredtiger_strerror(rc), rc );
343 goto done;
344 }
345
346 if( op->ors_scope == LDAP_SCOPE_CHILDREN ) {
347 cursor->next(cursor);
348 }
349
350 do {
351 rc = cursor->get_key(cursor, &key);
352 if( rc ){
353 Debug( LDAP_DEBUG_ANY,
354 "wt_dn2idl: get_key failed: %s (%d)\n",
355 wiredtiger_strerror(rc), rc );
356 goto done;
357 }
358 rc = cursor->get_value(cursor, &id, &pid);
359 if( rc ){
360 Debug( LDAP_DEBUG_ANY,
361 "wt_dn2id: get_value failed: %s (%d)\n",
362 wiredtiger_strerror(rc), rc );
363 goto done;
364 }
365
366 if( strncmp(revdn, key, revdn_len) ){
367 break;
368 }
369
370 if( op->ors_scope == LDAP_SCOPE_ONELEVEL && e->e_id != pid ){
371 goto next;
372 }
373 wt_idl_append_one(ids, id);
374 next:
375 rc = cursor->next(cursor);
376 }while(rc == 0);
377
378 if (rc == WT_NOTFOUND ) {
379 rc = LDAP_SUCCESS;
380 }
381
382 wt_idl_sort(ids, stack);
383 Debug( LDAP_DEBUG_TRACE,
384 "<= wt_dn2idl_db: size=%ld first=%ld last=%ld\n",
385 (long) ids[0],
386 (long) WT_IDL_FIRST(ids),
387 (long) WT_IDL_LAST(ids) );
388
389 done:
390 if(revdn){
391 ch_free(revdn);
392 }
393 #ifdef WT_CURSOR_CACHE
394 if(cursor){
395 cursor->reset(cursor);
396 }
397 #else
398 if(cursor){
399 cursor->close(cursor);
400 wc->dn2id = NULL;
401 }
402 #endif
403 return rc;
404 }
405
406 int
407 wt_dn2idl(
408 Operation *op,
409 wt_ctx *wc,
410 struct berval *ndn,
411 Entry *e,
412 ID *ids,
413 ID *stack)
414 {
415 struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
416 int rc;
417
418 Debug( LDAP_DEBUG_TRACE,
419 "=> wt_dn2idl(\"%s\")\n", ndn->bv_val );
420
421 if(op->ors_scope != LDAP_SCOPE_ONELEVEL &&
422 be_issuffix( op->o_bd, &e->e_nname )){
423 WT_IDL_ALL(wi, ids);
424 return 0;
425 }
426
427 if (wi->wi_flags & WT_USE_IDLCACHE) {
428 rc = wt_idlcache_get(wc, ndn, op->ors_scope, ids);
429 if (rc == 0) {
430 /* cache hit */
431 return rc;
432 }
433 /* cache miss */
434 }
435
436 if ( wi->wi_flags & WT_USE_IDLCACHE ) {
437 wt_idlcache_begin(wc, ndn, op->ors_scope);
438 }
439 rc = wt_dn2idl_db(op, wc, ndn, e, ids, stack);
440 if ( rc == 0 && wi->wi_flags & WT_USE_IDLCACHE ) {
441 wt_idlcache_set(wc, ndn, op->ors_scope, ids);
442 }
443
444 return rc;
445 }
446
447 /*
448 * Local variables:
449 * indent-tabs-mode: t
450 * tab-width: 4
451 * c-basic-offset: 4
452 * End:
453 */