Debian Codegraph @

lib/rset.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2015 Brazil
3 Copyright (C) 2020-2024 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 "grn_db.h"
21
22 inline static void
23 grn_rset_subrecs_push(byte *subrecs,
24 size_t size,
25 size_t n_subrecs,
26 double score,
27 void *body,
28 int dir)
29 {
30 byte *v;
31 double *c2;
32 size_t n = n_subrecs - 1;
33 size_t n2;
34 while (n > 0) {
35 n2 = (n - 1) >> 1;
36 c2 = GRN_RSET_SUBRECS_NTH(subrecs,size,n2);
37 if (GRN_RSET_SUBRECS_CMP(score, *c2, dir) >= 0) { break; }
38 GRN_RSET_SUBRECS_COPY(subrecs,size,n,c2);
39 n = n2;
40 }
41 v = subrecs + n * (GRN_RSET_SCORE_SIZE + size);
42 *((double *)v) = score;
43 grn_memcpy(v + GRN_RSET_SCORE_SIZE, body, size);
44 }
45
46 inline static void
47 grn_rset_subrecs_replace_min(byte *subrecs,
48 size_t size,
49 size_t n_subrecs,
50 double score,
51 void *body,
52 int dir)
53 {
54 byte *v;
55 size_t n = 0;
56 size_t n1;
57 size_t n2;
58 double *c1, *c2;
59 for (;;) {
60 n1 = n * 2 + 1;
61 n2 = n1 + 1;
62 c1 = n1 < n_subrecs ? GRN_RSET_SUBRECS_NTH(subrecs,size,n1) : NULL;
63 c2 = n2 < n_subrecs ? GRN_RSET_SUBRECS_NTH(subrecs,size,n2) : NULL;
64 if (c1 && GRN_RSET_SUBRECS_CMP(score, *c1, dir) > 0) {
65 if (c2 &&
66 GRN_RSET_SUBRECS_CMP(score, *c2, dir) > 0 &&
67 GRN_RSET_SUBRECS_CMP(*c1, *c2, dir) > 0) {
68 GRN_RSET_SUBRECS_COPY(subrecs,size,n,c2);
69 n = n2;
70 } else {
71 GRN_RSET_SUBRECS_COPY(subrecs,size,n,c1);
72 n = n1;
73 }
74 } else {
75 if (c2 && GRN_RSET_SUBRECS_CMP(score, *c2, dir) > 0) {
76 GRN_RSET_SUBRECS_COPY(subrecs,size,n,c2);
77 n = n2;
78 } else {
79 break;
80 }
81 }
82 }
83 v = subrecs + n * (GRN_RSET_SCORE_SIZE + size);
84 grn_memcpy(v, &score, GRN_RSET_SCORE_SIZE);
85 grn_memcpy(v + GRN_RSET_SCORE_SIZE, body, size);
86 }
87
88 void
89 grn_rset_add_subrec(grn_ctx *ctx,
90 grn_rset_recinfo *ri,
91 grn_obj *table,
92 double score,
93 grn_rset_posinfo *pi,
94 int dir)
95 {
96 uint32_t limit = DB_OBJ(table)->max_n_subrecs;
97 if (limit == 0) {
98 return;
99 }
100
101 if (!pi) {
102 return;
103 }
104
105 size_t subrec_size = DB_OBJ(table)->subrec_size;
106 size_t n_subrecs = GRN_RSET_N_SUBRECS(ri);
107 byte *body = (byte *)pi + DB_OBJ(table)->subrec_offset;
108 if (limit < n_subrecs) {
109 if (GRN_RSET_SUBRECS_CMP(score, *((double *)(ri->subrecs)), dir) > 0) {
110 grn_rset_subrecs_replace_min((byte *)ri->subrecs,
111 subrec_size,
112 limit,
113 score,
114 body,
115 dir);
116 }
117 } else {
118 grn_rset_subrecs_push((byte *)ri->subrecs,
119 subrec_size,
120 n_subrecs,
121 score,
122 body,
123 dir);
124 }
125 }
126
127 uint32_t
128 grn_rset_recinfo_calc_values_size(grn_ctx *ctx, grn_table_group_flags flags)
129 {
130 uint32_t size = 0;
131
132 if (flags & GRN_TABLE_GROUP_CALC_MAX) {
133 size += GRN_RSET_MAX_SIZE;
134 }
135 if (flags & GRN_TABLE_GROUP_CALC_MIN) {
136 size += GRN_RSET_MIN_SIZE;
137 }
138 if (flags & GRN_TABLE_GROUP_CALC_SUM) {
139 size += GRN_RSET_SUM_SIZE;
140 }
141 if (flags & GRN_TABLE_GROUP_CALC_MEAN) {
142 size += GRN_RSET_MEAN_SIZE;
143 }
144
145 return size;
146 }
147
148 typedef struct {
149 grn_rset_recinfo *ri;
150 byte *values;
151 grn_table_group_flags flags;
152 bool is_float_source;
153 grn_obj value_int64;
154 grn_obj value_float;
155 } grn_rset_recinfo_update_calc_values_data;
156
157 static void
158 grn_rset_recinfo_update_calc_values_bulk(
159 grn_ctx *ctx,
160 grn_rset_recinfo_update_calc_values_data *data,
161 grn_obj *value,
162 bool is_first_value)
163 {
164 grn_obj *value_int64 = &(data->value_int64);
165 grn_obj *value_float = &(data->value_float);
166 bool need_int64_value = false;
167 bool need_float_value = false;
168
169 if (data->flags & (GRN_TABLE_GROUP_CALC_MAX |
170 GRN_TABLE_GROUP_CALC_MIN |
171 GRN_TABLE_GROUP_CALC_SUM)) {
172 if (data->is_float_source) {
173 need_float_value = true;
174 } else {
175 need_int64_value = true;
176 }
177 }
178 if (data->flags & GRN_TABLE_GROUP_CALC_MEAN) {
179 need_float_value = true;
180 }
181 if (need_int64_value) {
182 GRN_BULK_REWIND(value_int64);
183 grn_obj_cast(ctx, value, value_int64, false);
184 }
185 if (need_float_value) {
186 GRN_BULK_REWIND(value_float);
187 grn_obj_cast(ctx, value, value_float, false);
188 }
189
190 byte *values = data->values;
191 if (data->flags & GRN_TABLE_GROUP_CALC_MAX) {
192 grn_rset_aggregated_value *current_max = (grn_rset_aggregated_value *)values;
193 if (data->is_float_source) {
194 double value_raw = GRN_FLOAT_VALUE(value_float);
195 if (is_first_value || value_raw > current_max->value_double) {
196 current_max->value_double = value_raw;
197 }
198 } else {
199 int64_t value_raw = GRN_INT64_VALUE(value_int64);
200 if (is_first_value || value_raw > current_max->value_int64) {
201 current_max->value_int64 = value_raw;
202 }
203 }
204 values += GRN_RSET_MAX_SIZE;
205 }
206 if (data->flags & GRN_TABLE_GROUP_CALC_MIN) {
207 grn_rset_aggregated_value *current_min = (grn_rset_aggregated_value *)values;
208 if (data->is_float_source) {
209 double value_raw = GRN_FLOAT_VALUE(value_float);
210 if (is_first_value || value_raw < current_min->value_double) {
211 current_min->value_double = value_raw;
212 }
213 } else {
214 int64_t value_raw = GRN_INT64_VALUE(value_int64);
215 if (is_first_value || value_raw < current_min->value_int64) {
216 current_min->value_int64 = value_raw;
217 }
218 }
219 values += GRN_RSET_MIN_SIZE;
220 }
221 if (data->flags & GRN_TABLE_GROUP_CALC_SUM) {
222 grn_rset_aggregated_value *current_sum = (grn_rset_aggregated_value *)values;
223 if (data->is_float_source) {
224 double value_raw = GRN_FLOAT_VALUE(value_float);
225 current_sum->value_double += value_raw;
226 } else {
227 int64_t value_raw = GRN_INT64_VALUE(value_int64);
228 current_sum->value_int64 += value_raw;
229 }
230 values += GRN_RSET_SUM_SIZE;
231 }
232 if (data->flags & GRN_TABLE_GROUP_CALC_MEAN) {
233 double *current_mean = (double *)values;
234 uint64_t *n_values = (uint64_t *)(((double *)values) + 1);
235 double value_raw = GRN_FLOAT_VALUE(value_float);
236 (*n_values)++;
237 *current_mean += (value_raw - *current_mean) / (double)(*n_values);
238 values += GRN_RSET_MEAN_SIZE;
239 }
240 }
241
242 static void
243 grn_rset_recinfo_update_calc_values_uvector(
244 grn_ctx *ctx,
245 grn_rset_recinfo_update_calc_values_data *data,
246 grn_obj *value)
247 {
248 grn_obj element_value;
249 unsigned int element_size;
250 size_t i, n_elements;
251
252 element_size = grn_uvector_element_size(ctx, value);
253 if (element_size == 0) {
254 return;
255 }
256
257 GRN_VALUE_FIX_SIZE_INIT(&element_value, 0, value->header.domain);
258 n_elements = grn_vector_size(ctx, value);
259 for (i = 0; i < n_elements; i++) {
260 GRN_BULK_REWIND(&element_value);
261 grn_bulk_write(ctx,
262 &element_value,
263 GRN_BULK_HEAD(value) + (element_size * i),
264 element_size);
265 const bool is_first_value = (data->ri->n_subrecs == 1 && i == 0);
266 grn_rset_recinfo_update_calc_values_bulk(ctx,
267 data,
268 &element_value,
269 is_first_value);
270 }
271 GRN_OBJ_FIN(ctx, &element_value);
272 }
273
274 void
275 grn_rset_recinfo_update_calc_values(grn_ctx *ctx,
276 grn_rset_recinfo *ri,
277 grn_obj *table,
278 grn_obj *value)
279 {
280 grn_rset_recinfo_update_calc_values_data data;
281
282 data.ri = ri;
283 data.values = (((byte *)ri->subrecs) +
284 GRN_RSET_SUBRECS_SIZE(DB_OBJ(table)->subrec_size,
285 DB_OBJ(table)->max_n_subrecs));
286 data.flags = DB_OBJ(table)->group.flags;
287 data.is_float_source =
288 (DB_OBJ(table)->group.aggregated_value_type_id == GRN_DB_FLOAT);
289 GRN_INT64_INIT(&(data.value_int64), 0);
290 GRN_FLOAT_INIT(&(data.value_float), 0);
291
292 switch (value->header.type) {
293 case GRN_BULK :
294 grn_rset_recinfo_update_calc_values_bulk(ctx,
295 &data,
296 value,
297 ri->n_subrecs == 1);
298 break;
299 case GRN_UVECTOR :
300 grn_rset_recinfo_update_calc_values_uvector(ctx, &data, value);
301 break;
302 default :
303 break;
304 }
305
306 GRN_OBJ_FIN(ctx, &(data.value_float));
307 GRN_OBJ_FIN(ctx, &(data.value_int64));
308 }
309
310 grn_rset_aggregated_value *
311 grn_rset_recinfo_get_max_(grn_ctx *ctx,
312 grn_rset_recinfo *ri,
313 grn_obj *table)
314 {
315 const grn_table_group_flags flags = DB_OBJ(table)->group.flags;
316 if (!(flags & GRN_TABLE_GROUP_CALC_MAX)) {
317 return NULL;
318 }
319
320 byte *values = (((byte *)ri->subrecs) +
321 GRN_RSET_SUBRECS_SIZE(DB_OBJ(table)->subrec_size,
322 DB_OBJ(table)->max_n_subrecs));
323 return (grn_rset_aggregated_value *)values;
324 }
325
326 grn_rset_aggregated_value
327 grn_rset_recinfo_get_max(grn_ctx *ctx,
328 grn_rset_recinfo *ri,
329 grn_obj *table)
330 {
331 grn_rset_aggregated_value *max_address;
332
333 max_address = grn_rset_recinfo_get_max_(ctx, ri, table);
334 if (max_address) {
335 return *max_address;
336 } else {
337 grn_rset_aggregated_value value = {0};
338 return value;
339 }
340 }
341
342 void
343 grn_rset_recinfo_set_max(grn_ctx *ctx,
344 grn_rset_recinfo *ri,
345 grn_obj *table,
346 grn_rset_aggregated_value max)
347 {
348 grn_rset_aggregated_value *max_address =
349 grn_rset_recinfo_get_max_(ctx, ri, table);
350 if (!max_address) {
351 return;
352 }
353 *max_address = max;
354 }
355
356 grn_rset_aggregated_value *
357 grn_rset_recinfo_get_min_(grn_ctx *ctx,
358 grn_rset_recinfo *ri,
359 grn_obj *table)
360 {
361 const grn_table_group_flags flags = DB_OBJ(table)->group.flags;
362 if (!(flags & GRN_TABLE_GROUP_CALC_MIN)) {
363 return NULL;
364 }
365
366 byte *values = (((byte *)ri->subrecs) +
367 GRN_RSET_SUBRECS_SIZE(DB_OBJ(table)->subrec_size,
368 DB_OBJ(table)->max_n_subrecs));
369
370 if (flags & GRN_TABLE_GROUP_CALC_MAX) {
371 values += GRN_RSET_MAX_SIZE;
372 }
373
374 return (grn_rset_aggregated_value *)values;
375 }
376
377 grn_rset_aggregated_value
378 grn_rset_recinfo_get_min(grn_ctx *ctx,
379 grn_rset_recinfo *ri,
380 grn_obj *table)
381 {
382 grn_rset_aggregated_value *min_address =
383 grn_rset_recinfo_get_min_(ctx, ri, table);
384 if (min_address) {
385 return *min_address;
386 } else {
387 grn_rset_aggregated_value value = {0};
388 return value;
389 }
390 }
391
392 void
393 grn_rset_recinfo_set_min(grn_ctx *ctx,
394 grn_rset_recinfo *ri,
395 grn_obj *table,
396 grn_rset_aggregated_value min)
397 {
398 grn_rset_aggregated_value *min_address =
399 grn_rset_recinfo_get_min_(ctx, ri, table);
400 if (!min_address) {
401 return;
402 }
403 *min_address = min;
404 }
405
406 grn_rset_aggregated_value *
407 grn_rset_recinfo_get_sum_(grn_ctx *ctx,
408 grn_rset_recinfo *ri,
409 grn_obj *table)
410 {
411 const grn_table_group_flags flags = DB_OBJ(table)->group.flags;
412 if (!(flags & GRN_TABLE_GROUP_CALC_SUM)) {
413 return NULL;
414 }
415
416 byte *values = (((byte *)ri->subrecs) +
417 GRN_RSET_SUBRECS_SIZE(DB_OBJ(table)->subrec_size,
418 DB_OBJ(table)->max_n_subrecs));
419
420 if (flags & GRN_TABLE_GROUP_CALC_MAX) {
421 values += GRN_RSET_MAX_SIZE;
422 }
423 if (flags & GRN_TABLE_GROUP_CALC_MIN) {
424 values += GRN_RSET_MIN_SIZE;
425 }
426
427 return (grn_rset_aggregated_value *)values;
428 }
429
430 grn_rset_aggregated_value
431 grn_rset_recinfo_get_sum(grn_ctx *ctx,
432 grn_rset_recinfo *ri,
433 grn_obj *table)
434 {
435 grn_rset_aggregated_value *sum_address =
436 grn_rset_recinfo_get_sum_(ctx, ri, table);
437 if (sum_address) {
438 return *sum_address;
439 } else {
440 grn_rset_aggregated_value value = {0};
441 return value;
442 }
443 }
444
445 void
446 grn_rset_recinfo_set_sum(grn_ctx *ctx,
447 grn_rset_recinfo *ri,
448 grn_obj *table,
449 grn_rset_aggregated_value sum)
450 {
451 grn_rset_aggregated_value *sum_address =
452 grn_rset_recinfo_get_sum_(ctx, ri, table);
453 if (!sum_address) {
454 return;
455 }
456 *sum_address = sum;
457 }
458
459 double *
460 grn_rset_recinfo_get_mean_(grn_ctx *ctx,
461 grn_rset_recinfo *ri,
462 grn_obj *table)
463 {
464 const grn_table_group_flags flags = DB_OBJ(table)->group.flags;
465 if (!(flags & GRN_TABLE_GROUP_CALC_MEAN)) {
466 return NULL;
467 }
468
469 byte *values = (((byte *)ri->subrecs) +
470 GRN_RSET_SUBRECS_SIZE(DB_OBJ(table)->subrec_size,
471 DB_OBJ(table)->max_n_subrecs));
472
473 if (flags & GRN_TABLE_GROUP_CALC_MAX) {
474 values += GRN_RSET_MAX_SIZE;
475 }
476 if (flags & GRN_TABLE_GROUP_CALC_MIN) {
477 values += GRN_RSET_MIN_SIZE;
478 }
479 if (flags & GRN_TABLE_GROUP_CALC_SUM) {
480 values += GRN_RSET_SUM_SIZE;
481 }
482
483 return (double *)values;
484 }
485
486 double
487 grn_rset_recinfo_get_mean(grn_ctx *ctx,
488 grn_rset_recinfo *ri,
489 grn_obj *table)
490 {
491 double *mean_address = grn_rset_recinfo_get_mean_(ctx, ri, table);
492 if (mean_address) {
493 return *mean_address;
494 } else {
495 return 0;
496 }
497 }
498
499 void
500 grn_rset_recinfo_set_mean(grn_ctx *ctx,
501 grn_rset_recinfo *ri,
502 grn_obj *table,
503 double mean)
504 {
505 double *mean_address = grn_rset_recinfo_get_mean_(ctx, ri, table);
506 if (!mean_address) {
507 return;
508 }
509 *mean_address = mean;
510 }