summaryrefslogtreecommitdiffstats
path: root/servers/slapd/back-wt/id2entry.c
blob: 95b197cd4f5ed0972f6cfce50ee67c84f16bc577 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* OpenLDAP WiredTiger backend */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2002-2022 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
 * based on back-bdb for inclusion in OpenLDAP Software.
 * WiredTiger is a product of MongoDB Inc.
 */

#include "back-wt.h"
#include "slap-config.h"

static int wt_id2entry_put(
	Operation *op,
	wt_ctx *wc,
	Entry *e,
	WT_CURSOR *cursor)
{
	struct berval bv;
	WT_ITEM item;
	int rc;

	rc = entry_encode( e, &bv );
	if(rc != LDAP_SUCCESS){
		return -1;
	}
	item.size = bv.bv_len;
	item.data = bv.bv_val;

	cursor->set_key(cursor, e->e_id);
	cursor->set_value(cursor, e->e_ndn, &item);
	rc = cursor->insert(cursor);
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			   "wt_id2entry_put: insert failed: %s (%d)\n",
			   wiredtiger_strerror(rc), rc );
		goto done;
	}

done:
	ch_free( bv.bv_val );

	return rc;
}

int wt_id2entry_add(
	Operation *op,
	wt_ctx *wc,
	Entry *e )
{
	WT_SESSION *session = wc->session;
	WT_CURSOR *cursor = wc->id2entry_add;
	int rc;

	if(!cursor){
		rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL,
								  "overwrite=false", &cursor);
		if ( rc ) {
			Debug( LDAP_DEBUG_ANY,
				   "wt_id2entry_put: open_cursor failed: %s (%d)\n",
				   wiredtiger_strerror(rc), rc );
			return rc;
		}
		wc->id2entry_add = cursor;
	}

	rc = wt_id2entry_put(op, wc, e, cursor);

#ifdef WT_CURSOR_CACHE
	if(cursor){
		cursor->reset(cursor);
	}
#else
	if(cursor){
		cursor->close(cursor);
		wc->id2entry_add = NULL;
	}
#endif

	return rc;
}

int wt_id2entry_update(
	Operation *op,
	wt_ctx *wc,
	Entry *e )
{
	WT_SESSION *session = wc->session;
	WT_CURSOR *cursor = wc->id2entry_update;
	int rc;

	if(!cursor){
		rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL,
								  "overwrite=true", &cursor);
		if ( rc ) {
			Debug( LDAP_DEBUG_ANY,
				   "wt_id2entry_put: open_cursor failed: %s (%d)\n",
				   wiredtiger_strerror(rc), rc );
			return rc;
		}
		wc->id2entry_update = cursor;
	}
	rc = wt_id2entry_put(op, wc, e, cursor);

#ifdef WT_CURSOR_CACHE
	if(cursor){
		cursor->reset(cursor);
	}
#else
	if(cursor){
		cursor->close(cursor);
		wc->id2entry_update = NULL;
	}
#endif
	return rc;
}

int wt_id2entry_delete(
	Operation *op,
	wt_ctx *wc,
	Entry *e )
{
	int rc;
	WT_SESSION *session = wc->session;
	WT_CURSOR *cursor = NULL;

	rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL,
							  NULL, &cursor);
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			   "wt_id2entry_delete: open_cursor failed: %s (%d)\n",
			   wiredtiger_strerror(rc), rc );
		goto done;
	}
	cursor->set_key(cursor, e->e_id);
	rc = cursor->remove(cursor);
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			   "wt_id2entry_delete: remove failed: %s (%d)\n",
			   wiredtiger_strerror(rc), rc );
		goto done;
	}

done:
	if(cursor){
		cursor->close(cursor);
	}
	return rc;
}

int wt_id2entry( BackendDB *be,
				 wt_ctx *wc,
				 ID id,
				 Entry **ep ){
	int rc;
	WT_SESSION *session = wc->session;
	WT_CURSOR *cursor = wc->id2entry;
	WT_ITEM item;
	EntryHeader eh;
	int eoff;
	Entry *e = NULL;

	if(!cursor){
		rc = session->open_cursor(session, WT_TABLE_ID2ENTRY"(entry)", NULL,
								  NULL, &cursor);
		if ( rc ) {
			Debug( LDAP_DEBUG_ANY,
				   "wt_id2entry: open_cursor failed: %s (%d)\n",
				   wiredtiger_strerror(rc), rc );
			goto done;
		}
		wc->id2entry = cursor;
	}

	cursor->set_key(cursor, id);
	rc = cursor->search(cursor);
	if ( rc ) {
		goto done;
	}

	cursor->get_value(cursor, &item);
	rc = wt_entry_header( &item,  &eh );
	eoff = eh.data - (char *)item.data;
	eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size;
	eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
	memset(eh.bv.bv_val, 0xff, eh.bv.bv_len);
	eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
	memcpy(eh.data, item.data, item.size);
	eh.data += eoff;
	rc = entry_decode( &eh, &e );
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			   "wt_id2entry: entry decode error: %s (%d)\n",
			   wiredtiger_strerror(rc), rc );
		goto done;
	}
	e->e_id = id;
	*ep = e;

