| 1 | /* |
| 2 | Copyright (C) 2009-2016 Brazil |
| 3 | Copyright (C) 2020-2024 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.h" |
| 23 | #include "grn_error.h" |
| 24 | |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #ifdef HAVE_SIGNAL_H |
| 28 | # include <signal.h> |
| 29 | # define GRN_BREAK_POINT raise(SIGTRAP) |
| 30 | #endif /* HAVE_SIGNAL_H */ |
| 31 | |
| 32 | #include "grn_alloc.h" |
| 33 | #include "grn_time.h" |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | /**** api in/out ****/ |
| 40 | |
| 41 | #define GRN_API_ENTER \ |
| 42 | do { \ |
| 43 | if ((ctx)->seqno & 1) { \ |
| 44 | (ctx)->subno++; \ |
| 45 | } else { \ |
| 46 | (ctx)->errlvl = GRN_OK; \ |
| 47 | if ((ctx)->rc != GRN_CANCEL) { \ |
| 48 | (ctx)->rc = GRN_SUCCESS; \ |
| 49 | } \ |
| 50 | (ctx)->seqno++; \ |
| 51 | } \ |
| 52 | GRN_TEST_YIELD(); \ |
| 53 | } while (0) |
| 54 | |
| 55 | /* CAUTION!! : pass only variables or constants as r */ |
| 56 | #define GRN_API_RETURN(...) \ |
| 57 | do { \ |
| 58 | if (ctx->subno) { \ |
| 59 | ctx->subno--; \ |
| 60 | } else { \ |
| 61 | ctx->seqno++; \ |
| 62 | } \ |
| 63 | GRN_TEST_YIELD(); \ |
| 64 | return __VA_ARGS__; \ |
| 65 | } while (0) |
| 66 | |
| 67 | #ifdef DEBUG |
| 68 | # define GRN_ASSERT(s) grn_assert(ctx, (s), __FILE__, __LINE__, __FUNCTION__) |
| 69 | #else |
| 70 | # define GRN_ASSERT(s) |
| 71 | #endif |
| 72 | |
| 73 | void |
| 74 | grn_assert( |
| 75 | grn_ctx *ctx, int cond, const char *file, int line, const char *func); |
| 76 | |
| 77 | /**** grn_ctx ****/ |
| 78 | |
| 79 | GRN_VAR grn_ctx grn_gctx; |
| 80 | extern int grn_pagesize; |
| 81 | extern grn_critical_section grn_glock; |
| 82 | extern uint32_t grn_gtick; |
| 83 | extern int grn_lock_timeout; |
| 84 | |
| 85 | #define GRN_CTX_ALLOCATED (0x80) |
| 86 | #define GRN_CTX_TEMPORARY_DISABLE_II_RESOLVE_SEL_AND (0x40) |
| 87 | |
| 88 | extern grn_timeval grn_starttime; |
| 89 | |
| 90 | void |
| 91 | grn_ctx_loader_clear(grn_ctx *ctx); |
| 92 | void |
| 93 | grn_log_reopen(grn_ctx *ctx); |
| 94 | |
| 95 | GRN_API grn_rc |
| 96 | grn_ctx_sendv(grn_ctx *ctx, int argc, char **argv, int flags); |
| 97 | void |
| 98 | grn_ctx_set_keep_command(grn_ctx *ctx, grn_obj *command); |
| 99 | |
| 100 | grn_ctx * |
| 101 | grn_ctx_pull_child(grn_ctx *ctx); |
| 102 | grn_rc |
| 103 | grn_ctx_release_child(grn_ctx *ctx, grn_ctx *child_ctx); |
| 104 | |
| 105 | void * |
| 106 | grn_ctx_expr_parser_pull(grn_ctx *ctx); |
| 107 | grn_rc |
| 108 | grn_ctx_expr_parser_release(grn_ctx *ctx, void *parser); |
| 109 | grn_rc |
| 110 | grn_ctx_expr_parsers_clear(grn_ctx *ctx); |
| 111 | |
| 112 | void |
| 113 | grn_ctx_call_progress_callback(grn_ctx *ctx, grn_progress *progress); |
| 114 | |
| 115 | void |
| 116 | grn_ctx_trace_log_enable(grn_ctx *ctx); |
| 117 | void |
| 118 | grn_ctx_trace_log_disable(grn_ctx *ctx); |
| 119 | void |
| 120 | grn_ctx_trace_log_dump(grn_ctx *ctx, grn_ctx *dump_ctx, grn_obj *output); |
| 121 | size_t |
| 122 | grn_ctx_trace_log_restore(grn_ctx *ctx, |
| 123 | const uint8_t *value, |
| 124 | size_t value_size); |
| 125 | uint16_t |
| 126 | grn_ctx_trace_log_get_current_depth(grn_ctx *ctx); |
| 127 | void |
| 128 | grn_ctx_trace_log_set_current_depth(grn_ctx *ctx, uint16_t depth); |
| 129 | void |
| 130 | grn_ctx_trace_log_push(grn_ctx *ctx); |
| 131 | void |
| 132 | grn_ctx_trace_log_pop(grn_ctx *ctx); |
| 133 | void |
| 134 | grn_ctx_trace_log_emit_string(grn_ctx *ctx, |
| 135 | const char *name, |
| 136 | const char *value, |
| 137 | size_t value_length); |
| 138 | void |
| 139 | grn_ctx_trace_log_emit_cstring(grn_ctx *ctx, |
| 140 | const char *name, |
| 141 | const char *value); |
| 142 | void |
| 143 | grn_ctx_trace_log_emit_uint32(grn_ctx *ctx, const char *name, uint32_t n); |
| 144 | void |
| 145 | grn_ctx_trace_log_emit_record_key(grn_ctx *ctx, |
| 146 | const char *name, |
| 147 | grn_obj *table, |
| 148 | grn_id id); |
| 149 | void |
| 150 | grn_ctx_trace_log_emit_object(grn_ctx *ctx, const char *name, grn_obj *object); |
| 151 | |
| 152 | grn_content_type |
| 153 | grn_get_ctype(grn_obj *var); |
| 154 | grn_content_type |
| 155 | grn_content_type_parse(grn_ctx *ctx, |
| 156 | grn_obj *var, |
| 157 | grn_content_type default_value); |
| 158 | |
| 159 | /**** db_obj ****/ |
| 160 | |
| 161 | /* flag values used for grn_obj.header.impl_flags */ |
| 162 | |
| 163 | #define GRN_OBJ_ALLOCATED (0x01 << 2) /* allocated by ctx */ |
| 164 | #define GRN_OBJ_EXPRVALUE (0x01 << 3) /* value allocated by grn_expr */ |
| 165 | #define GRN_OBJ_EXPRCONST (0x01 << 4) /* constant allocated by grn_expr */ |
| 166 | |
| 167 | typedef struct _grn_hook grn_hook; |
| 168 | |
| 169 | typedef struct { |
| 170 | grn_obj_header header; |
| 171 | grn_id range; /* table: type of subrecords, column: type of values */ |
| 172 | /* -- compatible with grn_accessor -- */ |
| 173 | grn_id id; |
| 174 | grn_obj *db; |
| 175 | grn_user_data user_data; |
| 176 | grn_proc_func *finalizer; |
| 177 | grn_hook *hooks[5]; |
| 178 | void *source; |
| 179 | uint32_t source_size; |
| 180 | uint32_t max_n_subrecs; |
| 181 | uint8_t subrec_size; |
| 182 | uint8_t subrec_offset; |
| 183 | uint8_t record_unit; |
| 184 | uint8_t subrec_unit; |
| 185 | struct { |
| 186 | grn_table_group_flags flags; |
| 187 | grn_obj *calc_target; |
| 188 | grn_id aggregated_value_type_id; |
| 189 | grn_table_group_aggregator **aggregators; |
| 190 | uint32_t n_aggregators; |
| 191 | } group; |
| 192 | uint32_t reference_count; |
| 193 | } grn_db_obj; |
| 194 | |
| 195 | #define GRN_DB_OBJ_SET_TYPE(db_obj, obj_type) \ |
| 196 | do { \ |
| 197 | (db_obj)->obj.header.type = (obj_type); \ |
| 198 | (db_obj)->obj.header.impl_flags = 0; \ |
| 199 | (db_obj)->obj.header.flags = 0; \ |
| 200 | (db_obj)->obj.header.domain = GRN_ID_NIL; \ |
| 201 | (db_obj)->obj.id = GRN_ID_NIL; \ |
| 202 | (db_obj)->obj.user_data.ptr = NULL; \ |
| 203 | (db_obj)->obj.finalizer = NULL; \ |
| 204 | (db_obj)->obj.hooks[0] = NULL; \ |
| 205 | (db_obj)->obj.hooks[1] = NULL; \ |
| 206 | (db_obj)->obj.hooks[2] = NULL; \ |
| 207 | (db_obj)->obj.hooks[3] = NULL; \ |
| 208 | (db_obj)->obj.hooks[4] = NULL; \ |
| 209 | (db_obj)->obj.source = NULL; \ |
| 210 | (db_obj)->obj.source_size = 0; \ |
| 211 | (db_obj)->obj.reference_count = 1; \ |
| 212 | } while (0) |
| 213 | |
| 214 | /**** receive handler ****/ |
| 215 | |
| 216 | GRN_API void |
| 217 | grn_ctx_stream_out_func(grn_ctx *c, int flags, void *stream); |
| 218 | |
| 219 | grn_rc |
| 220 | grn_db_init_builtin_procs(grn_ctx *ctx); |
| 221 | |
| 222 | #ifdef __cplusplus |
| 223 | } |
| 224 | #endif |