1 /* -*- c-basic-offset: 2 -*- */
2 /*
3   Copyright(C) 2009-2016 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-1335  USA
17 */
18 
19 #pragma once
20 
21 #ifndef GRN_CTX_H
22 # include "grn_ctx.h"
23 #endif /* GRN_CTX_H */
24 
25 #ifndef GRN_COM_H
26 # include "grn_com.h"
27 #endif /* GRN_COM_H */
28 
29 #include "grn_msgpack.h"
30 
31 #ifdef GRN_WITH_MRUBY
32 # include <mruby.h>
33 #endif
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**** grn_expr ****/
40 
41 #define GRN_EXPR_MISSING_NAME          "expr_missing"
42 
43 /**** grn_ctx_impl ****/
44 
45 #define GRN_CTX_INITED    0x00
46 #define GRN_CTX_QUITTING  0x0f
47 
48 typedef enum {
49   GRN_LOADER_BEGIN = 0,
50   GRN_LOADER_TOKEN,
51   GRN_LOADER_STRING,
52   GRN_LOADER_SYMBOL,
53   GRN_LOADER_NUMBER,
54   GRN_LOADER_STRING_ESC,
55   GRN_LOADER_UNICODE0,
56   GRN_LOADER_UNICODE1,
57   GRN_LOADER_UNICODE2,
58   GRN_LOADER_UNICODE3,
59   GRN_LOADER_END
60 } grn_loader_stat;
61 
62 /*
63  * Status of target columns used in Format 1.
64  * Target columns are specified via --columns or the first array in a Format 1
65  * JSON object.
66  */
67 typedef enum {
68   GRN_LOADER_COLUMNS_UNSET = 0, /* Columns are not available. */
69   GRN_LOADER_COLUMNS_SET,       /* Columns are available. */
70   GRN_LOADER_COLUMNS_BROKEN     /* Columns are specified but broken. */
71 } grn_loader_columns_status;
72 
73 typedef struct {
74   grn_obj values;
75   grn_obj level;
76   grn_obj columns;
77   grn_obj ids;
78   grn_obj return_codes;
79   grn_obj error_messages;
80   uint32_t emit_level;
81   int32_t id_offset;  /* Position of _id in values or -1 if _id is N/A. */
82   int32_t key_offset; /* Position of _key in values or -1 if _key is N/A. */
83   grn_obj *table;
84   grn_obj *last;
85   grn_obj *ifexists;
86   grn_obj *each;
87   uint32_t unichar;
88   uint32_t values_size;
89   uint32_t nrecords;
90   grn_loader_stat stat;
91   grn_content_type input_type;
92   grn_loader_columns_status columns_status;
93   grn_rc rc;
94   char errbuf[GRN_CTX_MSGSIZE];
95   grn_bool output_ids;
96   grn_bool output_errors;
97 } grn_loader;
98 
99 #define GRN_CTX_N_SEGMENTS 512
100 
101 #ifdef USE_MEMORY_DEBUG
102 typedef struct _grn_alloc_info grn_alloc_info;
103 struct _grn_alloc_info
104 {
105   void *address;
106   int freed;
107   size_t size;
108   char alloc_backtrace[4096];
109   char free_backtrace[4096];
110   char *file;
111   int line;
112   char *func;
113   grn_alloc_info *next;
114 };
115 #endif
116 
117 typedef struct _grn_mrb_data grn_mrb_data;
118 struct _grn_mrb_data {
119   grn_bool initialized;
120 #ifdef GRN_WITH_MRUBY
121   mrb_state *state;
122   char base_directory[PATH_MAX];
123   struct RClass *module;
124   struct RClass *object_class;
125   grn_hash *checked_procs;
126   grn_hash *registered_plugins;
127   struct {
128     grn_obj from;
129     grn_obj to;
130   } buffer;
131   struct {
132     struct RClass *time_class;
133   } builtin;
134   struct {
135     struct RClass *operator_class;
136   } groonga;
137 #endif
138 };
139 
140 struct _grn_ctx_impl {
141   grn_encoding encoding;
142 
143   /* memory pool portion */
144   int32_t lifoseg;
145   int32_t currseg;
146   grn_critical_section lock;
147   grn_io_mapinfo segs[GRN_CTX_N_SEGMENTS];
148 
149 #ifdef USE_DYNAMIC_MALLOC_CHANGE
150   /* memory allocation portion */
151   grn_malloc_func malloc_func;
152   grn_calloc_func calloc_func;
153   grn_realloc_func realloc_func;
154   grn_strdup_func strdup_func;
155   grn_free_func free_func;
156 #endif
157 
158 #ifdef USE_MEMORY_DEBUG
159   /* memory debug portion */
160   grn_alloc_info *alloc_info;
161 #endif
162 
163   /* expression portion */
164   grn_obj *stack[GRN_STACK_SIZE];
165   uint32_t stack_curr;
166   grn_hash *expr_vars;
167   grn_obj *curr_expr;
168   grn_obj current_request_id;
169   void *current_request_timer_id;
170   void *parser;
171   grn_timeval tv;
172 
173   /* loader portion */
174   grn_edge *edge;
175   grn_loader loader;
176 
177   /* plugin portion */
178   const char *plugin_path;
179 
180   /* output portion */
181   struct {
182     grn_obj *buf;
183     void (*func)(grn_ctx *, int, void *);
184     union {
185       void *ptr;
186       int fd;
187       uint32_t u32;
188       uint64_t u64;
189     } data;
190     grn_content_type type;
191     const char *mime_type;
192     grn_bool is_pretty;
193     grn_obj names;
194     grn_obj levels;
195 #ifdef GRN_WITH_MESSAGE_PACK
196     msgpack_packer msgpacker;
197 #endif
198   } output;
199 
200   struct {
201     int flags;
202     grn_command_version version;
203     struct {
204       grn_obj *command;
205       grn_command_version version;
206     } keep;
207   } command;
208 
209   /* match escalation portion */
210   int64_t match_escalation_threshold;
211 
212   /* lifetime portion */
213   grn_proc_func *finalizer;
214 
215   grn_obj *db;
216   grn_array *values;        /* temporary objects */
217   grn_pat *temporary_columns;
218   grn_hash *ios;        /* IOs */
219   grn_com *com;
220   unsigned int com_status;
221 
222   grn_obj query_log_buf;
223 
224   char previous_errbuf[GRN_CTX_MSGSIZE];
225   unsigned int n_same_error_messages;
226 
227   grn_mrb_data mrb;
228 
229   struct {
230     grn_obj stack;
231     grn_obj *current;
232   } temporary_open_spaces;
233 };
234 
235 #ifdef __cplusplus
236 }
237 #endif
238