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