File: index_column.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 (497 lines) | stat: -rw-r--r-- 15,984 bytes parent folder | download | duplicates (2)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
  Copyright (C) 2017  Brazil
  Copyright (C) 2019-2022  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 functions_time
#endif

#include <groonga/plugin.h>

static grn_rc
selector_index_column_df_ratio_between(grn_ctx *ctx,
                                       grn_obj *table,
                                       grn_obj *index,
                                       int n_args,
                                       grn_obj **args,
                                       grn_obj *res,
                                       grn_operator op)
{
  grn_rc rc = GRN_SUCCESS;
  grn_obj *index_column;
  grn_ii *ii;
  double min;
  double max;
  grn_obj *source_table;
  unsigned int n_documents;
  grn_posting posting;

  if ((n_args - 1) != 3) {
    GRN_PLUGIN_ERROR(ctx,
                     GRN_INVALID_ARGUMENT,
                     "index_column_df_ratio_between(): "
                     "wrong number of arguments (%d for 3)", n_args - 1);
    rc = ctx->rc;
    goto exit;
  }

  index_column = args[1];
  ii = (grn_ii *)index_column;
  min = GRN_FLOAT_VALUE(args[2]);
  max = GRN_FLOAT_VALUE(args[3]);

  source_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, index_column));
  n_documents = grn_table_size(ctx, source_table);
  memset(&posting, 0, sizeof(grn_posting));
  posting.sid = 1;

  if (op == GRN_OP_AND) {
    GRN_TABLE_EACH_BEGIN(ctx, res, cursor, record_id) {
      void *key;
      grn_id term_id;
      uint32_t n_match_documents;
      double df_ratio;

      grn_table_cursor_get_key(ctx, cursor, &key);
      term_id = *(grn_id *)key;
      n_match_documents = grn_ii_estimate_size(ctx, ii, term_id);
      if (n_match_documents > n_documents) {
        n_match_documents = n_documents;
      }
      df_ratio = (double)n_match_documents / (double)n_documents;
      if (min <= df_ratio && df_ratio <= max) {
        posting.rid = term_id;
        grn_ii_posting_add(ctx, &posting, (grn_hash *)res, op);
      }
    } GRN_TABLE_EACH_END(ctx, cursor);
    grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op);
  } else {
    GRN_TABLE_EACH_BEGIN(ctx, table, cursor, term_id) {
      uint32_t n_match_documents;
      double df_ratio;

      n_match_documents = grn_ii_estimate_size(ctx, ii, term_id);
      if (n_match_documents > n_documents) {
        n_match_documents = n_documents;
      }
      df_ratio = (double)n_match_documents / (double)n_documents;
      {
        void *key;
        int key_size;
        key_size = grn_table_cursor_get_key(ctx, cursor, &key);
      }
      if (min <= df_ratio && df_ratio <= max) {
        posting.rid = term_id;
        grn_ii_posting_add(ctx, &posting, (grn_hash *)res, op);
      }
    } GRN_TABLE_EACH_END(ctx, cursor);
  }

exit :
  return rc;
}

typedef struct {
  grn_id term_id;
  grn_obj *term_table;
  grn_obj *index_column;
} caller_index_info;

static void
caller_index_info_fin(grn_ctx *ctx,
                      caller_index_info *caller_index_info)
{
  if (caller_index_info->index_column) {
    grn_obj_unref(ctx, caller_index_info->index_column);
  }
  if (caller_index_info->term_table) {
    grn_obj_unref(ctx, caller_index_info->term_table);
  }
}

