| 1 | /* |
| 2 | Copyright(C) 2015-2018 Brazil |
| 3 | Copyright(C) 2020-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 <string.h> |
| 21 | |
| 22 | #include "grn.h" |
| 23 | #include "grn_db.h" |
| 24 | #include "grn_expr.h" |
| 25 | #include "grn_scorer.h" |
| 26 | #include <groonga/scorer.h> |
| 27 | |
| 28 | grn_obj * |
| 29 | grn_scorer_matched_record_get_table(grn_ctx *ctx, |
| 30 | grn_scorer_matched_record *record) |
| 31 | { |
| 32 | return record->table; |
| 33 | } |
| 34 | |
| 35 | grn_obj * |
| 36 | grn_scorer_matched_record_get_lexicon(grn_ctx *ctx, |
| 37 | grn_scorer_matched_record *record) |
| 38 | { |
| 39 | return record->lexicon; |
| 40 | } |
| 41 | |
| 42 | grn_id |
| 43 | grn_scorer_matched_record_get_id(grn_ctx *ctx, |
| 44 | grn_scorer_matched_record *record) |
| 45 | { |
| 46 | return record->id; |
| 47 | } |
| 48 | |
| 49 | grn_obj * |
| 50 | grn_scorer_matched_record_get_terms(grn_ctx *ctx, |
| 51 | grn_scorer_matched_record *record) |
| 52 | { |
| 53 | return &(record->terms); |
| 54 | } |
| 55 | |
| 56 | grn_obj * |
| 57 | grn_scorer_matched_record_get_term_weights(grn_ctx *ctx, |
| 58 | grn_scorer_matched_record *record) |
| 59 | { |
| 60 | return &(record->term_weights); |
| 61 | } |
| 62 | |
| 63 | uint32_t |
| 64 | grn_scorer_matched_record_get_total_term_weights(grn_ctx *ctx, |
| 65 | grn_scorer_matched_record *record) |
| 66 | { |
| 67 | return record->total_term_weights; |
| 68 | } |
| 69 | |
| 70 | uint64_t |
| 71 | grn_scorer_matched_record_get_n_documents(grn_ctx *ctx, |
| 72 | grn_scorer_matched_record *record) |
| 73 | { |
| 74 | return record->n_documents; |
| 75 | } |
| 76 | |
| 77 | uint32_t |
| 78 | grn_scorer_matched_record_get_n_occurrences(grn_ctx *ctx, |
| 79 | grn_scorer_matched_record *record) |
| 80 | { |
| 81 | return record->n_occurrences; |
| 82 | } |
| 83 | |
| 84 | uint64_t |
| 85 | grn_scorer_matched_record_get_n_candidates(grn_ctx *ctx, |
| 86 | grn_scorer_matched_record *record) |
| 87 | { |
| 88 | return record->n_candidates; |
| 89 | } |
| 90 | |
| 91 | uint32_t |
| 92 | grn_scorer_matched_record_get_n_tokens(grn_ctx *ctx, |
| 93 | grn_scorer_matched_record *record) |
| 94 | { |
| 95 | return record->n_tokens; |
| 96 | } |
| 97 | |
| 98 | int |
| 99 | grn_scorer_matched_record_get_weight(grn_ctx *ctx, |
| 100 | grn_scorer_matched_record *record) |
| 101 | { |
| 102 | return record->weight; |
| 103 | } |
| 104 | |
| 105 | grn_obj * |
| 106 | grn_scorer_matched_record_get_arg(grn_ctx *ctx, |
| 107 | grn_scorer_matched_record *record, |
| 108 | unsigned int i) |
| 109 | { |
| 110 | grn_expr *expr; |
| 111 | grn_expr_code *codes_original; |
| 112 | uint32_t codes_curr_original; |
| 113 | grn_obj *arg; |
| 114 | |
| 115 | if (!record->args_expr) { |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | expr = (grn_expr *)(record->args_expr); |
| 120 | /* TODO: support getting column value */ |
| 121 | codes_original = expr->codes; |
| 122 | codes_curr_original = expr->codes_curr; |
| 123 | expr->codes += record->args_expr_offset; |
| 124 | expr->codes_curr = 1; /* TODO: support 1 or more codes */ |
| 125 | arg = grn_expr_exec(ctx, (grn_obj *)expr, 0); |
| 126 | expr->codes_curr = codes_curr_original; |
| 127 | expr->codes = codes_original; |
| 128 | |
| 129 | return arg; |
| 130 | } |
| 131 | |
| 132 | unsigned int |
| 133 | grn_scorer_matched_record_get_n_args(grn_ctx *ctx, |
| 134 | grn_scorer_matched_record *record) |
| 135 | { |
| 136 | grn_expr *expr; |
| 137 | grn_expr_code *codes; |
| 138 | unsigned int n_args = 0; |
| 139 | |
| 140 | if (!record->args_expr) { |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | expr = (grn_expr *)(record->args_expr); |
| 145 | codes = expr->codes + record->args_expr_offset; |
| 146 | if (codes[0].op == GRN_OP_CALL) { |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | n_args++; |
| 151 | for (; codes[0].op != GRN_OP_CALL; codes++) { |
| 152 | if (codes[0].op == GRN_OP_COMMA) { |
| 153 | n_args++; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return n_args; |
| 158 | } |
| 159 | |
| 160 | grn_rc |
| 161 | grn_scorer_register(grn_ctx *ctx, |
| 162 | const char *scorer_name_ptr, |
| 163 | int scorer_name_length, |
| 164 | grn_scorer_score_func *score) |
| 165 | { |
| 166 | if (scorer_name_length == -1) { |
| 167 | scorer_name_length = (int)strlen(scorer_name_ptr); |
| 168 | } |
| 169 | |
| 170 | { |
| 171 | grn_obj *scorer_object = grn_proc_create(ctx, |
| 172 | scorer_name_ptr, |
| 173 | scorer_name_length, |
| 174 | GRN_PROC_SCORER, |
| 175 | NULL, NULL, NULL, 0, NULL); |
| 176 | if (scorer_object == NULL) { |
| 177 | GRN_PLUGIN_ERROR(ctx, GRN_SCORER_ERROR, |
| 178 | "[scorer][%.*s] failed to grn_proc_create()", |
| 179 | scorer_name_length, scorer_name_ptr); |
| 180 | return ctx->rc; |
| 181 | } |
| 182 | |
| 183 | { |
| 184 | grn_proc *scorer = (grn_proc *)scorer_object; |
| 185 | scorer->callbacks.scorer.score = score; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return GRN_SUCCESS; |
| 190 | } |