File: h3_index.c

package info (click to toggle)
groonga 15.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 171,500 kB
  • sloc: ansic: 772,536; cpp: 51,530; ruby: 40,538; javascript: 10,250; yacc: 7,045; sh: 5,622; python: 2,821; makefile: 1,677
file content (229 lines) | stat: -rw-r--r-- 6,798 bytes parent folder | download
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
/*
  Copyright (C) 2024  Sutou Kouhei <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  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
*/

#ifdef GRN_EMBEDDED
#  define GRN_PLUGIN_FUNCTION_TAG tokenizers_h3_index
#endif

#include <groonga.h>
#include <groonga/tokenizer.h>

static const char *grn_h3_index_tag = "[tokenizer][h3-index]";

typedef struct {
  uint8_t resolution;
} grn_h3_index_options;

static void
grn_h3_index_options_init(grn_ctx *ctx,
                          grn_h3_index_options *options,
                          int32_t resolution)
{
  options->resolution = resolution;
}

static void
grn_h3_index_options_fin(grn_ctx *ctx, grn_h3_index_options *options)
{
}

typedef struct {
  grn_h3_index_options options;
  grn_geo_point geo_point;
  uint64_t index;
} grn_h3_index_tokenizer;

static void
grn_h3_index_close_options(grn_ctx *ctx, void *data)
{
  grn_h3_index_options *options = data;
  grn_h3_index_options_fin(ctx, options);
  GRN_PLUGIN_FREE(ctx, options);
}

static void *
grn_h3_index_open_options(grn_ctx *ctx,
                          grn_obj *tokenizer,
                          grn_obj *raw_options,
                          void *user_data)
{
  grn_h3_index_options *options =
    GRN_PLUGIN_CALLOC(ctx, sizeof(grn_h3_index_options));
  if (!options) {
    GRN_PLUGIN_ERROR(ctx,
                     GRN_NO_MEMORY_AVAILABLE,
                     "%s failed to allocate memory for options",
                     grn_h3_index_tag);
    return NULL;
  }

  grn_h3_index_options_init(ctx, options, 15);
  GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
  {
    grn_raw_string name_raw;
    name_raw.value = name;
    name_raw.length = name_length;

    if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "resolution")) {
      options->resolution =
        grn_vector_get_element_uint8(ctx, raw_options, i, options->resolution);
    }
  }
  GRN_OPTION_VALUES_EACH_END();

  return options;
}

static void
grn_h3_index_fin(grn_ctx *ctx, void *user_data)
{
  grn_h3_index_tokenizer *tokenizer = user_data;

  if (!tokenizer) {
    return;
  }

  GRN_PLUGIN_FREE(ctx, tokenizer);
}

static void *
grn_h3_index_init(grn_ctx *ctx, grn_tokenizer_query *query)
{
  grn_geo_point geo_point;
  size_t query_size;
  grn_id query_domain;
  const char *raw_query =
    grn_tokenizer_query_get_data(ctx, query, &query_size, &query_domain);
  if (query_domain == GRN_DB_WGS84_GEO_POINT) {
    if (query_size != sizeof(grn_geo_point)) {
      GRN_PLUGIN_ERROR(ctx,
                       GRN_INVALID_ARGUMENT,
                       "%s query must be a WGS84GeoPoint: %zu != %zu",
                       grn_h3_index_tag,
                       query_size,
                       sizeof(grn_geo_point));
      return NULL;
    }
    geo_point = *((const grn_geo_point *)raw_query);
  } else {
    if (grn_type_id_is_text_family(ctx, query_domain)) {
      grn_obj text_geo_point;
      GRN_TEXT_INIT(&text_geo_point, GRN_OBJ_DO_SHALLOW_COPY);
      GRN_TEXT_SET(ctx, &text_geo_point, raw_query, query_size);
      grn_obj geo_point_buffer;
      GRN_WGS84_GEO_POINT_INIT(&geo_point_buffer, 0);
      grn_rc rc = grn_obj_cast(ctx, &text_geo_point, &geo_point_buffer, false);
      if (rc != GRN_SUCCESS) {
        GRN_OBJ_FIN(ctx, &geo_point_buffer);
        GRN_PLUGIN_ERROR(
          ctx,
          rc,
          "%s query must be valid WGS84GeoPoint text representation: <%.*s>",
          grn_h3_index_tag,
          (int)query_size,
          raw_query);
        return NULL;
      }
      GRN_GEO_POINT_VALUE(&geo_point_buffer,
                          geo_point.latitude,
                          geo_point.longitude);
      GRN_OBJ_FIN(ctx, &geo_point_buffer);
    } else {
      GRN_PLUGIN_ERROR(ctx,
                       GRN_INVALID_ARGUMENT,
                       "%s query must be WGS84GeoPoint: %s(%u)",
                       grn_h3_index_tag,
                       grn_type_id_to_string_builtin(ctx, query_domain),
                       query_domain);
      return NULL;
    }
  }

  grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
  grn_h3_index_options *options =
    grn_table_cache_default_tokenizer_options(ctx,
                                              lexicon,
                                              grn_h3_index_open_options,
                                              grn_h3_index_close_options,
                                              query);
  if (ctx->rc != GRN_SUCCESS) {
    return NULL;
  }

  grn_h3_index_tokenizer *tokenizer =
    GRN_PLUGIN_CALLOC(ctx, sizeof(grn_h3_index_tokenizer));
  if (!tokenizer) {
    GRN_PLUGIN_ERROR(ctx,
                     GRN_NO_MEMORY_AVAILABLE,
                     "%s failed to allocate tokenizer",
                     grn_h3_index_tag);
    return NULL;
  }

  tokenizer->options = *options;
  tokenizer->geo_point = geo_point;

  return tokenizer;
}

static void
grn_h3_index_next(grn_ctx *ctx,
                  grn_tokenizer_query *query,
                  grn_token *token,
                  void *user_data)
{
  grn_h3_index_tokenizer *tokenizer = user_data;

  tokenizer->index = grn_h3_compute_cell(ctx,
                                         &(tokenizer->geo_point),
                                         tokenizer->options.resolution,
                                         grn_h3_index_tag);
  if (ctx->rc != GRN_SUCCESS) {
    return;
  }
  grn_token_set_data(ctx,
                     token,
                     (const char *)&(tokenizer->index),
                     sizeof(uint64_t));
  grn_token_set_domain(ctx, token, GRN_DB_UINT64);
  grn_token_set_status(ctx, token, GRN_TOKEN_LAST);
}

grn_rc
GRN_PLUGIN_INIT(grn_ctx *ctx)
{
  return GRN_SUCCESS;
}

grn_rc
GRN_PLUGIN_REGISTER(grn_ctx *ctx)
{
  grn_obj *tokenizer = grn_tokenizer_create(ctx, "TokenH3Index", -1);
  if (tokenizer) {
    grn_tokenizer_set_init_func(ctx, tokenizer, grn_h3_index_init);
    grn_tokenizer_set_next_func(ctx, tokenizer, grn_h3_index_next);
    grn_tokenizer_set_fin_func(ctx, tokenizer, grn_h3_index_fin);
  }
  return ctx->rc;
}

grn_rc
GRN_PLUGIN_FIN(grn_ctx *ctx)
{
  return GRN_SUCCESS;
}