| 1 | /* |
| 2 | Copyright (C) 2017 Brazil |
| 3 | Copyright (C) 2022 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_proc.h" |
| 21 | |
| 22 | #include <groonga/plugin.h> |
| 23 | |
| 24 | static grn_obj * |
| 25 | command_query_log_flags_get(grn_ctx *ctx, |
| 26 | int nargs, |
| 27 | grn_obj **args, |
| 28 | grn_user_data *user_data) |
| 29 | { |
| 30 | unsigned int current_flags; |
| 31 | grn_obj inspected_flags; |
| 32 | |
| 33 | current_flags = grn_query_logger_get_flags(ctx); |
| 34 | GRN_TEXT_INIT(&inspected_flags, 0); |
| 35 | |
| 36 | grn_inspect_query_log_flags(ctx, &inspected_flags, current_flags); |
| 37 | grn_ctx_output_str(ctx, |
| 38 | GRN_TEXT_VALUE(&inspected_flags), |
| 39 | GRN_TEXT_LEN(&inspected_flags)); |
| 40 | |
| 41 | GRN_OBJ_FIN(ctx, &inspected_flags); |
| 42 | |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | grn_proc_init_query_log_flags_get(grn_ctx *ctx) |
| 48 | { |
| 49 | grn_plugin_command_create(ctx, |
| 50 | "query_log_flags_get", |
| 51 | -1, |
| 52 | command_query_log_flags_get, |
| 53 | 0, |
| 54 | NULL); |
| 55 | } |
| 56 | |
| 57 | typedef enum { |
| 58 | UPDATE_SET, |
| 59 | UPDATE_ADD, |
| 60 | UPDATE_REMOVE |
| 61 | } grn_query_log_flags_update_mode; |
| 62 | |
| 63 | static void |
| 64 | grn_query_log_flags_update(grn_ctx *ctx, |
| 65 | grn_obj *flags_text, |
| 66 | grn_query_log_flags_update_mode mode, |
| 67 | const char *error_message_tag) |
| 68 | { |
| 69 | unsigned int previous_flags; |
| 70 | unsigned int flags = 0; |
| 71 | |
| 72 | previous_flags = grn_query_logger_get_flags(ctx); |
| 73 | if (GRN_TEXT_LEN(flags_text) == 0) { |
| 74 | GRN_PLUGIN_ERROR(ctx, |
| 75 | GRN_INVALID_ARGUMENT, |
| 76 | "%s no query log flags", |
| 77 | error_message_tag); |
| 78 | grn_ctx_output_null(ctx); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (!grn_query_log_flags_parse(GRN_TEXT_VALUE(flags_text), |
| 83 | (int)GRN_TEXT_LEN(flags_text), |
| 84 | &flags)) { |
| 85 | GRN_PLUGIN_ERROR(ctx, |
| 86 | GRN_INVALID_ARGUMENT, |
| 87 | "%s invalid query log flags: <%.*s>", |
| 88 | error_message_tag, |
| 89 | (int)GRN_TEXT_LEN(flags_text), |
| 90 | GRN_TEXT_VALUE(flags_text)); |
| 91 | grn_ctx_output_null(ctx); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | switch (mode) { |
| 96 | case UPDATE_SET: |
| 97 | grn_query_logger_set_flags(ctx, flags); |
| 98 | break; |
| 99 | case UPDATE_ADD: |
| 100 | grn_query_logger_add_flags(ctx, flags); |
| 101 | break; |
| 102 | case UPDATE_REMOVE: |
| 103 | grn_query_logger_remove_flags(ctx, flags); |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | { |
| 108 | unsigned int current_flags; |
| 109 | grn_obj inspected_flags; |
| 110 | |
| 111 | current_flags = grn_query_logger_get_flags(ctx); |
| 112 | GRN_TEXT_INIT(&inspected_flags, 0); |
| 113 | |
| 114 | grn_ctx_output_map_open(ctx, "query_log_flags", 2); |
| 115 | |
| 116 | grn_inspect_query_log_flags(ctx, &inspected_flags, previous_flags); |
| 117 | grn_ctx_output_cstr(ctx, "previous"); |
| 118 | grn_ctx_output_str(ctx, |
| 119 | GRN_TEXT_VALUE(&inspected_flags), |
| 120 | GRN_TEXT_LEN(&inspected_flags)); |
| 121 | |
| 122 | GRN_BULK_REWIND(&inspected_flags); |
| 123 | grn_inspect_query_log_flags(ctx, &inspected_flags, current_flags); |
| 124 | grn_ctx_output_cstr(ctx, "current"); |
| 125 | grn_ctx_output_str(ctx, |
| 126 | GRN_TEXT_VALUE(&inspected_flags), |
| 127 | GRN_TEXT_LEN(&inspected_flags)); |
| 128 | |
| 129 | grn_ctx_output_map_close(ctx); |
| 130 | |
| 131 | GRN_OBJ_FIN(ctx, &inspected_flags); |
| 132 | } |
| 133 | |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | static grn_obj * |
| 138 | command_query_log_flags_set(grn_ctx *ctx, |
| 139 | int nargs, |
| 140 | grn_obj **args, |
| 141 | grn_user_data *user_data) |
| 142 | { |
| 143 | grn_obj *flags_text; |
| 144 | |
| 145 | flags_text = grn_plugin_proc_get_var(ctx, user_data, "flags", -1); |
| 146 | grn_query_log_flags_update(ctx, |
| 147 | flags_text, |
| 148 | UPDATE_SET, |
| 149 | "[query-log][flags][set]"); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | grn_proc_init_query_log_flags_set(grn_ctx *ctx) |
| 155 | { |
| 156 | grn_expr_var vars[1]; |
| 157 | |
| 158 | grn_plugin_expr_var_init(ctx, &(vars[0]), "flags", -1); |
| 159 | grn_plugin_command_create(ctx, |
| 160 | "query_log_flags_set", |
| 161 | -1, |
| 162 | command_query_log_flags_set, |
| 163 | 1, |
| 164 | vars); |
| 165 | } |
| 166 | |
| 167 | static grn_obj * |
| 168 | command_query_log_flags_add(grn_ctx *ctx, |
| 169 | int nargs, |
| 170 | grn_obj **args, |
| 171 | grn_user_data *user_data) |
| 172 | { |
| 173 | grn_obj *flags_text; |
| 174 | |
| 175 | flags_text = grn_plugin_proc_get_var(ctx, user_data, "flags", -1); |
| 176 | grn_query_log_flags_update(ctx, |
| 177 | flags_text, |
| 178 | UPDATE_ADD, |
| 179 | "[query-log][flags][add]"); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | grn_proc_init_query_log_flags_add(grn_ctx *ctx) |
| 185 | { |
| 186 | grn_expr_var vars[1]; |
| 187 | |
| 188 | grn_plugin_expr_var_init(ctx, &(vars[0]), "flags", -1); |
| 189 | grn_plugin_command_create(ctx, |
| 190 | "query_log_flags_add", |
| 191 | -1, |
| 192 | command_query_log_flags_add, |
| 193 | 1, |
| 194 | vars); |
| 195 | } |
| 196 | |
| 197 | static grn_obj * |
| 198 | command_query_log_flags_remove(grn_ctx *ctx, |
| 199 | int nargs, |
| 200 | grn_obj **args, |
| 201 | grn_user_data *user_data) |
| 202 | { |
| 203 | grn_obj *flags_text; |
| 204 | |
| 205 | flags_text = grn_plugin_proc_get_var(ctx, user_data, "flags", -1); |
| 206 | grn_query_log_flags_update(ctx, |
| 207 | flags_text, |
| 208 | UPDATE_REMOVE, |
| 209 | "[query-log][flags][remove]"); |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | grn_proc_init_query_log_flags_remove(grn_ctx *ctx) |
| 215 | { |
| 216 | grn_expr_var vars[1]; |
| 217 | |
| 218 | grn_plugin_expr_var_init(ctx, &(vars[0]), "flags", -1); |
| 219 | grn_plugin_command_create(ctx, |
| 220 | "query_log_flags_remove", |
| 221 | -1, |
| 222 | command_query_log_flags_remove, |
| 223 | 1, |
| 224 | vars); |
| 225 | } |