static grn_rc
caller_index_info_init(grn_ctx *ctx,
                       caller_index_info *caller_index_info,
                       grn_obj *index_column_name,
                       grn_user_data *user_data,
                       const char *error_message_label)
{
  caller_index_info->term_id = GRN_ID_NIL;
  caller_index_info->term_table = NULL;
  caller_index_info->index_column = NULL;

  {
    grn_obj *expr;
    grn_obj *variable;

    expr = grn_plugin_proc_get_caller(ctx, user_data);
    if (!expr) {
      GRN_PLUGIN_ERROR(ctx,
                       GRN_INVALID_ARGUMENT,
                       "%s: "
                       "called directly", error_message_label);
      return ctx->rc;
    }

    variable = grn_expr_get_var_by_offset(ctx, expr, 0);
    if (!variable) {
      GRN_PLUGIN_ERROR(ctx,
                       GRN_INVALID_ARGUMENT,
                       "%s: "
                       "caller expression must have target record information",
                       error_message_label);
      return ctx->rc;
    }

    caller_index_info->term_table = grn_ctx_at(ctx, variable->header.domain);
    caller_index_info->term_id = GRN_RECORD_VALUE(variable);
    while (true) {
      grn_obj *key_type;

      key_type = grn_ctx_at(ctx, caller_index_info->term_table->header.domain);
      if (!grn_obj_is_table(ctx, key_type)) {
        grn_obj_unref(ctx, key_type);
        break;
      }

      grn_table_get_key(ctx,
                        caller_index_info->term_table,
                        caller_index_info->term_id,
                        &(caller_index_info->term_id),
                        sizeof(grn_id));
      grn_obj_unref(ctx, caller_index_info->term_table);
      caller_index_info->term_table = key_type;
    }
  }

  if (!grn_obj_is_text_family_bulk(ctx, index_column_name)) {
    grn_obj inspected;
    GRN_TEXT_INIT(&inspected, 0);
    grn_inspect(ctx, &inspected, index_column_name);
    GRN_PLUGIN_ERROR(ctx,
                     GRN_INVALID_ARGUMENT,
                     "%s: "
                     "the first argument must be index column name: %.*s",
                     error_message_label,
                     (int)GRN_TEXT_LEN(&inspected),
                     GRN_TEXT_VALUE(&inspected));
    GRN_OBJ_FIN(ctx, &inspected);
    caller_index_info_fin(ctx, caller_index_info);
    return ctx->rc;
  }

  caller_index_info->index_column =
    grn_obj_column(ctx,
                   caller_index_info->term_table,
                   GRN_TEXT_VALUE(index_column_name),
                   (uint32_t)GRN_TEXT_LEN(index_column_name));
  if (!caller_index_info->index_column) {
    GRN_PLUGIN_ERROR(ctx,
                     GRN_INVALID_ARGUMENT,
                     "%s: "
                     "nonexistent object: <%.*s>",
                     error_message_label,
                     (int)GRN_TEXT_LEN(index_column_name),
                     GRN_TEXT_VALUE(index_column_name));
    caller_index_info_fin(ctx, caller_index_info);
    return ctx->rc;
  }

  return GRN_SUCCESS;
}

static grn_obj *
func_index_column_df_ratio(grn_ctx *ctx,
                           int n_args,
                           grn_obj **args,
                           grn_user_data *user_data)
{
  grn_ii *ii;
  caller_index_info caller_index_info;

  if (n_args != 1) {
    GRN_PLUGIN_ERROR(ctx,
                     GRN_INVALID_ARGUMENT,
                     "index_column_df_ratio(): "
                     "wrong number of arguments (%d for 1)", n_args - 1);
    return NULL;
  }

  if (caller_index_info_init(ctx,
                             &caller_index_info,
                             args[0],
                             user_data,
                             "index_column_df_ratio()") != GRN_SUCCESS) {
    return NULL;
  }

  ii = (grn_ii *)caller_index_info.index_column;

  {
    grn_obj *source_table;
    unsigned int n_documents;
    uint32_t n_match_documents;
    double df_ratio;
    grn_obj *df_ratio_value;

    source_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, caller_index_info.index_column));
    n_documents = grn_table_size(ctx, source_table);
    n_match_documents = grn_ii_estimate_size(ctx, ii, caller_index_info.term_id);
    if (n_match_documents > n_documents) {
      n_match_documents = n_documents;
    }
    df_ratio = (double)n_match_documents / (double)n_documents;
    grn_obj_unref(ctx, source_table);

    df_ratio_value = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_FLOAT, 0);
    if (df_ratio_value) {
      GRN_FLOAT_SET(ctx, df_ratio_value, df_ratio);
    }
    caller_index_info_fin(ctx, &caller_index_info);
    return df_ratio_value;
  }
}

