1/*
2 Copyright(C) 2009-2016 Brazil
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#pragma once
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25#define GRN_TABLE_MAX_KEY_SIZE (0x1000)
26
27GRN_API grn_obj *grn_table_create(grn_ctx *ctx,
28 const char *name, unsigned int name_size,
29 const char *path, grn_table_flags flags,
30 grn_obj *key_type, grn_obj *value_type);
31
32#define GRN_TABLE_OPEN_OR_CREATE(ctx,name,name_size,path,flags,key_type,value_type,table) \
33 (((table) = grn_ctx_get((ctx), (name), (name_size))) ||\
34 ((table) = grn_table_create((ctx), (name), (name_size), (path), (flags), (key_type), (value_type))))
35
36/* TODO: int *added -> grn_bool *added */
37GRN_API grn_id grn_table_add(grn_ctx *ctx, grn_obj *table,
38 const void *key, unsigned int key_size, int *added);
39GRN_API grn_id grn_table_get(grn_ctx *ctx, grn_obj *table,
40 const void *key, unsigned int key_size);
41GRN_API grn_id grn_table_at(grn_ctx *ctx, grn_obj *table, grn_id id);
42GRN_API grn_id grn_table_lcp_search(grn_ctx *ctx, grn_obj *table,
43 const void *key, unsigned int key_size);
44GRN_API int grn_table_get_key(grn_ctx *ctx, grn_obj *table,
45 grn_id id, void *keybuf, int buf_size);
46GRN_API grn_rc grn_table_delete(grn_ctx *ctx, grn_obj *table,
47 const void *key, unsigned int key_size);
48GRN_API grn_rc grn_table_delete_by_id(grn_ctx *ctx, grn_obj *table, grn_id id);
49GRN_API grn_rc grn_table_update_by_id(grn_ctx *ctx, grn_obj *table, grn_id id,
50 const void *dest_key, unsigned int dest_key_size);
51GRN_API grn_rc grn_table_update(grn_ctx *ctx, grn_obj *table,
52 const void *src_key, unsigned int src_key_size,
53 const void *dest_key, unsigned int dest_key_size);
54GRN_API grn_rc grn_table_truncate(grn_ctx *ctx, grn_obj *table);
55
56#define GRN_CURSOR_ASCENDING (0x00<<0)
57#define GRN_CURSOR_DESCENDING (0x01<<0)
58#define GRN_CURSOR_GE (0x00<<1)
59#define GRN_CURSOR_GT (0x01<<1)
60#define GRN_CURSOR_LE (0x00<<2)
61#define GRN_CURSOR_LT (0x01<<2)
62#define GRN_CURSOR_BY_KEY (0x00<<3)
63#define GRN_CURSOR_BY_ID (0x01<<3)
64#define GRN_CURSOR_PREFIX (0x01<<4)
65#define GRN_CURSOR_SIZE_BY_BIT (0x01<<5)
66#define GRN_CURSOR_RK (0x01<<6)
67
68GRN_API grn_table_cursor *grn_table_cursor_open(grn_ctx *ctx, grn_obj *table,
69 const void *min, unsigned int min_size,
70 const void *max, unsigned int max_size,
71 int offset, int limit, int flags);
72GRN_API grn_rc grn_table_cursor_close(grn_ctx *ctx, grn_table_cursor *tc);
73GRN_API grn_id grn_table_cursor_next(grn_ctx *ctx, grn_table_cursor *tc);
74GRN_API int grn_table_cursor_get_key(grn_ctx *ctx, grn_table_cursor *tc, void **key);
75GRN_API int grn_table_cursor_get_value(grn_ctx *ctx, grn_table_cursor *tc, void **value);
76GRN_API grn_rc grn_table_cursor_set_value(grn_ctx *ctx, grn_table_cursor *tc,
77 const void *value, int flags);
78GRN_API grn_rc grn_table_cursor_delete(grn_ctx *ctx, grn_table_cursor *tc);
79GRN_API grn_obj *grn_table_cursor_table(grn_ctx *ctx, grn_table_cursor *tc);
80
81GRN_API grn_obj *grn_index_cursor_open(grn_ctx *ctx, grn_table_cursor *tc, grn_obj *index,
82 grn_id rid_min, grn_id rid_max, int flags);
83GRN_API grn_posting *grn_index_cursor_next(grn_ctx *ctx, grn_obj *ic, grn_id *tid);
84
85#define GRN_TABLE_EACH(ctx,table,head,tail,id,key,key_size,value,block) do {\
86 (ctx)->errlvl = GRN_LOG_NOTICE;\
87 (ctx)->rc = GRN_SUCCESS;\
88 if ((ctx)->seqno & 1) {\
89 (ctx)->subno++;\
90 } else {\
91 (ctx)->seqno++;\
92 }\
93 if (table) {\
94 switch ((table)->header.type) {\
95 case GRN_TABLE_PAT_KEY :\
96 GRN_PAT_EACH((ctx), (grn_pat *)(table), (id), (key), (key_size), (value), block);\
97 break;\
98 case GRN_TABLE_DAT_KEY :\
99 GRN_DAT_EACH((ctx), (grn_dat *)(table), (id), (key), (key_size), block);\
100 break;\
101 case GRN_TABLE_HASH_KEY :\
102 GRN_HASH_EACH((ctx), (grn_hash *)(table), (id), (key), (key_size), (value), block);\
103 break;\
104 case GRN_TABLE_NO_KEY :\
105 GRN_ARRAY_EACH((ctx), (grn_array *)(table), (head), (tail), (id), (value), block);\
106 break;\
107 }\
108 }\
109 if ((ctx)->subno) {\
110 (ctx)->subno--;\
111 } else {\
112 (ctx)->seqno++;\
113 }\
114} while (0)
115
116#define GRN_TABLE_EACH_BEGIN(ctx, table, cursor, id) do {\
117 if ((table)) {\
118 grn_table_cursor *cursor;\
119 cursor = grn_table_cursor_open((ctx), (table),\
120 NULL, 0,\
121 NULL, 0,\
122 0, -1, GRN_CURSOR_ASCENDING);\
123 if (cursor) {\
124 grn_id id;\
125 while ((id = grn_table_cursor_next((ctx), cursor))) {
126
127#define GRN_TABLE_EACH_BEGIN_FLAGS(ctx, table, cursor, id, flags) do {\
128 if ((table)) {\
129 grn_table_cursor *cursor;\
130 cursor = grn_table_cursor_open((ctx), (table),\
131 NULL, 0,\
132 NULL, 0,\
133 0, -1, (flags));\
134 if (cursor) {\
135 grn_id id;\
136 while ((id = grn_table_cursor_next((ctx), cursor))) {
137
138#define GRN_TABLE_EACH_BEGIN_MIN(ctx, table, cursor, id,\
139 min, min_size, flags) do {\
140 if ((table)) {\
141 grn_table_cursor *cursor;\
142 cursor = grn_table_cursor_open((ctx), (table),\
143 (min), (min_size),\
144 NULL, 0,\
145 0, -1, (flags));\
146 if (cursor) {\
147 grn_id id;\
148 while ((id = grn_table_cursor_next((ctx), cursor))) {
149
150#define GRN_TABLE_EACH_END(ctx, cursor)\
151 }\
152 grn_table_cursor_close((ctx), cursor);\
153 }\
154 }\
155} while (0)
156
157typedef struct _grn_table_sort_key grn_table_sort_key;
158typedef unsigned char grn_table_sort_flags;
159
160#define GRN_TABLE_SORT_ASC (0x00<<0)
161#define GRN_TABLE_SORT_DESC (0x01<<0)
162
163struct _grn_table_sort_key {
164 grn_obj *key;
165 grn_table_sort_flags flags;
166 int offset;
167};
168
169GRN_API int grn_table_sort(grn_ctx *ctx, grn_obj *table, int offset, int limit,
170 grn_obj *result, grn_table_sort_key *keys, int n_keys);
171
172typedef struct _grn_table_group_result grn_table_group_result;
173typedef uint32_t grn_table_group_flags;
174
175#define GRN_TABLE_GROUP_CALC_COUNT (0x01<<3)
176#define GRN_TABLE_GROUP_CALC_MAX (0x01<<4)
177#define GRN_TABLE_GROUP_CALC_MIN (0x01<<5)
178#define GRN_TABLE_GROUP_CALC_SUM (0x01<<6)
179#define GRN_TABLE_GROUP_CALC_AVG (0x01<<7)
180
181struct _grn_table_group_result {
182 grn_obj *table;
183 unsigned char key_begin;
184 unsigned char key_end;
185 int limit;
186 grn_table_group_flags flags;
187 grn_operator op;
188 unsigned int max_n_subrecs;
189 grn_obj *calc_target;
190};
191
192GRN_API grn_rc grn_table_group(grn_ctx *ctx, grn_obj *table,
193 grn_table_sort_key *keys, int n_keys,
194 grn_table_group_result *results, int n_results);
195GRN_API grn_rc grn_table_setoperation(grn_ctx *ctx, grn_obj *table1, grn_obj *table2,
196 grn_obj *res, grn_operator op);
197GRN_API grn_rc grn_table_difference(grn_ctx *ctx, grn_obj *table1, grn_obj *table2,
198 grn_obj *res1, grn_obj *res2);
199GRN_API int grn_table_columns(grn_ctx *ctx, grn_obj *table,
200 const char *name, unsigned int name_size,
201 grn_obj *res);
202
203GRN_API unsigned int grn_table_size(grn_ctx *ctx, grn_obj *table);
204
205GRN_API grn_rc grn_table_rename(grn_ctx *ctx, grn_obj *table,
206 const char *name, unsigned int name_size);
207
208GRN_API grn_obj *grn_table_select(grn_ctx *ctx, grn_obj *table, grn_obj *expr,
209 grn_obj *res, grn_operator op);
210
211GRN_API grn_table_sort_key *grn_table_sort_key_from_str(grn_ctx *ctx,
212 const char *str, unsigned int str_size,
213 grn_obj *table, unsigned int *nkeys);
214GRN_API grn_rc grn_table_sort_key_close(grn_ctx *ctx,
215 grn_table_sort_key *keys, unsigned int nkeys);
216
217GRN_API grn_bool grn_table_is_grouped(grn_ctx *ctx, grn_obj *table);
218
219GRN_API unsigned int grn_table_max_n_subrecs(grn_ctx *ctx, grn_obj *table);
220
221GRN_API grn_obj *grn_table_create_for_group(grn_ctx *ctx,
222 const char *name,
223 unsigned int name_size,
224 const char *path,
225 grn_obj *group_key,
226 grn_obj *value_type,
227 unsigned int max_n_subrecs);
228
229GRN_API unsigned int grn_table_get_subrecs(grn_ctx *ctx, grn_obj *table,
230 grn_id id, grn_id *subrecbuf,
231 int *scorebuf, int buf_size);
232
233GRN_API grn_obj *grn_table_tokenize(grn_ctx *ctx, grn_obj *table,
234 const char *str, unsigned int str_len,
235 grn_obj *buf, grn_bool addp);
236
237GRN_API grn_rc grn_table_apply_expr(grn_ctx *ctx,
238 grn_obj *table,
239 grn_obj *output_column,
240 grn_obj *expr);
241
242GRN_API grn_id grn_table_find_reference_object(grn_ctx *ctx, grn_obj *table);
243
244#ifdef __cplusplus
245}
246#endif
247