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
|
/*-
* Copyright (c) 2014-2015 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __find_next_col --
* Find the next column to use for a plan.
*/
static int
__find_next_col(WT_SESSION_IMPL *session, WT_TABLE *table,
WT_CONFIG_ITEM *colname, u_int *cgnump, u_int *colnump, char *coltype)
{
WT_COLGROUP *colgroup;
WT_CONFIG conf;
WT_CONFIG_ITEM cval, k, v;
WT_DECL_RET;
u_int cg, col, foundcg, foundcol, matchcg, matchcol;
bool getnext;
foundcg = foundcol = UINT_MAX;
matchcg = *cgnump;
matchcol = (*coltype == WT_PROJ_KEY) ?
*colnump : *colnump + table->nkey_columns;
getnext = true;
for (colgroup = NULL, cg = 0; cg < WT_COLGROUPS(table); cg++) {
colgroup = table->cgroups[cg];
/*
* If there is only one column group, we just scan through all
* of the columns. For tables with multiple column groups, we
* look at the key columns once, then go through the value
* columns for each group.
*/
if (cg == 0) {
cval = table->colconf;
col = 0;
} else {
cgcols: cval = colgroup->colconf;
col = table->nkey_columns;
}
WT_RET(__wt_config_subinit(session, &conf, &cval));
for (; (ret = __wt_config_next(&conf, &k, &v)) == 0; col++) {
if (k.len == colname->len &&
strncmp(colname->str, k.str, k.len) == 0) {
if (getnext) {
foundcg = cg;
foundcol = col;
}
getnext = cg == matchcg && col == matchcol;
}
if (cg == 0 && table->ncolgroups > 0 &&
col == table->nkey_columns - 1)
goto cgcols;
}
WT_RET_TEST(ret != WT_NOTFOUND, ret);
colgroup = NULL;
}
if (foundcg == UINT_MAX)
return (WT_NOTFOUND);
*cgnump = foundcg;
if (foundcol < table->nkey_columns) {
*coltype = WT_PROJ_KEY;
*colnump = foundcol;
} else {
*coltype = WT_PROJ_VALUE;
*colnump = foundcol - table->nkey_columns;
}
return (0);
}
/*
* __wt_schema_colcheck --
* Check that a list of columns matches a (key,value) format pair.
*/
int
__wt_schema_colcheck(WT_SESSION_IMPL *session,
const char *key_format, const char *value_format, WT_CONFIG_ITEM *colconf,
u_int *kcolsp, u_int *vcolsp)
{
WT_CONFIG conf;
WT_CONFIG_ITEM k, v;
WT_DECL_PACK_VALUE(pv);
WT_DECL_RET;
WT_PACK pack;
u_int kcols, ncols, vcols;
WT_RET(__pack_init(session, &pack, key_format));
for (kcols = 0; (ret = __pack_next(&pack, &pv)) == 0; kcols++)
;
WT_RET_TEST(ret != WT_NOTFOUND, ret);
WT_RET(__pack_init(session, &pack, value_format));
for (vcols = 0; (ret = __pack_next(&pack, &pv)) == 0; vcols++)
;
WT_RET_TEST(ret != WT_NOTFOUND, ret);
/* Walk through the named columns. */
WT_RET(__wt_config_subinit(session, &conf, colconf));
for (ncols = 0; (ret = __wt_config_next(&conf, &k, &v)) == 0; ncols++)
;
WT_RET_TEST(ret != WT_NOTFOUND, ret);
if (ncols != 0 && ncols != kcols + vcols)
WT_RET_MSG(session, EINVAL, "Number of columns in '%.*s' "
"does not match key format '%s' plus value format '%s'",
(int)colconf->len, colconf->str, key_format, value_format);
if (kcolsp != NULL)
*kcolsp = kcols;
if (vcolsp != NULL)
*vcolsp = vcols;
return (0);
}
/*
* __wt_table_check --
* Make sure all columns appear in a column group.
*/
int
__wt_table_check(WT_SESSION_IMPL *session, WT_TABLE *table)
{
WT_CONFIG conf;
WT_CONFIG_ITEM k, v;
WT_DECL_RET;
u_int cg, col, i;
char coltype;
if (table->is_simple)
return (0);
/* Walk through the columns. */
WT_RET(__wt_config_subinit(session, &conf, &table->colconf));
/* Skip over the key columns. */
for (i = 0; i < table->nkey_columns; i++)
WT_RET(__wt_config_next(&conf, &k, &v));
cg = col = 0;
coltype = 0;
while ((ret = __wt_config_next(&conf, &k, &v)) == 0) {
if (__find_next_col(
session, table, &k, &cg, &col, &coltype) != 0)
WT_RET_MSG(session, EINVAL,
"Column '%.*s' in '%s' does not appear in a "
"column group",
(int)k.len, k.str, table->name);
/*
* Column groups can't store key columns in their value:
* __wt_struct_reformat should have already detected this case.
*/
WT_ASSERT(session, coltype == WT_PROJ_VALUE);
}
WT_RET_TEST(ret != WT_NOTFOUND, ret);
return (0);
}
/*
* __wt_struct_plan --
* Given a table cursor containing a complete table, build the "projection
* plan" to distribute the columns to dependent stores. A string
* representing the plan will be appended to the plan buffer.
*/
int
__wt_struct_plan(WT_SESSION_IMPL *session, WT_TABLE *table,
const char *columns, size_t len, bool value_only, WT_ITEM *plan)
{
WT_CONFIG conf;
WT_CONFIG_ITEM k, v;
WT_DECL_RET;
u_int cg, col, current_cg, current_col, i, start_cg, start_col;
bool have_it;
char coltype, current_coltype;
start_cg = start_col = UINT_MAX; /* -Wuninitialized */
/* Work through the value columns by skipping over the key columns. */
WT_RET(__wt_config_initn(session, &conf, columns, len));
if (value_only)
for (i = 0; i < table->nkey_columns; i++)
WT_RET(__wt_config_next(&conf, &k, &v));
current_cg = cg = 0;
current_col = col = INT_MAX;
current_coltype = coltype = WT_PROJ_KEY; /* Keep lint quiet. */
for (i = 0; (ret = __wt_config_next(&conf, &k, &v)) == 0; i++) {
have_it = false;
while ((ret = __find_next_col(session, table,
&k, &cg, &col, &coltype)) == 0 &&
(!have_it || cg != start_cg || col != start_col)) {
/*
* First we move to the column. If that is in a
* different column group to the last column we
* accessed, or before the last column in the same
* column group, or moving from the key to the value,
* we need to switch column groups or rewind.
*/
if (current_cg != cg || current_col > col ||
current_coltype != coltype) {
WT_ASSERT(session, !value_only ||
coltype == WT_PROJ_VALUE);
WT_RET(__wt_buf_catfmt(
session, plan, "%d%c", cg, coltype));
/*
* Set the current column group and column
* within the table.
*/
current_cg = cg;
current_col = 0;
current_coltype = coltype;
}
/* Now move to the column we want. */
if (current_col < col) {
if (col - current_col > 1)
WT_RET(__wt_buf_catfmt(session,
plan, "%d", col - current_col));
WT_RET(__wt_buf_catfmt(session,
plan, "%c", WT_PROJ_SKIP));
}
/*
* Now copy the value in / out. In the common case,
* where each value is used in one column, we do a
* "next" operation. If the value is used again, we do
* a "reuse" operation to avoid making another copy.
*/
if (!have_it) {
WT_RET(__wt_buf_catfmt(session,
plan, "%c", WT_PROJ_NEXT));
start_cg = cg;
start_col = col;
have_it = true;
} else
WT_RET(__wt_buf_catfmt(session,
plan, "%c", WT_PROJ_REUSE));
current_col = col + 1;
}
/*
* We may fail to find a column if it is a custom extractor.
* In that case, treat it as the first value column: we only
* ever use such plans to extract the primary key from the
* index.
*/
if (ret == WT_NOTFOUND)
WT_RET(__wt_buf_catfmt(session, plan,
"0%c%c", WT_PROJ_VALUE, WT_PROJ_NEXT));
}
WT_RET_TEST(ret != WT_NOTFOUND, ret);
/* Special case empty plans. */
if (i == 0 && plan->size == 0)
WT_RET(__wt_buf_set(session, plan, "", 1));
return (0);
}
/*
* __find_column_format --
* Find the format of the named column.
*/
static int
__find_column_format(WT_SESSION_IMPL *session, WT_TABLE *table,
WT_CONFIG_ITEM *colname, bool value_only, WT_PACK_VALUE *pv)
{
WT_CONFIG conf;
WT_CONFIG_ITEM k, v;
WT_DECL_RET;
WT_PACK pack;
bool inkey;
WT_RET(__wt_config_subinit(session, &conf, &table->colconf));
WT_RET(__pack_init(session, &pack, table->key_format));
inkey = true;
while ((ret = __wt_config_next(&conf, &k, &v)) == 0) {
if ((ret = __pack_next(&pack, pv)) == WT_NOTFOUND && inkey) {
ret = __pack_init(session, &pack, table->value_format);
if (ret == 0)
ret = __pack_next(&pack, pv);
inkey = false;
}
if (ret != 0)
return (ret);
if (k.len == colname->len &&
strncmp(colname->str, k.str, k.len) == 0) {
if (value_only && inkey)
return (EINVAL);
return (0);
}
}
return (ret);
}
/*
* __wt_struct_reformat --
* Given a table and a list of columns (which could be values in a column
* group or index keys), calculate the resulting new format string.
* The result will be appended to the format buffer.
*/
int
__wt_struct_reformat(WT_SESSION_IMPL *session, WT_TABLE *table,
const char *columns, size_t len, const char *extra_cols, bool value_only,
WT_ITEM *format)
{
WT_CONFIG config;
WT_CONFIG_ITEM k, next_k, next_v;
WT_DECL_PACK_VALUE(pv);
WT_DECL_RET;
bool have_next;
WT_RET(__wt_config_initn(session, &config, columns, len));
/*
* If an empty column list is specified, this will fail with
* WT_NOTFOUND, that's okay.
*/
WT_RET_NOTFOUND_OK(ret = __wt_config_next(&config, &next_k, &next_v));
if (ret == WT_NOTFOUND) {
if (extra_cols != NULL) {
WT_RET(__wt_config_init(session, &config, extra_cols));
WT_RET(__wt_config_next(&config, &next_k, &next_v));
extra_cols = NULL;
} else if (format->size == 0) {
WT_RET(__wt_buf_set(session, format, "", 1));
return (0);
}
}
do {
k = next_k;
ret = __wt_config_next(&config, &next_k, &next_v);
if (ret != 0 && ret != WT_NOTFOUND)
return (ret);
have_next = ret == 0;
if (!have_next && extra_cols != NULL) {
WT_RET(__wt_config_init(session, &config, extra_cols));
WT_RET(__wt_config_next(&config, &next_k, &next_v));
have_next = true;
extra_cols = NULL;
}
if ((ret = __find_column_format(session,
table, &k, value_only, &pv)) != 0) {
if (value_only && ret == EINVAL)
WT_RET_MSG(session, EINVAL,
"A column group cannot store key column "
"'%.*s' in its value", (int)k.len, k.str);
WT_RET_MSG(session, EINVAL,
"Column '%.*s' not found", (int)k.len, k.str);
}
/*
* Check whether we're moving an unsized WT_ITEM from the end
* to the middle, or vice-versa. This determines whether the
* size needs to be prepended. This is the only case where the
* destination size can be larger than the source size.
*/
if (pv.type == 'u' && !pv.havesize && have_next)
pv.type = 'U';
else if (pv.type == 'U' && !have_next)
pv.type = 'u';
if (pv.havesize)
WT_RET(__wt_buf_catfmt(
session, format, "%d%c", (int)pv.size, pv.type));
else
WT_RET(__wt_buf_catfmt(session, format, "%c", pv.type));
} while (have_next);
return (0);
}
/*
* __wt_struct_truncate --
* Return a packing string for the first N columns in a value.
*/
int
__wt_struct_truncate(WT_SESSION_IMPL *session,
const char *input_fmt, u_int ncols, WT_ITEM *format)
{
WT_DECL_PACK_VALUE(pv);
WT_PACK pack;
WT_RET(__pack_init(session, &pack, input_fmt));
while (ncols-- > 0) {
WT_RET(__pack_next(&pack, &pv));
if (pv.havesize)
WT_RET(__wt_buf_catfmt(
session, format, "%d%c", (int)pv.size, pv.type));
else
WT_RET(__wt_buf_catfmt(session, format, "%c", pv.type));
}
return (0);
}
|