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
|
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2015-2016 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "grn_ctx_impl.h"
#include "grn_config.h"
#include <string.h>
grn_rc
grn_config_set(grn_ctx *ctx,
const char *key, int32_t key_size,
const char *value, int32_t value_size)
{
grn_obj *db;
grn_hash *config;
void *packed_value;
grn_id id;
GRN_API_ENTER;
if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
ERR(GRN_INVALID_ARGUMENT, "[config][set] DB isn't initialized");
GRN_API_RETURN(ctx->rc);
}
if (key_size == -1) {
key_size = strlen(key);
}
if (key_size > GRN_CONFIG_MAX_KEY_SIZE) {
ERR(GRN_INVALID_ARGUMENT,
"[config][set] too large key: max=<%d>: <%d>",
GRN_CONFIG_MAX_KEY_SIZE, key_size);
GRN_API_RETURN(ctx->rc);
}
if (value_size == -1) {
value_size = strlen(value);
}
if (value_size > GRN_CONFIG_MAX_VALUE_SIZE) {
ERR(GRN_INVALID_ARGUMENT,
"[config][set] too large value: max=<%" GRN_FMT_SIZE ">: <%d>",
GRN_CONFIG_MAX_VALUE_SIZE, value_size);
GRN_API_RETURN(ctx->rc);
}
config = ((grn_db *)db)->config;
{
grn_rc rc;
rc = grn_io_lock(ctx, config->io, grn_lock_timeout);
if (rc != GRN_SUCCESS) {
if (ctx->rc == GRN_SUCCESS) {
ERR(rc, "[config][set] failed to lock");
}
GRN_API_RETURN(rc);
}
id = grn_hash_add(ctx, config, key, key_size, &packed_value, NULL);
grn_io_unlock(config->io);
}
if (id == GRN_ID_NIL && ctx->rc == GRN_SUCCESS) {
ERR(GRN_INVALID_ARGUMENT,
"[config][set] failed to set: name=<%.*s>: <%d>",
key_size, key, value_size);
}
*((uint32_t *)packed_value) = (uint32_t)value_size;
grn_memcpy((char *)packed_value + sizeof(uint32_t),
value, value_size);
((char *)packed_value)[sizeof(uint32_t) + value_size] = '\0';
GRN_API_RETURN(ctx->rc);
}
grn_rc
grn_config_get(grn_ctx *ctx,
const char *key, int32_t key_size,
const char **value, uint32_t *value_size)
{
grn_obj *db;
grn_hash *config;
grn_id id;
void *packed_value;
GRN_API_ENTER;
if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
ERR(GRN_INVALID_ARGUMENT, "[config][get] DB isn't initialized");
GRN_API_RETURN(ctx->rc);
}
if (key_size == -1) {
key_size = strlen(key);
}
if (key_size > GRN_CONFIG_MAX_KEY_SIZE) {
ERR(GRN_INVALID_ARGUMENT,
"[config][get] too large key: max=<%d>: <%d>",
GRN_CONFIG_MAX_KEY_SIZE, key_size);
GRN_API_RETURN(ctx->rc);
}
config = ((grn_db *)db)->config;
id = grn_hash_get(ctx, config, key, key_size, &packed_value);
if (id == GRN_ID_NIL) {
*value = NULL;
*value_size = 0;
GRN_API_RETURN(GRN_SUCCESS);
}
*value = (char *)packed_value + sizeof(uint32_t);
*value_size = *((uint32_t *)packed_value);
GRN_API_RETURN(GRN_SUCCESS);
}
grn_rc
grn_config_delete(grn_ctx *ctx,
const char *key, int key_size)
{
grn_obj *db;
grn_hash *config;
GRN_API_ENTER;
if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
ERR(GRN_INVALID_ARGUMENT, "[config][delete] DB isn't initialized");
GRN_API_RETURN(ctx->rc);
}
if (key_size == -1) {
key_size = strlen(key);
}
if (key_size > GRN_CONFIG_MAX_KEY_SIZE) {
ERR(GRN_INVALID_ARGUMENT,
"[config][delete] too large key: max=<%d>: <%d>",
GRN_CONFIG_MAX_KEY_SIZE, key_size);
GRN_API_RETURN(ctx->rc);
}
config = ((grn_db *)db)->config;
{
grn_rc rc;
rc = grn_io_lock(ctx, config->io, grn_lock_timeout);
if (rc != GRN_SUCCESS) {
if (ctx->rc == GRN_SUCCESS) {
ERR(rc, "[config][delete] failed to lock");
}
GRN_API_RETURN(rc);
}
rc = grn_hash_delete(ctx, config, key, key_size, NULL);
grn_io_unlock(config->io);
if (rc != GRN_SUCCESS) {
if (ctx->rc == GRN_SUCCESS) {
ERR(rc, "[config][delete] failed to delete");
}
}
}
GRN_API_RETURN(ctx->rc);
}
grn_obj *
grn_config_cursor_open(grn_ctx *ctx)
{
grn_obj *db;
grn_hash *config;
grn_config_cursor *cursor;
grn_id id;
GRN_API_ENTER;
if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
ERR(GRN_INVALID_ARGUMENT, "[config][cursor][open] DB isn't initialized");
GRN_API_RETURN(NULL);
}
config = ((grn_db *)db)->config;
cursor = GRN_MALLOCN(grn_config_cursor, 1);
if (!cursor) {
ERR(GRN_NO_MEMORY_AVAILABLE,
"[config][cursor][open] failed to allocate memory for config cursor");
GRN_API_RETURN(NULL);
}
GRN_DB_OBJ_SET_TYPE(cursor, GRN_CURSOR_CONFIG);
cursor->hash_cursor = grn_hash_cursor_open(ctx, config,
NULL, -1,
NULL, -1,
0, -1, 0);
if (!cursor->hash_cursor) {
GRN_FREE(cursor);
ERR(GRN_NO_MEMORY_AVAILABLE,
"[config][cursor][open] failed to allocate memory for hash cursor");
GRN_API_RETURN(NULL);
}
id = grn_obj_register(ctx, ctx->impl->db, NULL, 0);
DB_OBJ(cursor)->header.domain = GRN_ID_NIL;
DB_OBJ(cursor)->range = GRN_ID_NIL;
grn_db_obj_init(ctx, ctx->impl->db, id, DB_OBJ(cursor));
GRN_API_RETURN((grn_obj *)cursor);
}
grn_rc
grn_config_cursor_close(grn_ctx *ctx, grn_config_cursor *cursor)
{
grn_hash_cursor_close(ctx, cursor->hash_cursor);
GRN_FREE(cursor);
return GRN_SUCCESS;
}
grn_bool
grn_config_cursor_next(grn_ctx *ctx, grn_obj *cursor)
{
grn_bool have_next;
grn_config_cursor *config_cursor = (grn_config_cursor *)cursor;
GRN_API_ENTER;
have_next = grn_hash_cursor_next(ctx, config_cursor->hash_cursor) != GRN_ID_NIL;
GRN_API_RETURN(have_next);
}
uint32_t
grn_config_cursor_get_key(grn_ctx *ctx, grn_obj *cursor, const char **key)
{
void *key_raw;
uint32_t key_size;
grn_config_cursor *config_cursor = (grn_config_cursor *)cursor;
GRN_API_ENTER;
key_size = grn_hash_cursor_get_key(ctx, config_cursor->hash_cursor, &key_raw);
*key = key_raw;
GRN_API_RETURN(key_size);
}
uint32_t
grn_config_cursor_get_value(grn_ctx *ctx, grn_obj *cursor, const char **value)
{
void *value_raw;
uint32_t value_size;
uint32_t value_size_raw;
grn_config_cursor *config_cursor = (grn_config_cursor *)cursor;
GRN_API_ENTER;
value_size_raw = grn_hash_cursor_get_value(ctx,
config_cursor->hash_cursor,
&value_raw);
*value = (char *)value_raw + sizeof(uint32_t);
value_size = *((uint32_t *)value_raw);
GRN_API_RETURN(value_size);
}
/* Deprecated since 5.1.2. Use grn_config_* instead. */
grn_rc
grn_conf_set(grn_ctx *ctx,
const char *key, int32_t key_size,
const char *value, int32_t value_size)
{
return grn_config_set(ctx, key, key_size, value, value_size);
}
grn_rc
grn_conf_get(grn_ctx *ctx,
const char *key, int32_t key_size,
const char **value, uint32_t *value_size)
{
return grn_config_get(ctx, key, key_size, value, value_size);
}
|