1 /* $NetBSD: key.c,v 1.2 2021/08/14 16:15:02 christos Exp $ */
2
3 /* OpenLDAP WiredTiger backend */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2002-2021 The OpenLDAP Foundation.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
17 */
18 /* ACKNOWLEDGEMENTS:
19 * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
20 * based on back-bdb for inclusion in OpenLDAP Software.
21 * WiredTiger is a product of MongoDB Inc.
22 */
23
24 #include <sys/cdefs.h>
25 __RCSID("$NetBSD: key.c,v 1.2 2021/08/14 16:15:02 christos Exp $");
26
27 #include "portable.h"
28
29 #include <stdio.h>
30 #include <ac/string.h>
31 #include "back-wt.h"
32 #include "slap-config.h"
33 #include "idl.h"
34
35 /* read a key */
36 int
wt_key_read(Backend * be,WT_CURSOR * cursor,struct berval * k,ID * ids,WT_CURSOR ** saved_cursor,int get_flag)37 wt_key_read(
38 Backend *be,
39 WT_CURSOR *cursor,
40 struct berval *k,
41 ID *ids,
42 WT_CURSOR **saved_cursor,
43 int get_flag
44 )
45 {
46 int rc;
47 WT_ITEM key;
48 int exact;
49 WT_ITEM key2;
50 ID id;
51
52 Debug( LDAP_DEBUG_TRACE, "=> key_read\n" );
53
54 WT_IDL_ZERO(ids);
55
56 bv2ITEM(k, &key);
57 cursor->set_key(cursor, &key, 0);
58 rc = cursor->search_near(cursor, &exact);
59 if( rc ){
60 Debug( LDAP_DEBUG_ANY,
61 LDAP_XSTRING(wt_key_read)
62 ": search_near failed: %s (%d)\n",
63 wiredtiger_strerror(rc), rc );
64 goto done;
65 }
66
67 do {
68 rc = cursor->get_key(cursor, &key2, &id);
69 if( rc ){
70 Debug( LDAP_DEBUG_ANY,
71 LDAP_XSTRING(wt_key_read)
72 ": get_key failed: %s (%d)\n",
73 wiredtiger_strerror(rc), rc );
74 break;
75 }
76
77 if (key.size != key2.size || memcmp(key.data, key2.data, key.size)) {
78 if(exact < 0){
79 rc = cursor->next(cursor);
80 if (rc) {
81 break;
82 }else{
83 continue;
84 }
85 }
86 break;
87 }
88 exact = 0;
89 wt_idl_append_one(ids, id);
90 rc = cursor->next(cursor);
91 } while(rc == 0);
92
93 if (rc == WT_NOTFOUND ) {
94 rc = LDAP_SUCCESS;
95 }
96
97 done:
98 if( rc != LDAP_SUCCESS ) {
99 Debug( LDAP_DEBUG_TRACE, "<= wt_key_read: failed (%d)\n",
100 rc );
101 } else {
102 Debug( LDAP_DEBUG_TRACE, "<= wt_key_read %ld candidates\n",
103 (long) WT_IDL_N(ids) );
104 }
105
106 return rc;
107 }
108
109 /* Add or remove stuff from index files */
110 int
wt_key_change(Backend * be,WT_CURSOR * cursor,struct berval * k,ID id,int op)111 wt_key_change(
112 Backend *be,
113 WT_CURSOR *cursor,
114 struct berval *k,
115 ID id,
116 int op
117 )
118 {
119 int rc;
120 WT_ITEM item;
121
122 Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n",
123 op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id );
124
125 bv2ITEM(k, &item);
126 cursor->set_key(cursor, &item, id);
127 cursor->set_value(cursor, NULL);
128
129 if (op == SLAP_INDEX_ADD_OP) {
130 /* Add values */
131 rc = cursor->insert(cursor);
132 if ( rc == WT_DUPLICATE_KEY ) rc = 0;
133 } else {
134 /* Delete values */
135 rc = cursor->remove(cursor);
136 if ( rc == WT_NOTFOUND ) rc = 0;
137 }
138 if( rc ) {
139 Debug( LDAP_DEBUG_ANY,
140 LDAP_XSTRING(wt_key_change)
141 ": error: %s (%d)\n",
142 wiredtiger_strerror(rc), rc );
143 return rc;
144 }
145
146 Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc );
147
148 return rc;
149 }
150
151 /*
152 * Local variables:
153 * indent-tabs-mode: t
154 * tab-width: 4
155 * c-basic-offset: 4
156 * End:
157 */
158