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
410
411
412
413
414
415
416
|
/*-
* Copyright (c) 2008-2012 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __wt_schema_colgroup_name --
* Get the URI for a column group. This is used for metadata lookups.
* The only complexity here is that simple tables (with a single column
* group) use a simpler naming scheme.
*/
int
__wt_schema_colgroup_name(WT_SESSION_IMPL *session,
WT_TABLE *table, const char *cgname, size_t len, WT_ITEM *namebuf)
{
const char *tablename;
tablename = table->name;
(void)WT_PREFIX_SKIP(tablename, "table:");
return ((table->ncolgroups == 0) ?
__wt_buf_fmt(session, namebuf, "colgroup:%s", tablename) :
__wt_buf_fmt(session, namebuf, "colgroup:%s:%.*s",
tablename, (int)len, cgname));
}
/*
* __wt_schema_get_btree --
* Get the btree (into session->btree) for the named schema object
* (either a column group or an index).
*/
int
__wt_schema_get_btree(WT_SESSION_IMPL *session,
const char *objname, size_t len, const char *cfg[], uint32_t flags)
{
WT_CONFIG_ITEM cval;
WT_CURSOR *cursor;
WT_DECL_RET;
WT_ITEM *uribuf;
const char *fileuri, *name, *objconf;
cursor = NULL;
uribuf = NULL;
name = objname;
if (len != strlen(objname))
WT_ERR(__wt_strndup(session, objname, len, &name));
WT_ERR(__wt_metadata_cursor(session, NULL, &cursor));
cursor->set_key(cursor, name);
WT_ERR(cursor->search(cursor));
WT_ERR(cursor->get_value(cursor, &objconf));
/* Get the filename from the metadata. */
WT_ERR(__wt_scr_alloc(session, 0, &uribuf));
WT_ERR(__wt_config_getones(session, objconf, "filename", &cval));
WT_ERR(__wt_buf_fmt(
session, uribuf, "file:%.*s", (int)cval.len, cval.str));
fileuri = uribuf->data;
/* !!! Close the schema cursor first, this overwrites session->btree. */
ret = cursor->close(cursor);
cursor = NULL;
if (ret != 0)
goto err;
ret = __wt_session_get_btree(session, fileuri, cfg, flags);
if (ret == ENOENT)
__wt_errx(session,
"%s created but '%s' is missing", objname, fileuri);
err: __wt_scr_free(&uribuf);
if (name != objname)
__wt_free(session, name);
if (cursor != NULL)
WT_TRET(cursor->close(cursor));
return (ret);
}
/*
* __wt_schema_open_colgroups --
* Open the column groups for a table.
*/
int
__wt_schema_open_colgroups(WT_SESSION_IMPL *session, WT_TABLE *table)
{
WT_CONFIG cparser;
WT_CONFIG_ITEM ckey, cval;
WT_DECL_RET;
WT_ITEM namebuf, plan;
const char *cgname, *fileconf;
int i;
if (table->cg_complete)
return (0);
WT_CLEAR(namebuf);
fileconf = NULL;
WT_RET(__wt_config_subinit(session, &cparser, &table->cgconf));
/* Open each column group. */
for (i = 0; i < WT_COLGROUPS(table); i++) {
if (table->ncolgroups > 0)
WT_ERR(__wt_config_next(&cparser, &ckey, &cval));
else
WT_CLEAR(ckey);
if ((cgname = table->cg_name[i]) == NULL) {
WT_ERR(__wt_schema_colgroup_name(session, table,
ckey.str, ckey.len, &namebuf));
cgname = table->cg_name[i] =
__wt_buf_steal(session, &namebuf, NULL);
}
ret = __wt_schema_get_btree(session,
cgname, strlen(cgname), NULL, 0);
if (ret != 0) {
/* It is okay if the table is not yet complete. */
if (ret == WT_NOTFOUND)
ret = 0;
goto err;
}
WT_ERR(__wt_session_release_btree(session));
}
if (!table->is_simple) {
WT_ERR(__wt_table_check(session, table));
WT_CLEAR(plan);
WT_ERR(__wt_struct_plan(session,
table, table->colconf.str, table->colconf.len, 1, &plan));
table->plan = __wt_buf_steal(session, &plan, NULL);
}
table->cg_complete = 1;
err: __wt_buf_free(session, &namebuf);
__wt_free(session, fileconf);
return (ret);
}
/*
* ___open_index --
* Open an index.
*/
static int
__open_index(WT_SESSION_IMPL *session, WT_TABLE *table,
const char *uri, const char *idxconf)
{
WT_BTREE *btree;
WT_CONFIG colconf;
WT_CONFIG_ITEM ckey, cval, icols;
WT_DECL_RET;
WT_ITEM cols, fmt, plan, uribuf;
const char *fileuri;
u_int cursor_key_cols;
int i;
WT_CLEAR(uribuf);
/* Get the filename from the index config. */
WT_ERR(__wt_config_getones(session, idxconf, "filename", &cval));
WT_ERR(__wt_buf_fmt(
session, &uribuf, "file:%.*s", (int)cval.len, cval.str));
fileuri = uribuf.data;
ret = __wt_session_get_btree(
session, fileuri, NULL, WT_BTREE_EXCLUSIVE);
if (ret == ENOENT)
__wt_errx(session,
"Index '%s' created but '%s' is missing", uri, fileuri);
/* Other errors will already have generated an error message. */
if (ret != 0)
goto err;
btree = session->btree;
/*
* The key format for an index is somewhat subtle: the application
* specifies a set of columns that it will use for the key, but the
* engine usually adds some hidden columns in order to derive the
* primary key. These hidden columns are part of the file's key.
*
* The file's key_format is stored persistently, we need to calculate
* the index cursor key format (which will usually omit some of those
* keys).
*/
WT_ERR(__wt_config_getones(session, idxconf, "columns", &icols));
/* Start with the declared index columns. */
WT_ERR(__wt_config_subinit(session, &colconf, &icols));
WT_CLEAR(cols);
cursor_key_cols = 0;
while ((ret = __wt_config_next(&colconf, &ckey, &cval)) == 0) {
WT_ERR(__wt_buf_catfmt(
session, &cols, "%.*s,", (int)ckey.len, ckey.str));
++cursor_key_cols;
}
if (ret != 0 && ret != WT_NOTFOUND)
goto err;
/*
* Now add any primary key columns from the table that are not
* already part of the index key.
*/
WT_ERR(__wt_config_subinit(session, &colconf, &table->colconf));
for (i = 0; i < table->nkey_columns &&
(ret = __wt_config_next(&colconf, &ckey, &cval)) == 0;
i++) {
/*
* If the primary key column is already in the secondary key,
* don't add it again.
*/
if (__wt_config_subgetraw(session, &icols, &ckey, &cval) == 0)
continue;
WT_ERR(__wt_buf_catfmt(
session, &cols, "%.*s,", (int)ckey.len, ckey.str));
}
if (ret != 0 && ret != WT_NOTFOUND)
goto err;
WT_CLEAR(plan);
WT_ERR(__wt_struct_plan(session,
table, cols.data, cols.size, 0, &plan));
btree->key_plan = __wt_buf_steal(session, &plan, NULL);
/* Set up the cursor key format (the visible columns). */
WT_CLEAR(fmt);
WT_ERR(__wt_struct_truncate(session,
btree->key_format, cursor_key_cols, &fmt));
btree->idxkey_format = __wt_buf_steal(session, &fmt, NULL);
/* By default, index cursor values are the table value columns. */
/* TODO Optimize to use index columns in preference to table lookups. */
WT_ERR(__wt_struct_plan(session,
table, table->colconf.str, table->colconf.len, 1, &plan));
btree->value_plan = __wt_buf_steal(session, &plan, NULL);
err: __wt_buf_free(session, &cols);
__wt_buf_free(session, &uribuf);
if (btree != NULL) {
session->btree = btree;
WT_TRET(__wt_session_release_btree(session));
}
return (ret);
}
/*
* __wt_schema_open_indices --
* Open the indices for a table.
*/
int
__wt_schema_open_index(
WT_SESSION_IMPL *session, WT_TABLE *table, const char *idxname, size_t len)
{
WT_CURSOR *cursor;
WT_DECL_RET;
const char *idxconf, *name, *tablename, *uri;
int i, match, skipped;
cursor = NULL;
skipped = 0;
idxconf = NULL;
tablename = table->name;
(void)WT_PREFIX_SKIP(tablename, "table:");
if (len == 0 && table->idx_complete)
return (0);
/*
* XXX
* Do a full scan through the metadata to find all matching indices.
* This scan be optimized with search + next.
*/
WT_RET(__wt_metadata_cursor(session, NULL, &cursor));
/* Open each index. */
for (i = 0; (ret = cursor->next(cursor)) == 0;) {
WT_ERR(cursor->get_key(cursor, &uri));
name = uri;
if (!WT_PREFIX_SKIP(name, "index:") ||
!WT_PREFIX_SKIP(name, tablename) ||
!WT_PREFIX_SKIP(name, ":"))
continue;
/* Is this the index we are looking for? */
match = (len > 0 &&
strncmp(name, idxname, len) == 0 && name[len] == '\0');
if ((size_t)i * sizeof(const char *) >= table->idx_name_alloc)
WT_ERR(__wt_realloc(session, &table->idx_name_alloc,
WT_MAX(10 * sizeof(const char *),
2 * table->idx_name_alloc), &table->idx_name));
if (table->idx_name[i] == NULL)
WT_ERR(__wt_strdup(session, uri, &table->idx_name[i]));
if (len == 0 || match) {
WT_ERR(cursor->get_value(cursor, &idxconf));
WT_ERR(__open_index(session, table, uri, idxconf));
} else
skipped = 1;
if (match) {
ret = cursor->close(cursor);
cursor = NULL;
break;
}
i++;
}
/* Did we make it all the way through? */
if (ret == WT_NOTFOUND) {
ret = 0;
if (!skipped) {
table->nindices = i;
table->idx_complete = 1;
}
}
err: if (cursor != NULL)
WT_TRET(cursor->close(cursor));
return (ret);
}
/*
* __wt_schema_open_table --
* Open a named table.
*/
int
__wt_schema_open_table(WT_SESSION_IMPL *session,
const char *name, size_t namelen, WT_TABLE **tablep)
{
WT_CONFIG cparser;
WT_CONFIG_ITEM ckey, cval;
WT_CURSOR *cursor;
WT_DECL_RET;
WT_ITEM buf;
WT_TABLE *table;
const char *tconfig;
char *tablename;
cursor = NULL;
table = NULL;
WT_CLEAR(buf);
WT_RET(__wt_buf_fmt(session, &buf, "table:%.*s", (int)namelen, name));
tablename = __wt_buf_steal(session, &buf, NULL);
WT_ERR(__wt_metadata_cursor(session, NULL, &cursor));
cursor->set_key(cursor, tablename);
WT_ERR(cursor->search(cursor));
WT_ERR(cursor->get_value(cursor, &tconfig));
WT_ERR(__wt_calloc_def(session, 1, &table));
table->name = tablename;
tablename = NULL;
WT_ERR(__wt_config_getones(session, tconfig, "columns", &cval));
WT_ERR(__wt_config_getones(session, tconfig, "key_format", &cval));
WT_ERR(__wt_strndup(session, cval.str, cval.len, &table->key_format));
WT_ERR(__wt_config_getones(session, tconfig, "value_format", &cval));
WT_ERR(__wt_strndup(session, cval.str, cval.len, &table->value_format));
WT_ERR(__wt_strdup(session, tconfig, &table->config));
/* Point to some items in the copy to save re-parsing. */
WT_ERR(__wt_config_getones(session, table->config,
"columns", &table->colconf));
/*
* Count the number of columns: tables are "simple" if the columns
* are not named.
*/
WT_ERR(__wt_config_subinit(session, &cparser, &table->colconf));
table->is_simple = 1;
while ((ret = __wt_config_next(&cparser, &ckey, &cval)) == 0)
table->is_simple = 0;
if (ret != WT_NOTFOUND)
goto err;
/* Check that the columns match the key and value formats. */
if (!table->is_simple)
WT_ERR(__wt_schema_colcheck(session,
table->key_format, table->value_format, &table->colconf,
&table->nkey_columns, NULL));
WT_ERR(__wt_config_getones(session, table->config,
"colgroups", &table->cgconf));
/* Count the number of column groups. */
WT_ERR(__wt_config_subinit(session, &cparser, &table->cgconf));
table->ncolgroups = 0;
while ((ret = __wt_config_next(&cparser, &ckey, &cval)) == 0)
++table->ncolgroups;
if (ret != WT_NOTFOUND)
goto err;
WT_ERR(__wt_calloc_def(session, WT_COLGROUPS(table), &table->cg_name));
WT_ERR(__wt_schema_open_colgroups(session, table));
*tablep = table;
if (0) {
err: if (table != NULL)
__wt_schema_destroy_table(session, table);
}
if (cursor != NULL)
WT_TRET(cursor->close(cursor));
__wt_free(session, tablename);
return (ret);
}
|