Debian Codegraph @

lib/index_cursor.c

raw symbols SCIP
1 /*
2 Copyright (C) 2010-2015 Brazil
3 Copyright (C) 2020-2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn_ctx_impl.h"
21 #include "grn_ii.h"
22 #include "grn_index_cursor.h"
23
24 typedef struct {
25 grn_db_obj obj;
26 grn_obj *index_column;
27 grn_table_cursor *tc;
28 grn_ii_cursor *iic;
29 bool next_called;
30 grn_id term_id;
31 grn_id input_term_id;
32 grn_id rid_min;
33 grn_id rid_max;
34 int flags;
35 uint32_t section_id;
36 float scale;
37 float *scales;
38 size_t n_scales;
39 struct {
40 bool specified;
41 uint32_t start;
42 } position;
43 } grn_index_cursor;
44
45 grn_obj *
46 grn_index_cursor_open(grn_ctx *ctx,
47 grn_table_cursor *tc,
48 grn_obj *index_column,
49 grn_id rid_min,
50 grn_id rid_max,
51 int flags)
52 {
53 GRN_API_ENTER;
54 grn_index_cursor *ic = GRN_CALLOC(sizeof(grn_index_cursor));
55 if (!ic) {
56 GRN_API_RETURN(NULL);
57 }
58
59 ic->tc = tc;
60 ic->index_column = index_column;
61 ic->iic = NULL;
62 ic->next_called = false;
63 ic->term_id = GRN_ID_NIL;
64 ic->input_term_id = GRN_ID_NIL;
65 ic->rid_min = rid_min;
66 ic->rid_max = rid_max;
67 ic->flags = flags;
68 ic->section_id = 0;
69 ic->scale = 1.0;
70 ic->scales = NULL;
71 ic->n_scales = 0;
72 ic->position.specified = false;
73 ic->position.start = 0;
74 GRN_DB_OBJ_SET_TYPE(ic, GRN_CURSOR_COLUMN_INDEX);
75 {
76 grn_id id = grn_obj_register(ctx, ctx->impl->db, NULL, 0);
77 DB_OBJ(ic)->header.domain = GRN_ID_NIL;
78 DB_OBJ(ic)->range = GRN_ID_NIL;
79 grn_db_obj_init(ctx, ctx->impl->db, id, DB_OBJ(ic));
80 }
81 GRN_API_RETURN((grn_obj *)ic);
82 }
83
84 void
85 grn_index_cursor_close(grn_ctx *ctx, grn_obj *index_cursor)
86 {
87 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
88 if (ic->iic) {
89 grn_ii_cursor_close(ctx, ic->iic);
90 }
91 GRN_FREE(ic);
92 }
93
94 grn_obj *
95 grn_index_cursor_get_index_column(grn_ctx *ctx,
96 grn_obj *index_cursor)
97 {
98 GRN_API_ENTER;
99 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
100 grn_obj *index_column = NULL;
101 if (ic) {
102 index_column = ic->index_column;
103 }
104 GRN_API_RETURN(index_column);
105 }
106
107 grn_rc
108 grn_index_cursor_set_term_id(grn_ctx *ctx,
109 grn_obj *index_cursor,
110 grn_id term_id)
111 {
112 grn_index_cursor *cursor = (grn_index_cursor *)index_cursor;
113 GRN_API_ENTER;
114 if (!cursor) {
115 ERR(GRN_INVALID_ARGUMENT, "[index-cursor][set-term-id] must not NULL");
116 goto exit;
117 }
118 if (cursor->tc) {
119 ERR(GRN_INVALID_ARGUMENT,
120 "[index-cursor][set-term-id] "
121 "setting term ID against index cursor with table cursor "
122 "isn't supported");
123 goto exit;
124 }
125 cursor->input_term_id = term_id;
126 exit :
127 GRN_API_RETURN(ctx->rc);
128 }
129
130 grn_rc
131 grn_index_cursor_set_section_id(grn_ctx *ctx,
132 grn_obj *index_cursor,
133 uint32_t section_id)
134 {
135 GRN_API_ENTER;
136 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
137 if (ic) {
138 ic->section_id = section_id;
139 }
140 GRN_API_RETURN(ctx->rc);
141 }
142
143 uint32_t
144 grn_index_cursor_get_section_id(grn_ctx *ctx,
145 grn_obj *index_cursor)
146 {
147 GRN_API_ENTER;
148 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
149 if (ic) {
150 GRN_API_RETURN(ic->section_id);
151 } else {
152 GRN_API_RETURN(0);
153 }
154 }
155
156 grn_rc
157 grn_index_cursor_set_scale(grn_ctx *ctx,
158 grn_obj *index_cursor,
159 float scale)
160 {
161 GRN_API_ENTER;
162 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
163 if (ic) {
164 ic->scale = scale;
165 }
166 GRN_API_RETURN(ctx->rc);
167 }
168
169 grn_rc
170 grn_index_cursor_set_scales(grn_ctx *ctx,
171 grn_obj *index_cursor,
172 float *scales,
173 size_t n_scales)
174 {
175 GRN_API_ENTER;
176 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
177 if (ic) {
178 ic->scales = scales;
179 ic->n_scales = n_scales;
180 }
181 GRN_API_RETURN(ctx->rc);
182 }
183
184 grn_rc
185 grn_index_cursor_set_start_position(grn_ctx *ctx,
186 grn_obj *index_cursor,
187 uint32_t position)
188 {
189 GRN_API_ENTER;
190 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
191 if (ic) {
192 ic->position.specified = true;
193 ic->position.start = position;
194 }
195 GRN_API_RETURN(ctx->rc);
196 }
197
198 grn_rc
199 grn_index_cursor_reset_start_position(grn_ctx *ctx,
200 grn_obj *index_cursor)
201 {
202 GRN_API_ENTER;
203 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
204 if (ic) {
205 ic->position.specified = false;
206 }
207 GRN_API_RETURN(ctx->rc);
208 }
209
210 uint32_t
211 grn_index_cursor_get_start_position(grn_ctx *ctx,
212 grn_obj *index_cursor)
213 {
214 GRN_API_ENTER;
215 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
216 if (ic && ic->position.specified) {
217 GRN_API_RETURN(ic->position.start);
218 } else {
219 GRN_API_RETURN(0);
220 }
221 }
222
223 grn_posting *
224 grn_index_cursor_next_internal(grn_ctx *ctx,
225 grn_obj *index_cursor,
226 grn_id *term_id)
227 {
228 grn_posting *posting = NULL;
229 grn_index_cursor *ic = (grn_index_cursor *)index_cursor;
230 while (true) {
231 if (ic->iic) {
232 if (ic->flags & GRN_OBJ_WITH_POSITION) {
233 while (true) {
234 if (ic->next_called) {
235 posting = grn_ii_cursor_next_pos(ctx, ic->iic);
236 if (posting) {
237 if (ic->position.specified) {
238 if (posting->pos == ic->position.start) {
239 break;
240 }
241 } else {
242 break;
243 }
244 }
245 }
246
247 grn_posting *ii_posting;
248 while ((ii_posting = grn_ii_cursor_next(ctx, ic->iic))) {
249 if (!(ic->section_id == 0 || ii_posting->sid == ic->section_id)) {
250 continue;
251 }
252 break;
253 }
254 ic->next_called = true;
255 if (!ii_posting) {
256 break;
257 }
258 }
259 } else {
260 while ((posting = grn_ii_cursor_next(ctx, ic->iic))) {
261 if (!(ic->section_id == 0 || posting->sid == ic->section_id)) {
262 continue;
263 }
264 if (ic->position.specified) {
265 while ((posting = grn_ii_cursor_next_pos(ctx, ic->iic))) {
266 if (posting->pos == ic->position.start) {
267 break;
268 }
269 }
270 if (!posting) {
271 continue;
272 }
273 }
274 break;
275 }
276 }
277 }
278 if (posting) {
279 break;
280 }
281
282 if (ic->tc) {
283 ic->term_id = grn_table_cursor_next(ctx, ic->tc);
284 } else {
285 ic->term_id = ic->input_term_id;
286 ic->input_term_id = GRN_ID_NIL;
287 }
288 if (ic->term_id == GRN_ID_NIL) {
289 break;
290 }
291 grn_ii *ii = (grn_ii *)ic->index_column;
292 if (ic->iic) {
293 grn_ii_cursor_close(ctx, ic->iic);
294 }
295 ic->iic = grn_ii_cursor_open(ctx,
296 ii,
297 ic->term_id,
298 ic->rid_min,
299 ic->rid_max,
300 (int)(ii->n_elements),
301 ic->flags);
302 if (ic->iic) {
303 if (ic->n_scales > 0) {
304 grn_ii_cursor_set_scales(ctx, ic->iic, ic->scales, ic->n_scales);
305 } else {
306 grn_ii_cursor_set_scale(ctx, ic->iic, ic->scale);
307 }
308 }
309 ic->next_called = false;
310 }
311 if (term_id) {
312 *term_id = ic->term_id;
313 }
314 return posting;
315 }
316
317 grn_posting *
318 grn_index_cursor_next(grn_ctx *ctx,
319 grn_obj *index_cursor,
320 grn_id *term_id)
321 {
322 GRN_API_ENTER;
323 grn_posting *posting = grn_index_cursor_next_internal(ctx,
324 index_cursor,
325 term_id);
326 GRN_API_RETURN(posting);
327 }