| 1 | /* |
| 2 | Copyright (C) 2019-2025 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_proc.h" |
| 20 | |
| 21 | #include "../grn_ctx.h" |
| 22 | #include "../grn_db.h" |
| 23 | #include "../grn_index_column.h" |
| 24 | #include "../grn_str.h" |
| 25 | |
| 26 | #include <groonga/plugin.h> |
| 27 | |
| 28 | static const char *remains_column_name = "remains"; |
| 29 | static const char *missings_column_name = "missings"; |
| 30 | |
| 31 | typedef struct { |
| 32 | const char *tag; |
| 33 | int n_args; |
| 34 | grn_obj **args; |
| 35 | grn_user_data *user_data; |
| 36 | grn_raw_string table_name; |
| 37 | grn_obj *table; |
| 38 | grn_raw_string column_name; |
| 39 | grn_obj *column; |
| 40 | grn_index_column_diff_options options; |
| 41 | } index_column_data; |
| 42 | |
| 43 | static bool |
| 44 | index_column_data_init(grn_ctx *ctx, index_column_data *data) |
| 45 | { |
| 46 | grn_index_column_diff_options_init(ctx, &(data->options)); |
| 47 | |
| 48 | data->table_name.value = |
| 49 | grn_plugin_proc_get_var_string(ctx, |
| 50 | data->user_data, |
| 51 | "table", |
| 52 | -1, |
| 53 | &(data->table_name.length)); |
| 54 | data->column_name.value = |
| 55 | grn_plugin_proc_get_var_string(ctx, |
| 56 | data->user_data, |
| 57 | "name", |
| 58 | -1, |
| 59 | &(data->column_name.length)); |
| 60 | |
| 61 | data->table = |
| 62 | grn_ctx_get(ctx, data->table_name.value, (int)(data->table_name.length)); |
| 63 | if (!data->table) { |
| 64 | GRN_PLUGIN_ERROR(ctx, |
| 65 | GRN_INVALID_ARGUMENT, |
| 66 | "[index-column]%s table doesn't exist: <%.*s>", |
| 67 | data->tag, |
| 68 | (int)(data->table_name.length), |
| 69 | data->table_name.value); |
| 70 | return false; |
| 71 | } |
| 72 | if (!grn_obj_is_table_with_key(ctx, data->table)) { |
| 73 | GRN_PLUGIN_ERROR(ctx, |
| 74 | GRN_INVALID_ARGUMENT, |
| 75 | "[index-column]%s table must be lexicon: <%.*s>: %s", |
| 76 | data->tag, |
| 77 | (int)(data->table_name.length), |
| 78 | data->table_name.value, |
| 79 | grn_obj_type_to_string(data->table->header.type)); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | data->column = grn_obj_column(ctx, |
| 84 | data->table, |
| 85 | data->column_name.value, |
| 86 | (uint32_t)(data->column_name.length)); |
| 87 | if (!data->column) { |
| 88 | GRN_PLUGIN_ERROR(ctx, |
| 89 | GRN_INVALID_ARGUMENT, |
| 90 | "[index-column]%s column doesn't exist: <%.*s>: <%.*s>", |
| 91 | data->tag, |
| 92 | (int)(data->table_name.length), |
| 93 | data->table_name.value, |
| 94 | (int)(data->column_name.length), |
| 95 | data->column_name.value); |
| 96 | return false; |
| 97 | } |
| 98 | if (!grn_obj_is_index_column(ctx, data->column)) { |
| 99 | GRN_PLUGIN_ERROR(ctx, |
| 100 | GRN_INVALID_ARGUMENT, |
| 101 | "[index-column]%s column must be index column: " |
| 102 | "<%.*s>: <%.*s>: %s", |
| 103 | data->tag, |
| 104 | (int)(data->table_name.length), |
| 105 | data->table_name.value, |
| 106 | (int)(data->column_name.length), |
| 107 | data->column_name.value, |
| 108 | grn_obj_type_to_string(data->column->header.type)); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | { |
| 113 | grn_log_level default_value = |
| 114 | grn_index_column_diff_options_get_progress_log_level(ctx, |
| 115 | &(data->options)); |
| 116 | grn_index_column_diff_options_set_progress_log_level( |
| 117 | ctx, |
| 118 | &(data->options), |
| 119 | grn_plugin_proc_get_var_log_level(ctx, |
| 120 | data->user_data, |
| 121 | "progress_log_level", |
| 122 | -1, |
| 123 | default_value)); |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | index_column_data_fin(grn_ctx *ctx, index_column_data *data) |
| 131 | { |
| 132 | if (grn_obj_is_accessor(ctx, data->column)) { |
| 133 | grn_obj_close(ctx, data->column); |
| 134 | } |
| 135 | grn_index_column_diff_options_fin(ctx, &(data->options)); |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | index_column_diff_output_postings(grn_ctx *ctx, |
| 140 | grn_column_flags index_column_flags, |
| 141 | grn_obj *postings, |
| 142 | const char *name) |
| 143 | { |
| 144 | size_t i; |
| 145 | size_t n_elements = 1; |
| 146 | if (index_column_flags & GRN_OBJ_WITH_SECTION) { |
| 147 | n_elements++; |
| 148 | } |
| 149 | if (index_column_flags & GRN_OBJ_WITH_POSITION) { |
| 150 | n_elements++; |
| 151 | } |
| 152 | size_t n = GRN_UINT32_VECTOR_SIZE(postings); |
| 153 | grn_ctx_output_array_open(ctx, name, (int)(n / n_elements)); |
| 154 | for (i = 0; i < n; i += n_elements) { |
| 155 | grn_ctx_output_map_open(ctx, "posting", (int)n_elements); |
| 156 | { |
| 157 | size_t j = i; |
| 158 | grn_ctx_output_cstr(ctx, "record_id"); |
| 159 | grn_id record_id = GRN_UINT32_VALUE_AT(postings, j); |
| 160 | grn_ctx_output_uint32(ctx, record_id); |
| 161 | if (index_column_flags & GRN_OBJ_WITH_SECTION) { |
| 162 | j++; |
| 163 | grn_ctx_output_cstr(ctx, "section_id"); |
| 164 | grn_id section_id = GRN_UINT32_VALUE_AT(postings, j); |
| 165 | grn_ctx_output_uint32(ctx, section_id); |
| 166 | } |
| 167 | if (index_column_flags & GRN_OBJ_WITH_POSITION) { |
| 168 | j++; |
| 169 | grn_ctx_output_cstr(ctx, "position"); |
| 170 | uint32_t position = GRN_UINT32_VALUE_AT(postings, j); |
| 171 | grn_ctx_output_uint32(ctx, position); |
| 172 | } |
| 173 | } |
| 174 | grn_ctx_output_map_close(ctx); |
| 175 | } |
| 176 | grn_ctx_output_array_close(ctx); |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | index_column_diff_output(grn_ctx *ctx, |
| 181 | grn_obj *diff, |
| 182 | grn_obj *lexicon, |
| 183 | grn_column_flags index_column_flags) |
| 184 | { |
| 185 | grn_obj *remains_column = |
| 186 | grn_obj_column(ctx, |
| 187 | diff, |
| 188 | remains_column_name, |
| 189 | (uint32_t)strlen(remains_column_name)); |
| 190 | grn_obj *missings_column = |
| 191 | grn_obj_column(ctx, |
| 192 | diff, |
| 193 | missings_column_name, |
| 194 | (uint32_t)strlen(missings_column_name)); |
| 195 | grn_obj key; |
| 196 | GRN_OBJ_INIT(&key, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, lexicon->header.domain); |
| 197 | grn_obj remains; |
| 198 | GRN_UINT32_INIT(&remains, GRN_OBJ_VECTOR); |
| 199 | grn_obj missings; |
| 200 | GRN_UINT32_INIT(&missings, GRN_OBJ_VECTOR); |
| 201 | grn_ctx_output_array_open(ctx, "diffs", (int)grn_table_size(ctx, diff)); |
| 202 | { |
| 203 | GRN_TABLE_EACH_BEGIN(ctx, diff, cursor, id) |
| 204 | { |
| 205 | grn_ctx_output_map_open(ctx, "diff", 3); |
| 206 | { |
| 207 | grn_ctx_output_cstr(ctx, "token"); |
| 208 | grn_ctx_output_map_open(ctx, "token", 2); |
| 209 | { |
| 210 | grn_ctx_output_cstr(ctx, "id"); |
| 211 | void *token_id_buffer; |
| 212 | grn_table_cursor_get_key(ctx, cursor, &token_id_buffer); |
| 213 | grn_id token_id = *((grn_id *)token_id_buffer); |
| 214 | grn_ctx_output_uint32(ctx, token_id); |
| 215 | |
| 216 | grn_ctx_output_cstr(ctx, "value"); |
| 217 | char key_buffer[GRN_TABLE_MAX_KEY_SIZE]; |
| 218 | int key_size = grn_table_get_key(ctx, |
| 219 | lexicon, |
| 220 | token_id, |
| 221 | key_buffer, |
| 222 | sizeof(key_buffer)); |
| 223 | GRN_TEXT_SET(ctx, &key, key_buffer, key_size); |
| 224 | grn_ctx_output_obj(ctx, &key, NULL); |
| 225 | } |
| 226 | grn_ctx_output_map_close(ctx); |
| 227 | |
| 228 | grn_ctx_output_cstr(ctx, "remains"); |
| 229 | GRN_BULK_REWIND(&remains); |
| 230 | grn_obj_get_value(ctx, remains_column, id, &remains); |
| 231 | index_column_diff_output_postings(ctx, |
| 232 | index_column_flags, |
| 233 | &remains, |
| 234 | "remains"); |
| 235 | |
| 236 | grn_ctx_output_cstr(ctx, "missings"); |
| 237 | GRN_BULK_REWIND(&missings); |
| 238 | grn_obj_get_value(ctx, missings_column, id, &missings); |
| 239 | index_column_diff_output_postings(ctx, |
| 240 | index_column_flags, |
| 241 | &missings, |
| 242 | "missings"); |
| 243 | } |
| 244 | grn_ctx_output_map_close(ctx); |
| 245 | } |
| 246 | GRN_TABLE_EACH_END(ctx, cursor); |
| 247 | } |
| 248 | grn_ctx_output_array_close(ctx); |
| 249 | GRN_OBJ_FIN(ctx, &missings); |
| 250 | GRN_OBJ_FIN(ctx, &remains); |
| 251 | GRN_OBJ_FIN(ctx, &key); |
| 252 | } |
| 253 | |
| 254 | static grn_obj * |
| 255 | command_index_column_diff(grn_ctx *ctx, |
| 256 | int n_args, |
| 257 | grn_obj **args, |
| 258 | grn_user_data *user_data) |
| 259 | { |
| 260 | index_column_data data; |
| 261 | grn_obj *diff = NULL; |
| 262 | |
| 263 | data.tag = "[diff]"; |
| 264 | data.n_args = n_args; |
| 265 | data.args = args; |
| 266 | data.user_data = user_data; |
| 267 | data.table = NULL; |
| 268 | data.column = NULL; |
| 269 | if (!index_column_data_init(ctx, &data)) { |
| 270 | goto exit; |
| 271 | } |
| 272 | |
| 273 | grn_index_column_diff_full(ctx, data.column, &(data.options), &diff); |
| 274 | if (ctx->rc != GRN_SUCCESS) { |
| 275 | GRN_PLUGIN_ERROR(ctx, |
| 276 | ctx->rc, |
| 277 | "[index-column]%s failed to diff: " |
| 278 | "<%.*s>: <%.*s>: %s", |
| 279 | data.tag, |
| 280 | (int)(data.table_name.length), |
| 281 | data.table_name.value, |
| 282 | (int)(data.column_name.length), |
| 283 | data.column_name.value, |
| 284 | ctx->errbuf); |
| 285 | goto exit; |
| 286 | } |
| 287 | |
| 288 | index_column_diff_output(ctx, |
| 289 | diff, |
| 290 | data.table, |
| 291 | grn_column_get_flags(ctx, data.column)); |
| 292 | |
| 293 | exit: |
| 294 | index_column_data_fin(ctx, &data); |
| 295 | |
| 296 | if (diff) { |
| 297 | grn_obj_close(ctx, diff); |
| 298 | } |
| 299 | |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | grn_proc_init_index_column_diff(grn_ctx *ctx) |
| 305 | { |
| 306 | grn_expr_var vars[3]; |
| 307 | unsigned int n_vars = 0; |
| 308 | |
| 309 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "table", -1); |
| 310 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "name", -1); |
| 311 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "progress_log_level", -1); |
| 312 | grn_plugin_command_create(ctx, |
| 313 | "index_column_diff", |
| 314 | -1, |
| 315 | command_index_column_diff, |
| 316 | n_vars, |
| 317 | vars); |
| 318 | } |