CUBRID Engine  latest
xasl_stream.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 //
20 // XASL stream - common interface for xasl_to_stream and stream_to_xasl
21 //
22 
23 #ifndef _XASL_STREAM_HPP_
24 #define _XASL_STREAM_HPP_
25 
26 #include "json_table_def.h"
27 #include "thread_compat.hpp"
28 #include "xasl_unpack_info.hpp"
29 
30 #include <cstddef>
31 
32 // forward def
33 struct db_value;
34 class regu_variable_node;
35 
36 namespace cubxasl
37 {
38  namespace json_table
39  {
40  struct column;
41  struct node;
42  struct spec_node;
43  } // namespace json_table
44 } // namespace cubxasl
45 
46 const size_t OFFSETS_PER_BLOCK = 4096;
47 const size_t START_PTR_PER_BLOCK = 15;
48 /*
49  * the linear byte stream for store the given XASL tree is allocated
50  * and expanded dynamically on demand by the following amount of bytes
51  */
52 const size_t STREAM_EXPANSION_UNIT = OFFSETS_PER_BLOCK * sizeof (int);
53 
54 const int XASL_STREAM_ALIGN_UNIT = sizeof (double);
56 
57 inline int xasl_stream_make_align (int x);
58 
59 int stx_get_xasl_errcode (THREAD_ENTRY *thread_p);
60 void stx_set_xasl_errcode (THREAD_ENTRY *thread_p, int errcode);
61 int stx_init_xasl_unpack_info (THREAD_ENTRY *thread_p, char *xasl_stream, int xasl_stream_size);
62 
63 int stx_mark_struct_visited (THREAD_ENTRY *thread_p, const void *ptr, void *str);
64 void *stx_get_struct_visited_ptr (THREAD_ENTRY *thread_p, const void *ptr);
65 void stx_free_visited_ptrs (THREAD_ENTRY *thread_p);
66 char *stx_alloc_struct (THREAD_ENTRY *thread_p, int size);
67 
68 // all stx_build overloads
69 char *stx_build (THREAD_ENTRY *thread_p, char *ptr, cubxasl::json_table::spec_node &jts);
70 char *stx_build (THREAD_ENTRY *thread_p, char *ptr, cubxasl::json_table::column &jtc);
71 char *stx_build (THREAD_ENTRY *thread_p, char *ptr, cubxasl::json_table::node &jtn);
72 // next stx_build functions are not ported to xasl_stream.cpp and cannot be used for debug checks
73 char *stx_build (THREAD_ENTRY *thread_p, char *ptr, db_value &val);
74 char *stx_build (THREAD_ENTRY *thread_p, char *ptr, regu_variable_node &reguvar);
75 
76 // dependencies not ported
77 char *stx_build_db_value (THREAD_ENTRY *thread_p, char *tmp, db_value *ptr);
78 char *stx_build_string (THREAD_ENTRY *thread_p, char *tmp, char *ptr);
79 
80 // restore string; return restored string, updates stream pointer
81 char *stx_restore_string (THREAD_ENTRY *thread_p, char *&ptr);
82 
83 // all stx_unpack overloads; equivalent to stx_build, but never used for stx_restore
84 char *stx_unpack (THREAD_ENTRY *thread_p, char *tmp, json_table_column_behavior &behavior);
85 
86 // xasl stream compare functions - used for debugging to compare originals and packed/unpacked objects
90 
91 template <typename T>
92 static void stx_alloc (THREAD_ENTRY *thread_p, T *&ptr);
93 template <typename T>
94 static void stx_alloc_array (THREAD_ENTRY *thread_p, T *&ptr, std::size_t count);
95 
96 template <typename T>
97 void stx_restore (THREAD_ENTRY *thread_p, char *&ptr, T *&target);
98 
100 // Template and inline implementation
102 
103 #include "object_representation.h"
104 #include "system.h"
105 
106 #include <cassert>
107 
108 inline int
110 {
111  return (((x) & ~XASL_STREAM_ALIGN_MASK) + (((x) & XASL_STREAM_ALIGN_MASK) ? XASL_STREAM_ALIGN_UNIT : 0));
112 }
113 
114 // restore from stream buffer
115 //
116 // template T should have an overload of stx_build.
117 //
118 // if you want to prevent saving to and restoring from stream buffer, use stx_unpack instead of stx_build.
119 //
120 template <typename T>
121 static void
122 stx_restore (THREAD_ENTRY *thread_p, char *&ptr, T *&target)
123 {
124 #if !defined (CS_MODE)
125  int offset;
126 
127  ptr = or_unpack_int (ptr, &offset);
128  if (offset == 0)
129  {
130  target = NULL;
131  }
132  else
133  {
134  char *bufptr = &get_xasl_unpack_info_ptr (thread_p)->packed_xasl[offset];
135  target = (T *) stx_get_struct_visited_ptr (thread_p, bufptr);
136  if (target != NULL)
137  {
138  return;
139  }
140  target = (T *) stx_alloc_struct (thread_p, (int) sizeof (T));
141  if (target == NULL)
142  {
143  assert (false);
144  return;
145  }
146  if (stx_mark_struct_visited (thread_p, bufptr, target) != NO_ERROR)
147  {
148  assert (false);
149  return;
150  }
151  if (stx_build (thread_p, bufptr, *target) == NULL)
152  {
153  assert (false);
154  }
155  }
156 #else // CS_MODE
157  // NOTE - in CS_MODE, we only need to do some debug checks and we don't have to do actual restoring
158  int dummy;
159  ptr = or_unpack_int (ptr, &dummy);
160  target = NULL;
161 #endif // CS_MODE
162 }
163 
164 template <typename T>
165 void stx_alloc (THREAD_ENTRY *thread_p, T *&ptr)
166 {
167  ptr = (T *) stx_alloc_struct (thread_p, (int) sizeof (T));
168 }
169 
170 template <typename T>
171 static void stx_alloc_array (THREAD_ENTRY *thread_p, T *&ptr, std::size_t count)
172 {
173  ptr = (T *) stx_alloc_struct (thread_p, (int) (count * sizeof (T)));
174 }
175 
176 #endif // _XASL_STREAM_HPP_
char * stx_unpack(THREAD_ENTRY *thread_p, char *tmp, json_table_column_behavior &behavior)
bool xasl_stream_compare(const cubxasl::json_table::column &first, const cubxasl::json_table::column &second)
#define NO_ERROR
Definition: error_code.h:46
const int XASL_STREAM_ALIGN_MASK
Definition: xasl_stream.hpp:55
char * stx_alloc_struct(THREAD_ENTRY *thread_p, int size)
int stx_init_xasl_unpack_info(THREAD_ENTRY *thread_p, char *xasl_stream, int xasl_stream_size)
Definition: xasl_stream.cpp:72
static void stx_alloc(THREAD_ENTRY *thread_p, T *&ptr)
XASL_UNPACK_INFO * get_xasl_unpack_info_ptr(THREAD_ENTRY *thread_p)
const size_t OFFSETS_PER_BLOCK
Definition: xasl_stream.hpp:46
const size_t STREAM_EXPANSION_UNIT
Definition: xasl_stream.hpp:52
void THREAD_ENTRY
int xasl_stream_make_align(int x)
const size_t START_PTR_PER_BLOCK
Definition: xasl_stream.hpp:47
#define assert(x)
char * stx_build_string(THREAD_ENTRY *thread_p, char *tmp, char *ptr)
void * stx_get_struct_visited_ptr(THREAD_ENTRY *thread_p, const void *ptr)
int stx_get_xasl_errcode(THREAD_ENTRY *thread_p)
Definition: xasl_stream.cpp:39
#define NULL
Definition: freelistheap.h:34
char * stx_build_db_value(THREAD_ENTRY *thread_p, char *tmp, db_value *ptr)
char * stx_build(THREAD_ENTRY *thread_p, char *ptr, cubxasl::json_table::spec_node &jts)
char * or_unpack_int(char *ptr, int *number)
int stx_mark_struct_visited(THREAD_ENTRY *thread_p, const void *ptr, void *str)
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
void stx_restore(THREAD_ENTRY *thread_p, char *&ptr, T *&target)
void stx_free_visited_ptrs(THREAD_ENTRY *thread_p)
static void stx_alloc_array(THREAD_ENTRY *thread_p, T *&ptr, std::size_t count)
const int XASL_STREAM_ALIGN_UNIT
Definition: xasl_stream.hpp:54
void stx_set_xasl_errcode(THREAD_ENTRY *thread_p, int errcode)
Definition: xasl_stream.cpp:54
char * stx_restore_string(THREAD_ENTRY *thread_p, char *&ptr)