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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2008-2011 WiredTiger, Inc.
* All rights reserved.
*/
#include "wt_internal.h"
static int
__find_next_col(WT_SESSION_IMPL *session, WT_TABLE *table,
WT_CONFIG_ITEM *colname, int *cgnump, int *colnump, char *coltype)
{
WT_BTREE *cgtree;
WT_CONFIG conf;
WT_CONFIG_ITEM cval, k, v;
int cg, col, foundcg, foundcol, getnext;
foundcg = foundcol = -1;
getnext = 1;
for (cg = 0; cg < WT_COLGROUPS(table); cg++) {
if ((cgtree = table->colgroup[cg]) == NULL)
continue;
/*
* 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: WT_RET(__wt_config_getones(session,
cgtree->config, "columns", &cval));
col = table->nkey_columns;
}
WT_RET(__wt_config_subinit(session, &conf, &cval));
for (; __wt_config_next(&conf, &k, &v) == 0; col++) {
if (cg == *cgnump && col == *colnump)
getnext = 1;
if (getnext && k.len == colname->len &&
strncmp(colname->str, k.str, k.len) == 0) {
foundcg = cg;
foundcol = col;
getnext = 0;
}
if (cg == 0 && table->ncolgroups > 0 &&
col == table->nkey_columns - 1)
goto cgcols;
}
}
if (foundcg == -1)
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_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_PACK pack;
WT_PACK_VALUE pv;
int cg, col, i, ret;
char coltype;
if (table->is_simple)
return (0);
/* Count the number of key columns. */
WT_RET(__pack_init(session, &pack, table->key_format));
table->nkey_columns = 0;
while ((ret = __pack_next(&pack, &pv)) == 0)
++table->nkey_columns;
WT_ASSERT(session, ret == WT_NOTFOUND);
/* 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;
while ((ret = __wt_config_next(&conf, &k, &v)) == 0) {
if (__find_next_col(session, table,
&k, &cg, &col, &coltype) != 0) {
__wt_errx(session, "Column '%.*s' in table '%s' "
"does not appear in a column group",
(int)k.len, k.str, table->name);
return (EINVAL);
}
/*
* 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);
}
if (ret != WT_NOTFOUND)
return (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, int value_only, WT_BUF *plan)
{
WT_CONFIG conf;
WT_CONFIG_ITEM k, v;
int cg, col, current_cg, current_col, start_cg, start_col;
int i, have_it;
char coltype, current_coltype;
/* 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. */
while (__wt_config_next(&conf, &k, &v) == 0) {
have_it = 0;
while (__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 = 1;
} else
WT_RET(__wt_buf_catfmt(session,
plan, "%c", WT_PROJ_REUSE));
current_col = col + 1;
}
}
return (0);
}
static int
__find_column_format(WT_SESSION_IMPL *session,
WT_TABLE *table, WT_CONFIG_ITEM *colname, int value_only, WT_PACK_VALUE *pv)
{
WT_CONFIG conf;
WT_CONFIG_ITEM k, v;
WT_PACK pack;
int inkey, ret;
WT_RET(__wt_config_subinit(session, &conf, &table->colconf));
WT_RET(__pack_init(session, &pack, table->key_format));
inkey = 1;
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 = 0;
}
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, int value_only,
WT_BUF *format)
{
WT_CONFIG config;
WT_CONFIG_ITEM k, next_k, next_v;
WT_PACK_VALUE pv;
int have_next, ret;
WT_RET(__wt_config_initn(session, &config, columns, len));
WT_RET(__wt_config_next(&config, &next_k, &next_v));
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 = 1;
extra_cols = NULL;
}
if ((ret = __find_column_format(session,
table, &k, value_only, &pv)) != 0) {
if (value_only && ret == EINVAL)
__wt_errx(session,
"A column group cannot store key column "
"'%.*s' in its value", (int)k.len, k.str);
else
__wt_errx(session, "Column '%.*s' not found",
(int)k.len, k.str);
return (EINVAL);
}
/*
* 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_BUF *format)
{
WT_PACK pack;
WT_PACK_VALUE pv;
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);
}
|