| 1 | /* |
| 2 | Copyright (C) 2018 Brazil |
| 3 | Copyright (C) 2020-2023 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.h" |
| 21 | #include "grn_ctx.h" |
| 22 | #include "grn_float.h" |
| 23 | #include "grn_io.h" |
| 24 | #include "grn_vector.h" |
| 25 | |
| 26 | #include <groonga/bulk.hpp> |
| 27 | |
| 28 | #include <cstring> |
| 29 | |
| 30 | namespace grn { |
| 31 | namespace { |
| 32 | template <typename NUMERIC, typename FLOAT> |
| 33 | NUMERIC |
| 34 | cast_value(const char *raw_value) |
| 35 | { |
| 36 | return static_cast<NUMERIC>(*reinterpret_cast<const FLOAT *>(raw_value)); |
| 37 | } |
| 38 | |
| 39 | template <> |
| 40 | bool |
| 41 | cast_value<bool, float>(const char *raw_value) |
| 42 | { |
| 43 | float value = *reinterpret_cast<const float *>(raw_value); |
| 44 | return grn_float32_is_zero(value); |
| 45 | } |
| 46 | |
| 47 | template <> |
| 48 | bool |
| 49 | cast_value<bool, double>(const char *raw_value) |
| 50 | { |
| 51 | double value = *reinterpret_cast<const double *>(raw_value); |
| 52 | return grn_float_is_zero(value); |
| 53 | } |
| 54 | } // namespace |
| 55 | |
| 56 | template <typename NUMERIC> |
| 57 | NUMERIC |
| 58 | vector_get_element(grn_ctx *ctx, |
| 59 | grn_obj *vector, |
| 60 | uint32_t offset, |
| 61 | NUMERIC default_value) |
| 62 | { |
| 63 | const char *raw_value = NULL; |
| 64 | grn_id domain; |
| 65 | uint32_t length; |
| 66 | NUMERIC value = default_value; |
| 67 | |
| 68 | GRN_API_ENTER; |
| 69 | |
| 70 | length = |
| 71 | grn_vector_get_element(ctx, vector, offset, &raw_value, NULL, &domain); |
| 72 | if (length > 0) { |
| 73 | switch (domain) { |
| 74 | case GRN_DB_BOOL: |
| 75 | value = *reinterpret_cast<const bool *>(raw_value); |
| 76 | break; |
| 77 | case GRN_DB_INT8: |
| 78 | value = |
| 79 | static_cast<NUMERIC>(*reinterpret_cast<const int8_t *>(raw_value)); |
| 80 | break; |
| 81 | case GRN_DB_UINT8: |
| 82 | value = |
| 83 | static_cast<NUMERIC>(*reinterpret_cast<const uint8_t *>(raw_value)); |
| 84 | break; |
| 85 | case GRN_DB_INT16: |
| 86 | value = |
| 87 | static_cast<NUMERIC>(*reinterpret_cast<const int16_t *>(raw_value)); |
| 88 | break; |
| 89 | case GRN_DB_UINT16: |
| 90 | value = |
| 91 | static_cast<NUMERIC>(*reinterpret_cast<const uint16_t *>(raw_value)); |
| 92 | break; |
| 93 | case GRN_DB_INT32: |
| 94 | value = |
| 95 | static_cast<NUMERIC>(*reinterpret_cast<const int32_t *>(raw_value)); |
| 96 | break; |
| 97 | case GRN_DB_UINT32: |
| 98 | value = |
| 99 | static_cast<NUMERIC>(*reinterpret_cast<const uint32_t *>(raw_value)); |
| 100 | break; |
| 101 | case GRN_DB_INT64: |
| 102 | value = |
| 103 | static_cast<NUMERIC>(*reinterpret_cast<const int64_t *>(raw_value)); |
| 104 | break; |
| 105 | case GRN_DB_UINT64: |
| 106 | value = |
| 107 | static_cast<NUMERIC>(*reinterpret_cast<const uint64_t *>(raw_value)); |
| 108 | break; |
| 109 | case GRN_DB_FLOAT32: |
| 110 | value = cast_value<NUMERIC, float>(raw_value); |
| 111 | break; |
| 112 | case GRN_DB_FLOAT: |
| 113 | value = cast_value<NUMERIC, double>(raw_value); |
| 114 | break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | GRN_API_RETURN(value); |
| 121 | } |
| 122 | } // namespace grn |
| 123 | |
| 124 | extern "C" { |
| 125 | bool |
| 126 | grn_vector_get_element_bool(grn_ctx *ctx, |
| 127 | grn_obj *vector, |
| 128 | uint32_t offset, |
| 129 | bool default_value) |
| 130 | { |
| 131 | return grn::vector_get_element<bool>(ctx, vector, offset, default_value); |
| 132 | } |
| 133 | |
| 134 | int8_t |
| 135 | grn_vector_get_element_int8(grn_ctx *ctx, |
| 136 | grn_obj *vector, |
| 137 | uint32_t offset, |
| 138 | int8_t default_value) |
| 139 | { |
| 140 | return grn::vector_get_element<int8_t>(ctx, vector, offset, default_value); |
| 141 | } |
| 142 | |
| 143 | uint8_t |
| 144 | grn_vector_get_element_uint8(grn_ctx *ctx, |
| 145 | grn_obj *vector, |
| 146 | uint32_t offset, |
| 147 | uint8_t default_value) |
| 148 | { |
| 149 | return grn::vector_get_element<uint8_t>(ctx, vector, offset, default_value); |
| 150 | } |
| 151 | |
| 152 | int16_t |
| 153 | grn_vector_get_element_int16(grn_ctx *ctx, |
| 154 | grn_obj *vector, |
| 155 | uint32_t offset, |
| 156 | int16_t default_value) |
| 157 | { |
| 158 | return grn::vector_get_element<int16_t>(ctx, vector, offset, default_value); |
| 159 | } |
| 160 | |
| 161 | uint16_t |
| 162 | grn_vector_get_element_uint16(grn_ctx *ctx, |
| 163 | grn_obj *vector, |
| 164 | uint32_t offset, |
| 165 | uint16_t default_value) |
| 166 | { |
| 167 | return grn::vector_get_element<uint16_t>(ctx, vector, offset, default_value); |
| 168 | } |
| 169 | |
| 170 | int32_t |
| 171 | grn_vector_get_element_int32(grn_ctx *ctx, |
| 172 | grn_obj *vector, |
| 173 | uint32_t offset, |
| 174 | int32_t default_value) |
| 175 | { |
| 176 | return grn::vector_get_element<int32_t>(ctx, vector, offset, default_value); |
| 177 | } |
| 178 | |
| 179 | uint32_t |
| 180 | grn_vector_get_element_uint32(grn_ctx *ctx, |
| 181 | grn_obj *vector, |
| 182 | uint32_t offset, |
| 183 | uint32_t default_value) |
| 184 | { |
| 185 | return grn::vector_get_element<uint32_t>(ctx, vector, offset, default_value); |
| 186 | } |
| 187 | |
| 188 | int64_t |
| 189 | grn_vector_get_element_int64(grn_ctx *ctx, |
| 190 | grn_obj *vector, |
| 191 | uint32_t offset, |
| 192 | int64_t default_value) |
| 193 | { |
| 194 | return grn::vector_get_element<int64_t>(ctx, vector, offset, default_value); |
| 195 | } |
| 196 | |
| 197 | uint64_t |
| 198 | grn_vector_get_element_uint64(grn_ctx *ctx, |
| 199 | grn_obj *vector, |
| 200 | uint32_t offset, |
| 201 | uint64_t default_value) |
| 202 | { |
| 203 | return grn::vector_get_element<uint64_t>(ctx, vector, offset, default_value); |
| 204 | } |
| 205 | |
| 206 | float |
| 207 | grn_vector_get_element_float32(grn_ctx *ctx, |
| 208 | grn_obj *vector, |
| 209 | uint32_t offset, |
| 210 | float default_value) |
| 211 | { |
| 212 | return grn::vector_get_element<float>(ctx, vector, offset, default_value); |
| 213 | } |
| 214 | |
| 215 | double |
| 216 | grn_vector_get_element_float64(grn_ctx *ctx, |
| 217 | grn_obj *vector, |
| 218 | uint32_t offset, |
| 219 | double default_value) |
| 220 | { |
| 221 | return grn::vector_get_element<double>(ctx, vector, offset, default_value); |
| 222 | } |
| 223 | |
| 224 | static uint32_t |
| 225 | grn_uvector_element_size_internal(grn_ctx *ctx, grn_obj *uvector) |
| 226 | { |
| 227 | size_t element_size = grn_type_id_size(ctx, uvector->header.domain); |
| 228 | if (grn_obj_is_weight_uvector(ctx, uvector)) { |
| 229 | #ifdef GRN_HAVE_BFLOAT16 |
| 230 | if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) { |
| 231 | element_size += sizeof(grn_bfloat16); |
| 232 | } else { |
| 233 | element_size += sizeof(float); |
| 234 | } |
| 235 | #else |
| 236 | element_size += sizeof(float); |
| 237 | #endif |
| 238 | } |
| 239 | return static_cast<uint32_t>(element_size); |
| 240 | } |
| 241 | |
| 242 | static uint32_t |
| 243 | grn_uvector_size_internal(grn_ctx *ctx, grn_obj *uvector) |
| 244 | { |
| 245 | uint32_t element_size = grn_uvector_element_size_internal(ctx, uvector); |
| 246 | return static_cast<uint32_t>(GRN_BULK_VSIZE(uvector) / element_size); |
| 247 | } |
| 248 | |
| 249 | uint32_t |
| 250 | grn_vector_size(grn_ctx *ctx, grn_obj *vector) |
| 251 | { |
| 252 | uint32_t size; |
| 253 | if (!vector) { |
| 254 | ERR(GRN_INVALID_ARGUMENT, "[vector][size] vector is null"); |
| 255 | return 0; |
| 256 | } |
| 257 | GRN_API_ENTER; |
| 258 | switch (vector->header.type) { |
| 259 | case GRN_BULK: |
| 260 | size = static_cast<uint32_t>(GRN_BULK_VSIZE(vector)); |
| 261 | break; |
| 262 | case GRN_UVECTOR: |
| 263 | size = grn_uvector_size_internal(ctx, vector); |
| 264 | break; |
| 265 | case GRN_PVECTOR: |
| 266 | size = static_cast<uint32_t>(GRN_BULK_VSIZE(vector) / sizeof(grn_obj *)); |
| 267 | break; |
| 268 | case GRN_VECTOR: |
| 269 | size = vector->u.v.n_sections; |
| 270 | break; |
| 271 | default: |
| 272 | { |
| 273 | grn_obj inspected; |
| 274 | GRN_TEXT_INIT(&inspected, 0); |
| 275 | grn_inspect(ctx, &inspected, vector); |
| 276 | ERR(GRN_INVALID_ARGUMENT, |
| 277 | "[vector][size] not vector: %.*s", |
| 278 | (int)GRN_TEXT_LEN(&inspected), |
| 279 | GRN_TEXT_VALUE(&inspected)); |
| 280 | GRN_OBJ_FIN(ctx, &inspected); |
| 281 | } |
| 282 | size = 0; |
| 283 | break; |
| 284 | } |
| 285 | GRN_API_RETURN(size); |
| 286 | } |
| 287 | |
| 288 | grn_obj * |
| 289 | grn_vector_body(grn_ctx *ctx, grn_obj *v) |
| 290 | { |
| 291 | if (!v) { |
| 292 | ERR(GRN_INVALID_ARGUMENT, "invalid argument"); |
| 293 | return NULL; |
| 294 | } |
| 295 | switch (v->header.type) { |
| 296 | case GRN_VECTOR: |
| 297 | if (!v->u.v.body) { |
| 298 | v->u.v.body = grn_obj_open(ctx, GRN_BULK, 0, v->header.domain); |
| 299 | } |
| 300 | return v->u.v.body; |
| 301 | case GRN_BULK: |
| 302 | case GRN_UVECTOR: |
| 303 | return v; |
| 304 | default: |
| 305 | return NULL; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | uint32_t |
| 310 | grn_vector_get_element(grn_ctx *ctx, |
| 311 | grn_obj *vector, |
| 312 | uint32_t offset, |
| 313 | const char **str, |
| 314 | uint32_t *weight, |
| 315 | grn_id *domain) |
| 316 | { |
| 317 | float weight_float; |
| 318 | uint32_t length = grn_vector_get_element_float(ctx, |
| 319 | vector, |
| 320 | offset, |
| 321 | str, |
| 322 | &weight_float, |
| 323 | domain); |
| 324 | if (weight) { |
| 325 | *weight = static_cast<uint32_t>(weight_float); |
| 326 | } |
| 327 | return length; |
| 328 | } |
| 329 | |
| 330 | uint32_t |
| 331 | grn_vector_get_element_float(grn_ctx *ctx, |
| 332 | grn_obj *vector, |
| 333 | uint32_t offset, |
| 334 | const char **str, |
| 335 | float *weight, |
| 336 | grn_id *domain) |
| 337 | { |
| 338 | uint32_t length = 0; |
| 339 | GRN_API_ENTER; |
| 340 | if (!vector || vector->header.type != GRN_VECTOR) { |
| 341 | ERR(GRN_INVALID_ARGUMENT, "invalid vector"); |
| 342 | goto exit; |
| 343 | } |
| 344 | if (vector->u.v.n_sections <= offset) { |
| 345 | ERR(GRN_RANGE_ERROR, "offset out of range"); |
| 346 | goto exit; |
| 347 | } |
| 348 | { |
| 349 | grn_section *vp = &vector->u.v.sections[offset]; |
| 350 | grn_obj *body = grn_vector_body(ctx, vector); |
| 351 | *str = GRN_BULK_HEAD(body) + vp->offset; |
| 352 | if (weight) { |
| 353 | *weight = vp->weight; |
| 354 | } |
| 355 | if (domain) { |
| 356 | *domain = vp->domain; |
| 357 | } |
| 358 | length = vp->length; |
| 359 | } |
| 360 | exit: |
| 361 | GRN_API_RETURN(length); |
| 362 | } |
| 363 | |
| 364 | uint32_t |
| 365 | grn_vector_pop_element(grn_ctx *ctx, |
| 366 | grn_obj *vector, |
| 367 | const char **str, |
| 368 | uint32_t *weight, |
| 369 | grn_id *domain) |
| 370 | { |
| 371 | float weight_float; |
| 372 | uint32_t length = |
| 373 | grn_vector_pop_element_float(ctx, vector, str, &weight_float, domain); |
| 374 | if (weight) { |
| 375 | *weight = static_cast<uint32_t>(weight_float); |
| 376 | } |
| 377 | return length; |
| 378 | } |
| 379 | |
| 380 | uint32_t |
| 381 | grn_vector_pop_element_float(grn_ctx *ctx, |
| 382 | grn_obj *vector, |
| 383 | const char **str, |
| 384 | float *weight, |
| 385 | grn_id *domain) |
| 386 | { |
| 387 | uint32_t offset, length = 0; |
| 388 | GRN_API_ENTER; |
| 389 | if (!vector || vector->header.type != GRN_VECTOR) { |
| 390 | ERR(GRN_INVALID_ARGUMENT, "invalid vector"); |
| 391 | goto exit; |
| 392 | } |
| 393 | if (!vector->u.v.n_sections) { |
| 394 | ERR(GRN_RANGE_ERROR, "offset out of range"); |
| 395 | goto exit; |
| 396 | } |
| 397 | offset = --vector->u.v.n_sections; |
| 398 | { |
| 399 | grn_section *vp = &vector->u.v.sections[offset]; |
| 400 | grn_obj *body = grn_vector_body(ctx, vector); |
| 401 | *str = GRN_BULK_HEAD(body) + vp->offset; |
| 402 | if (weight) { |
| 403 | *weight = vp->weight; |
| 404 | } |
| 405 | if (domain) { |
| 406 | *domain = vp->domain; |
| 407 | } |
| 408 | length = vp->length; |
| 409 | grn_bulk_truncate(ctx, body, vp->offset); |
| 410 | } |
| 411 | exit: |
| 412 | GRN_API_RETURN(length); |
| 413 | } |
| 414 | |
| 415 | #define W_SECTIONS_UNIT 8 |
| 416 | #define S_SECTIONS_UNIT (1 << W_SECTIONS_UNIT) |
| 417 | #define M_SECTIONS_UNIT (S_SECTIONS_UNIT - 1) |
| 418 | |
| 419 | grn_rc |
| 420 | grn_vector_delimit(grn_ctx *ctx, grn_obj *v, float weight, grn_id domain) |
| 421 | { |
| 422 | if (v->header.type != GRN_VECTOR) { |
| 423 | return GRN_INVALID_ARGUMENT; |
| 424 | } |
| 425 | if (!(v->u.v.n_sections & M_SECTIONS_UNIT)) { |
| 426 | size_t size = sizeof(grn_section) * (v->u.v.n_sections + S_SECTIONS_UNIT); |
| 427 | grn_section *vp = |
| 428 | static_cast<grn_section *>(GRN_REALLOC(v->u.v.sections, size)); |
| 429 | if (!vp) { |
| 430 | return GRN_NO_MEMORY_AVAILABLE; |
| 431 | } |
| 432 | v->u.v.sections = vp; |
| 433 | } |
| 434 | { |
| 435 | grn_obj *body = grn_vector_body(ctx, v); |
| 436 | grn_section *vp = &v->u.v.sections[v->u.v.n_sections]; |
| 437 | vp->offset = v->u.v.n_sections ? vp[-1].offset + vp[-1].length : 0; |
| 438 | vp->length = static_cast<uint32_t>(GRN_BULK_VSIZE(body) - vp->offset); |
| 439 | vp->weight = weight; |
| 440 | vp->domain = domain; |
| 441 | } |
| 442 | v->u.v.n_sections++; |
| 443 | return GRN_SUCCESS; |
| 444 | } |
| 445 | |
| 446 | grn_obj * |
| 447 | grn_vector_pack(grn_ctx *ctx, |
| 448 | grn_obj *vector, |
| 449 | uint32_t offset, |
| 450 | uint32_t n, |
| 451 | grn_vector_pack_flags flags, |
| 452 | grn_obj *header, |
| 453 | grn_obj *footer) |
| 454 | { |
| 455 | bool need_footer = false; |
| 456 | grn_text_benc(ctx, header, n); |
| 457 | for (uint32_t i = 0; i < n; ++i) { |
| 458 | grn_section *section = &(vector->u.v.sections[i + offset]); |
| 459 | grn_text_benc(ctx, header, section->length); |
| 460 | if (section->weight > 0 || section->domain != GRN_ID_NIL) { |
| 461 | need_footer = true; |
| 462 | } |
| 463 | } |
| 464 | if (need_footer) { |
| 465 | for (uint32_t i = 0; i < n; ++i) { |
| 466 | grn_section *section = &(vector->u.v.sections[i + offset]); |
| 467 | if (flags & GRN_VECTOR_PACK_WEIGHT_FLOAT32) { |
| 468 | GRN_FLOAT32_PUT(ctx, footer, section->weight); |
| 469 | } else if (flags & GRN_VECTOR_PACK_WEIGHT_BFLOAT16) { |
| 470 | #ifdef GRN_HAVE_BFLOAT16 |
| 471 | GRN_BFLOAT16_PUT(ctx, |
| 472 | footer, |
| 473 | grn::numeric::to_bfloat16(section->weight)); |
| 474 | #else |
| 475 | GRN_FLOAT32_PUT(ctx, footer, section->weight); |
| 476 | #endif |
| 477 | } else { |
| 478 | grn_text_benc(ctx, footer, static_cast<unsigned int>(section->weight)); |
| 479 | } |
| 480 | grn_text_benc(ctx, footer, section->domain); |
| 481 | } |
| 482 | } |
| 483 | return grn_vector_body(ctx, vector); |
| 484 | } |
| 485 | |
| 486 | grn_rc |
| 487 | grn_vector_unpack(grn_ctx *ctx, |
| 488 | grn_obj *vector, |
| 489 | const uint8_t *data, |
| 490 | uint32_t data_size, |
| 491 | grn_vector_pack_flags flags, |
| 492 | uint32_t *used_size) |
| 493 | { |
| 494 | auto start = data; |
| 495 | auto p = start; |
| 496 | auto pe = p + data_size; |
| 497 | uint32_t n0 = vector->u.v.n_sections; |
| 498 | uint32_t n; |
| 499 | GRN_B_DEC(n, p); |
| 500 | if (((n0 + M_SECTIONS_UNIT) >> W_SECTIONS_UNIT) != |
| 501 | ((n0 + n + M_SECTIONS_UNIT) >> W_SECTIONS_UNIT)) { |
| 502 | size_t size = |
| 503 | sizeof(grn_section) * ((n0 + n + M_SECTIONS_UNIT) & ~M_SECTIONS_UNIT); |
| 504 | grn_section *sections = |
| 505 | static_cast<grn_section *>(GRN_REALLOC(vector->u.v.sections, size)); |
| 506 | if (!sections) { |
| 507 | return GRN_NO_MEMORY_AVAILABLE; |
| 508 | } |
| 509 | vector->u.v.sections = sections; |
| 510 | } |
| 511 | { |
| 512 | grn_obj *body = grn_vector_body(ctx, vector); |
| 513 | uint32_t offset = static_cast<uint32_t>(GRN_BULK_VSIZE(body)); |
| 514 | uint32_t o = 0; |
| 515 | for (uint32_t i = 0; i < n; ++i) { |
| 516 | grn_section *section = vector->u.v.sections + n0 + i; |
| 517 | if (pe <= p) { |
| 518 | return GRN_INVALID_ARGUMENT; |
| 519 | } |
| 520 | uint32_t length; |
| 521 | GRN_B_DEC(length, p); |
| 522 | section->length = length; |
| 523 | section->offset = offset + o; |
| 524 | section->weight = 0; |
| 525 | section->domain = 0; |
| 526 | o += length; |
| 527 | } |
| 528 | if (pe < p + o) { |
| 529 | return GRN_INVALID_ARGUMENT; |
| 530 | } |
| 531 | grn_bulk_write(ctx, body, (char *)p, o); |
| 532 | p += o; |
| 533 | if (p < pe) { |
| 534 | for (uint32_t i = 0; i < n; ++i) { |
| 535 | grn_section *section = vector->u.v.sections + n0 + i; |
| 536 | if (pe <= p) { |
| 537 | return GRN_INVALID_ARGUMENT; |
| 538 | } |
| 539 | if (flags & GRN_VECTOR_PACK_WEIGHT_FLOAT32) { |
| 540 | grn_memcpy(&(section->weight), p, sizeof(float)); |
| 541 | p += sizeof(float); |
| 542 | } else if (flags & GRN_VECTOR_PACK_WEIGHT_BFLOAT16) { |
| 543 | #ifdef GRN_HAVE_BFLOAT16 |
| 544 | grn_bfloat16 weight_bfloat16; |
| 545 | grn_memcpy(&weight_bfloat16, p, sizeof(grn_bfloat16)); |
| 546 | p += sizeof(grn_bfloat16); |
| 547 | section->weight = grn_bfloat16_to_float32(weight_bfloat16); |
| 548 | #else |
| 549 | grn_memcpy(&(section->weight), p, sizeof(float)); |
| 550 | p += sizeof(float); |
| 551 | #endif |
| 552 | } else { |
| 553 | uint32_t weight; |
| 554 | GRN_B_DEC(weight, p); |
| 555 | section->weight = static_cast<float>(weight); |
| 556 | } |
| 557 | GRN_B_DEC(section->domain, p); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | vector->u.v.n_sections += n; |
| 562 | if (used_size) { |
| 563 | *used_size = p - start; |
| 564 | } |
| 565 | return GRN_SUCCESS; |
| 566 | } |
| 567 | |
| 568 | grn_rc |
| 569 | grn_vector_add_element(grn_ctx *ctx, |
| 570 | grn_obj *vector, |
| 571 | const char *str, |
| 572 | uint32_t str_len, |
| 573 | uint32_t weight, |
| 574 | grn_id domain) |
| 575 | { |
| 576 | return grn_vector_add_element_float(ctx, |
| 577 | vector, |
| 578 | str, |
| 579 | str_len, |
| 580 | static_cast<float>(weight), |
| 581 | domain); |
| 582 | } |
| 583 | |
| 584 | grn_rc |
| 585 | grn_vector_add_element_float(grn_ctx *ctx, |
| 586 | grn_obj *vector, |
| 587 | const char *str, |
| 588 | uint32_t str_len, |
| 589 | float weight, |
| 590 | grn_id domain) |
| 591 | { |
| 592 | grn_obj *body; |
| 593 | GRN_API_ENTER; |
| 594 | if (!vector) { |
| 595 | ERR(GRN_INVALID_ARGUMENT, "vector is null"); |
| 596 | goto exit; |
| 597 | } |
| 598 | if ((body = grn_vector_body(ctx, vector))) { |
| 599 | grn_bulk_write(ctx, body, str, str_len); |
| 600 | grn_vector_delimit(ctx, vector, weight, domain); |
| 601 | } |
| 602 | exit: |
| 603 | GRN_API_RETURN(ctx->rc); |
| 604 | } |
| 605 | |
| 606 | grn_rc |
| 607 | grn_vector_copy(grn_ctx *ctx, grn_obj *src, grn_obj *dest) |
| 608 | { |
| 609 | GRN_API_ENTER; |
| 610 | uint32_t n_elements = grn_vector_size(ctx, src); |
| 611 | uint32_t i; |
| 612 | for (i = 0; i < n_elements; i++) { |
| 613 | const char *content; |
| 614 | float weight; |
| 615 | grn_id domain; |
| 616 | uint32_t content_size = |
| 617 | grn_vector_get_element_float(ctx, src, i, &content, &weight, &domain); |
| 618 | grn_vector_add_element_float(ctx, |
| 619 | dest, |
| 620 | content, |
| 621 | content_size, |
| 622 | weight, |
| 623 | domain); |
| 624 | } |
| 625 | GRN_API_RETURN(ctx->rc); |
| 626 | } |
| 627 | |
| 628 | grn_obj * |
| 629 | grn_vector_join(grn_ctx *ctx, |
| 630 | grn_obj *vector, |
| 631 | const char *separator, |
| 632 | int separator_length, |
| 633 | grn_obj *destination) |
| 634 | { |
| 635 | GRN_API_ENTER; |
| 636 | if (!vector) { |
| 637 | ERR(GRN_INVALID_ARGUMENT, "[vector][join] vector is NULL"); |
| 638 | GRN_API_RETURN(NULL); |
| 639 | } |
| 640 | if (vector->header.type != GRN_VECTOR) { |
| 641 | grn_obj type_name; |
| 642 | GRN_TEXT_INIT(&type_name, 0); |
| 643 | grn_inspect_type(ctx, &type_name, vector->header.type); |
| 644 | ERR(GRN_INVALID_ARGUMENT, |
| 645 | "[vector][join] must be GRN_UVECTOR: %.*s", |
| 646 | (int)GRN_TEXT_LEN(&type_name), |
| 647 | GRN_TEXT_VALUE(&type_name)); |
| 648 | GRN_OBJ_FIN(ctx, &type_name); |
| 649 | GRN_API_RETURN(NULL); |
| 650 | } |
| 651 | if (separator_length < 0) { |
| 652 | separator_length = (int)strlen(separator); |
| 653 | } |
| 654 | if (!destination) { |
| 655 | destination = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_TEXT); |
| 656 | } |
| 657 | uint32_t n_elements = grn_vector_size(ctx, vector); |
| 658 | uint32_t i; |
| 659 | grn_obj element; |
| 660 | GRN_TEXT_INIT(&element, GRN_OBJ_DO_SHALLOW_COPY); |
| 661 | for (i = 0; i < n_elements; i++) { |
| 662 | if (i > 0 && separator_length > 0) { |
| 663 | GRN_TEXT_PUT(ctx, destination, separator, separator_length); |
| 664 | } |
| 665 | const char *content; |
| 666 | grn_id domain; |
| 667 | uint32_t content_size = |
| 668 | grn_vector_get_element_float(ctx, vector, i, &content, NULL, &domain); |
| 669 | GRN_TEXT_SET(ctx, &element, content, content_size); |
| 670 | element.header.domain = domain; |
| 671 | grn_obj_cast(ctx, &element, destination, false); |
| 672 | element.header.domain = GRN_DB_TEXT; |
| 673 | } |
| 674 | GRN_OBJ_FIN(ctx, &element); |
| 675 | GRN_API_RETURN(destination); |
| 676 | } |
| 677 | |
| 678 | uint32_t |
| 679 | grn_uvector_size(grn_ctx *ctx, grn_obj *uvector) |
| 680 | { |
| 681 | if (!uvector) { |
| 682 | ERR(GRN_INVALID_ARGUMENT, "uvector must not be NULL"); |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | if (uvector->header.type != GRN_UVECTOR) { |
| 687 | grn_obj type_name; |
| 688 | GRN_TEXT_INIT(&type_name, 0); |
| 689 | grn_inspect_type(ctx, &type_name, uvector->header.type); |
| 690 | ERR(GRN_INVALID_ARGUMENT, |
| 691 | "must be GRN_UVECTOR: %.*s", |
| 692 | (int)GRN_TEXT_LEN(&type_name), |
| 693 | GRN_TEXT_VALUE(&type_name)); |
| 694 | GRN_OBJ_FIN(ctx, &type_name); |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | GRN_API_ENTER; |
| 699 | size_t size = grn_uvector_size_internal(ctx, uvector); |
| 700 | GRN_API_RETURN(static_cast<uint32_t>(size)); |
| 701 | } |
| 702 | |
| 703 | uint32_t |
| 704 | grn_uvector_element_size(grn_ctx *ctx, grn_obj *uvector) |
| 705 | { |
| 706 | if (!uvector) { |
| 707 | ERR(GRN_INVALID_ARGUMENT, "uvector must not be NULL"); |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | if (uvector->header.type != GRN_UVECTOR) { |
| 712 | grn_obj type_name; |
| 713 | GRN_TEXT_INIT(&type_name, 0); |
| 714 | grn_inspect_type(ctx, &type_name, uvector->header.type); |
| 715 | ERR(GRN_INVALID_ARGUMENT, |
| 716 | "must be GRN_UVECTOR: %.*s", |
| 717 | (int)GRN_TEXT_LEN(&type_name), |
| 718 | GRN_TEXT_VALUE(&type_name)); |
| 719 | GRN_OBJ_FIN(ctx, &type_name); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | GRN_API_ENTER; |
| 724 | uint32_t element_size = grn_uvector_element_size_internal(ctx, uvector); |
| 725 | GRN_API_RETURN(element_size); |
| 726 | } |
| 727 | |
| 728 | /* For reference uvector. We can't use this for integer family uvector |
| 729 | such as Int64 uvector. */ |
| 730 | grn_rc |
| 731 | grn_uvector_add_element(grn_ctx *ctx, |
| 732 | grn_obj *uvector, |
| 733 | grn_id id, |
| 734 | uint32_t weight) |
| 735 | { |
| 736 | return grn_uvector_add_element_record(ctx, |
| 737 | uvector, |
| 738 | id, |
| 739 | static_cast<float>(weight)); |
| 740 | } |
| 741 | |
| 742 | grn_rc |
| 743 | grn_uvector_add_element_record(grn_ctx *ctx, |
| 744 | grn_obj *uvector, |
| 745 | grn_id id, |
| 746 | float weight) |
| 747 | { |
| 748 | GRN_API_ENTER; |
| 749 | if (!uvector) { |
| 750 | ERR(GRN_INVALID_ARGUMENT, "[uvector][add-element][record] uvector is null"); |
| 751 | goto exit; |
| 752 | } |
| 753 | GRN_RECORD_PUT(ctx, uvector, id); |
| 754 | if (grn_obj_is_weight_uvector(ctx, uvector)) { |
| 755 | if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) { |
| 756 | #ifdef GRN_HAVE_BFLOAT16 |
| 757 | GRN_BFLOAT16_PUT(ctx, uvector, grn::numeric::to_bfloat16(weight)); |
| 758 | #else |
| 759 | GRN_FLOAT32_PUT(ctx, uvector, weight); |
| 760 | #endif |
| 761 | } else { |
| 762 | GRN_FLOAT32_PUT(ctx, uvector, weight); |
| 763 | } |
| 764 | } |
| 765 | exit: |
| 766 | GRN_API_RETURN(ctx->rc); |
| 767 | } |
| 768 | |
| 769 | /* For reference uvector. We can't use this for integer family uvector |
| 770 | such as Int64 uvector. */ |
| 771 | grn_id |
| 772 | grn_uvector_get_element(grn_ctx *ctx, |
| 773 | grn_obj *uvector, |
| 774 | uint32_t offset, |
| 775 | uint32_t *weight) |
| 776 | { |
| 777 | float weight_float; |
| 778 | grn_id id = |
| 779 | grn_uvector_get_element_record(ctx, uvector, offset, &weight_float); |
| 780 | if (weight) { |
| 781 | *weight = static_cast<uint32_t>(weight_float); |
| 782 | } |
| 783 | return id; |
| 784 | } |
| 785 | |
| 786 | grn_id |
| 787 | grn_uvector_get_element_record(grn_ctx *ctx, |
| 788 | grn_obj *uvector, |
| 789 | uint32_t offset, |
| 790 | float *weight) |
| 791 | { |
| 792 | grn_id id = GRN_ID_NIL; |
| 793 | |
| 794 | GRN_API_ENTER; |
| 795 | if (!uvector) { |
| 796 | ERR(GRN_INVALID_ARGUMENT, "[uvector][get-element][record] uvector is null"); |
| 797 | goto exit; |
| 798 | } |
| 799 | if (uvector->header.type != GRN_UVECTOR) { |
| 800 | ERR(GRN_INVALID_ARGUMENT, "[uvector][get-element][record] invalid uvector"); |
| 801 | goto exit; |
| 802 | } |
| 803 | |
| 804 | { |
| 805 | size_t element_value_size = sizeof(grn_id); |
| 806 | size_t element_size = element_value_size; |
| 807 | if (grn_obj_is_weight_uvector(ctx, uvector)) { |
| 808 | if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) { |
| 809 | #ifdef GRN_HAVE_BFLOAT16 |
| 810 | element_size += sizeof(grn_bfloat16); |
| 811 | #else |
| 812 | element_size += sizeof(float); |
| 813 | #endif |
| 814 | } else { |
| 815 | element_size += sizeof(float); |
| 816 | } |
| 817 | } |
| 818 | const char *elements_start = GRN_BULK_HEAD(uvector); |
| 819 | const char *elements_end = GRN_BULK_CURR(uvector); |
| 820 | if (offset > elements_end - elements_start) { |
| 821 | ERR(GRN_RANGE_ERROR, |
| 822 | "[uvector][get-element][record] " |
| 823 | "offset out of range: <%u>/<%" GRN_FMT_SIZE ">", |
| 824 | offset, |
| 825 | elements_end - elements_start); |
| 826 | goto exit; |
| 827 | } |
| 828 | |
| 829 | id = *((grn_id *)(elements_start + (element_size * offset))); |
| 830 | if (weight) { |
| 831 | if (grn_obj_is_weight_uvector(ctx, uvector)) { |
| 832 | if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) { |
| 833 | #ifdef GRN_HAVE_BFLOAT16 |
| 834 | grn_bfloat16 weight_bfloat16 = |
| 835 | *reinterpret_cast<const grn_bfloat16 *>( |
| 836 | elements_start + (element_size * offset) + element_value_size); |
| 837 | *weight = grn_bfloat16_to_float32(weight_bfloat16); |
| 838 | #else |
| 839 | *weight = *reinterpret_cast<const float *>( |
| 840 | elements_start + (element_size * offset) + element_value_size); |
| 841 | #endif |
| 842 | } else { |
| 843 | *weight = *reinterpret_cast<const float *>( |
| 844 | elements_start + (element_size * offset) + element_value_size); |
| 845 | } |
| 846 | } else { |
| 847 | *weight = 0.0; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | exit: |
| 852 | GRN_API_RETURN(id); |
| 853 | } |
| 854 | |
| 855 | grn_rc |
| 856 | grn_uvector_copy(grn_ctx *ctx, grn_obj *src, grn_obj *dest) |
| 857 | { |
| 858 | GRN_API_ENTER; |
| 859 | if (src->header.domain != dest->header.domain) { |
| 860 | ERR(GRN_INVALID_ARGUMENT, |
| 861 | "[uvector][copy] different domain: " |
| 862 | "source=<%u> " |
| 863 | "destination=<%u>", |
| 864 | src->header.domain, |
| 865 | dest->header.domain); |
| 866 | GRN_API_RETURN(ctx->rc); |
| 867 | } |
| 868 | bool src_have_weight = grn_obj_is_weight_uvector(ctx, src); |
| 869 | bool dest_have_weight = grn_obj_is_weight_uvector(ctx, dest); |
| 870 | if (src_have_weight != dest_have_weight) { |
| 871 | ERR(GRN_INVALID_ARGUMENT, |
| 872 | "[uvector][copy] different weight availability: " |
| 873 | "source=<%s> " |
| 874 | "destination=<%s>", |
| 875 | src_have_weight ? "true" : "false", |
| 876 | dest_have_weight ? "true" : "false"); |
| 877 | GRN_API_RETURN(ctx->rc); |
| 878 | } |
| 879 | grn_bulk_write(ctx, dest, GRN_BULK_HEAD(src), GRN_BULK_VSIZE(src)); |
| 880 | GRN_API_RETURN(ctx->rc); |
| 881 | } |
| 882 | |
| 883 | grn_obj * |
| 884 | grn_uvector_join(grn_ctx *ctx, |
| 885 | grn_obj *uvector, |
| 886 | const char *separator, |
| 887 | int separator_length, |
| 888 | grn_obj *destination) |
| 889 | { |
| 890 | GRN_API_ENTER; |
| 891 | if (!uvector) { |
| 892 | ERR(GRN_INVALID_ARGUMENT, "[uvector][join] uvector is NULL"); |
| 893 | GRN_API_RETURN(NULL); |
| 894 | } |
| 895 | if (uvector->header.type != GRN_UVECTOR) { |
| 896 | grn_obj type_name; |
| 897 | GRN_TEXT_INIT(&type_name, 0); |
| 898 | grn_inspect_type(ctx, &type_name, uvector->header.type); |
| 899 | ERR(GRN_INVALID_ARGUMENT, |
| 900 | "[uvector][join] must be GRN_UVECTOR: %.*s", |
| 901 | (int)GRN_TEXT_LEN(&type_name), |
| 902 | GRN_TEXT_VALUE(&type_name)); |
| 903 | GRN_OBJ_FIN(ctx, &type_name); |
| 904 | GRN_API_RETURN(NULL); |
| 905 | } |
| 906 | if (separator_length < 0) { |
| 907 | separator_length = (int)strlen(separator); |
| 908 | } |
| 909 | if (!destination) { |
| 910 | destination = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_TEXT); |
| 911 | } |
| 912 | uint32_t element_size = grn_uvector_element_size_internal(ctx, uvector); |
| 913 | uint32_t element_content_size = element_size; |
| 914 | if (grn_obj_is_weight_uvector(ctx, uvector)) { |
| 915 | if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) { |
| 916 | #ifdef GRN_HAVE_BFLOAT16 |
| 917 | element_content_size -= static_cast<uint32_t>(sizeof(grn_bfloat16)); |
| 918 | #else |
| 919 | element_content_size -= static_cast<uint32_t>(sizeof(float)); |
| 920 | #endif |
| 921 | } else { |
| 922 | element_content_size -= static_cast<uint32_t>(sizeof(float)); |
| 923 | } |
| 924 | } |
| 925 | uint32_t n_elements = grn_uvector_size_internal(ctx, uvector); |
| 926 | uint32_t i; |
| 927 | grn_obj element; |
| 928 | GRN_VALUE_FIX_SIZE_INIT(&element, |
| 929 | GRN_OBJ_DO_SHALLOW_COPY, |
| 930 | uvector->header.domain); |
| 931 | for (i = 0; i < n_elements; i++) { |
| 932 | if (i > 0 && separator_length > 0) { |
| 933 | GRN_TEXT_PUT(ctx, destination, separator, separator_length); |
| 934 | } |
| 935 | GRN_TEXT_SET(ctx, |
| 936 | &element, |
| 937 | GRN_BULK_HEAD(uvector) + (element_size * i), |
| 938 | element_content_size); |
| 939 | grn_obj_cast(ctx, &element, destination, false); |
| 940 | } |
| 941 | GRN_OBJ_FIN(ctx, &element); |
| 942 | GRN_API_RETURN(destination); |
| 943 | } |
| 944 | |
| 945 | grn_obj * |
| 946 | grn_pvector_join(grn_ctx *ctx, |
| 947 | grn_obj *pvector, |
| 948 | const char *separator, |
| 949 | int separator_length, |
| 950 | grn_obj *destination) |
| 951 | { |
| 952 | GRN_API_ENTER; |
| 953 | if (!pvector) { |
| 954 | ERR(GRN_INVALID_ARGUMENT, "[pvector][join] pvector is NULL"); |
| 955 | GRN_API_RETURN(NULL); |
| 956 | } |
| 957 | if (pvector->header.type != GRN_PVECTOR) { |
| 958 | grn_obj type_name; |
| 959 | GRN_TEXT_INIT(&type_name, 0); |
| 960 | grn_inspect_type(ctx, &type_name, pvector->header.type); |
| 961 | ERR(GRN_INVALID_ARGUMENT, |
| 962 | "[pvector][join] must be GRN_PVECTOR: %.*s", |
| 963 | (int)GRN_TEXT_LEN(&type_name), |
| 964 | GRN_TEXT_VALUE(&type_name)); |
| 965 | GRN_OBJ_FIN(ctx, &type_name); |
| 966 | GRN_API_RETURN(NULL); |
| 967 | } |
| 968 | if (separator_length < 0) { |
| 969 | separator_length = (int)strlen(separator); |
| 970 | } |
| 971 | if (!destination) { |
| 972 | destination = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_TEXT); |
| 973 | } |
| 974 | size_t n_elements = GRN_PTR_VECTOR_SIZE(pvector); |
| 975 | size_t i; |
| 976 | for (i = 0; i < n_elements; i++) { |
| 977 | if (i > 0 && separator_length > 0) { |
| 978 | GRN_TEXT_PUT(ctx, destination, separator, separator_length); |
| 979 | } |
| 980 | grn_obj *element = GRN_PTR_VALUE_AT(pvector, i); |
| 981 | grn_obj_cast(ctx, element, destination, false); |
| 982 | } |
| 983 | GRN_API_RETURN(destination); |
| 984 | } |
| 985 | } |