| 1 | /* |
| 2 | Copyright (C) 2018-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_token_metadata.h" |
| 20 | |
| 21 | void |
| 22 | grn_token_metadata_init(grn_ctx *ctx, |
| 23 | grn_obj *metadata) |
| 24 | { |
| 25 | GRN_TEXT_INIT(metadata, GRN_OBJ_VECTOR); |
| 26 | } |
| 27 | |
| 28 | void |
| 29 | grn_token_metadata_fin(grn_ctx *ctx, |
| 30 | grn_obj *metadata) |
| 31 | { |
| 32 | GRN_OBJ_FIN(ctx, metadata); |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | grn_token_metadata_reset(grn_ctx *ctx, |
| 37 | grn_obj *metadata) |
| 38 | { |
| 39 | GRN_BULK_REWIND(metadata); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | grn_token_metadata_copy(grn_ctx *ctx, |
| 44 | grn_obj *metadata, |
| 45 | grn_obj *source) |
| 46 | { |
| 47 | uint32_t n = grn_vector_size(ctx, source); |
| 48 | uint32_t i; |
| 49 | for (i = 0; i < n; i++) { |
| 50 | const char *value; |
| 51 | unsigned int value_length; |
| 52 | grn_id domain; |
| 53 | value_length = grn_vector_get_element(ctx, source, i, &value, NULL, &domain); |
| 54 | grn_vector_add_element(ctx, |
| 55 | metadata, |
| 56 | value, |
| 57 | value_length, |
| 58 | 0, |
| 59 | domain); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | size_t |
| 64 | grn_token_metadata_get_size(grn_ctx *ctx, |
| 65 | grn_obj *metadata) |
| 66 | { |
| 67 | size_t size; |
| 68 | GRN_API_ENTER; |
| 69 | if (!metadata) { |
| 70 | ERR(GRN_INVALID_ARGUMENT, |
| 71 | "[token][metadata][get][size] token metadata must not be NULL"); |
| 72 | GRN_API_RETURN(0); |
| 73 | } |
| 74 | size = grn_vector_size(ctx, metadata) / 2; |
| 75 | GRN_API_RETURN(size); |
| 76 | } |
| 77 | |
| 78 | grn_rc |
| 79 | grn_token_metadata_at(grn_ctx *ctx, |
| 80 | grn_obj *metadata, |
| 81 | size_t i, |
| 82 | grn_obj *name, |
| 83 | grn_obj *value) |
| 84 | { |
| 85 | size_t n; |
| 86 | const char *raw_name; |
| 87 | unsigned int raw_name_length; |
| 88 | grn_id name_domain; |
| 89 | const char *raw_value; |
| 90 | unsigned int raw_value_length; |
| 91 | grn_id value_domain; |
| 92 | |
| 93 | GRN_API_ENTER; |
| 94 | if (!metadata) { |
| 95 | ERR(GRN_INVALID_ARGUMENT, |
| 96 | "[token][metadata][at] token metadata must not be NULL"); |
| 97 | GRN_API_RETURN(ctx->rc); |
| 98 | } |
| 99 | |
| 100 | n = grn_vector_size(ctx, metadata) / 2; |
| 101 | if (i >= n) { |
| 102 | GRN_BULK_REWIND(name); |
| 103 | GRN_BULK_REWIND(value); |
| 104 | GRN_API_RETURN(GRN_SUCCESS); |
| 105 | } |
| 106 | |
| 107 | raw_name_length = grn_vector_get_element(ctx, |
| 108 | metadata, |
| 109 | (uint32_t)(i * 2), |
| 110 | &raw_name, |
| 111 | NULL, |
| 112 | &name_domain); |
| 113 | grn_obj_reinit(ctx, name, name_domain, 0); |
| 114 | grn_bulk_write(ctx, name, raw_name, raw_name_length); |
| 115 | |
| 116 | raw_value_length = grn_vector_get_element(ctx, |
| 117 | metadata, |
| 118 | (uint32_t)(i * 2 + 1), |
| 119 | &raw_value, |
| 120 | NULL, |
| 121 | &value_domain); |
| 122 | grn_obj_reinit(ctx, value, value_domain, 0); |
| 123 | grn_bulk_write(ctx, value, raw_value, raw_value_length); |
| 124 | |
| 125 | GRN_API_RETURN(GRN_SUCCESS); |
| 126 | } |
| 127 | |
| 128 | grn_rc |
| 129 | grn_token_metadata_get(grn_ctx *ctx, |
| 130 | grn_obj *metadata, |
| 131 | const char *name, |
| 132 | int name_length, |
| 133 | grn_obj *value) |
| 134 | { |
| 135 | size_t i; |
| 136 | size_t n; |
| 137 | |
| 138 | GRN_API_ENTER; |
| 139 | if (!metadata) { |
| 140 | ERR(GRN_INVALID_ARGUMENT, |
| 141 | "[token][metadata][get] token metadata must not be NULL"); |
| 142 | GRN_API_RETURN(ctx->rc); |
| 143 | } |
| 144 | |
| 145 | if (name_length < 0) { |
| 146 | name_length = (int)strlen(name); |
| 147 | } |
| 148 | |
| 149 | n = grn_vector_size(ctx, metadata) / 2; |
| 150 | for (i = 0; i < n; i++) { |
| 151 | const char *current_name; |
| 152 | unsigned int current_name_length; |
| 153 | |
| 154 | current_name_length = grn_vector_get_element(ctx, |
| 155 | metadata, |
| 156 | (uint32_t)(i * 2), |
| 157 | ¤t_name, |
| 158 | NULL, |
| 159 | NULL); |
| 160 | if ((unsigned int)name_length == current_name_length && |
| 161 | memcmp(name, current_name, (size_t)name_length) == 0) { |
| 162 | const char *raw_value; |
| 163 | unsigned int raw_value_length; |
| 164 | grn_id domain; |
| 165 | |
| 166 | raw_value_length = grn_vector_get_element(ctx, |
| 167 | metadata, |
| 168 | (uint32_t)(i * 2 + 1), |
| 169 | &raw_value, |
| 170 | NULL, |
| 171 | &domain); |
| 172 | grn_obj_reinit(ctx, value, domain, 0); |
| 173 | grn_bulk_write(ctx, value, raw_value, raw_value_length); |
| 174 | GRN_API_RETURN(GRN_SUCCESS); |
| 175 | } |
| 176 | } |
| 177 | GRN_BULK_REWIND(value); |
| 178 | |
| 179 | GRN_API_RETURN(GRN_SUCCESS); |
| 180 | } |
| 181 | |
| 182 | grn_rc |
| 183 | grn_token_metadata_add(grn_ctx *ctx, |
| 184 | grn_obj *metadata, |
| 185 | const char *name, |
| 186 | int name_length, |
| 187 | grn_obj *value) |
| 188 | { |
| 189 | GRN_API_ENTER; |
| 190 | if (!metadata) { |
| 191 | ERR(GRN_INVALID_ARGUMENT, |
| 192 | "[token][metadata][add] token metadata must not be NULL"); |
| 193 | GRN_API_RETURN(ctx->rc); |
| 194 | } |
| 195 | |
| 196 | if (name_length < 0) { |
| 197 | name_length = (int)strlen(name); |
| 198 | } |
| 199 | |
| 200 | grn_vector_add_element(ctx, |
| 201 | metadata, |
| 202 | name, |
| 203 | (uint32_t)name_length, |
| 204 | 0, |
| 205 | GRN_DB_TEXT); |
| 206 | if (ctx->rc == GRN_SUCCESS) { |
| 207 | grn_vector_add_element(ctx, |
| 208 | metadata, |
| 209 | GRN_BULK_HEAD(value), |
| 210 | (uint32_t)GRN_BULK_VSIZE(value), |
| 211 | 0, |
| 212 | value->header.domain); |
| 213 | } |
| 214 | |
| 215 | GRN_API_RETURN(ctx->rc); |
| 216 | } |