static grn_obj *
func_index_column_source_records(grn_ctx *ctx,
                                 int n_args,
                                 grn_obj **args,
                                 grn_user_data *user_data)
{
  grn_ii *ii;
  caller_index_info caller_index_info;
  int64_t limit = -1;

  if (n_args < 1 || n_args > 2) {
    GRN_PLUGIN_ERROR(ctx,
                     GRN_INVALID_ARGUMENT,
                     "index_column_source_records(): "
                     "wrong number of arguments (%d for 1..2)", n_args - 1);
    return NULL;
  }

  if (caller_index_info_init(ctx,
                             &caller_index_info,
                             args[0],
                             user_data,
                             "index_column_source_records()") != GRN_SUCCESS) {
    return NULL;
  }

  if (n_args == 2) {
    grn_obj *options = args[1];

    switch (options->header.type) {
    case GRN_TABLE_HASH_KEY :
      {
        grn_hash_cursor *cursor;
        void *key;
        int key_size;
        cursor = grn_hash_cursor_open(ctx, (grn_hash *)options,
                                      NULL, 0, NULL, 0,
                                      0, -1, 0);
        if (!cursor) {
          GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE,
                           "index_column_source_records(): failed to open cursor for options");
          caller_index_info_fin(ctx, &caller_index_info);
          return NULL;
        }
        while (grn_hash_cursor_next(ctx, cursor) != GRN_ID_NIL) {
          void *value;
          grn_obj *proc_value;
          grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value);
          proc_value = value;

#define KEY_EQUAL(name)                                                 \
          (key_size == strlen(name) && memcmp(key, name, strlen(name)) == 0)

          if (KEY_EQUAL("limit")) {
            limit = grn_plugin_proc_get_value_int64(ctx,
                                                    proc_value,
                                                    limit,
                                                    "index_column_source_records()");
            if (ctx->rc != GRN_SUCCESS) {
              grn_hash_cursor_close(ctx, cursor);
              caller_index_info_fin(ctx, &caller_index_info);
              return NULL;
            }
          } else {
            GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                             "index_column_source_records(): unknown option name: <%.*s>",
                             key_size, (char *)key);
            grn_hash_cursor_close(ctx, cursor);
            caller_index_info_fin(ctx, &caller_index_info);
            return NULL;
          }
#undef KEY_EQUAL
        }
        grn_hash_cursor_close(ctx, cursor);
      }
      break;
    default :
      {
        grn_obj inspected;
        GRN_TEXT_INIT(&inspected, 0);
        grn_inspect(ctx, &inspected, options);
        GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                         "index_column_source_records(): "
                         "2nd argument must be object literal: <%.*s>",
                         (int)GRN_TEXT_LEN(&inspected),
                         GRN_TEXT_VALUE(&inspected));
        GRN_OBJ_FIN(ctx, &inspected);
        caller_index_info_fin(ctx, &caller_index_info);
        return NULL;
      }
    }
  }

  ii = (grn_ii *)caller_index_info.index_column;

  {
    grn_obj *records;

    records = grn_plugin_proc_alloc(ctx, user_data,
                                    grn_obj_get_range(ctx, caller_index_info.index_column),
                                    GRN_OBJ_VECTOR);
    if (!records) {
      caller_index_info_fin(ctx, &caller_index_info);
      return NULL;
    }

    {
      grn_ii_cursor *ii_cursor;
      grn_posting *posting;
      int64_t n_records = 0;
      ii_cursor = grn_ii_cursor_open(ctx,
                                     ii,
                                     caller_index_info.term_id,
                                     GRN_ID_NIL,
                                     GRN_ID_MAX,
                                     (int)grn_ii_get_n_elements(ctx, ii),
                                     0);
      if (ii_cursor) {
        while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) {
          if (limit > 0 && n_records >= limit) {
            break;
          }
          GRN_RECORD_PUT(ctx, records, posting->rid);
          n_records++;
        }
        grn_ii_cursor_close(ctx, ii_cursor);
      }
    }
    caller_index_info_fin(ctx, &caller_index_info);
    return records;
  }
}

static grn_obj *
func_index_column_have_source_record(grn_ctx *ctx,
                                     int n_args,
                                     grn_obj **args,
                                     grn_user_data *user_data)
{
  grn_ii *ii;
  caller_index_info caller_index_info;

  if (n_args != 1) {
    GRN_PLUGIN_ERROR(ctx,
                     GRN_INVALID_ARGUMENT,
                     "index_column_have_source_record(): "
                     "wrong number of arguments (%d for 1)", n_args - 1);
    return NULL;
  }

  grn_rc rc = caller_index_info_init(ctx,
                                     &caller_index_info,
                                     args[0],
                                     user_data,
                                     "index_column_have_source_record()");
  if (rc != GRN_SUCCESS) {
    return NULL;
  }

  ii = (grn_ii *)caller_index_info.index_column;
  grn_obj *have =
    grn_plugin_proc_alloc(ctx, user_data, GRN_DB_BOOL, 0);
  if (!have) {
    caller_index_info_fin(ctx, &caller_index_info);
    return NULL;
  }

  GRN_BOOL_SET(ctx, have, false);
  {
    grn_ii_cursor *ii_cursor;
    ii_cursor = grn_ii_cursor_open(ctx,
                                   ii,
                                   caller_index_info.term_id,
                                   GRN_ID_NIL,
                                   GRN_ID_MAX,
                                   (int)grn_ii_get_n_elements(ctx, ii),
                                   0);
    if (ii_cursor) {
      grn_posting *posting;
      while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) {
        GRN_BOOL_SET(ctx, have, true);
        true;
      }
      grn_ii_cursor_close(ctx, ii_cursor);
    }
  }
  caller_index_info_fin(ctx, &caller_index_info);
  return have;
}

grn_rc
GRN_PLUGIN_INIT(grn_ctx *ctx)
{
  return ctx->rc;
}

grn_rc
GRN_PLUGIN_REGISTER(grn_ctx *ctx)
{
  grn_obj *selector_proc;

  selector_proc = grn_proc_create(ctx, "index_column_df_ratio_between", -1,
                                  GRN_PROC_FUNCTION,
                                  NULL, NULL, NULL, 0, NULL);
  grn_proc_set_selector(ctx, selector_proc,
                        selector_index_column_df_ratio_between);
  grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_NOP);

  grn_proc_create(ctx, "index_column_df_ratio", -1,
                  GRN_PROC_FUNCTION,
                  func_index_column_df_ratio, NULL, NULL, 0, NULL);

  grn_proc_create(ctx, "index_column_source_records", -1,
                  GRN_PROC_FUNCTION,
                  func_index_column_source_records, NULL, NULL, 0, NULL);

  grn_proc_create(ctx, "index_column_have_source_record", -1,
                  GRN_PROC_FUNCTION,
                  func_index_column_have_source_record, NULL, NULL, 0, NULL);

  return ctx->rc;
}

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