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
447
|
/*-
* Copyright (c) 2008-2012 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __cursor_size_chk --
* Return if an inserted item is too large.
*/
static inline int
__cursor_size_chk(WT_SESSION_IMPL *session, WT_ITEM *kv)
{
WT_BTREE *btree;
btree = session->btree;
if (btree->type == BTREE_COL_FIX) {
/* Fixed-size column-stores take a single byte. */
if (kv->size != 1)
WT_RET_MSG(session, EINVAL,
"item size of %" PRIu32 " does not match "
"fixed-length file requirement of 1 byte",
kv->size);
} else {
if (kv->size > WT_BTREE_MAX_OBJECT_SIZE)
WT_RET_MSG(session, EINVAL,
"item size of %" PRIu32 " exceeds the maximum "
"supported size of %" PRIu32,
kv->size, WT_BTREE_MAX_OBJECT_SIZE);
}
return (0);
}
/*
* __cursor_fix_implicit --
* Return if search went past the end of the tree.
*/
static inline int
__cursor_fix_implicit(WT_BTREE *btree, WT_CURSOR_BTREE *cbt)
{
return (btree->type == BTREE_COL_FIX &&
!F_ISSET(cbt, WT_CBT_MAX_RECORD) ? 1 : 0);
}
/*
* __cursor_invalid --
* Return if the cursor references an invalid K/V pair (either the pair
* doesn't exist at all because the tree is empty, or the pair was deleted).
*/
static inline int
__cursor_invalid(WT_CURSOR_BTREE *cbt)
{
WT_BTREE *btree;
WT_CELL *cell;
WT_CELL_UNPACK unpack;
WT_COL *cip;
WT_INSERT *ins;
WT_PAGE *page;
btree = cbt->btree;
ins = cbt->ins;
page = cbt->page;
/* If we found an item on an insert list, check there. */
if (ins != NULL)
return (WT_UPDATE_DELETED_ISSET(ins->upd) ? 1 : 0);
/* The page may be empty, the search routine doesn't check. */
if (page->entries == 0)
return (1);
/* Otherwise, check for an update in the page's slots. */
switch (btree->type) {
case BTREE_COL_FIX:
break;
case BTREE_COL_VAR:
cip = &page->u.col_var.d[cbt->slot];
if ((cell = WT_COL_PTR(page, cip)) == NULL)
return (WT_NOTFOUND);
__wt_cell_unpack(cell, &unpack);
if (unpack.type == WT_CELL_DEL)
return (1);
break;
case BTREE_ROW:
if (page->u.row.upd != NULL &&
page->u.row.upd[cbt->slot] != NULL &&
WT_UPDATE_DELETED_ISSET(page->u.row.upd[cbt->slot]))
return (1);
break;
}
return (0);
}
/*
* __wt_btcur_reset --
* Invalidate the cursor position.
*/
int
__wt_btcur_reset(WT_CURSOR_BTREE *cbt)
{
WT_SESSION_IMPL *session;
session = (WT_SESSION_IMPL *)cbt->iface.session;
WT_BSTAT_INCR(session, cursor_resets);
__cursor_func_init(cbt, 1);
__cursor_search_clear(cbt);
return (0);
}
/*
* __wt_btcur_search --
* Search for a matching record in the tree.
*/
int
__wt_btcur_search(WT_CURSOR_BTREE *cbt)
{
WT_BTREE *btree;
WT_CURSOR *cursor;
WT_ITEM *val;
WT_SESSION_IMPL *session;
int ret;
btree = cbt->btree;
cursor = &cbt->iface;
session = (WT_SESSION_IMPL *)cursor->session;
WT_BSTAT_INCR(session, cursor_read);
if (btree->type == BTREE_ROW)
WT_RET(__cursor_size_chk(session, &cursor->key));
__cursor_func_init(cbt, 1);
WT_ERR(btree->type == BTREE_ROW ?
__wt_row_search(session, cbt, 0) :
__wt_col_search(session, cbt, 0));
if (cbt->compare != 0 || __cursor_invalid(cbt)) {
/*
* Creating a record past the end of the tree in a fixed-length
* column-store implicitly fills the gap with empty records.
*/
if (__cursor_fix_implicit(btree, cbt)) {
cbt->v = 0;
val = &cbt->iface.value;
val->data = &cbt->v;
val->size = 1;
} else
ret = WT_NOTFOUND;
} else
ret = __wt_kv_return(session, cbt, 0);
err: __cursor_func_resolve(cbt, ret);
return (ret);
}
/*
* __wt_btcur_search_near --
* Search for a record in the tree.
*/
int
__wt_btcur_search_near(WT_CURSOR_BTREE *cbt, int *exact)
{
WT_BTREE *btree;
WT_ITEM *val;
WT_CURSOR *cursor;
WT_SESSION_IMPL *session;
int ret;
btree = cbt->btree;
cursor = &cbt->iface;
session = (WT_SESSION_IMPL *)cursor->session;
WT_BSTAT_INCR(session, cursor_read_near);
if (btree->type == BTREE_ROW)
WT_RET(__cursor_size_chk(session, &cursor->key));
__cursor_func_init(cbt, 1);
WT_ERR(btree->type == BTREE_ROW ?
__wt_row_search(session, cbt, 0) :
__wt_col_search(session, cbt, 0));
/*
* Creating a record past the end of the tree in a fixed-length column-
* store implicitly fills the gap with empty records. In this case, we
* instantiate the empty record, it's an exact match.
*
* Else, if we find a valid key (one that wasn't deleted), return it.
*
* Else, if we found a deleted key, try to move to the next key in the
* tree (bias for prefix searches). Cursor next skips deleted records,
* so we don't have to test for them again.
*
* Else if there's no larger tree key, redo the search and try and find
* an earlier record. If that fails, quit, there's no record to return.
*/
if (cbt->compare != 0 && __cursor_fix_implicit(btree, cbt)) {
cbt->v = 0;
val = &cbt->iface.value;
val->data = &cbt->v;
val->size = 1;
*exact = 0;
} else if (!__cursor_invalid(cbt)) {
*exact = cbt->compare;
ret = __wt_kv_return(session, cbt, cbt->compare == 0 ? 0 : 1);
} else if ((ret = __wt_btcur_next(cbt)) != WT_NOTFOUND)
*exact = 1;
else {
WT_ERR(btree->type == BTREE_ROW ?
__wt_row_search(session, cbt, 0) :
__wt_col_search(session, cbt, 0));
if (!__cursor_invalid(cbt)) {
*exact = cbt->compare;
ret = __wt_kv_return(
session, cbt, cbt->compare == 0 ? 0 : 1);
} else if ((ret = __wt_btcur_prev(cbt)) != WT_NOTFOUND)
*exact = -1;
}
err: __cursor_func_resolve(cbt, ret);
return (ret);
}
/*
* __wt_btcur_insert --
* Insert a record into the tree.
*/
int
__wt_btcur_insert(WT_CURSOR_BTREE *cbt)
{
WT_BTREE *btree;
WT_CURSOR *cursor;
WT_SESSION_IMPL *session;
int ret;
btree = cbt->btree;
cursor = &cbt->iface;
session = (WT_SESSION_IMPL *)cursor->session;
WT_BSTAT_INCR(session, cursor_inserts);
if (btree->type == BTREE_ROW)
WT_RET(__cursor_size_chk(session, &cursor->key));
WT_RET(__cursor_size_chk(session, &cursor->value));
retry: __cursor_func_init(cbt, 1);
switch (btree->type) {
case BTREE_COL_FIX:
case BTREE_COL_VAR:
/*
* If WT_CURSTD_APPEND set insert a new record (ignoring the
* application's record number), return the record number.
*/
if (F_ISSET(cursor, WT_CURSTD_APPEND)) {
if ((ret =
__wt_col_modify(session, cbt, 1)) == WT_RESTART)
goto retry;
cbt->iface.recno = cbt->recno;
break;
}
WT_ERR(__wt_col_search(session, cbt, 1));
/*
* If WT_CURSTD_OVERWRITE set, insert/update the key/value pair.
*
* If WT_CURSTD_OVERWRITE not set, fail if the key exists, else
* insert the key/value pair. Creating a record past the end
* of the tree in a fixed-length column-store implicitly fills
* the gap with empty records. Fail in that case, the record
* exists.
*/
if (!F_ISSET(cursor, WT_CURSTD_OVERWRITE) &&
((cbt->compare == 0 && !__cursor_invalid(cbt)) ||
(cbt->compare != 0 && __cursor_fix_implicit(btree, cbt)))) {
ret = WT_DUPLICATE_KEY;
break;
}
if ((ret = __wt_col_modify(session, cbt, 3)) == WT_RESTART)
goto retry;
break;
case BTREE_ROW:
/*
* If WT_CURSTD_OVERWRITE not set, fail if the key exists, else
* insert the key/value pair.
*
* If WT_CURSTD_OVERWRITE set, insert/update the key/value pair.
*/
WT_ERR(__wt_row_search(session, cbt, 1));
if (cbt->compare == 0 &&
!__cursor_invalid(cbt) &&
!F_ISSET(cursor, WT_CURSTD_OVERWRITE)) {
ret = WT_DUPLICATE_KEY;
break;
}
if ((ret = __wt_row_modify(session, cbt, 0)) == WT_RESTART)
goto retry;
break;
WT_ILLEGAL_VALUE(session);
}
err: __cursor_func_resolve(cbt, ret);
return (ret);
}
/*
* __wt_btcur_remove --
* Remove a record from the tree.
*/
int
__wt_btcur_remove(WT_CURSOR_BTREE *cbt)
{
WT_BTREE *btree;
WT_CURSOR *cursor;
WT_SESSION_IMPL *session;
int ret;
btree = cbt->btree;
cursor = &cbt->iface;
session = (WT_SESSION_IMPL *)cursor->session;
WT_BSTAT_INCR(session, cursor_removes);
if (btree->type == BTREE_ROW)
WT_RET(__cursor_size_chk(session, &cursor->key));
retry: __cursor_func_init(cbt, 1);
switch (btree->type) {
case BTREE_COL_FIX:
case BTREE_COL_VAR:
WT_ERR(__wt_col_search(session, cbt, 1));
/*
* Remove the record if it exists. Creating a record past the
* end of the tree in a fixed-length column-store implicitly
* fills the gap with empty records. Return success in that
* case, the record was deleted successfully.
*/
if (cbt->compare != 0 || __cursor_invalid(cbt))
ret =
__cursor_fix_implicit(btree, cbt) ? 0 : WT_NOTFOUND;
else if ((ret = __wt_col_modify(session, cbt, 2)) == WT_RESTART)
goto retry;
break;
case BTREE_ROW:
/* Remove the record if it exists. */
WT_ERR(__wt_row_search(session, cbt, 1));
if (cbt->compare != 0 || __cursor_invalid(cbt))
ret = WT_NOTFOUND;
else if ((ret = __wt_row_modify(session, cbt, 1)) == WT_RESTART)
goto retry;
break;
WT_ILLEGAL_VALUE(session);
}
err: __cursor_func_resolve(cbt, ret);
return (ret);
}
/*
* __wt_btcur_update --
* Update a record in the tree.
*/
int
__wt_btcur_update(WT_CURSOR_BTREE *cbt)
{
WT_BTREE *btree;
WT_CURSOR *cursor;
WT_SESSION_IMPL *session;
int ret;
btree = cbt->btree;
cursor = &cbt->iface;
session = (WT_SESSION_IMPL *)cursor->session;
WT_BSTAT_INCR(session, cursor_updates);
if (btree->type == BTREE_ROW)
WT_RET(__cursor_size_chk(session, &cursor->key));
WT_RET(__cursor_size_chk(session, &cursor->value));
retry: __cursor_func_init(cbt, 1);
switch (btree->type) {
case BTREE_COL_FIX:
if (cursor->value.size != 1)
WT_RET_MSG(session, EINVAL,
"item size of %" PRIu32 " does not match "
"fixed-length file requirement of 1 byte",
cursor->value.size);
/* FALLTHROUGH */
case BTREE_COL_VAR:
WT_ERR(__wt_col_search(session, cbt, 1));
/*
* Update the record if it exists. Creating a record past the
* end of the tree in a fixed-length column-store implicitly
* fills the gap with empty records. Update the record in that
* case, the record exists.
*/
if ((cbt->compare != 0 || __cursor_invalid(cbt)) &&
!__cursor_fix_implicit(btree, cbt))
ret = WT_NOTFOUND;
else if ((ret = __wt_col_modify(session, cbt, 3)) == WT_RESTART)
goto retry;
break;
case BTREE_ROW:
/* Update the record it it exists. */
WT_ERR(__wt_row_search(session, cbt, 1));
if (cbt->compare != 0 || __cursor_invalid(cbt))
ret = WT_NOTFOUND;
else if ((ret = __wt_row_modify(session, cbt, 0)) == WT_RESTART)
goto retry;
break;
WT_ILLEGAL_VALUE(session);
}
err: __cursor_func_resolve(cbt, ret);
return (ret);
}
/*
* __wt_btcur_close --
* Close a btree cursor.
*/
int
__wt_btcur_close(WT_CURSOR_BTREE *cbt, const char *cfg[])
{
WT_SESSION_IMPL *session;
WT_UNUSED(cfg);
session = (WT_SESSION_IMPL *)cbt->iface.session;
__cursor_func_init(cbt, 1);
__wt_buf_free(session, &cbt->tmp);
return (0);
}
|