Debian Codegraph @

servers/slapd/back-wt/index.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 <ac/string.h>
26 #include "back-wt.h"
27 #include "slap-config.h"
28
29 static char presence_keyval[] = {0,0};
30 static struct berval presence_key = BER_BVC(presence_keyval);
31
32 AttrInfo *wt_index_mask(
33 Backend *be,
34 AttributeDescription *desc,
35 struct berval *atname )
36 {
37 AttributeType *at;
38 AttrInfo *ai = wt_attr_mask( be->be_private, desc );
39
40 if( ai ) {
41 *atname = desc->ad_cname;
42 return ai;
43 }
44
45 /* If there is a tagging option, did we ever index the base
46 * type? If so, check for mask, otherwise it's not there.
47 */
48 if( slap_ad_is_tagged( desc ) && desc != desc->ad_type->sat_ad ) {
49 /* has tagging option */
50 ai = wt_attr_mask( be->be_private, desc->ad_type->sat_ad );
51
52 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOTAGS ) ) {
53 *atname = desc->ad_type->sat_cname;
54 return ai;
55 }
56 }
57
58 /* see if supertype defined mask for its subtypes */
59 for( at = desc->ad_type; at != NULL ; at = at->sat_sup ) {
60 /* If no AD, we've never indexed this type */
61 if ( !at->sat_ad ) continue;
62
63 ai = wt_attr_mask( be->be_private, at->sat_ad );
64
65 if ( ai && !( ai->ai_indexmask & SLAP_INDEX_NOSUBTYPES ) ) {
66 *atname = at->sat_cname;
67 return ai;
68 }
69 }
70
71 return 0;
72 }
73
74 /* This function is only called when evaluating search filters.
75 */
76 int wt_index_param(
77 Backend *be,
78 AttributeDescription *desc,
79 int ftype,
80 slap_mask_t *maskp,
81 struct berval *prefixp )
82 {
83 AttrInfo *ai;
84 slap_mask_t mask, type = 0;
85
86 ai = wt_index_mask( be, desc, prefixp );
87
88 if ( !ai ) {
89 /* TODO: add monitor */
90 return LDAP_INAPPROPRIATE_MATCHING;
91 }
92 mask = ai->ai_indexmask;
93
94 switch( ftype ) {
95 case LDAP_FILTER_PRESENT:
96 type = SLAP_INDEX_PRESENT;
97 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
98 *prefixp = presence_key;
99 *maskp = mask;
100 return LDAP_SUCCESS;
101 }
102 break;
103
104 case LDAP_FILTER_APPROX:
105 type = SLAP_INDEX_APPROX;
106 if ( desc->ad_type->sat_approx ) {
107 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
108 *maskp = mask;
109 return LDAP_SUCCESS;
110 }
111 break;
112 }
113
114 /* Use EQUALITY rule and index for approximate match */
115 /* fall thru */
116
117 case LDAP_FILTER_EQUALITY:
118 type = SLAP_INDEX_EQUALITY;
119 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
120 *maskp = mask;
121 return LDAP_SUCCESS;
122 }
123 break;
124
125 case LDAP_FILTER_SUBSTRINGS:
126 type = SLAP_INDEX_SUBSTR;
127 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
128 *maskp = mask;
129 return LDAP_SUCCESS;
130 }
131 break;
132
133 default:
134 return LDAP_OTHER;
135 }
136
137 /* TODO: add monitor index */
138 return LDAP_INAPPROPRIATE_MATCHING;
139 }
140
141 static int indexer(
142 Operation *op,
143 wt_ctx *wc,
144 AttributeDescription *ad,
145 struct berval *atname,
146 BerVarray vals,
147 ID id,
148 int opid,
149 slap_mask_t mask )
150 {
151 int rc = LDAP_SUCCESS, i;
152 struct berval *keys;
153 WT_CURSOR *cursor = NULL;
154 assert( mask != 0 );
155
156 cursor = wt_index_open(wc, atname, 1);
157 if( !cursor ) {
158 Debug( LDAP_DEBUG_ANY,
159 "indexer: open index cursor failed: %s\n",
160 atname->bv_val );
161 goto done;
162 }
163
164 if( IS_SLAP_INDEX( mask, SLAP_INDEX_PRESENT ) ) {
165 rc = wt_key_change( op->o_bd, cursor, &presence_key, id, opid );
166 if( rc ) {
167 goto done;
168 }
169 }
170
171 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) ) {
172 rc = ad->ad_type->sat_equality->smr_indexer(
173 LDAP_FILTER_EQUALITY,
174 mask,
175 ad->ad_type->sat_syntax,
176 ad->ad_type->sat_equality,
177 atname, vals, &keys, op->o_tmpmemctx );
178
179 if( rc == LDAP_SUCCESS && keys != NULL ) {
180 for( i=0; keys[i].bv_val != NULL; i++ ) {
181 rc = wt_key_change( op->o_bd, cursor, &keys[i], id, opid );
182 if( rc ) {
183 ber_bvarray_free_x( keys, op->o_tmpmemctx );
184 goto done;
185 }
186 }
187 ber_bvarray_free_x( keys, op->o_tmpmemctx );
188 }
189 rc = LDAP_SUCCESS;
190 }
191
192 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) ) {
193 rc = ad->ad_type->sat_approx->smr_indexer(
194 LDAP_FILTER_APPROX,
195 mask,
196 ad->ad_type->sat_syntax,
197 ad->ad_type->sat_approx,
198 atname, vals, &keys, op->o_tmpmemctx );
199
200 if( rc == LDAP_SUCCESS && keys != NULL ) {
201 for( i=0; keys[i].bv_val != NULL; i++ ) {
202 rc = wt_key_change( op->o_bd, cursor, &keys[i], id, opid );
203 if( rc ) {
204 ber_bvarray_free_x( keys, op->o_tmpmemctx );
205 goto done;
206 }
207 }
208 ber_bvarray_free_x( keys, op->o_tmpmemctx );
209 }
210
211 rc = LDAP_SUCCESS;
212 }
213
214 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) ) {
215 rc = ad->ad_type->sat_substr->smr_indexer(
216 LDAP_FILTER_SUBSTRINGS,
217 mask,
218 ad->ad_type->sat_syntax,
219 ad->ad_type->sat_substr,
220 atname, vals, &keys, op->o_tmpmemctx );
221
222 if( rc == LDAP_SUCCESS && keys != NULL ) {
223 for( i=0; keys[i].bv_val != NULL; i++ ) {
224 rc = wt_key_change( op->o_bd, cursor, &keys[i], id, opid );
225 if( rc ) {
226 ber_bvarray_free_x( keys, op->o_tmpmemctx );
227 goto done;
228 }
229 }
230 ber_bvarray_free_x( keys, op->o_tmpmemctx );
231 }
232
233 rc = LDAP_SUCCESS;
234 }
235
236 done:
237 cursor->close(cursor);
238 return rc;
239 }
240
241 static int index_at_values(
242 Operation *op,
243 wt_ctx *wc,
244 AttributeDescription *ad,
245 AttributeType *type,
246 struct berval *tags,
247 BerVarray vals,
248 ID id,
249 int opid )
250 {
251 int rc = LDAP_SUCCESS;
252 slap_mask_t mask = 0;
253 int ixop = opid;
254 AttrInfo *ai = NULL;
255
256 if ( opid == WT_INDEX_UPDATE_OP )
257 ixop = SLAP_INDEX_ADD_OP;
258
259 if( type->sat_sup ) {
260 /* recurse */
261 rc = index_at_values( op, wc, NULL,
262 type->sat_sup, tags,
263 vals, id, opid );
264
265 if( rc ) return rc;
266 }
267
268 /* If this type has no AD, we've never used it before */
269 if( type->sat_ad ) {
270 ai = wt_attr_mask( op->o_bd->be_private, type->sat_ad );
271 if ( ai ) {
272 #ifdef LDAP_COMP_MATCH
273 /* component indexing */
274 if ( ai->ai_cr ) {
275 ComponentReference *cr;
276 for( cr = ai->ai_cr ; cr ; cr = cr->cr_next ) {
277 rc = indexer( op, wc, cr->cr_ad, &type->sat_cname,
278 cr->cr_nvals, id, ixop,
279 cr->cr_indexmask );
280 }
281 }
282 #endif
283 ad = type->sat_ad;
284 /* If we're updating the index, just set the new bits that aren't
285 * already in the old mask.
286 */
287 if ( opid == WT_INDEX_UPDATE_OP )
288 mask = ai->ai_newmask & ~ai->ai_indexmask;
289 else
290 /* For regular updates, if there is a newmask use it. Otherwise
291 * just use the old mask.
292 */
293 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
294 if( mask ) {
295 rc = indexer( op, wc, ad, &type->sat_cname,
296 vals, id, ixop, mask );
297 if( rc ) return rc;
298 }
299 }
300 }
301
302 if( tags->bv_len ) {
303 AttributeDescription *desc;
304
305 desc = ad_find_tags( type, tags );
306 if( desc ) {
307 ai = wt_attr_mask( op->o_bd->be_private, desc );
308
309 if( ai ) {
310 if ( opid == WT_INDEX_UPDATE_OP )
311 mask = ai->ai_newmask & ~ai->ai_indexmask;
312 else
313 mask = ai->ai_newmask ? ai->ai_newmask : ai->ai_indexmask;
314 if ( mask ) {
315 rc = indexer( op, wc, desc, &desc->ad_cname,
316 vals, id, ixop, mask );
317
318 if( rc ) {
319 return rc;
320 }
321 }
322 }
323 }
324 }
325
326 return LDAP_SUCCESS;
327 }
328
329 int wt_index_values(
330 Operation *op,
331 wt_ctx *wc,
332 AttributeDescription *desc,
333 BerVarray vals,
334 ID id,
335 int opid )
336 {
337 int rc;
338
339 /* Never index ID 0 */
340 if ( id == 0 )
341 return 0;
342
343 rc = index_at_values( op, wc, desc,
344 desc->ad_type, &desc->ad_tags,
345 vals, id, opid );
346
347 return rc;
348 }
349
350 int
351 wt_index_entry( Operation *op, wt_ctx *wc, int opid, Entry *e )
352 {
353 int rc;
354 Attribute *ap = e->e_attrs;
355
356 if ( e->e_id == 0 )
357 return 0;
358
359 Debug( LDAP_DEBUG_TRACE, "=> index_entry_%s( %ld, \"%s\" )\n",
360 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
361 (long) e->e_id, e->e_dn ? e->e_dn : "" );
362
363 for ( ; ap != NULL; ap = ap->a_next ) {
364 rc = wt_index_values( op, wc, ap->a_desc,
365 ap->a_nvals, e->e_id, opid );
366 if( rc != LDAP_SUCCESS ) {
367 Debug( LDAP_DEBUG_TRACE,
368 "<= index_entry_%s( %ld, \"%s\" ) failure\n",
369 opid == SLAP_INDEX_ADD_OP ? "add" : "del",
370 (long) e->e_id, e->e_dn );
371 return rc;
372 }
373 }
374
375 Debug( LDAP_DEBUG_TRACE, "<= index_entry_%s( %ld, \"%s\" ) success\n",
376 opid == SLAP_INDEX_DELETE_OP ? "del" : "add",
377 (long) e->e_id, e->e_dn ? e->e_dn : "" );
378 return 0;
379 }
380
381 WT_CURSOR *
382 wt_index_open(wt_ctx *wc, struct berval *name, int create)
383 {
384 WT_CURSOR *cursor = NULL;
385 WT_SESSION *session = wc->session;
386 char uri[1024];
387 int rc;
388
389 snprintf(uri, sizeof(uri), "table:%s", name->bv_val);
390
391 rc = session->open_cursor(session, uri, NULL, "overwrite=false", &cursor);
392 if (rc == ENOENT && create) {
393 rc = session->create(session, uri,
394 "key_format=uQ,"
395 "value_format=x,"
396 "columns=(key, id, none)");
397 if( rc ) {
398 Debug( LDAP_DEBUG_ANY,
399 "wt_index_open: table \"%s\": "
400 "cannot create index table: %s (%d)\n",
401 uri, wiredtiger_strerror(rc), rc);
402 return NULL;
403 }
404 rc = session->open_cursor(session, uri, NULL,
405 "overwrite=false", &cursor);
406 }
407 if ( rc ) {
408 Debug( LDAP_DEBUG_ANY,
409 "wt_index_open: table \"%s\": "
410 ": open cursor failed: %s (%d)\n",
411 uri, wiredtiger_strerror(rc), rc);
412 return NULL;
413 }
414 return cursor;
415 }
416
417 /*
418 * Local variables:
419 * indent-tabs-mode: t
420 * tab-width: 4
421 * c-basic-offset: 4
422 * End:
423 */