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
|
/*-
* Copyright (c) 2008-2012 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
static int __col_insert_alloc(
WT_SESSION_IMPL *, uint64_t, u_int, WT_INSERT **, size_t *);
/*
* __wt_col_modify --
* Column-store delete, insert, and update.
*/
int
__wt_col_modify(WT_SESSION_IMPL *session, WT_CURSOR_BTREE *cbt, int op)
{
WT_BTREE *btree;
WT_DECL_RET;
WT_INSERT *ins, *ins_copy;
WT_INSERT_HEAD **inshead, *new_inshead, **new_inslist;
WT_ITEM *value, _value;
WT_PAGE *page;
WT_UPDATE *upd;
size_t ins_size, new_inshead_size, new_inslist_size, upd_size;
uint64_t recno;
u_int skipdepth;
int i, logged;
btree = cbt->btree;
page = cbt->page;
recno = cbt->iface.recno;
logged = 0;
WT_ASSERT(session, op != 1);
switch (op) {
case 2: /* Remove */
if (btree->type == BTREE_COL_FIX) {
value = &_value;
value->data = "";
value->size = 1;
} else
value = NULL;
break;
case 3: /* Insert/Update */
default:
value = &cbt->iface.value;
/*
* There's some chance the application specified a record past
* the last record on the page. If that's the case, and we're
* inserting a new WT_INSERT/WT_UPDATE pair, it goes on the
* append list, not the update list.
*/
if (recno == 0 || recno > __col_last_recno(page))
op = 1;
break;
}
/* If we don't yet have a modify structure, we'll need one. */
WT_RET(__wt_page_modify_init(session, page));
ins = NULL;
new_inshead = NULL;
new_inslist = NULL;
upd = NULL;
/*
* Delete, insert or update a column-store entry.
*
* If modifying a previously modified record, create a new WT_UPDATE
* entry and have a serialized function link it into an existing
* WT_INSERT entry's WT_UPDATE list.
*
* Else, allocate an insert array as necessary, build a WT_INSERT and
* WT_UPDATE structure pair, and call a serialized function to insert
* the WT_INSERT structure.
*
* The test is slight tricky: we're either coming here after a search
* (in which case we check the search's comparison and the WT_INSERT
* structure), or a truncation (in which case we check the WT_INSERT
* structure, it's a match by definition).
*/
if (cbt->ins != NULL && (cbt->compare == 0 || op == 2)) {
/* Make sure the update can proceed. */
WT_ERR(__wt_update_check(session, page, cbt->ins->upd));
/* Allocate and insert a WT_UPDATE structure. */
WT_ERR(__wt_update_alloc(session, value, &upd, &upd_size));
logged = 1;
WT_ERR(__wt_update_serial(session, page,
cbt->write_gen, &cbt->ins->upd, NULL, 0, &upd, upd_size));
} else {
/* There may be no insert list, allocate as necessary. */
new_inshead_size = new_inslist_size = 0;
if (op == 1) {
if (page->modify->append == NULL) {
new_inslist_size = 1 * sizeof(WT_INSERT_HEAD *);
WT_ERR(
__wt_calloc_def(session, 1, &new_inslist));
inshead = &new_inslist[0];
} else
inshead = &page->modify->append[0];
cbt->ins_head = *inshead;
} else if (page->type == WT_PAGE_COL_FIX) {
if (page->modify->update == NULL) {
new_inslist_size = 1 * sizeof(WT_INSERT_HEAD *);
WT_ERR(
__wt_calloc_def(session, 1, &new_inslist));
inshead = &new_inslist[0];
} else
inshead = &page->modify->update[0];
} else {
if (page->modify->update == NULL) {
new_inslist_size =
page->entries * sizeof(WT_INSERT_HEAD *);
WT_ERR(__wt_calloc_def(
session, page->entries, &new_inslist));
inshead = &new_inslist[cbt->slot];
} else
inshead = &page->modify->update[cbt->slot];
}
/* There may be no WT_INSERT list, allocate as necessary. */
if (*inshead == NULL) {
new_inshead_size = sizeof(WT_INSERT_HEAD);
WT_ERR(__wt_calloc_def(session, 1, &new_inshead));
for (i = 0; i < WT_SKIP_MAXDEPTH; i++)
cbt->ins_stack[i] = &new_inshead->head[i];
cbt->ins_head = new_inshead;
}
/* Choose a skiplist depth for this insert. */
skipdepth = __wt_skip_choose_depth();
/*
* Allocate a WT_INSERT/WT_UPDATE pair, and update the cursor
* to reference it.
*/
WT_ERR(__col_insert_alloc(
session, recno, skipdepth, &ins, &ins_size));
WT_ERR(__wt_update_check(session, page, NULL));
WT_ERR(__wt_update_alloc(session, value, &upd, &upd_size));
logged = 1;
ins->upd = upd;
ins_size += upd_size;
cbt->ins = ins;
/*
* Insert or append the WT_INSERT structure.
*/
if (op == 1) {
/*
* The serialized function clears ins: take a copy of
* the pointer so we can look up the record number.
*/
ins_copy = ins;
WT_ERR(__wt_col_append_serial(session,
page, cbt->write_gen, inshead, cbt->ins_stack,
&new_inslist, new_inslist_size,
&new_inshead, new_inshead_size,
&ins, ins_size, skipdepth));
/* Put the new recno into the cursor. */
cbt->recno = WT_INSERT_RECNO(ins_copy);
} else
WT_ERR(__wt_insert_serial(session,
page, cbt->write_gen,
inshead, cbt->ins_stack,
&new_inslist, new_inslist_size,
&new_inshead, new_inshead_size,
&ins, ins_size, skipdepth));
}
if (0) {
err: /*
* Remove the update from the current transaction, so we don't
* try to modify it on rollback.
*/
if (logged)
__wt_txn_unmodify(session);
__wt_free(session, ins);
__wt_free(session, upd);
}
__wt_free(session, new_inslist);
__wt_free(session, new_inshead);
return (ret);
}
/*
* __col_insert_alloc --
* Column-store insert: allocate a WT_INSERT structure and fill it in.
*/
static int
__col_insert_alloc(WT_SESSION_IMPL *session,
uint64_t recno, u_int skipdepth, WT_INSERT **insp, size_t *ins_sizep)
{
WT_INSERT *ins;
size_t ins_size;
/*
* Allocate the WT_INSERT structure and skiplist pointers, then copy
* the record number into place.
*/
ins_size = sizeof(WT_INSERT) + skipdepth * sizeof(WT_INSERT *);
WT_RET(__wt_calloc(session, 1, ins_size, &ins));
WT_INSERT_RECNO(ins) = recno;
*insp = ins;
*ins_sizep = ins_size;
return (0);
}
/*
* __wt_col_append_serial_func --
* Server function to append an WT_INSERT entry to the tree.
*/
void
__wt_col_append_serial_func(WT_SESSION_IMPL *session)
{
WT_BTREE *btree;
WT_DECL_RET;
WT_INSERT *ins, *new_ins, ***ins_stack;
WT_INSERT_HEAD *inshead, **insheadp, **new_inslist, *new_inshead;
WT_PAGE *page;
uint64_t recno;
uint32_t write_gen;
u_int i, skipdepth;
btree = session->btree;
__wt_col_append_unpack(session, &page, &write_gen, &insheadp,
&ins_stack, &new_inslist, &new_inshead, &new_ins, &skipdepth);
/* Check the page's write-generation. */
WT_ERR(__wt_page_write_gen_check(session, page, write_gen));
if ((inshead = *insheadp) == NULL)
inshead = new_inshead;
/*
* If the application specified a record number, there's a race: the
* application may have searched for the record, not found it, then
* called into the append code, and another thread might have added
* the record. Fortunately, we're in the right place because if the
* record didn't exist at some point, it can only have been created
* on this list. Search for the record, if specified.
*/
if ((recno = WT_INSERT_RECNO(new_ins)) == 0)
recno = WT_INSERT_RECNO(new_ins) = ++btree->last_recno;
ins = __col_insert_search(inshead, ins_stack, recno);
/* If we find the record number, there's been a race. */
if (ins != NULL && WT_INSERT_RECNO(ins) == recno)
WT_ERR(WT_RESTART);
/*
* Publish: First, point the new WT_INSERT item's skiplist references
* to the next elements in the insert list, then flush memory. Second,
* update the skiplist elements that reference the new WT_INSERT item,
* this ensures the list is never inconsistent.
*/
for (i = 0; i < skipdepth; i++)
new_ins->next[i] = *ins_stack[i];
WT_WRITE_BARRIER();
for (i = 0; i < skipdepth; i++) {
if (inshead->tail[i] == NULL ||
ins_stack[i] == &inshead->tail[i]->next[i])
inshead->tail[i] = new_ins;
*ins_stack[i] = new_ins;
}
__wt_col_append_new_ins_taken(session, page);
/*
* If the insert head does not yet have an insert list, our caller
* passed us one.
*
* NOTE: it is important to do this after the item has been added to
* the list. Code can assume that if the list is set, it is non-empty.
*/
if (*insheadp == NULL) {
WT_PUBLISH(*insheadp, new_inshead);
__wt_col_append_new_inshead_taken(session, page);
}
/*
* If the page does not yet have an insert array, our caller passed
* us one.
*
* NOTE: it is important to do this after publishing the list entry.
* Code can assume that if the array is set, it is non-empty.
*/
if (page->modify->append == NULL) {
page->modify->append = new_inslist;
__wt_col_append_new_inslist_taken(session, page);
}
/*
* If we don't find the record, check to see if we extended the file,
* and update the last record number.
*/
if (recno > btree->last_recno)
btree->last_recno = recno;
err: __wt_session_serialize_wrapup(session, page, ret);
}
/*
* __wt_col_leaf_obsolete --
* Discard all obsolete updates on a column-store leaf page.
*/
void
__wt_col_leaf_obsolete(WT_SESSION_IMPL *session, WT_PAGE *page)
{
WT_INSERT *ins;
WT_COL *cip;
uint32_t i;
switch (page->type) {
case WT_PAGE_COL_FIX:
WT_SKIP_FOREACH(ins, WT_COL_UPDATE_SINGLE(page))
__wt_update_obsolete(session, page, ins->upd);
break;
case WT_PAGE_COL_VAR:
WT_COL_FOREACH(page, cip, i)
WT_SKIP_FOREACH(ins, WT_COL_UPDATE(page, cip))
__wt_update_obsolete(session, page, ins->upd);
break;
}
/* Walk any append list. */
WT_SKIP_FOREACH(ins, WT_COL_APPEND(page))
__wt_update_obsolete(session, page, ins->upd);
}
|