done:

#ifdef WT_CURSOR_CACHE
	if(cursor){
		cursor->reset(cursor);
	}
#else
	if(cursor){
		cursor->close(cursor);
		wc->id2entry = NULL;
	}
#endif
	return rc;
}

int wt_entry_return(
	Entry *e
	)
{
	if ( !e ) {
		return 0;
	}

    /* Our entries are allocated in two blocks; the data comes from
	 * the db itself and the Entry structure and associated pointers
	 * are allocated in entry_decode. The db data pointer is saved
	 * in e_bv.
	 */
	if ( e->e_bv.bv_val ) {
#if 0
		/* See if the DNs were changed by modrdn */
		if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
			e->e_bv.bv_val + e->e_bv.bv_len ) {
			ch_free(e->e_name.bv_val);
			ch_free(e->e_nname.bv_val);
		}
#endif
		e->e_name.bv_val = NULL;
		e->e_nname.bv_val = NULL;
		/* In tool mode the e_bv buffer is realloc'd, leave it alone */
		if( !(slapMode & SLAP_TOOL_MODE) ) {
			free( e->e_bv.bv_val );
		}
		BER_BVZERO( &e->e_bv );
	}

	entry_free( e );
}

int wt_entry_release(
	Operation *op,
	Entry *e,
	int rw )
{
	return wt_entry_return( e );
}

/*
 * return LDAP_SUCCESS IFF we can retrieve the specified entry.
 */
int wt_entry_get(
	Operation *op,
	struct berval *ndn,
	ObjectClass *oc,
	AttributeDescription *at,
	int rw,
	Entry **ent )
{
	struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
	wt_ctx *wc;
	Entry *e = NULL;
	int	rc;
	const char *at_name = at ? at->ad_cname.bv_val : "(null)";

	Debug( LDAP_DEBUG_ARGS,
		   "wt_entry_get: ndn: \"%s\"\n", ndn->bv_val );
	Debug( LDAP_DEBUG_ARGS,
		   "wt_entry_get: oc: \"%s\", at: \"%s\"\n",
		   oc ? oc->soc_cname.bv_val : "(null)", at_name );

	wc = wt_ctx_get(op, wi);
	if( !wc ){
		Debug( LDAP_DEBUG_ANY,
			   "wt_entry_get: wt_ctx_get failed\n" );
		return LDAP_OTHER;
	}
	rc = wt_dn2entry(op->o_bd, wc, ndn, &e);
	switch( rc ) {
	case 0:
		break;
	case WT_NOTFOUND:
		Debug( LDAP_DEBUG_ACL,
			   "wt_entry_get: cannot find entry: \"%s\"\n",
			   ndn->bv_val );
		return LDAP_NO_SUCH_OBJECT;
	default:
		Debug( LDAP_DEBUG_ANY,
			   "wt_entry_get: wt_dn2entry failed %s rc=%d\n",
			   wiredtiger_strerror(rc), rc );
		rc = LDAP_OTHER;
	}

	Debug( LDAP_DEBUG_ACL,
		   "wt_entry_get: found entry: \"%s\"\n", ndn->bv_val );

	if ( oc && !is_entry_objectclass( e, oc, 0 )) {
		Debug( LDAP_DEBUG_ACL,
			   "wt_entry_get: failed to find objectClass %s\n",
			   oc->soc_cname.bv_val );
		rc = LDAP_NO_SUCH_ATTRIBUTE;
		goto return_results;
	}

	/* NOTE: attr_find() or attrs_find()? */
	if ( at && attr_find( e->e_attrs, at ) == NULL ) {
		Debug( LDAP_DEBUG_ACL,
			   "wt_entry_get: failed to find attribute %s\n",
			   at->ad_cname.bv_val );
		rc = LDAP_NO_SUCH_ATTRIBUTE;
		goto return_results;
	}

return_results:
	if( rc != LDAP_SUCCESS ) {
		wt_entry_return( e );
	}else{
		*ent = e;
	}

	Debug( LDAP_DEBUG_TRACE, "wt_entry_get: rc=%d\n", rc );

	return rc;
}

/*
 * Local variables:
 * indent-tabs-mode: t
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */