Debian Codegraph @

plugins/tokenizers/h3_index.c

raw symbols SCIP
1 /*
2 Copyright (C) 2024 Sutou Kouhei <[email protected]>
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 #ifdef GRN_EMBEDDED
20 # define GRN_PLUGIN_FUNCTION_TAG tokenizers_h3_index
21 #endif
22
23 #include <groonga.h>
24 #include <groonga/tokenizer.h>
25
26 static const char *grn_h3_index_tag = "[tokenizer][h3-index]";
27
28 typedef struct {
29 uint8_t resolution;
30 } grn_h3_index_options;
31
32 static void
33 grn_h3_index_options_init(grn_ctx *ctx,
34 grn_h3_index_options *options,
35 int32_t resolution)
36 {
37 options->resolution = resolution;
38 }
39
40 static void
41 grn_h3_index_options_fin(grn_ctx *ctx, grn_h3_index_options *options)
42 {
43 }
44
45 typedef struct {
46 grn_h3_index_options options;
47 grn_geo_point geo_point;
48 uint64_t index;
49 } grn_h3_index_tokenizer;
50
51 static void
52 grn_h3_index_close_options(grn_ctx *ctx, void *data)
53 {
54 grn_h3_index_options *options = data;
55 grn_h3_index_options_fin(ctx, options);
56 GRN_PLUGIN_FREE(ctx, options);
57 }
58
59 static void *
60 grn_h3_index_open_options(grn_ctx *ctx,
61 grn_obj *tokenizer,
62 grn_obj *raw_options,
63 void *user_data)
64 {
65 grn_h3_index_options *options =
66 GRN_PLUGIN_CALLOC(ctx, sizeof(grn_h3_index_options));
67 if (!options) {
68 GRN_PLUGIN_ERROR(ctx,
69 GRN_NO_MEMORY_AVAILABLE,
70 "%s failed to allocate memory for options",
71 grn_h3_index_tag);
72 return NULL;
73 }
74
75 grn_h3_index_options_init(ctx, options, 15);
76 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
77 {
78 grn_raw_string name_raw;
79 name_raw.value = name;
80 name_raw.length = name_length;
81
82 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "resolution")) {
83 options->resolution =
84 grn_vector_get_element_uint8(ctx, raw_options, i, options->resolution);
85 }
86 }
87 GRN_OPTION_VALUES_EACH_END();
88
89 return options;
90 }
91
92 static void
93 grn_h3_index_fin(grn_ctx *ctx, void *user_data)
94 {
95 grn_h3_index_tokenizer *tokenizer = user_data;
96
97 if (!tokenizer) {
98 return;
99 }
100
101 GRN_PLUGIN_FREE(ctx, tokenizer);
102 }
103
104 static void *
105 grn_h3_index_init(grn_ctx *ctx, grn_tokenizer_query *query)
106 {
107 grn_geo_point geo_point;
108 size_t query_size;
109 grn_id query_domain;
110 const char *raw_query =
111 grn_tokenizer_query_get_data(ctx, query, &query_size, &query_domain);
112 if (query_domain == GRN_DB_WGS84_GEO_POINT) {
113 if (query_size != sizeof(grn_geo_point)) {
114 GRN_PLUGIN_ERROR(ctx,
115 GRN_INVALID_ARGUMENT,
116 "%s query must be a WGS84GeoPoint: %zu != %zu",
117 grn_h3_index_tag,
118 query_size,
119 sizeof(grn_geo_point));
120 return NULL;
121 }
122 geo_point = *((const grn_geo_point *)raw_query);
123 } else {
124 if (grn_type_id_is_text_family(ctx, query_domain)) {
125 grn_obj text_geo_point;
126 GRN_TEXT_INIT(&text_geo_point, GRN_OBJ_DO_SHALLOW_COPY);
127 GRN_TEXT_SET(ctx, &text_geo_point, raw_query, query_size);
128 grn_obj geo_point_buffer;
129 GRN_WGS84_GEO_POINT_INIT(&geo_point_buffer, 0);
130 grn_rc rc = grn_obj_cast(ctx, &text_geo_point, &geo_point_buffer, false);
131 if (rc != GRN_SUCCESS) {
132 GRN_OBJ_FIN(ctx, &geo_point_buffer);
133 GRN_PLUGIN_ERROR(
134 ctx,
135 rc,
136 "%s query must be valid WGS84GeoPoint text representation: <%.*s>",
137 grn_h3_index_tag,
138 (int)query_size,
139 raw_query);
140 return NULL;
141 }
142 GRN_GEO_POINT_VALUE(&geo_point_buffer,
143 geo_point.latitude,
144 geo_point.longitude);
145 GRN_OBJ_FIN(ctx, &geo_point_buffer);
146 } else {
147 GRN_PLUGIN_ERROR(ctx,
148 GRN_INVALID_ARGUMENT,
149 "%s query must be WGS84GeoPoint: %s(%u)",
150 grn_h3_index_tag,
151 grn_type_id_to_string_builtin(ctx, query_domain),
152 query_domain);
153 return NULL;
154 }
155 }
156
157 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
158 grn_h3_index_options *options =
159 grn_table_cache_default_tokenizer_options(ctx,
160 lexicon,
161 grn_h3_index_open_options,
162 grn_h3_index_close_options,
163 query);
164 if (ctx->rc != GRN_SUCCESS) {
165 return NULL;
166 }
167
168 grn_h3_index_tokenizer *tokenizer =
169 GRN_PLUGIN_CALLOC(ctx, sizeof(grn_h3_index_tokenizer));
170 if (!tokenizer) {
171 GRN_PLUGIN_ERROR(ctx,
172 GRN_NO_MEMORY_AVAILABLE,
173 "%s failed to allocate tokenizer",
174 grn_h3_index_tag);
175 return NULL;
176 }
177
178 tokenizer->options = *options;
179 tokenizer->geo_point = geo_point;
180
181 return tokenizer;
182 }
183
184 static void
185 grn_h3_index_next(grn_ctx *ctx,
186 grn_tokenizer_query *query,
187 grn_token *token,
188 void *user_data)
189 {
190 grn_h3_index_tokenizer *tokenizer = user_data;
191
192 tokenizer->index = grn_h3_compute_cell(ctx,
193 &(tokenizer->geo_point),
194 tokenizer->options.resolution,
195 grn_h3_index_tag);
196 if (ctx->rc != GRN_SUCCESS) {
197 return;
198 }
199 grn_token_set_data(ctx,
200 token,
201 (const char *)&(tokenizer->index),
202 sizeof(uint64_t));
203 grn_token_set_domain(ctx, token, GRN_DB_UINT64);
204 grn_token_set_status(ctx, token, GRN_TOKEN_LAST);
205 }
206
207 grn_rc
208 GRN_PLUGIN_INIT(grn_ctx *ctx)
209 {
210 return GRN_SUCCESS;
211 }
212
213 grn_rc
214 GRN_PLUGIN_REGISTER(grn_ctx *ctx)
215 {
216 grn_obj *tokenizer = grn_tokenizer_create(ctx, "TokenH3Index", -1);
217 if (tokenizer) {
218 grn_tokenizer_set_init_func(ctx, tokenizer, grn_h3_index_init);
219 grn_tokenizer_set_next_func(ctx, tokenizer, grn_h3_index_next);
220 grn_tokenizer_set_fin_func(ctx, tokenizer, grn_h3_index_fin);
221 }
222 return ctx->rc;
223 }
224
225 grn_rc
226 GRN_PLUGIN_FIN(grn_ctx *ctx)
227 {
228 return GRN_SUCCESS;
229 }