Debian Codegraph @

lib/proc/proc_fuzzy_search.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2016 Brazil
3 Copyright (C) 2019-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_db.h"cxx . . $ `<file>/lib/grn_db.h`/
21 #include "../grn_ii.h"cxx . . $ `<file>/lib/grn_ii.h`/
22 #include "../grn_proc.h"cxx . . $ `<file>/lib/grn_proc.h`/
23 #include "../grn_rset.h"cxx . . $ `<file>/lib/grn_rset.h`/
24 #include "../grn_posting.h"cxx . . $ `<file>/lib/grn_posting.h`/
25
26 #include <groonga/plugin.h>cxx . . $ `<file>/include/groonga/plugin.h`/
27
28 #include <string.h>
29
30 #define DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(ox,oy) (dists[((lx + 1) * (oy)) + (ox)])
31
32 static uint32_tcxx . . $ uint32_t#
33 calc_edit_distancecxx . . $ calc_edit_distance(7dc860abcac45052).(grn_ctxcxx . . $ grn_ctx# *ctx, char *sx, char *ex, char *sy, char *ey, int flags)
34 {
35 uint32_tcxx . . $ uint32_t# d = 0;
36 int cx;
37 int cy;
38 uint32_tcxx . . $ uint32_t# lx, ly, *dists;
39 char *px, *py;
40 for (px = sx, lx = 0;
41 px < ex && (cx = grn_charlencxx . . $ grn_charlen(d229e8bf7c87f0f0).(ctx, px, ex));
42 px += cx, lx++) {
43 }
44 for (py = sy, ly = 0;
45 py < ey && (cy = grn_charlencxx . . $ grn_charlen(d229e8bf7c87f0f0).(ctx, py, ey));
46 py += cy, ly++) {
47 }
48 if ((dists = GRN_PLUGIN_MALLOCcxx . . $ `include/groonga/plugin.h:88:9`!(ctx, (lx + 1) * (ly + 1) * sizeof(uint32_t)))) {
49 uint32_tcxx . . $ uint32_t# x, y;
50 for (x = 0; x <= lx; x++) { DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x, 0) = x; }
51 for (y = 0; y <= ly; y++) { DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(0, y) = y; }
52 for (x = 1, px = sx; x <= lx; x++, px += cx) {
53 cx = grn_charlencxx . . $ grn_charlen(d229e8bf7c87f0f0).(ctx, px, ex);
54 for (y = 1, py = sy; y <= ly; y++, py += cy) {
55 cy = grn_charlencxx . . $ grn_charlen(d229e8bf7c87f0f0).(ctx, py, ey);
56 if (cx == cy && !memcmpcxx . . $ memcmp(36f949851efa3055).(px, py, (size_t)cx)) {
57 DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x, y) = DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x - 1, y - 1);
58 } else {
59 uint32_tcxx . . $ uint32_t# a = DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x - 1, y) + 1;
60 uint32_tcxx . . $ uint32_t# b = DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x, y - 1) + 1;
61 uint32_tcxx . . $ uint32_t# c = DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x - 1, y - 1) + 1;
62 DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x, y) = ((a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c));
63 if (flags & GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITIONcxx . . $ `include/groonga/groonga.h:1958:9`! &&
64 x > 1 && y > 1 && cx == cy &&
65 memcmpcxx . . $ memcmp(36f949851efa3055).(px, py - cy, (size_t)cx) == 0 &&
66 memcmpcxx . . $ memcmp(36f949851efa3055).(px - cx, py, (size_t)cx) == 0) {
67 uint32_tcxx . . $ uint32_t# t = DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x - 2, y - 2) + 1;
68 DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x, y) = ((DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x, y) < t) ? DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(x, y) : t);
69 }
70 }
71 }
72 }
73 d = DISTcxx . . $ `lib/proc/proc_fuzzy_search.c:30:9`!(lx, ly);
74 GRN_PLUGIN_FREEcxx . . $ `include/groonga/plugin.h:100:9`!(ctx, dists);
75 }
76 return d;
77 }
78
79 static grn_objcxx . . $ grn_obj# *
80 func_edit_distancecxx . . $ func_edit_distance(fd082858389c8bb0).(grn_ctxcxx . . $ grn_ctx# *ctx, int nargs, grn_objcxx . . $ grn_obj# **args, grn_user_datacxx . . $ grn_user_data# *user_data)
81 {
82 #define N_REQUIRED_ARGScxx . . $ `lib/proc/proc_fuzzy_search.c:82:9`! 2
83 #define MAX_ARGScxx . . $ `lib/proc/proc_fuzzy_search.c:83:9`! 3
84 uint32_tcxx . . $ uint32_t# d = 0;
85 int flags = 0;
86 grn_objcxx . . $ grn_obj# *obj;
87 if (nargs >= N_REQUIRED_ARGScxx . . $ `lib/proc/proc_fuzzy_search.c:82:9`! && nargs <= MAX_ARGScxx . . $ `lib/proc/proc_fuzzy_search.c:83:9`!) {
88 if (nargs == MAX_ARGScxx . . $ `lib/proc/proc_fuzzy_search.c:83:9`! && GRN_BOOL_VALUEcxx . . $ `include/groonga/groonga.h:3097:9`!(args[2])) {
89 flags |= GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITIONcxx . . $ `include/groonga/groonga.h:1958:9`!;
90 }
91 d = calc_edit_distancecxx . . $ calc_edit_distance(7dc860abcac45052).(ctx,
92 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(args[0]),
93 GRN_BULK_CURRcxx . . $ `include/groonga/groonga.h:2650:9`!(args[0]),
94 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(args[1]),
95 GRN_BULK_CURRcxx . . $ `include/groonga/groonga.h:2650:9`!(args[1]),
96 flags);
97 }
98 if ((obj = grn_plugin_proc_alloccxx . . $ grn_plugin_proc_alloc(acb267eca6702fc).(ctx, user_data, GRN_DB_UINT32cxx . . $ GRN_DB_UINT32., 0))) {
99 GRN_UINT32_SETcxx . . $ `include/groonga/groonga.h:2909:9`!(ctx, obj, d);
100 }
101 return obj;
102 #undef N_REQUIRED_ARGScxx . . $ `lib/proc/proc_fuzzy_search.c:82:9`!
103 #undef MAX_ARGScxx . . $ `lib/proc/proc_fuzzy_search.c:83:9`!
104 }
105
106 void
107 grn_proc_init_edit_distancecxx . . $ grn_proc_init_edit_distance(b578cd6e53d5fe91).(grn_ctxcxx . . $ grn_ctx# *ctx)
108 {
109 grn_proc_createcxx . . $ grn_proc_create(195df001e5324f57).(ctx, "edit_distance", -1, GRN_PROC_FUNCTIONcxx . . $ GRN_PROC_FUNCTION.,
110 func_edit_distancecxx . . $ func_edit_distance(fd082858389c8bb0)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
111 }
112
113 #define SCORE_HEAP_SIZEcxx . . $ `lib/proc/proc_fuzzy_search.c:113:9`! 256
114
115 typedef structcxx . . $ $anonymous_type_192af11623a8c789_0# {
116 grn_idcxx . . $ grn_id# idcxx . . $ $anonymous_type_192af11623a8c789_0#id.;
117 uint32_tcxx . . $ uint32_t# scorecxx . . $ $anonymous_type_192af11623a8c789_0#score.;
118 } score_heap_nodecxx . . $ score_heap_node#;
119
120 typedef structcxx . . $ $anonymous_type_192af11623a8c789_1# {
121 uint32_tcxx . . $ uint32_t# n_entriescxx . . $ $anonymous_type_192af11623a8c789_1#n_entries.;
122 uint32_tcxx . . $ uint32_t# limitcxx . . $ $anonymous_type_192af11623a8c789_1#limit.;
123 score_heap_nodecxx . . $ score_heap_node# *nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.;
124 } score_heapcxx . . $ score_heap#;
125
126 static inline score_heapcxx . . $ score_heap# *
127 score_heap_opencxx . . $ score_heap_open(c0cc8115a85c7f25).(grn_ctxcxx . . $ grn_ctx# *ctx, uint32_tcxx . . $ uint32_t# max)
128 {
129 score_heapcxx . . $ score_heap# *h = GRN_PLUGIN_MALLOCcxx . . $ `include/groonga/plugin.h:88:9`!(ctx, sizeof(score_heap));
130 if (!h) { return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; }
131 h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes. = GRN_PLUGIN_MALLOCcxx . . $ `include/groonga/plugin.h:88:9`!(ctx, sizeof(score_heap_node) * max);
132 if (!h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.) {
133 GRN_PLUGIN_FREEcxx . . $ `include/groonga/plugin.h:100:9`!(ctx, h);
134 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
135 }
136 h->n_entriescxx . . $ $anonymous_type_192af11623a8c789_1#n_entries. = 0;
137 h->limitcxx . . $ $anonymous_type_192af11623a8c789_1#limit. = max;
138 return h;
139 }
140
141 static inline boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
142 score_heap_pushcxx . . $ score_heap_push(283f74e6b2224594).(grn_ctxcxx . . $ grn_ctx# *ctx, score_heapcxx . . $ score_heap# *h, grn_idcxx . . $ grn_id# id, uint32_tcxx . . $ uint32_t# score)
143 {
144 uint32_tcxx . . $ uint32_t# n, n2;
145 score_heap_nodecxx . . $ score_heap_node# node = {id, score};
146 score_heap_nodecxx . . $ score_heap_node# node2;
147 if (h->n_entriescxx . . $ $anonymous_type_192af11623a8c789_1#n_entries. >= h->limitcxx . . $ $anonymous_type_192af11623a8c789_1#limit.) {
148 uint32_tcxx . . $ uint32_t# max = h->limitcxx . . $ $anonymous_type_192af11623a8c789_1#limit. * 2;
149 score_heap_nodecxx . . $ score_heap_node# *nodes;
150 nodes = GRN_PLUGIN_REALLOCcxx . . $ `include/groonga/plugin.h:98:9`!(ctx, h->nodes, sizeof(score_heap) * max);
151 if (!nodes) {
152 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
153 }
154 h->limitcxx . . $ $anonymous_type_192af11623a8c789_1#limit. = max;
155 h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes. = nodes;
156 }
157 h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[h->n_entriescxx . . $ $anonymous_type_192af11623a8c789_1#n_entries.] = node;
158 n = h->n_entriescxx . . $ $anonymous_type_192af11623a8c789_1#n_entries.++;
159 while (n) {
160 n2 = (n - 1) >> 1;
161 if (h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[n2].scorecxx . . $ $anonymous_type_192af11623a8c789_0#score. <= h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[n].scorecxx . . $ $anonymous_type_192af11623a8c789_0#score.) { break; }
162 node2 = h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[n];
163 h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[n] = h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[n2];
164 h->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[n2] = node2;
165 n = n2;
166 }
167 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
168 }
169
170 static inline void
171 score_heap_closecxx . . $ score_heap_close(c9ca8bc7ec7051b6).(grn_ctxcxx . . $ grn_ctx# *ctx, score_heapcxx . . $ score_heap# *h)
172 {
173 GRN_PLUGIN_FREEcxx . . $ `include/groonga/plugin.h:100:9`!(ctx, h->nodes);
174 GRN_PLUGIN_FREEcxx . . $ `include/groonga/plugin.h:100:9`!(ctx, h);
175 }
176
177 static grn_rccxx . . $ grn_rc#
178 sequential_fuzzy_searchcxx . . $ sequential_fuzzy_search(a188f6aab4d31106).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_objcxx . . $ grn_obj# *table, grn_objcxx . . $ grn_obj# *column, grn_objcxx . . $ grn_obj# *query,
179 uint32_tcxx . . $ uint32_t# max_distance, uint32_tcxx . . $ uint32_t# prefix_match_size,
180 uint32_tcxx . . $ uint32_t# max_expansion, int flags, grn_objcxx . . $ grn_obj# *res, grn_operatorcxx . . $ grn_operator# op)
181 {
182 grn_table_cursorcxx . . $ grn_table_cursor# *tc;
183 char *sx = GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(query);
184 char *ex = GRN_BULK_CURRcxx . . $ `include/groonga/groonga.h:2650:9`!(query);
185
186 if (op == GRN_OP_ANDcxx . . $ GRN_OP_AND.) {
187 tc = grn_table_cursor_opencxx . . $ grn_table_cursor_open(a4cddc3087c69e91).(ctx, res, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, 0, -1, GRN_CURSOR_BY_IDcxx . . $ `include/groonga/table.h:280:9`!);
188 } else {
189 tc = grn_table_cursor_opencxx . . $ grn_table_cursor_open(a4cddc3087c69e91).(ctx, table, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, 0, -1, GRN_CURSOR_BY_IDcxx . . $ `include/groonga/table.h:280:9`!);
190 }
191 if (tc) {
192 grn_idcxx . . $ grn_id# id;
193 grn_objcxx . . $ grn_obj# value;
194 score_heapcxx . . $ score_heap# *heap;
195 uint32_tcxx . . $ uint32_t# i, n;
196 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&value, 0);
197
198 heap = score_heap_opencxx . . $ score_heap_open(c0cc8115a85c7f25).(ctx, SCORE_HEAP_SIZEcxx . . $ `lib/proc/proc_fuzzy_search.c:113:9`!);
199 if (!heap) {
200 grn_table_cursor_closecxx . . $ grn_table_cursor_close(4865d068a46d0f83).(ctx, tc);
201 grn_obj_unlinkcxx . . $ grn_obj_unlink(4865d068a46d0f83).(ctx, &value);
202 return GRN_NO_MEMORY_AVAILABLEcxx . . $ GRN_NO_MEMORY_AVAILABLE.;
203 }
204
205 while ((id = grn_table_cursor_nextcxx . . $ grn_table_cursor_next(8dd58acc9865e889).(ctx, tc))) {
206 unsigned int distance = 0;
207 grn_objcxx . . $ grn_obj# *domain;
208 grn_idcxx . . $ grn_id# record_id;
209
210 if (op == GRN_OP_ANDcxx . . $ GRN_OP_AND.) {
211 grn_idcxx . . $ grn_id# *key;
212 grn_table_cursor_get_keycxx . . $ grn_table_cursor_get_key(7d620ebe94cee13a).(ctx, tc, (void **)&key);
213 record_id = *key;
214 } else {
215 record_id = id;
216 }
217 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&value);
218 grn_obj_get_valuecxx . . $ grn_obj_get_value(6e06fec66698a6a).(ctx, column, record_id, &value);
219 domain = grn_ctx_atcxx . . $ grn_ctx_at(ac4544c8400954c9).(ctx, ((&value))->headercxx . . $ _grn_obj#header..domaincxx . . $ _grn_obj_header#domain.);
220 if ((&(value))->headercxx . . $ _grn_obj#header..typecxx . . $ _grn_obj_header#type. == GRN_VECTORcxx . . $ `include/groonga/groonga.h:877:9`!) {
221 n = grn_vector_sizecxx . . $ grn_vector_size(8dd58acc9865e889).(ctx, &value);
222 for (i = 0; i < n; i++) {
223 unsigned int length;
224 const char *vector_value = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
225 length = grn_vector_get_elementcxx . . $ grn_vector_get_element(744613554bd9830c).(ctx, &value, i, &vector_value, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
226
227 if (!prefix_match_size ||
228 (prefix_match_size > 0 && length >= prefix_match_size &&
229 !memcmpcxx . . $ memcmp(36f949851efa3055).(sx, vector_value, prefix_match_size))) {
230 distance = calc_edit_distancecxx . . $ calc_edit_distance(7dc860abcac45052).(ctx, sx, ex,
231 (char *)vector_value,
232 (char *)vector_value + length, flags);
233 if (distance <= max_distance) {
234 score_heap_pushcxx . . $ score_heap_push(283f74e6b2224594).(ctx, heap, record_id, distance);
235 break;
236 }
237 }
238 }
239 } else if ((&(value))->headercxx . . $ _grn_obj#header..typecxx . . $ _grn_obj_header#type. == GRN_UVECTORcxx . . $ `include/groonga/groonga.h:875:9`! &&
240 grn_obj_is_tablecxx . . $ grn_obj_is_table(a5760e5c319a06ee).(ctx, domain)) {
241 n = grn_vector_sizecxx . . $ grn_vector_size(8dd58acc9865e889).(ctx, &value);
242 for (i = 0; i < n; i++) {
243 grn_idcxx . . $ grn_id# rid;
244 char key_name[GRN_TABLE_MAX_KEY_SIZEcxx . . $ `include/groonga/table.h:26:9`!];
245 int key_length;
246 rid = grn_uvector_get_elementcxx . . $ grn_uvector_get_element(621459fee6c81b73).(ctx, &value, i, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
247 key_length = grn_table_get_keycxx . . $ grn_table_get_key(2c24f49dad267d35).(ctx, domain, rid, key_name, GRN_TABLE_MAX_KEY_SIZEcxx . . $ `include/groonga/table.h:26:9`!);
248
249 if (!prefix_match_size ||
250 (prefix_match_size > 0 &&
251 (uint32_tcxx . . $ uint32_t#)key_length >= prefix_match_size &&
252 !memcmpcxx . . $ memcmp(36f949851efa3055).(sx, key_name, prefix_match_size))) {
253 distance = calc_edit_distancecxx . . $ calc_edit_distance(7dc860abcac45052).(ctx, sx, ex,
254 key_name, key_name + key_length, flags);
255 if (distance <= max_distance) {
256 score_heap_pushcxx . . $ score_heap_push(283f74e6b2224594).(ctx, heap, record_id, distance);
257 break;
258 }
259 }
260 }
261 } else {
262 if (grn_obj_is_reference_columncxx . . $ grn_obj_is_reference_column(a5760e5c319a06ee).(ctx, column)) {
263 grn_idcxx . . $ grn_id# rid;
264 char key_name[GRN_TABLE_MAX_KEY_SIZEcxx . . $ `include/groonga/table.h:26:9`!];
265 int key_length;
266 rid = GRN_RECORD_VALUEcxx . . $ `include/groonga/groonga.h:3110:9`!(&value);
267 key_length = grn_table_get_keycxx . . $ grn_table_get_key(2c24f49dad267d35).(ctx, domain, rid, key_name, GRN_TABLE_MAX_KEY_SIZEcxx . . $ `include/groonga/table.h:26:9`!);
268 if (!prefix_match_size ||
269 (prefix_match_size > 0 &&
270 (uint32_tcxx . . $ uint32_t#)key_length >= prefix_match_size &&
271 !memcmpcxx . . $ memcmp(36f949851efa3055).(sx, key_name, prefix_match_size))) {
272 distance = calc_edit_distancecxx . . $ calc_edit_distance(7dc860abcac45052).(ctx, sx, ex,
273 key_name, key_name + key_length, flags);
274 if (distance <= max_distance) {
275 score_heap_pushcxx . . $ score_heap_push(283f74e6b2224594).(ctx, heap, record_id, distance);
276 }
277 }
278 } else {
279 if (!prefix_match_size ||
280 (prefix_match_size > 0 && GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&value) >= prefix_match_size &&
281 !memcmpcxx . . $ memcmp(36f949851efa3055).(sx, GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&value), prefix_match_size))) {
282 distance = calc_edit_distancecxx . . $ calc_edit_distance(7dc860abcac45052).(ctx, sx, ex,
283 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&value),
284 GRN_BULK_CURRcxx . . $ `include/groonga/groonga.h:2650:9`!(&value), flags);
285 if (distance <= max_distance) {
286 score_heap_pushcxx . . $ score_heap_push(283f74e6b2224594).(ctx, heap, record_id, distance);
287 }
288 }
289 }
290 }
291 grn_obj_unlinkcxx . . $ grn_obj_unlink(4865d068a46d0f83).(ctx, domain);
292 }
293 grn_table_cursor_closecxx . . $ grn_table_cursor_close(4865d068a46d0f83).(ctx, tc);
294 grn_obj_unlinkcxx . . $ grn_obj_unlink(4865d068a46d0f83).(ctx, &value);
295
296 for (i = 0; i < heap->n_entriescxx . . $ $anonymous_type_192af11623a8c789_1#n_entries.; i++) {
297 if (max_expansion > 0 && (size_t)i >= max_expansion) {
298 break;
299 }
300 {
301 grn_posting_internalcxx . . $ grn_posting_internal# posting = {0};
302 posting.ridcxx . . $ $anonymous_type_fe7b80c5b60468ac_0#rid. = heap->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[i].idcxx . . $ $anonymous_type_192af11623a8c789_0#id.;
303 posting.sidcxx . . $ $anonymous_type_fe7b80c5b60468ac_0#sid. = 1;
304 posting.poscxx . . $ $anonymous_type_fe7b80c5b60468ac_0#pos. = 0;
305 posting.weight_floatcxx . . $ $anonymous_type_fe7b80c5b60468ac_0#weight_float. = (float)(max_distance - heap->nodescxx . . $ $anonymous_type_192af11623a8c789_1#nodes.[i].scorecxx . . $ $anonymous_type_192af11623a8c789_0#score. + 1);
306 grn_ii_posting_add_floatcxx . . $ grn_ii_posting_add_float(62be95d159451db1).(ctx,
307 (grn_postingcxx . . $ grn_posting# *)(&posting),
308 (grn_hashcxx . . $ grn_hash# *)res,
309 op);
310 }
311 }
312 grn_ii_resolve_sel_andcxx . . $ grn_ii_resolve_sel_and(8582dd5db79648f2).(ctx, (grn_hashcxx . . $ grn_hash# *)res, op);
313 score_heap_closecxx . . $ score_heap_close(c9ca8bc7ec7051b6).(ctx, heap);
314 }
315
316 return GRN_SUCCESScxx . . $ GRN_SUCCESS.;
317 }
318
319 typedef structcxx . . $ $anonymous_type_192af11623a8c789_2# {
320 grn_objcxx . . $ grn_obj# *targetcxx . . $ $anonymous_type_192af11623a8c789_2#target.;
321 grn_objcxx . . $ grn_obj# *querycxx . . $ $anonymous_type_192af11623a8c789_2#query.;
322 uint32_tcxx . . $ uint32_t# max_distancecxx . . $ $anonymous_type_192af11623a8c789_2#max_distance.;
323 uint32_tcxx . . $ uint32_t# prefix_lengthcxx . . $ $anonymous_type_192af11623a8c789_2#prefix_length.;
324 uint32_tcxx . . $ uint32_t# prefix_match_sizecxx . . $ $anonymous_type_192af11623a8c789_2#prefix_match_size.;
325 uint32_tcxx . . $ uint32_t# max_expansionscxx . . $ $anonymous_type_192af11623a8c789_2#max_expansions.;
326 int flagscxx . . $ $anonymous_type_192af11623a8c789_2#flags.;
327 } fuzzy_search_datacxx . . $ fuzzy_search_data#;
328
329 static grn_rccxx . . $ grn_rc#
330 selector_fuzzy_search_executecxx . . $ selector_fuzzy_search_execute(779527b1763d3d8b).(grn_ctxcxx . . $ grn_ctx# *ctx,
331 grn_objcxx . . $ grn_obj# *index,
332 grn_operatorcxx . . $ grn_operator# op,
333 grn_objcxx . . $ grn_obj# *res,
334 grn_operatorcxx . . $ grn_operator# logical_op,
335 void *user_data)
336 {
337 grn_rccxx . . $ grn_rc# rc = GRN_SUCCESScxx . . $ GRN_SUCCESS.;
338 fuzzy_search_datacxx . . $ fuzzy_search_data# *data = user_data;
339 grn_objcxx . . $ grn_obj# *target = index;
340 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! use_sequential_search = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
341
342 if (grn_obj_is_accessorcxx . . $ grn_obj_is_accessor(a5760e5c319a06ee).(ctx, target)) {
343 if (grn_obj_is_key_accessorcxx . . $ grn_obj_is_key_accessor(a5760e5c319a06ee).(ctx, target) &&
344 ((grn_accessorcxx . . $ grn_accessor# *)target)->objcxx . . $ _grn_accessor#obj.->headercxx . . $ _grn_obj#header..typecxx . . $ _grn_obj_header#type. == GRN_TABLE_PAT_KEYcxx . . $ `include/groonga/groonga.h:896:9`!) {
345 target = ((grn_accessorcxx . . $ grn_accessor# *)target)->objcxx . . $ _grn_accessor#obj.;
346 use_sequential_search = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
347 } else {
348 use_sequential_search = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
349 }
350 } else if (target) {
351 use_sequential_search = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
352 if (target->headercxx . . $ _grn_obj#header..typecxx . . $ _grn_obj_header#type. == GRN_TABLE_PAT_KEYcxx . . $ `include/groonga/groonga.h:896:9`!) {
353 use_sequential_search = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
354 } else {
355 grn_objcxx . . $ grn_obj# *lexicon;
356 lexicon = grn_ctx_atcxx . . $ grn_ctx_at(ac4544c8400954c9).(ctx, target->headercxx . . $ _grn_obj#header..domaincxx . . $ _grn_obj_header#domain.);
357 if (lexicon && lexicon->headercxx . . $ _grn_obj#header..typecxx . . $ _grn_obj_header#type. == GRN_TABLE_PAT_KEYcxx . . $ `include/groonga/groonga.h:896:9`!) {
358 use_sequential_search = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
359 }
360 }
361 } else {
362 use_sequential_search = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
363 }
364
365 if (data->prefix_lengthcxx . . $ $anonymous_type_192af11623a8c789_2#prefix_length.) {
366 const char *s = GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(data->query);
367 const char *e = GRN_BULK_CURRcxx . . $ `include/groonga/groonga.h:2650:9`!(data->query);
368 const char *p;
369 int cl = 0;
370 unsigned int length = 0;
371 for (p = s; p < e && (cl = grn_charlencxx . . $ grn_charlen(d229e8bf7c87f0f0).(ctx, p, e)); p += cl) {
372 length++;
373 if (length > data->prefix_lengthcxx . . $ $anonymous_type_192af11623a8c789_2#prefix_length.) {
374 break;
375 }
376 }
377 data->prefix_match_sizecxx . . $ $anonymous_type_192af11623a8c789_2#prefix_match_size. = (uint32_tcxx . . $ uint32_t#)(p - s);
378 }
379
380 if (use_sequential_search) {
381 grn_objcxx . . $ grn_obj# *table;
382 table = grn_ctx_atcxx . . $ grn_ctx_at(ac4544c8400954c9).(ctx, res->headercxx . . $ _grn_obj#header..domaincxx . . $ _grn_obj_header#domain.);
383 rc = sequential_fuzzy_searchcxx . . $ sequential_fuzzy_search(a188f6aab4d31106).(ctx,
384 table,
385 data->targetcxx . . $ $anonymous_type_192af11623a8c789_2#target.,
386 data->querycxx . . $ $anonymous_type_192af11623a8c789_2#query.,
387 data->max_distancecxx . . $ $anonymous_type_192af11623a8c789_2#max_distance.,
388 data->prefix_match_sizecxx . . $ $anonymous_type_192af11623a8c789_2#prefix_match_size.,
389 data->max_expansionscxx . . $ $anonymous_type_192af11623a8c789_2#max_expansions.,
390 data->flagscxx . . $ $anonymous_type_192af11623a8c789_2#flags.,
391 res,
392 logical_op);
393 goto exit;
394 }
395
396 if (!target) {
397 grn_objcxx . . $ grn_obj# inspected;
398 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&inspected, 0);
399 grn_inspectcxx . . $ grn_inspect(88456f97fe0d41a).(ctx, &inspected, target);
400 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx, GRN_INVALID_ARGUMENT,
401 "fuzzy_search(): "
402 "column must be COLUMN_INDEX or TABLE_PAT_KEY: <%.*s>",
403 (int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&inspected),
404 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&inspected));
405 rc = ctx->rccxx . . $ _grn_ctx#rc.;
406 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &inspected);
407 } else {
408 grn_search_optargcxx . . $ grn_search_optarg# options = {0};
409 options.modecxx . . $ _grn_search_optarg#mode. = GRN_OP_FUZZYcxx . . $ GRN_OP_FUZZY.;
410 options.fuzzycxx . . $ _grn_search_optarg#fuzzy..prefix_match_sizecxx . . $ _grn_fuzzy_search_optarg#prefix_match_size. = data->prefix_match_sizecxx . . $ $anonymous_type_192af11623a8c789_2#prefix_match_size.;
411 options.fuzzycxx . . $ _grn_search_optarg#fuzzy..max_distancecxx . . $ _grn_fuzzy_search_optarg#max_distance. = data->max_distancecxx . . $ $anonymous_type_192af11623a8c789_2#max_distance.;
412 options.fuzzycxx . . $ _grn_search_optarg#fuzzy..max_expansioncxx . . $ _grn_fuzzy_search_optarg#max_expansion. = data->max_expansionscxx . . $ $anonymous_type_192af11623a8c789_2#max_expansions.;
413 options.fuzzycxx . . $ _grn_search_optarg#fuzzy..flagscxx . . $ _grn_fuzzy_search_optarg#flags. = data->flagscxx . . $ $anonymous_type_192af11623a8c789_2#flags.;
414 grn_obj_searchcxx . . $ grn_obj_search(a3553fe71ef945f4).(ctx, target, data->querycxx . . $ $anonymous_type_192af11623a8c789_2#query., res, logical_op, &options);
415 }
416
417 exit :
418 return rc;
419 }
420
421 static grn_rccxx . . $ grn_rc#
422 selector_fuzzy_searchcxx . . $ selector_fuzzy_search(2267fc2522c8e143).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_objcxx . . $ grn_obj# *table, grn_objcxx . . $ grn_obj# *index,
423 int nargs, grn_objcxx . . $ grn_obj# **args,
424 grn_objcxx . . $ grn_obj# *res, grn_operatorcxx . . $ grn_operator# op)
425 {
426 const char *tag = "[fuzzy-search]";
427 grn_rccxx . . $ grn_rc# rc = GRN_SUCCESScxx . . $ GRN_SUCCESS.;
428
429 if ((nargs - 1) < 2) {
430 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx, GRN_INVALID_ARGUMENT,
431 "%s wrong number of arguments (%d for 2..)",
432 tag,
433 nargs - 1);
434 rc = ctx->rccxx . . $ _grn_ctx#rc.;
435 goto exit;
436 }
437
438 fuzzy_search_datacxx . . $ fuzzy_search_data# data;
439 data.targetcxx . . $ $anonymous_type_192af11623a8c789_2#target. = args[1];
440 data.querycxx . . $ $anonymous_type_192af11623a8c789_2#query. = args[2];
441 data.max_distancecxx . . $ $anonymous_type_192af11623a8c789_2#max_distance. = 1;
442 data.prefix_lengthcxx . . $ $anonymous_type_192af11623a8c789_2#prefix_length. = 0;
443 data.prefix_match_sizecxx . . $ $anonymous_type_192af11623a8c789_2#prefix_match_size. = 0;
444 data.max_expansionscxx . . $ $anonymous_type_192af11623a8c789_2#max_expansions. = 0;
445 data.flagscxx . . $ $anonymous_type_192af11623a8c789_2#flags. = 0;
446
447 if (nargs == 4) {
448 grn_objcxx . . $ grn_obj# *options = args[3];
449
450 switch (options->headercxx . . $ _grn_obj#header..typecxx . . $ _grn_obj_header#type.) {
451 case GRN_BULKcxx . . $ `include/groonga/groonga.h:868:9`! :
452 data.max_distancecxx . . $ $anonymous_type_192af11623a8c789_2#max_distance. = GRN_UINT32_VALUEcxx . . $ `include/groonga/groonga.h:3103:9`!(options);
453 break;
454 case GRN_TABLE_HASH_KEYcxx . . $ `include/groonga/groonga.h:895:9`! :
455 {
456 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! with_transposition = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
457 rc = grn_proc_options_parsecxx . . $ grn_proc_options_parse(b3b87877968531ef).(ctx,
458 options,
459 tag,
460 "max_distance",
461 GRN_PROC_OPTION_VALUE_UINT32cxx . . $ GRN_PROC_OPTION_VALUE_UINT32.,
462 &(data.max_distancecxx . . $ $anonymous_type_192af11623a8c789_2#max_distance.),
463 "prefix_length",
464 GRN_PROC_OPTION_VALUE_UINT32cxx . . $ GRN_PROC_OPTION_VALUE_UINT32.,
465 &(data.prefix_lengthcxx . . $ $anonymous_type_192af11623a8c789_2#prefix_length.),
466 /* Deprecated since 13.0.8.
467 * Keep this for backward compatibility. */
468 "max_expansion",
469 GRN_PROC_OPTION_VALUE_UINT32cxx . . $ GRN_PROC_OPTION_VALUE_UINT32.,
470 &(data.max_expansionscxx . . $ $anonymous_type_192af11623a8c789_2#max_expansions.),
471 "max_expansions",
472 GRN_PROC_OPTION_VALUE_UINT32cxx . . $ GRN_PROC_OPTION_VALUE_UINT32.,
473 &(data.max_expansionscxx . . $ $anonymous_type_192af11623a8c789_2#max_expansions.),
474 "with_transposition",
475 GRN_PROC_OPTION_VALUE_BOOLcxx . . $ GRN_PROC_OPTION_VALUE_BOOL.,
476 &with_transposition,
477 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
478 if (rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) {
479 goto exit;
480 }
481 if (with_transposition) {
482 data.flagscxx . . $ $anonymous_type_192af11623a8c789_2#flags. |= GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITIONcxx . . $ `include/groonga/groonga.h:1958:9`!;
483 }
484 }
485 break;
486 default :
487 GRN_PLUGIN_ERRORcxx . . $ `include/groonga/plugin.h:145:9`!(ctx, GRN_INVALID_ARGUMENT,
488 "%s "
489 "3rd argument must be integer or object literal: <%.*s>",
490 tag,
491 (int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(options),
492 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(options));
493 goto exit;
494 }
495 }
496
497 if (grn_obj_is_accessorcxx . . $ grn_obj_is_accessor(a5760e5c319a06ee).(ctx, data.targetcxx . . $ $anonymous_type_192af11623a8c789_2#target.)) {
498 rc = grn_accessor_executecxx . . $ grn_accessor_execute(b07a7b8125570271).(ctx,
499 data.targetcxx . . $ $anonymous_type_192af11623a8c789_2#target.,
500 selector_fuzzy_search_executecxx . . $ selector_fuzzy_search_execute(779527b1763d3d8b).,
501 &data,
502 GRN_OP_FUZZYcxx . . $ GRN_OP_FUZZY.,
503 res,
504 op);
505 } else {
506 grn_index_datumcxx . . $ grn_index_datum# index_datum;
507 unsigned int n_index_datum;
508 if (grn_obj_is_index_columncxx . . $ grn_obj_is_index_column(a5760e5c319a06ee).(ctx, data.targetcxx . . $ $anonymous_type_192af11623a8c789_2#target.)) {
509 index_datum.indexcxx . . $ _grn_index_datum#index. = data.targetcxx . . $ $anonymous_type_192af11623a8c789_2#target.;
510 n_index_datum = 1;
511 } else {
512 n_index_datum = grn_column_find_index_datacxx . . $ grn_column_find_index_data(bf138cd289124d2c).(ctx,
513 data.targetcxx . . $ $anonymous_type_192af11623a8c789_2#target.,
514 GRN_OP_FUZZYcxx . . $ GRN_OP_FUZZY.,
515 &index_datum,
516 1);
517 }
518 if (n_index_datum == 0) {
519 index_datum.indexcxx . . $ _grn_index_datum#index. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
520 }
521 rc = selector_fuzzy_search_executecxx . . $ selector_fuzzy_search_execute(779527b1763d3d8b).(ctx,
522 index_datum.indexcxx . . $ _grn_index_datum#index.,
523 GRN_OP_FUZZYcxx . . $ GRN_OP_FUZZY.,
524 res,
525 op,
526 &data);
527 }
528
529 exit :
530 return rc;
531 }
532
533 void
534 grn_proc_init_fuzzy_searchcxx . . $ grn_proc_init_fuzzy_search(b578cd6e53d5fe91).(grn_ctxcxx . . $ grn_ctx# *ctx)
535 {
536 grn_objcxx . . $ grn_obj# *selector_proc;
537
538 selector_proc = grn_proc_createcxx . . $ grn_proc_create(195df001e5324f57).(ctx, "fuzzy_search", -1,
539 GRN_PROC_FUNCTIONcxx . . $ GRN_PROC_FUNCTION.,
540 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
541 grn_proc_set_selectorcxx . . $ grn_proc_set_selector(7ce76c9564b607b5).(ctx, selector_proc, selector_fuzzy_searchcxx . . $ selector_fuzzy_search(2267fc2522c8e143).);
542 grn_proc_set_selector_operatorcxx . . $ grn_proc_set_selector_operator(bbc69e63741a8d0a).(ctx, selector_proc, GRN_OP_NOPcxx . . $ GRN_OP_NOP.);
543 }