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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*-
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* Custom NEED macros for metadata cursors - that copy the values into the
* backing metadata table cursor.
*/
#define WT_MD_CURSOR_NEEDKEY(cursor) do { \
WT_CURSOR_NEEDKEY(cursor); \
WT_ERR(__wt_buf_set(session, \
&((WT_CURSOR_METADATA *)(cursor))->file_cursor->key, \
cursor->key.data, cursor->key.size)); \
F_SET(((WT_CURSOR_METADATA *)(cursor))->file_cursor, \
WT_CURSTD_KEY_EXT); \
} while (0)
#define WT_MD_CURSOR_NEEDVALUE(cursor) do { \
WT_CURSOR_NEEDVALUE(cursor); \
WT_ERR(__wt_buf_set(session, \
&((WT_CURSOR_METADATA *)(cursor))->file_cursor->value, \
cursor->value.data, cursor->value.size)); \
F_SET(((WT_CURSOR_METADATA *)(cursor))->file_cursor, \
WT_CURSTD_VALUE_EXT); \
} while (0)
#define WT_MD_SET_KEY_VALUE(c, mc, fc) do { \
(c)->key.data = (fc)->key.data; \
(c)->key.size = (fc)->key.size; \
(c)->value.data = (fc)->value.data; \
(c)->value.size = (fc)->value.size; \
F_SET((c), WT_CURSTD_KEY_EXT | WT_CURSTD_VALUE_EXT); \
F_CLR((mc), WT_MDC_ONMETADATA); \
F_SET((mc), WT_MDC_POSITIONED); \
} while (0)
/*
* Check if a key matches the metadata. The public value is "metadata:",
* but also check for the internal version of the URI.
*/
#define WT_KEY_IS_METADATA(key) \
(WT_STRING_MATCH(WT_METADATA_URI, (key)->data, (key)->size - 1) ||\
WT_STRING_MATCH(WT_METAFILE_URI, (key)->data, (key)->size - 1))
/*
* __curmetadata_metadata_search --
* Retrieve the metadata for the metadata table
*/
static int
__curmetadata_metadata_search(WT_SESSION_IMPL *session, WT_CURSOR *cursor)
{
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
const char *value;
mdc = (WT_CURSOR_METADATA *)cursor;
/* The metadata search interface allocates a new string in value. */
WT_RET(__wt_metadata_search(session, WT_METAFILE_URI, &value));
/*
* Copy the value to the underlying btree cursor's tmp item which will
* be freed when the cursor is closed.
*/
ret = __wt_buf_setstr(session, &cursor->value, value);
__wt_free(session, value);
WT_RET(ret);
WT_RET(__wt_buf_setstr(session, &cursor->key, WT_METADATA_URI));
F_SET(mdc, WT_MDC_ONMETADATA | WT_MDC_POSITIONED);
F_SET(cursor, WT_CURSTD_KEY_EXT | WT_CURSTD_VALUE_EXT);
return (0);
}
/*
* __curmetadata_compare --
* WT_CURSOR->compare method for the metadata cursor type.
*/
static int
__curmetadata_compare(WT_CURSOR *a, WT_CURSOR *b, int *cmpp)
{
WT_CURSOR *a_file_cursor, *b_file_cursor;
WT_CURSOR_METADATA *a_mdc, *b_mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
a_mdc = ((WT_CURSOR_METADATA *)a);
b_mdc = ((WT_CURSOR_METADATA *)b);
a_file_cursor = a_mdc->file_cursor;
b_file_cursor = b_mdc->file_cursor;
CURSOR_API_CALL(a, session,
compare, ((WT_CURSOR_BTREE *)a_file_cursor)->btree);
if (b->compare != __curmetadata_compare)
WT_ERR_MSG(session, EINVAL,
"Can only compare cursors of the same type");
WT_MD_CURSOR_NEEDKEY(a);
WT_MD_CURSOR_NEEDKEY(b);
if (F_ISSET(a_mdc, WT_MDC_ONMETADATA)) {
if (F_ISSET(b_mdc, WT_MDC_ONMETADATA))
*cmpp = 0;
else
*cmpp = 1;
} else if (F_ISSET(b_mdc, WT_MDC_ONMETADATA))
*cmpp = -1;
else
ret = a_file_cursor->compare(
a_file_cursor, b_file_cursor, cmpp);
err: API_END_RET(session, ret);
}
/*
* __curmetadata_next --
* WT_CURSOR->next method for the metadata cursor type.
*/
static int
__curmetadata_next(WT_CURSOR *cursor)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
next, ((WT_CURSOR_BTREE *)file_cursor)->btree);
if (!F_ISSET(mdc, WT_MDC_POSITIONED))
WT_ERR(__curmetadata_metadata_search(session, cursor));
else {
WT_ERR(file_cursor->next(mdc->file_cursor));
WT_MD_SET_KEY_VALUE(cursor, mdc, file_cursor);
}
err: if (ret != 0) {
F_CLR(mdc, WT_MDC_POSITIONED | WT_MDC_ONMETADATA);
F_CLR(cursor, WT_CURSTD_KEY_EXT | WT_CURSTD_VALUE_EXT);
}
API_END_RET(session, ret);
}
/*
* __curmetadata_prev --
* WT_CURSOR->prev method for the metadata cursor type.
*/
static int
__curmetadata_prev(WT_CURSOR *cursor)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
prev, ((WT_CURSOR_BTREE *)file_cursor)->btree);
if (F_ISSET(mdc, WT_MDC_ONMETADATA)) {
ret = WT_NOTFOUND;
goto err;
}
ret = file_cursor->prev(file_cursor);
if (ret == 0) {
WT_MD_SET_KEY_VALUE(cursor, mdc, file_cursor);
} else if (ret == WT_NOTFOUND)
WT_ERR(__curmetadata_metadata_search(session, cursor));
err: if (ret != 0) {
F_CLR(mdc, WT_MDC_POSITIONED | WT_MDC_ONMETADATA);
F_CLR(cursor, WT_CURSTD_KEY_EXT | WT_CURSTD_VALUE_EXT);
}
API_END_RET(session, ret);
}
/*
* __curmetadata_reset --
* WT_CURSOR->reset method for the metadata cursor type.
*/
static int
__curmetadata_reset(WT_CURSOR *cursor)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
reset, ((WT_CURSOR_BTREE *)file_cursor)->btree);
if (F_ISSET(mdc, WT_MDC_POSITIONED) &&
!F_ISSET(mdc, WT_MDC_ONMETADATA))
ret = file_cursor->reset(file_cursor);
F_CLR(mdc,
WT_MDC_POSITIONED | WT_MDC_ONMETADATA);
F_CLR(cursor, WT_CURSTD_KEY_SET | WT_CURSTD_VALUE_SET);
err: API_END_RET(session, ret);
}
/*
* __curmetadata_search --
* WT_CURSOR->search method for the metadata cursor type.
*/
static int
__curmetadata_search(WT_CURSOR *cursor)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
search, ((WT_CURSOR_BTREE *)file_cursor)->btree);
WT_MD_CURSOR_NEEDKEY(cursor);
if (WT_KEY_IS_METADATA(&cursor->key))
WT_ERR(__curmetadata_metadata_search(session, cursor));
else {
WT_ERR(file_cursor->search(file_cursor));
WT_MD_SET_KEY_VALUE(cursor, mdc, file_cursor);
}
err: if (ret != 0) {
F_CLR(mdc, WT_MDC_POSITIONED | WT_MDC_ONMETADATA);
F_CLR(cursor, WT_CURSTD_KEY_EXT | WT_CURSTD_VALUE_EXT);
}
API_END_RET(session, ret);
}
/*
* __curmetadata_search_near --
* WT_CURSOR->search_near method for the metadata cursor type.
*/
static int
__curmetadata_search_near(WT_CURSOR *cursor, int *exact)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
search_near, ((WT_CURSOR_BTREE *)file_cursor)->btree);
WT_MD_CURSOR_NEEDKEY(cursor);
if (WT_KEY_IS_METADATA(&cursor->key)) {
WT_ERR(__curmetadata_metadata_search(session, cursor));
*exact = 1;
} else {
WT_ERR(file_cursor->search_near(file_cursor, exact));
WT_MD_SET_KEY_VALUE(cursor, mdc, file_cursor);
}
err: if (ret != 0) {
F_CLR(mdc, WT_MDC_POSITIONED | WT_MDC_ONMETADATA);
F_CLR(cursor, WT_CURSTD_KEY_EXT | WT_CURSTD_VALUE_EXT);
}
API_END_RET(session, ret);
}
/*
* __curmetadata_insert --
* WT_CURSOR->insert method for the metadata cursor type.
*/
static int
__curmetadata_insert(WT_CURSOR *cursor)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
insert, ((WT_CURSOR_BTREE *)file_cursor)->btree);
WT_MD_CURSOR_NEEDKEY(cursor);
WT_MD_CURSOR_NEEDVALUE(cursor);
/*
* Since the key/value formats are 's' the WT_ITEMs must contain a
* NULL terminated string.
*/
ret =
__wt_metadata_insert(session, cursor->key.data, cursor->value.data);
err: API_END_RET(session, ret);
}
/*
* __curmetadata_update --
* WT_CURSOR->update method for the metadata cursor type.
*/
static int
__curmetadata_update(WT_CURSOR *cursor)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
update, ((WT_CURSOR_BTREE *)file_cursor)->btree);
WT_MD_CURSOR_NEEDKEY(cursor);
WT_MD_CURSOR_NEEDVALUE(cursor);
/*
* Since the key/value formats are 's' the WT_ITEMs must contain a
* NULL terminated string.
*/
ret =
__wt_metadata_update(session, cursor->key.data, cursor->value.data);
err: API_END_RET(session, ret);
}
/*
* __curmetadata_remove --
* WT_CURSOR->remove method for the metadata cursor type.
*/
static int
__curmetadata_remove(WT_CURSOR *cursor)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
remove, ((WT_CURSOR_BTREE *)file_cursor)->btree);
WT_MD_CURSOR_NEEDKEY(cursor);
/*
* Since the key format is 's' the WT_ITEM must contain a NULL
* terminated string.
*/
ret = __wt_metadata_remove(session, cursor->key.data);
err: API_END_RET(session, ret);
}
/*
* __curmetadata_close --
* WT_CURSOR->close method for the metadata cursor type.
*/
static int
__curmetadata_close(WT_CURSOR *cursor)
{
WT_CURSOR *file_cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_SESSION_IMPL *session;
mdc = (WT_CURSOR_METADATA *)cursor;
file_cursor = mdc->file_cursor;
CURSOR_API_CALL(cursor, session,
close, ((WT_CURSOR_BTREE *)file_cursor)->btree);
ret = file_cursor->close(file_cursor);
WT_TRET(__wt_cursor_close(cursor));
err: API_END_RET(session, ret);
}
/*
* __wt_curmetadata_open --
* WT_SESSION->open_cursor method for metadata cursors.
*
* Metadata cursors are a similar to a file cursor on the special metadata
* table, except that the metadata for the metadata table (which is stored
* in the turtle file) can also be queried.
*
* Metadata cursors are read-only default.
*/
int
__wt_curmetadata_open(WT_SESSION_IMPL *session,
const char *uri, WT_CURSOR *owner, const char *cfg[], WT_CURSOR **cursorp)
{
WT_CURSOR_STATIC_INIT(iface,
NULL, /* get-key */
NULL, /* get-value */
NULL, /* set-key */
NULL, /* set-value */
__curmetadata_compare, /* compare */
__curmetadata_next, /* next */
__curmetadata_prev, /* prev */
__curmetadata_reset, /* reset */
__curmetadata_search, /* search */
__curmetadata_search_near, /* search-near */
__curmetadata_insert, /* insert */
__curmetadata_update, /* update */
__curmetadata_remove, /* remove */
__curmetadata_close); /* close */
WT_CURSOR *cursor;
WT_CURSOR_METADATA *mdc;
WT_DECL_RET;
WT_RET(__wt_calloc(session, 1, sizeof(WT_CURSOR_METADATA), &mdc));
cursor = &mdc->iface;
*cursor = iface;
cursor->session = &session->iface;
cursor->key_format = "S";
cursor->value_format = "S";
/* Open the file cursor for operations on the regular metadata */
WT_ERR(__wt_metadata_cursor(session, cfg[1], &mdc->file_cursor));
WT_ERR(__wt_cursor_init(cursor, uri, owner, cfg, cursorp));
/* Metadata cursors default to read only. */
WT_ERR(__wt_cursor_config_readonly(cursor, cfg, 1));
if (0) {
err: __wt_free(session, mdc);
}
return (ret);
}
|