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
|
/*-
* Copyright (c) 2014-2019 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __bulk_col_keycmp_err --
* Error routine when column-store keys inserted out-of-order.
*/
static int
__bulk_col_keycmp_err(WT_CURSOR_BULK *cbulk)
{
WT_CURSOR *cursor;
WT_SESSION_IMPL *session;
session = (WT_SESSION_IMPL *)cbulk->cbt.iface.session;
cursor = &cbulk->cbt.iface;
WT_RET_MSG(session, EINVAL,
"bulk-load presented with out-of-order keys: %" PRIu64 " is less "
"than previously inserted key %" PRIu64,
cursor->recno, cbulk->recno);
}
/*
* __curbulk_insert_fix --
* Fixed-length column-store bulk cursor insert.
*/
static int
__curbulk_insert_fix(WT_CURSOR *cursor)
{
WT_BTREE *btree;
WT_CURSOR_BULK *cbulk;
WT_DECL_RET;
WT_SESSION_IMPL *session;
uint64_t recno;
cbulk = (WT_CURSOR_BULK *)cursor;
btree = cbulk->cbt.btree;
/*
* Bulk cursor inserts are updates, but don't need auto-commit
* transactions because they are single-threaded and not visible
* until the bulk cursor is closed.
*/
CURSOR_API_CALL(cursor, session, insert, btree);
WT_STAT_CONN_INCR(session, cursor_insert_bulk);
WT_STAT_DATA_INCR(session, cursor_insert_bulk);
/*
* If the "append" flag was configured, the application doesn't have to
* supply a key, else require a key.
*/
if (F_ISSET(cursor, WT_CURSTD_APPEND))
recno = cbulk->recno + 1;
else {
WT_ERR(__cursor_checkkey(cursor));
if ((recno = cursor->recno) <= cbulk->recno)
WT_ERR(__bulk_col_keycmp_err(cbulk));
}
WT_ERR(__cursor_checkvalue(cursor));
/*
* Insert any skipped records as deleted records, update the current
* record count.
*/
for (; recno != cbulk->recno + 1; ++cbulk->recno)
WT_ERR(__wt_bulk_insert_fix(session, cbulk, true));
cbulk->recno = recno;
/* Insert the current record. */
ret = __wt_bulk_insert_fix(session, cbulk, false);
err: API_END_RET(session, ret);
}
/*
* __curbulk_insert_fix_bitmap --
* Fixed-length column-store bulk cursor insert for bitmaps.
*/
static int
__curbulk_insert_fix_bitmap(WT_CURSOR *cursor)
{
WT_BTREE *btree;
WT_CURSOR_BULK *cbulk;
WT_DECL_RET;
WT_SESSION_IMPL *session;
cbulk = (WT_CURSOR_BULK *)cursor;
btree = cbulk->cbt.btree;
/*
* Bulk cursor inserts are updates, but don't need auto-commit
* transactions because they are single-threaded and not visible
* until the bulk cursor is closed.
*/
CURSOR_API_CALL(cursor, session, insert, btree);
WT_STAT_CONN_INCR(session, cursor_insert_bulk);
WT_STAT_DATA_INCR(session, cursor_insert_bulk);
WT_ERR(__cursor_checkvalue(cursor));
/* Insert the current record. */
ret = __wt_bulk_insert_fix_bitmap(session, cbulk);
err: API_END_RET(session, ret);
}
/*
* __curbulk_insert_var --
* Variable-length column-store bulk cursor insert.
*/
static int
__curbulk_insert_var(WT_CURSOR *cursor)
{
WT_BTREE *btree;
WT_CURSOR_BULK *cbulk;
WT_DECL_RET;
WT_SESSION_IMPL *session;
uint64_t recno;
cbulk = (WT_CURSOR_BULK *)cursor;
btree = cbulk->cbt.btree;
/*
* Bulk cursor inserts are updates, but don't need auto-commit
* transactions because they are single-threaded and not visible
* until the bulk cursor is closed.
*/
CURSOR_API_CALL(cursor, session, insert, btree);
WT_STAT_CONN_INCR(session, cursor_insert_bulk);
WT_STAT_DATA_INCR(session, cursor_insert_bulk);
/*
* If the "append" flag was configured, the application doesn't have to
* supply a key, else require a key.
*/
if (F_ISSET(cursor, WT_CURSTD_APPEND))
recno = cbulk->recno + 1;
else {
WT_ERR(__cursor_checkkey(cursor));
if ((recno = cursor->recno) <= cbulk->recno)
WT_ERR(__bulk_col_keycmp_err(cbulk));
}
WT_ERR(__cursor_checkvalue(cursor));
if (!cbulk->first_insert) {
/*
* If not the first insert and the key space is sequential,
* compare the current value against the last value; if the
* same, just increment the RLE count.
*/
if (recno == cbulk->recno + 1 &&
cbulk->last.size == cursor->value.size &&
memcmp(cbulk->last.data,
cursor->value.data, cursor->value.size) == 0) {
++cbulk->rle;
++cbulk->recno;
goto duplicate;
}
/* Insert the previous key/value pair. */
WT_ERR(__wt_bulk_insert_var(session, cbulk, false));
} else
cbulk->first_insert = false;
/*
* Insert any skipped records as deleted records, update the current
* record count and RLE counter.
*/
if (recno != cbulk->recno + 1) {
cbulk->rle = (recno - cbulk->recno) - 1;
WT_ERR(__wt_bulk_insert_var(session, cbulk, true));
}
cbulk->rle = 1;
cbulk->recno = recno;
/* Save a copy of the value for the next comparison. */
ret = __wt_buf_set(session,
&cbulk->last, cursor->value.data, cursor->value.size);
duplicate:
err: API_END_RET(session, ret);
}
/*
* __bulk_row_keycmp_err --
* Error routine when row-store keys inserted out-of-order.
*/
static int
__bulk_row_keycmp_err(WT_CURSOR_BULK *cbulk)
{
WT_CURSOR *cursor;
WT_DECL_ITEM(a);
WT_DECL_ITEM(b);
WT_DECL_RET;
WT_SESSION_IMPL *session;
session = (WT_SESSION_IMPL *)cbulk->cbt.iface.session;
cursor = &cbulk->cbt.iface;
WT_ERR(__wt_scr_alloc(session, 512, &a));
WT_ERR(__wt_scr_alloc(session, 512, &b));
WT_ERR_MSG(session, EINVAL,
"bulk-load presented with out-of-order keys: %s compares smaller "
"than previously inserted key %s",
__wt_buf_set_printable(
session, cursor->key.data, cursor->key.size, a),
__wt_buf_set_printable(
session, cbulk->last.data, cbulk->last.size, b));
err: __wt_scr_free(session, &a);
__wt_scr_free(session, &b);
return (ret);
}
/*
* __curbulk_insert_row --
* Row-store bulk cursor insert, with key-sort checks.
*/
static int
__curbulk_insert_row(WT_CURSOR *cursor)
{
WT_BTREE *btree;
WT_CURSOR_BULK *cbulk;
WT_DECL_RET;
WT_SESSION_IMPL *session;
int cmp;
cbulk = (WT_CURSOR_BULK *)cursor;
btree = cbulk->cbt.btree;
/*
* Bulk cursor inserts are updates, but don't need auto-commit
* transactions because they are single-threaded and not visible
* until the bulk cursor is closed.
*/
CURSOR_API_CALL(cursor, session, insert, btree);
WT_STAT_CONN_INCR(session, cursor_insert_bulk);
WT_STAT_DATA_INCR(session, cursor_insert_bulk);
WT_ERR(__cursor_checkkey(cursor));
WT_ERR(__cursor_checkvalue(cursor));
/*
* If this isn't the first key inserted, compare it against the last key
* to ensure the application doesn't accidentally corrupt the table.
*/
if (!cbulk->first_insert) {
WT_ERR(__wt_compare(session,
btree->collator, &cursor->key, &cbulk->last, &cmp));
if (cmp <= 0)
WT_ERR(__bulk_row_keycmp_err(cbulk));
} else
cbulk->first_insert = false;
/* Save a copy of the key for the next comparison. */
WT_ERR(__wt_buf_set(session,
&cbulk->last, cursor->key.data, cursor->key.size));
ret = __wt_bulk_insert_row(session, cbulk);
err: API_END_RET(session, ret);
}
/*
* __curbulk_insert_row_skip_check --
* Row-store bulk cursor insert, without key-sort checks.
*/
static int
__curbulk_insert_row_skip_check(WT_CURSOR *cursor)
{
WT_BTREE *btree;
WT_CURSOR_BULK *cbulk;
WT_DECL_RET;
WT_SESSION_IMPL *session;
cbulk = (WT_CURSOR_BULK *)cursor;
btree = cbulk->cbt.btree;
/*
* Bulk cursor inserts are updates, but don't need auto-commit
* transactions because they are single-threaded and not visible
* until the bulk cursor is closed.
*/
CURSOR_API_CALL(cursor, session, insert, btree);
WT_STAT_CONN_INCR(session, cursor_insert_bulk);
WT_STAT_DATA_INCR(session, cursor_insert_bulk);
WT_ERR(__cursor_checkkey(cursor));
WT_ERR(__cursor_checkvalue(cursor));
ret = __wt_bulk_insert_row(session, cbulk);
err: API_END_RET(session, ret);
}
/*
* __wt_curbulk_init --
* Initialize a bulk cursor.
*/
int
__wt_curbulk_init(WT_SESSION_IMPL *session,
WT_CURSOR_BULK *cbulk, bool bitmap, bool skip_sort_check)
{
WT_CURSOR *c;
WT_CURSOR_BTREE *cbt;
c = &cbulk->cbt.iface;
cbt = &cbulk->cbt;
/* Bulk cursors only support insert and close (reset is a no-op). */
__wt_cursor_set_notsup(c);
switch (cbt->btree->type) {
case BTREE_COL_FIX:
c->insert = bitmap ?
__curbulk_insert_fix_bitmap : __curbulk_insert_fix;
break;
case BTREE_COL_VAR:
c->insert = __curbulk_insert_var;
break;
case BTREE_ROW:
/*
* Row-store order comparisons are expensive, so we optionally
* skip them when we know the input is correct.
*/
c->insert = skip_sort_check ?
__curbulk_insert_row_skip_check : __curbulk_insert_row;
break;
}
cbulk->first_insert = true;
cbulk->recno = 0;
cbulk->bitmap = bitmap;
if (bitmap)
F_SET(c, WT_CURSTD_RAW);
return (__wt_bulk_init(session, cbulk));
}
|