1 From 8bc97078153cfae322a36151143f1a06196730d7 Mon Sep 17 00:00:00 2001
2 From: Adrian Nuta <adriannuta@gmail.com>
3 Date: Tue, 27 Sep 2016 16:41:36 +0300
4 Subject: [PATCH] sphinxse patches for mariadb 10.1 and mysql 5.7
7 mysqlse/ha_sphinx.cc | 170 +++++++++++++++++++++++++++++++++++++++++----------
8 mysqlse/ha_sphinx.h | 30 ++++++---
9 2 files changed, 161 insertions(+), 39 deletions(-)
11 diff --git a/mysqlse/ha_sphinx.cc b/mysqlse/ha_sphinx.cc
12 index 9b0c3a8e..080f7ce0 100644
13 --- a/mysqlse/ha_sphinx.cc
14 +++ b/mysqlse/ha_sphinx.cc
16 #include "../mysql_priv.h"
19 +#if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID>=50709
20 +#include "item_timefunc.h"
21 +#define sphinx_append push_back
22 +#define sphinx_array std::vector
23 +#define sphinx_elements size
25 +#define __WIN__ _WIN32
26 +#define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0)
27 +#define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
28 +#define pthread_mutex_unlock(A) (LeaveCriticalSection(A), 0)
29 +#define pthread_mutex_destroy(A) (DeleteCriticalSection(A), 0)
30 +#define in_addr_t uint32
31 +#include <winsock2.h>
34 +#define sphinx_append append
35 +#define sphinx_array Dynamic_array
36 +#define sphinx_elements elements
39 #include <mysys_err.h>
41 #include <mysql.h> // include client for INSERT table (sort of redoing federated..)
46 + #if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID>=50709
47 + #include <arpa/inet.h>
54 @@ -286,6 +310,12 @@ inline void SPH_DEBUG ( const char *, ... ) {}
55 #define SafeDelete(_arg) { if ( _arg ) delete ( _arg ); (_arg) = NULL; }
56 #define SafeDeleteArray(_arg) { if ( _arg ) delete [] ( _arg ); (_arg) = NULL; }
58 +#if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID>=50709
60 +typedef native_mutex_t pthread_mutex_t;
64 //////////////////////////////////////////////////////////////////////////////
66 /// per-table structure that will be shared among all open Sphinx SE handlers
67 @@ -602,10 +632,10 @@ struct CSphSEQuery
69 char * m_sName; ///< points to query buffer
71 - Dynamic_array<ulonglong> m_dIds;
72 - Dynamic_array<Value_t> m_dValues;
73 + sphinx_array<ulonglong> m_dIds;
74 + sphinx_array<Value_t> m_dValues;
76 - Dynamic_array<Override_t *> m_dOverrides;
77 + sphinx_array<Override_t *> m_dOverrides;
80 char m_sParseError[256];
81 @@ -634,10 +664,10 @@ struct CSphSEQuery
82 void SendString ( const char * v ) { int iLen = strlen(v); SendDword(iLen); SendBytes ( v, iLen ); }
83 void SendFloat ( float v ) { SendDword ( sphF2DW(v) ); }
86 +#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
87 template int CSphSEQuery::ParseArray<uint32> ( uint32 **, const char * );
88 template int CSphSEQuery::ParseArray<longlong> ( longlong **, const char * );
91 //////////////////////////////////////////////////////////////////////////////
93 #if MYSQL_VERSION_ID>50100
94 @@ -733,13 +763,21 @@ static int sphinx_init_func ( void * p )
97 void ( pthread_mutex_init ( &sphinx_mutex, MY_MUTEX_INIT_FAST ) );
98 + #if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 50709
99 + sphinx_hash_init ( &sphinx_open_tables, system_charset_info, 32, 0, 0,
100 + sphinx_get_key, 0, 0, 0 );
102 sphinx_hash_init ( &sphinx_open_tables, system_charset_info, 32, 0, 0,
103 sphinx_get_key, 0, 0 );
106 #if MYSQL_VERSION_ID > 50100
107 handlerton * hton = (handlerton*) p;
108 hton->state = SHOW_OPTION_YES;
109 + #if !defined(MARIADB_BASE_VERSION)
110 hton->db_type = DB_TYPE_FIRST_DYNAMIC;
112 + hton->db_type = DB_TYPE_AUTOASSIGN;
114 hton->create = sphinx_create_handler;
115 hton->close_connection = sphinx_close_connection;
116 hton->show_status = sphinx_show_status;
117 @@ -855,10 +893,15 @@ bool sphinx_show_status ( THD * thd )
120 CSphTLS * pTls = (CSphTLS*) thd->ha_data[sphinx_hton.slot];
122 + #ifndef MARIADB_BASE_VERSION
123 field_list.push_back ( new Item_empty_string ( "Type", 10 ) );
124 field_list.push_back ( new Item_empty_string ( "Name", FN_REFLEN ) );
125 field_list.push_back ( new Item_empty_string ( "Status", 10 ) );
127 + field_list.push_back ( new Item_empty_string ( thd, "Type", 10 ) );
128 + field_list.push_back ( new Item_empty_string ( thd, "Name", FN_REFLEN ) );
129 + field_list.push_back ( new Item_empty_string ( thd, "Status", 10 ) );
131 if ( protocol->send_fields ( &field_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF ) )
134 @@ -1321,7 +1364,7 @@ CSphSEQuery::~CSphSEQuery ()
135 SafeDeleteArray ( m_sQueryBuffer );
136 SafeDeleteArray ( m_pWeights );
137 SafeDeleteArray ( m_pBuf );
138 - for ( int i=0; i<m_dOverrides.elements(); i++ )
139 + for ( int i=0; i<m_dOverrides.sphinx_elements(); i++ )
140 SafeDelete ( m_dOverrides.at(i) );
143 @@ -1412,6 +1455,15 @@ static bool myismagic ( char c )
147 +static bool myisjson ( char c )
157 bool CSphSEQuery::ParseField ( char * sField )
159 @@ -1623,7 +1675,7 @@ bool CSphSEQuery::ParseField ( char * sField )
162 tFilter.m_sAttrName = sValue;
163 - while ( (*sValue) && ( myisattr(*sValue) || myismagic(*sValue) ) )
164 + while ( (*sValue) && ( myisattr(*sValue) || myismagic(*sValue) ) || myisjson(*sValue) )
168 @@ -1789,7 +1841,7 @@ bool CSphSEQuery::ParseField ( char * sField )
169 pOverride = new CSphSEQuery::Override_t;
170 pOverride->m_sName = chop(sName);
171 pOverride->m_iType = iType;
172 - m_dOverrides.append ( pOverride );
173 + m_dOverrides.sphinx_append ( pOverride );
176 ulonglong uId = strtoull ( sId, NULL, 10 );
177 @@ -1801,8 +1853,8 @@ bool CSphSEQuery::ParseField ( char * sField )
179 tValue.m_uValue = (uint32)strtoul ( sValue, NULL, 10 );
181 - pOverride->m_dIds.append ( uId );
182 - pOverride->m_dValues.append ( tValue );
183 + pOverride->m_dIds.sphinx_append ( uId );
184 + pOverride->m_dValues.sphinx_append ( tValue );
188 @@ -1906,11 +1958,11 @@ int CSphSEQuery::BuildRequest ( char ** ppBuffer )
189 iReqSize += 8 + strlen(m_sFieldWeight[i] );
192 - for ( int i=0; i<m_dOverrides.elements(); i++ )
193 + for ( int i=0; i<m_dOverrides.sphinx_elements(); i++ )
195 CSphSEQuery::Override_t * pOverride = m_dOverrides.at(i);
196 const uint32 uSize = pOverride->m_iType==SPH_ATTR_BIGINT ? 16 : 12; // id64 + value
197 - iReqSize += strlen ( pOverride->m_sName ) + 12 + uSize*pOverride->m_dIds.elements();
198 + iReqSize += strlen ( pOverride->m_sName ) + 12 + uSize*pOverride->m_dIds.sphinx_elements();
202 @@ -2012,14 +2064,14 @@ int CSphSEQuery::BuildRequest ( char ** ppBuffer )
203 SendString ( m_sComment );
206 - SendInt ( m_dOverrides.elements() );
207 - for ( int i=0; i<m_dOverrides.elements(); i++ )
208 + SendInt ( m_dOverrides.sphinx_elements() );
209 + for ( int i=0; i<m_dOverrides.sphinx_elements(); i++ )
211 CSphSEQuery::Override_t * pOverride = m_dOverrides.at(i);
212 SendString ( pOverride->m_sName );
213 SendDword ( pOverride->m_iType );
214 - SendInt ( pOverride->m_dIds.elements() );
215 - for ( int j=0; j<pOverride->m_dIds.elements(); j++ )
216 + SendInt ( pOverride->m_dIds.sphinx_elements() );
217 + for ( int j=0; j<pOverride->m_dIds.sphinx_elements(); j++ )
219 SendUint64 ( pOverride->m_dIds.at(j) );
220 if ( pOverride->m_iType==SPH_ATTR_FLOAT )
221 @@ -2045,9 +2097,9 @@ int CSphSEQuery::BuildRequest ( char ** ppBuffer )
222 //////////////////////////////////////////////////////////////////////////////
224 //////////////////////////////////////////////////////////////////////////////
226 +#ifndef MARIADB_BASE_VERSION
227 static const char * ha_sphinx_exts[] = { NullS };
231 #if MYSQL_VERSION_ID<50100
232 ha_sphinx::ha_sphinx ( TABLE_ARG * table )
233 @@ -2073,8 +2125,10 @@ ha_sphinx::ha_sphinx ( handlerton * hton, TABLE_ARG * table )
234 , m_dUnboundFields ( NULL )
237 + #if MYSQL_VERSION_ID < 50709
239 current_thd->variables.engine_condition_pushdown = true;
244 @@ -2082,11 +2136,24 @@ ha_sphinx::ha_sphinx ( handlerton * hton, TABLE_ARG * table )
245 // If frm_error() is called then we will use this to to find out what file extentions
246 // exist for the storage engine. This is also used by the default rename_table and
247 // delete_table method in handler.cc.
248 +#ifndef MARIADB_BASE_VERSION
249 const char ** ha_sphinx::bas_ext() const
251 return ha_sphinx_exts;
255 +ha_sphinx::~ha_sphinx()
257 + SafeDeleteArray ( m_dAttrs );
258 + SafeDeleteArray ( m_dUnboundFields );
261 + for (uint32 i=0; i< m_iFields; i++ )
262 + SafeDeleteArray ( m_dFields[i] );
263 + delete [] m_dFields;
268 // Used for opening tables. The name will be the name of the file.
269 // A table is opened when it needs to be opened. For instance
270 @@ -2349,11 +2416,19 @@ int ha_sphinx::write_row ( byte * )
271 sQuery.append ( "''" );
276 + #if MYSQL_VERSION_ID>=100000
277 + THD *thd= ha_thd();
279 if ( (*ppField)->type()==MYSQL_TYPE_TIMESTAMP )
281 - Item_field * pWrap = new Item_field ( *ppField ); // autofreed by query arena, I assume
282 - Item_func_unix_timestamp * pConv = new Item_func_unix_timestamp ( pWrap );
283 + #if MYSQL_VERSION_ID>=100000
284 + Item_field * pWrap = new (thd->mem_root) Item_field(thd, *ppField); // autofreed by query arena, I assume
285 + Item_func_unix_timestamp * pConv = new (thd->mem_root) Item_func_unix_timestamp(thd, pWrap);
287 + Item_field * pWrap = new Item_field ( *ppField ); // autofreed by query arena, I assume
288 + Item_func_unix_timestamp * pConv = new Item_func_unix_timestamp ( pWrap );
290 pConv->quick_fix_field();
291 unsigned int uTs = (unsigned int) pConv->val_int();
293 @@ -2390,7 +2465,10 @@ int ha_sphinx::write_row ( byte * )
295 unsigned int uTimeout = 1;
296 mysql_options ( pConn, MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&uTimeout );
298 + #ifdef MARIADB_BASE_VERSION
299 + my_bool my_true= 1;
300 + mysql_options(pConn, MYSQL_OPT_USE_THREAD_SPECIFIC_MEMORY, (char*) &my_true);
302 if ( !mysql_real_connect ( pConn, m_pShare->m_sHost, "root", "", "", m_pShare->m_iPort, m_pShare->m_sSocket, 0 ) )
303 SPH_RET ( HandleMysqlError ( pConn, ER_CONNECT_TO_FOREIGN_DATA_SOURCE ) );
305 @@ -2449,6 +2527,10 @@ int ha_sphinx::delete_row ( const byte * )
307 unsigned int uTimeout = 1;
308 mysql_options ( pConn, MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&uTimeout );
309 + #ifdef MARIADB_BASE_VERSION
310 + my_bool my_true= 1;
311 + mysql_options(pConn, MYSQL_OPT_USE_THREAD_SPECIFIC_MEMORY, (char*) &my_true);
314 if ( !mysql_real_connect ( pConn, m_pShare->m_sHost, "root", "", "", m_pShare->m_iPort, m_pShare->m_sSocket, 0 ) )
315 SPH_RET ( HandleMysqlError ( pConn, ER_CONNECT_TO_FOREIGN_DATA_SOURCE ) );
316 @@ -2756,11 +2838,19 @@ const Item * ha_sphinx::cond_push ( const Item *cond )
319 // copy the query, and let know that we intercepted this condition
320 - Item_string * pString = (Item_string *) args[1];
321 - pTable->m_bQuery = true;
322 + #if MYSQL_VERSION_ID>=100000
323 + String *pString= args[1]->val_str(NULL);
324 + pTable->m_bQuery = true;
325 + strncpy ( pTable->m_sQuery, pString->c_ptr(), sizeof(pTable->m_sQuery) );
326 + pTable->m_sQuery[sizeof(pTable->m_sQuery)-1] = '\0';
327 + pTable->m_pQueryCharset = pString->charset();
329 + Item_string * pString = (Item_string *) args[1];
330 + pTable->m_bQuery = true;
331 strncpy ( pTable->m_sQuery, pString->str_value.c_ptr(), sizeof(pTable->m_sQuery) );
332 pTable->m_sQuery[sizeof(pTable->m_sQuery)-1] = '\0';
333 pTable->m_pQueryCharset = pString->str_value.charset();
338 @@ -3540,7 +3630,7 @@ CSphSEStats * sphinx_get_stats ( THD * thd, SHOW_VAR * out )
341 out->type = SHOW_CHAR;
347 @@ -3673,7 +3763,25 @@ struct st_mysql_show_var sphinx_status_vars[] =
348 {0, 0, (enum_mysql_show_type)0}
352 +#ifdef MARIADB_BASE_VERSION
353 +maria_declare_plugin(sphinx)
355 + MYSQL_STORAGE_ENGINE_PLUGIN,
356 + &sphinx_storage_engine,
358 + "Sphinx developers",
359 + sphinx_hton_comment,
360 + PLUGIN_LICENSE_GPL,
361 + sphinx_init_func, // Plugin Init
362 + sphinx_done_func, // Plugin Deinit
364 + sphinx_status_vars,
366 + SPHINXSE_VERSION, // string version
367 +MariaDB_PLUGIN_MATURITY_GAMMA
369 +maria_declare_plugin_end;
371 mysql_declare_plugin(sphinx)
373 MYSQL_STORAGE_ENGINE_PLUGIN,
374 @@ -3690,7 +3798,7 @@ mysql_declare_plugin(sphinx)
377 mysql_declare_plugin_end;
383 diff --git a/mysqlse/ha_sphinx.h b/mysqlse/ha_sphinx.h
384 index c44ffca8..897a0e5a 100644
385 --- a/mysqlse/ha_sphinx.h
386 +++ b/mysqlse/ha_sphinx.h
387 @@ -53,14 +53,24 @@ class ha_sphinx : public handler
389 ha_sphinx ( handlerton * hton, TABLE_ARG * table_arg );
392 +#ifndef MARIADB_BASE_VERSION
398 const char * table_type () const { return "SPHINX"; } ///< SE name for display purposes
399 const char * index_type ( uint ) { return "HASH"; } ///< index type name for display purposes
400 - const char ** bas_ext () const; ///< my file extensions
402 + #ifndef MARIADB_BASE_VERSION
403 + const char ** bas_ext () const;
405 #if MYSQL_VERSION_ID>50100
406 - ulonglong table_flags () const { return HA_CAN_INDEX_BLOBS; } ///< bitmap of implemented flags (see handler.h for more info)
407 + #ifdef MARIADB_BASE_VERSION
408 + ulonglong table_flags () const { return HA_CAN_INDEX_BLOBS |
409 + HA_CAN_TABLE_CONDITION_PUSHDOWN; } ///< bitmap of implemented flags (see handler.h for more info)
411 + ulonglong table_flags () const { return HA_CAN_INDEX_BLOBS; }
414 ulong table_flags () const { return HA_CAN_INDEX_BLOBS; } ///< bitmap of implemented flags (see handler.h for more info)
416 @@ -77,8 +87,12 @@ class ha_sphinx : public handler
418 virtual double scan_time () { return (double)( records+deleted )/20.0 + 10; } ///< called in test_quick_select to determine if indexes should be used
421 - virtual double read_time ( ha_rows rows ) { return (double)rows/20.0 + 1; } ///< index read time estimate
422 + #ifdef MARIADB_BASE_VERSION
423 + virtual double read_time(uint index, uint ranges, ha_rows rows)
424 + { return ranges + (double)rows/20.0 + 1; } ///< index read time estimate
426 + virtual double read_time ( ha_rows rows ) { return (double)rows/20.0 + 1; }
430 int open ( const char * name, int mode, uint test_if_locked );
431 --- storage/sphinx/snippets_udf.cc~ 2016-07-19 12:42:18.000000000 +0200
432 +++ storage/sphinx/snippets_udf.cc 2018-03-10 20:16:19.241113584 +0100
434 #include "../mysql_priv.h"
437 +#if !defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID>=50709
438 + #include <arpa/inet.h>
440 #include <mysys_err.h>