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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2008-2011 WiredTiger, Inc.
* All rights reserved.
*/
#include "wt_internal.h"
/*
* __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_leaf.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_leaf.upd != NULL &&
page->u.row_leaf.upd[cbt->slot] != NULL &&
WT_UPDATE_DELETED_ISSET(page->u.row_leaf.upd[cbt->slot]))
return (1);
break;
}
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_SESSION_IMPL *session;
int ret;
btree = cbt->btree;
cursor = &cbt->iface;
session = (WT_SESSION_IMPL *)cursor->session;
WT_BSTAT_INCR(session, cursor_read);
__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))
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_CURSOR *cursor;
WT_SESSION_IMPL *session;
int (*srch)(WT_SESSION_IMPL *, WT_CURSOR_BTREE *, int);
int ret;
btree = cbt->btree;
cursor = &cbt->iface;
session = (WT_SESSION_IMPL *)cursor->session;
srch = btree->type == BTREE_ROW ? __wt_row_search : __wt_col_search;
WT_BSTAT_INCR(session, cursor_read_near);
/*
* We assume "range-prefix" semantics are more likely, so where we don't
* have an exact match we prefer to return tree keys greater than the
* search key, rather than less than the search key.
*
* If we find an exact match, or the search key is smaller than the tree
* key, and the tree key has not been deleted, return the tree key.
*/
__cursor_func_init(cbt, 1);
WT_ERR(srch(session, cbt, 0));
if (cbt->compare == 0 || cbt->compare == 1)
if (!__cursor_invalid(cbt)) {
*exact = cbt->compare;
ret = __wt_kv_return(session, cbt, 1);
goto done;
}
/*
* Otherwise, we have a deleted key, or the tree key is smaller than the
* search key: move to the next key in the tree. Cursor next skips over
* deleted records, so we don't have to test for them.
*/
*exact = 1;
if ((ret = __wt_btcur_next(cbt)) != WT_NOTFOUND)
goto done;
/*
* If there's no larger tree key, we repeat the search (we've discarded
* the original position information). This time we'll take anything
* that's not deleted, and we'll look for a previous record instead of
* a subsequent record. If we don't find a previous record, there's no
* record to return, quit.
*/
__cursor_func_init(cbt, 1);
WT_ERR(srch(session, cbt, 0));
if (!__cursor_invalid(cbt)) {
*exact = cbt->compare;
ret = __wt_kv_return(session, cbt, 1);
goto done;
}
*exact = -1;
ret = __wt_btcur_prev(cbt);
err:
done: __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);
retry: __cursor_func_init(cbt, 1);
switch (btree->type) {
case BTREE_COL_FIX:
if (cursor->value.size != 1) {
__wt_errx(session,
"item size of %" PRIu32 " does not match "
"fixed-length file requirement of 1 byte",
cursor->value.size);
return (WT_ERROR);
}
/* FALLTHROUGH */
case BTREE_COL_VAR:
/*
* If WT_CURSTD_OVERWRITE set, insert/update the application
* specified record, otherwise insert a new record (ignoring
* the application's record number), return the record number
* to the application.
*/
if (F_ISSET(cursor, WT_CURSTD_OVERWRITE)) {
WT_ERR(__wt_col_search(session, cbt, 1));
if ((ret = __wt_col_modify(session,
cbt, cbt->compare == 0 ? 3 : 1)) == WT_RESTART)
goto retry;
} else {
if ((ret =
__wt_col_modify(session, cbt, 1)) == WT_RESTART)
goto retry;
cbt->iface.recno = cbt->recno;
}
break;
case BTREE_ROW:
/*
* Insert in row stores fails if the key exists (and the
* configuration "overwrite" not set), otherwise creates
* a new record.
*/
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_FORMAT(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);
retry: __cursor_func_init(cbt, 1);
switch (btree->type) {
case BTREE_COL_FIX:
case BTREE_COL_VAR:
/* Remove the record if it exists. */
WT_ERR(__wt_col_search(session, cbt, 1));
if (cbt->compare != 0 || __cursor_invalid(cbt))
ret = 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_FORMAT(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);
retry: __cursor_func_init(cbt, 1);
switch (btree->type) {
case BTREE_COL_FIX:
if (cursor->value.size != 1) {
__wt_errx(session,
"item size of %" PRIu32 " does not match "
"fixed-length file requirement of 1 byte",
cursor->value.size);
return (WT_ERROR);
}
/* FALLTHROUGH */
case BTREE_COL_VAR:
/* Update the record. */
WT_ERR(__wt_col_search(session, cbt, 1));
if (cbt->compare != 0 || __cursor_invalid(cbt))
ret = WT_NOTFOUND;
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_FORMAT(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 *config)
{
WT_SESSION_IMPL *session;
WT_UNUSED(config);
session = (WT_SESSION_IMPL *)cbt->iface.session;
__cursor_func_init(cbt, 1);
__wt_buf_free(session, &cbt->value);
return (0);
}
|