| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2025 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 | #pragma once |
| 21 | |
| 22 | #include "grn_ctx.h" |
| 23 | #include "grn_com.h" |
| 24 | #include "grn_options.h" |
| 25 | #include "grn_msgpack.h" |
| 26 | #include "grn_load.h" |
| 27 | #include "grn_arrow.h" |
| 28 | |
| 29 | #ifdef GRN_WITH_MRUBY |
| 30 | # include <mruby.h> |
| 31 | #endif |
| 32 | |
| 33 | #ifdef GRN_WITH_LUAJIT |
| 34 | # include <lauxlib.h> |
| 35 | # include <lualib.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | /**** grn_expr ****/ |
| 43 | |
| 44 | #define GRN_EXPR_MISSING_NAME "expr_missing" |
| 45 | |
| 46 | /**** grn_ctx_impl ****/ |
| 47 | |
| 48 | #define GRN_CTX_INITED 0x00 |
| 49 | #define GRN_CTX_QUITTING 0x0f |
| 50 | |
| 51 | #define GRN_CTX_N_SEGMENTS 512 |
| 52 | |
| 53 | #ifdef GRN_WITH_MEMORY_DEBUG |
| 54 | typedef struct _grn_alloc_info grn_alloc_info; |
| 55 | struct _grn_alloc_info { |
| 56 | void *address; |
| 57 | bool freed; |
| 58 | size_t size; |
| 59 | char alloc_backtrace[4096]; |
| 60 | char free_backtrace[4096]; |
| 61 | char *file; |
| 62 | int line; |
| 63 | char *func; |
| 64 | grn_alloc_info *next; |
| 65 | }; |
| 66 | #endif |
| 67 | |
| 68 | typedef struct _grn_mrb_data grn_mrb_data; |
| 69 | struct _grn_mrb_data { |
| 70 | bool initialized; |
| 71 | #ifdef GRN_WITH_MRUBY |
| 72 | mrb_state *state; |
| 73 | char base_directory[PATH_MAX]; |
| 74 | struct RClass *module; |
| 75 | struct RClass *object_class; |
| 76 | grn_hash *checked_procs; |
| 77 | grn_hash *registered_plugins; |
| 78 | struct { |
| 79 | grn_obj from; |
| 80 | grn_obj to; |
| 81 | } buffer; |
| 82 | struct { |
| 83 | struct RClass *time_class; |
| 84 | } builtin; |
| 85 | struct { |
| 86 | struct RClass *operator_class; |
| 87 | } groonga; |
| 88 | #endif |
| 89 | }; |
| 90 | |
| 91 | typedef struct _grn_lua_data grn_lua_data; |
| 92 | struct _grn_lua_data { |
| 93 | bool initialized; |
| 94 | #ifdef GRN_WITH_LUAJIT |
| 95 | lua_State *state; |
| 96 | #endif |
| 97 | }; |
| 98 | |
| 99 | struct _grn_ctx_impl { |
| 100 | grn_encoding encoding; |
| 101 | |
| 102 | /* memory pool portion */ |
| 103 | int32_t lifoseg; |
| 104 | int32_t currseg; |
| 105 | grn_critical_section lock; |
| 106 | grn_io_mapinfo segs[GRN_CTX_N_SEGMENTS]; |
| 107 | |
| 108 | #ifdef GRN_WITH_MEMORY_DEBUG |
| 109 | /* memory debug portion */ |
| 110 | grn_alloc_info *alloc_info; |
| 111 | #endif |
| 112 | |
| 113 | /* expression portion */ |
| 114 | grn_obj **stack; |
| 115 | uint32_t stack_curr; |
| 116 | uint32_t stack_size; |
| 117 | grn_hash *expr_vars; |
| 118 | grn_obj *curr_expr; |
| 119 | grn_obj current_request_id; |
| 120 | void *current_request_timer_id; |
| 121 | grn_obj expr_parsers; |
| 122 | grn_timeval tv; |
| 123 | grn_selector_data *current_selector_data; |
| 124 | |
| 125 | /* loader portion */ |
| 126 | grn_loader loader; |
| 127 | grn_arrow_stream_loader *arrow_stream_loader; |
| 128 | |
| 129 | /* plugin portion */ |
| 130 | const char *plugin_path; |
| 131 | |
| 132 | /* output portion */ |
| 133 | struct { |
| 134 | grn_obj *buf; |
| 135 | grn_recv_handler_func func; |
| 136 | union { |
| 137 | void *ptr; |
| 138 | int fd; |
| 139 | uint32_t u32; |
| 140 | uint64_t u64; |
| 141 | } data; |
| 142 | grn_content_type type; |
| 143 | const char *mime_type; |
| 144 | bool is_pretty; |
| 145 | grn_obj names; |
| 146 | grn_obj levels; |
| 147 | #ifdef GRN_WITH_MESSAGE_PACK |
| 148 | msgpack_packer msgpacker; |
| 149 | #endif |
| 150 | grn_arrow_stream_writer *arrow_stream_writer; |
| 151 | } output; |
| 152 | |
| 153 | struct { |
| 154 | int32_t n_workers; |
| 155 | void *task_executor; |
| 156 | } parallel; |
| 157 | |
| 158 | struct { |
| 159 | int flags; |
| 160 | grn_command_version version; |
| 161 | struct { |
| 162 | grn_obj *command; |
| 163 | grn_command_version version; |
| 164 | int32_t n_workers; |
| 165 | } keep; |
| 166 | } command; |
| 167 | |
| 168 | /* match escalation portion */ |
| 169 | int64_t match_escalation_threshold; |
| 170 | bool force_match_escalation; |
| 171 | |
| 172 | /* lifetime portion */ |
| 173 | grn_proc_func *finalizer; |
| 174 | |
| 175 | grn_obj *db; |
| 176 | grn_array *values; /* temporary objects */ |
| 177 | grn_pat *temporary_columns; |
| 178 | grn_options *temporary_options; |
| 179 | grn_critical_section columns_cache_lock; |
| 180 | grn_hash *columns_cache; |
| 181 | grn_hash *ios; /* IOs */ |
| 182 | grn_com *com; |
| 183 | unsigned int com_status; |
| 184 | |
| 185 | grn_obj query_log_buf; |
| 186 | |
| 187 | char previous_errbuf[GRN_CTX_MSGSIZE]; |
| 188 | unsigned int n_same_error_messages; |
| 189 | |
| 190 | struct { |
| 191 | grn_obj start_times; |
| 192 | } slow_log; |
| 193 | |
| 194 | grn_mrb_data mrb; |
| 195 | grn_lua_data lua; |
| 196 | |
| 197 | struct { |
| 198 | grn_obj stack; |
| 199 | grn_obj *current; |
| 200 | } temporary_open_spaces; |
| 201 | |
| 202 | grn_hash *variables; |
| 203 | |
| 204 | struct { |
| 205 | grn_wal_role role; |
| 206 | } wal; |
| 207 | |
| 208 | struct { |
| 209 | grn_critical_section lock; |
| 210 | grn_obj pool; |
| 211 | } children; |
| 212 | grn_ctx *parent; |
| 213 | grn_critical_section temporary_objects_lock; |
| 214 | |
| 215 | struct { |
| 216 | grn_progress_callback_func callback; |
| 217 | void *user_data; |
| 218 | } progress; |
| 219 | |
| 220 | struct { |
| 221 | uint64_t start_time; |
| 222 | uint16_t current_depth; |
| 223 | grn_obj depths; |
| 224 | grn_obj sequence_stack; |
| 225 | grn_obj sequences; |
| 226 | grn_obj names; |
| 227 | grn_obj values; |
| 228 | /* in nano seconds */ |
| 229 | grn_obj elapsed_times; |
| 230 | } trace_log; |
| 231 | }; |
| 232 | |
| 233 | #define GRN_CTX_GET_WAL_ROLE(ctx) ((ctx)->impl->wal.role) |
| 234 | |
| 235 | static inline bool |
| 236 | grn_ctx_trace_log_is_enabled(grn_ctx *ctx) |
| 237 | { |
| 238 | if (!ctx) { |
| 239 | return false; |
| 240 | } |
| 241 | if (!ctx->impl) { |
| 242 | return false; |
| 243 | } |
| 244 | return ctx->impl->trace_log.start_time != 0; |
| 245 | } |
| 246 | |
| 247 | void |
| 248 | grn_ctx_impl_columns_cache_delete(grn_ctx *ctx, grn_id table_id); |
| 249 | void |
| 250 | grn_ctx_impl_columns_cache_clear(grn_ctx *ctx); |
| 251 | |
| 252 | #ifdef __cplusplus |
| 253 | } |
| 254 | #endif |