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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2008-2011 WiredTiger, Inc.
* All rights reserved.
*/
#include "wt_internal.h"
static const char *schematab_config = "key_format=S,value_format=S";
/*
* __open_schema_table --
* Opens the schema table, sets session->schematab.
*/
static inline int
__open_schema_table(WT_SESSION_IMPL *session)
{
const char *cfg[] = API_CONF_DEFAULTS(file, meta, schematab_config);
const char *schemaconf;
int ret;
ret = 0;
if (session->schematab != NULL)
return (0);
WT_RET(__wt_config_collapse(session, cfg, &schemaconf));
WT_ERR(__wt_schema_create(session,
"file:" WT_SCHEMA_FILENAME, schemaconf));
session->schematab = session->btree;
err: __wt_free(session, schemaconf);
return (ret);
}
/*
* __wt_schema_table_cursor --
* Opens a cursor on the schema table.
*/
int
__wt_schema_table_cursor(
WT_SESSION_IMPL *session, const char *config, WT_CURSOR **cursorp)
{
const char *cfg[] = API_CONF_DEFAULTS(session, open_cursor, config);
WT_RET(__open_schema_table(session));
session->btree = session->schematab;
return (__wt_curfile_create(session, cfg, cursorp));
}
/*
* __wt_schema_table_insert --
* Insert a row into the schema table.
*/
int
__wt_schema_table_insert(
WT_SESSION_IMPL *session, const char *key, const char *value)
{
WT_CURSOR *cursor;
int ret;
ret = 0;
WT_RET(__wt_schema_table_cursor(session, NULL, &cursor));
cursor->set_key(cursor, key);
cursor->set_value(cursor, value);
WT_TRET(cursor->insert(cursor));
WT_TRET(cursor->close(cursor, NULL));
return (ret);
}
/*
* __wt_schema_table_update --
* Update a row in the schema table.
*/
int
__wt_schema_table_update(
WT_SESSION_IMPL *session, const char *key, const char *value)
{
WT_CURSOR *cursor;
int ret;
ret = 0;
WT_RET(__wt_schema_table_cursor(session, "overwrite", &cursor));
cursor->set_key(cursor, key);
cursor->set_value(cursor, value);
WT_TRET(cursor->insert(cursor));
WT_TRET(cursor->close(cursor, NULL));
return (ret);
}
/*
* __wt_schema_table_remove --
* Removes a row from the schema table.
*/
int
__wt_schema_table_remove(WT_SESSION_IMPL *session, const char *key)
{
WT_CURSOR *cursor;
int ret;
ret = 0;
WT_RET(__wt_schema_table_cursor(session, NULL, &cursor));
cursor->set_key(cursor, key);
WT_TRET(cursor->remove(cursor));
WT_TRET(cursor->close(cursor, NULL));
return (ret);
}
/*
* __wt_schema_table_read --
* Reads and copies a row from the schema table.
* The caller is responsible for freeing the allocated memory.
*/
int
__wt_schema_table_read(
WT_SESSION_IMPL *session, const char *key, const char **valuep)
{
WT_CURSOR *cursor;
const char *value;
int ret;
ret = 0;
WT_RET(__wt_schema_table_cursor(session, NULL, &cursor));
cursor->set_key(cursor, key);
WT_ERR(cursor->search(cursor));
WT_ERR(cursor->get_value(cursor, &value));
WT_ERR(__wt_strdup(session, value, valuep));
err: WT_TRET(cursor->close(cursor, NULL));
return (ret);
}
|