Debian Codegraph @

lib/proc/proc_escalate.c

raw symbols SCIP
1 /*
2 Copyright(C) 2022 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 #include "../grn_ctx.h"
20 #include "../grn_output.h"
21 #include "../grn_proc.h"
22
23 #include <groonga/plugin.h>
24
25 static grn_rc
26 selector_escalate(grn_ctx *ctx,
27 grn_obj *table,
28 grn_obj *index,
29 int n_args,
30 grn_obj **args,
31 grn_obj *result_set,
32 grn_operator op)
33 {
34 const char *tag = "[escalate]";
35
36 if (n_args < 2) {
37 ERR(GRN_INVALID_ARGUMENT,
38 "%s wrong number of arguments (%d for 1..)",
39 tag,
40 n_args - 1);
41 return ctx->rc;
42 }
43
44 if (((n_args - 1) % 2) != 1) {
45 ERR(GRN_INVALID_ARGUMENT,
46 "%s wrong number of arguments (%d for 1, 3, 5, ...)",
47 tag,
48 n_args - 1);
49 return ctx->rc;
50 }
51
52 grn_obj *sub_result_set =
53 grn_table_create(ctx,
54 NULL, 0,
55 NULL,
56 GRN_OBJ_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC,
57 table,
58 NULL);
59
60 int i;
61 for (i = 1; i < n_args; i += 2) {
62 grn_obj *condition_text = args[i];
63 if (!grn_obj_is_text_family_bulk(ctx, condition_text)) {
64 grn_obj inspected;
65 GRN_TEXT_INIT(&inspected, 0);
66 grn_inspect(ctx, &inspected, condition_text);
67 ERR(GRN_INVALID_ARGUMENT,
68 "%s the %dth argument must be condition as string: %.*s",
69 tag,
70 (i - 1),
71 (int)GRN_TEXT_LEN(&inspected),
72 GRN_TEXT_VALUE(&inspected));
73 GRN_OBJ_FIN(ctx, &inspected);
74 goto exit;
75 }
76
77 grn_obj *dummy_variable;
78 grn_obj *condition;
79 GRN_EXPR_CREATE_FOR_QUERY(ctx, table, condition, dummy_variable);
80 if (!condition) {
81 goto exit;
82 }
83
84 grn_expr_parse(ctx,
85 condition,
86 GRN_TEXT_VALUE(condition_text),
87 (unsigned int)GRN_TEXT_LEN(condition_text),
88 NULL,
89 GRN_OP_MATCH,
90 GRN_OP_AND,
91 GRN_EXPR_SYNTAX_SCRIPT);
92 if (ctx->rc != GRN_SUCCESS) {
93 grn_obj_close(ctx, condition);
94 goto exit;
95 }
96
97 grn_table_select(ctx, table, condition, sub_result_set, GRN_OP_OR);
98 grn_obj_close(ctx, condition);
99 if (ctx->rc != GRN_SUCCESS) {
100 goto exit;
101 }
102
103 if ((i + 1) == n_args) {
104 break;
105 }
106
107 grn_obj *threashold = args[i + 1];
108 if (!grn_obj_is_number_family_bulk(ctx, threashold)) {
109 grn_obj inspected;
110 GRN_TEXT_INIT(&inspected, 0);
111 grn_inspect(ctx, &inspected, threashold);
112 ERR(GRN_INVALID_ARGUMENT,
113 "%s the %dth argument must be threshold as number: %.*s",
114 tag,
115 (i - 1) + 1,
116 (int)GRN_TEXT_LEN(&inspected),
117 GRN_TEXT_VALUE(&inspected));
118 GRN_OBJ_FIN(ctx, &inspected);
119 goto exit;
120 }
121
122 grn_obj casted_threshold;
123 GRN_INT64_INIT(&casted_threshold, 0);
124 grn_obj_cast(ctx, threashold, &casted_threshold, false);
125 int64_t raw_threshold = GRN_INT64_VALUE(&casted_threshold);
126 GRN_OBJ_FIN(ctx, &casted_threshold);
127 bool need_to_evaluate_condition =
128 (raw_threshold < 0 ||
129 (int64_t)grn_table_size(ctx, sub_result_set) <= raw_threshold);
130 if (!need_to_evaluate_condition) {
131 break;
132 }
133 }
134
135 grn_table_setoperation(ctx,
136 result_set,
137 sub_result_set,
138 result_set,
139 op);
140
141 exit :
142 if (sub_result_set) {
143 grn_obj_close(ctx, sub_result_set);
144 }
145
146 return ctx->rc;
147 }
148
149 void
150 grn_proc_init_escalate(grn_ctx *ctx)
151 {
152 grn_obj *selector_proc;
153
154 selector_proc = grn_proc_create(ctx,
155 "escalate", -1,
156 GRN_PROC_FUNCTION,
157 NULL,
158 NULL,
159 NULL,
160 0,
161 NULL);
162 grn_proc_set_selector(ctx, selector_proc, selector_escalate);
163 grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_NOP);
164 }