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