1 Star 0 Fork 511

angelicaiia/sequoiasql-mysql

forked from SequoiaDB/sequoiasql-mysql 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ha_sdb.cc 63.29 KB
一键复制 编辑 原始数据 按行查看 历史
yuttellite 提交于 2019-03-29 16:24 . Support HA_EXTRA_KEYREAD.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
/* Copyright (c) 2018, SequoiaDB and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef MYSQL_SERVER
#define MYSQL_SERVER
#endif
#include "ha_sdb.h"
#include <sql_class.h>
#include <sql_table.h>
#include <mysql/plugin.h>
#include <mysql/psi/mysql_file.h>
#include <json_dom.h>
#include <time.h>
#include <client.hpp>
#include "sdb_log.h"
#include "sdb_conf.h"
#include "sdb_cl.h"
#include "sdb_conn.h"
#include "sdb_thd.h"
#include "sdb_util.h"
#include "sdb_condition.h"
#include "sdb_errcode.h"
#include "sdb_idx.h"
using namespace sdbclient;
#ifndef SDB_DRIVER_VERSION
#define SDB_DRIVER_VERSION "UNKNOWN"
#endif
#ifndef SDB_PLUGIN_VERSION
#define SDB_PLUGIN_VERSION "UNKNOWN"
#endif
#ifdef DEBUG
#ifdef SDB_ENTERPRISE
#define SDB_ENGINE_EDITION "Enterprise-Debug"
#else /* SDB_ENTERPRISE */
#define SDB_ENGINE_EDITION "Community-Debug"
#endif /* SDB_ENTERPRISE */
#else /* DEBUG */
#ifdef SDB_ENTERPRISE
#define SDB_ENGINE_EDITION "Enterprise"
#else /* SDB_ENTERPRISE */
#define SDB_ENGINE_EDITION "Community"
#endif /* SDB_ENTERPRISE */
#endif /* DEBUG */
#define SDB_ENGINE_INFO "SequoiaDB storage engine(" SDB_ENGINE_EDITION ")"
#define SDB_VERSION_INFO \
"Plugin: " SDB_PLUGIN_VERSION ", Driver: " SDB_DRIVER_VERSION \
", BuildTime: " __DATE__ " " __TIME__
#ifndef FLG_INSERT_REPLACEONDUP
#define FLG_INSERT_REPLACEONDUP 0x00000004
#endif
#define SDB_OID_LEN 12
#define SDB_OID_FIELD "_id"
#define SDB_FIELD_MAX_LEN (16 * 1024 * 1024)
#define SDB_COMMENT "sequoiadb"
const static char *sdb_plugin_info = SDB_ENGINE_INFO ". " SDB_VERSION_INFO ".";
handlerton *sdb_hton = NULL;
mysql_mutex_t sdb_mutex;
static PSI_mutex_key key_mutex_sdb, key_mutex_SDB_SHARE_mutex;
static HASH sdb_open_tables;
static PSI_memory_key key_memory_sdb_share;
static PSI_memory_key sdb_key_memory_blobroot;
static const Alter_inplace_info::HA_ALTER_FLAGS INPLACE_ONLINE_ADDIDX =
Alter_inplace_info::ADD_INDEX | Alter_inplace_info::ADD_UNIQUE_INDEX |
Alter_inplace_info::ADD_PK_INDEX |
Alter_inplace_info::ALTER_COLUMN_NOT_NULLABLE;
static const Alter_inplace_info::HA_ALTER_FLAGS INPLACE_ONLINE_DROPIDX =
Alter_inplace_info::DROP_INDEX | Alter_inplace_info::DROP_UNIQUE_INDEX |
Alter_inplace_info::DROP_PK_INDEX |
Alter_inplace_info::ALTER_COLUMN_NULLABLE;
static const Alter_inplace_info::HA_ALTER_FLAGS INPLACE_ONLINE_OPERATIONS =
INPLACE_ONLINE_ADDIDX | INPLACE_ONLINE_DROPIDX |
Alter_inplace_info::ADD_COLUMN | Alter_inplace_info::DROP_COLUMN |
Alter_inplace_info::ALTER_STORED_COLUMN_ORDER |
Alter_inplace_info::ALTER_STORED_COLUMN_TYPE |
Alter_inplace_info::ALTER_COLUMN_DEFAULT |
Alter_inplace_info::ALTER_COLUMN_EQUAL_PACK_LENGTH |
Alter_inplace_info::CHANGE_CREATE_OPTION | Alter_inplace_info::RENAME_INDEX;
static uchar *sdb_get_key(Sdb_share *share, size_t *length,
my_bool not_used MY_ATTRIBUTE((unused))) {
*length = share->table_name_length;
return (uchar *)share->table_name;
}
static Sdb_share *get_sdb_share(const char *table_name, TABLE *table) {
Sdb_share *share = NULL;
char *tmp_name = NULL;
uint length;
mysql_mutex_lock(&sdb_mutex);
length = (uint)strlen(table_name);
/*
If share is not present in the hash, create a new share and
initialize its members.
*/
if (!(share = (Sdb_share *)my_hash_search(&sdb_open_tables,
(uchar *)table_name, length))) {
if (!my_multi_malloc(key_memory_sdb_share, MYF(MY_WME | MY_ZEROFILL),
&share, sizeof(*share), &tmp_name, length + 1,
NullS)) {
goto error;
}
share->use_count = 0;
share->table_name_length = length;
share->table_name = tmp_name;
strncpy(share->table_name, table_name, length);
if (my_hash_insert(&sdb_open_tables, (uchar *)share)) {
goto error;
}
thr_lock_init(&share->lock);
}
share->use_count++;
done:
mysql_mutex_unlock(&sdb_mutex);
return share;
error:
if (share) {
my_free(share);
share = NULL;
}
goto done;
}
static int free_sdb_share(Sdb_share *share) {
mysql_mutex_lock(&sdb_mutex);
if (!--share->use_count) {
my_hash_delete(&sdb_open_tables, (uchar *)share);
thr_lock_delete(&share->lock);
my_free(share);
}
mysql_mutex_unlock(&sdb_mutex);
return 0;
}
ha_sdb::ha_sdb(handlerton *hton, TABLE_SHARE *table_arg)
: handler(hton, table_arg) {
active_index = MAX_KEY;
share = NULL;
m_lock_type = TL_IGNORE;
collection = NULL;
first_read = true;
count_times = 0;
last_count_time = time(NULL);
m_ignore_dup_key = false;
m_write_can_replace = false;
m_use_bulk_insert = false;
stats.records = 0;
memset(db_name, 0, SDB_CS_NAME_MAX_SIZE + 1);
memset(table_name, 0, SDB_CL_NAME_MAX_SIZE + 1);
init_alloc_root(sdb_key_memory_blobroot, &blobroot, 8 * 1024, 0);
}
ha_sdb::~ha_sdb() {
free_root(&blobroot, MYF(0));
if (NULL != collection) {
delete collection;
collection = NULL;
}
}
const char **ha_sdb::bas_ext() const {
/*
If frm_error() is called then we will use this to find out
what file extensions exist for the storage engine. This is
also used by the default rename_table and delete_table method
in handler.cc.
SequoiaDB is a distributed database, and we have implemented delete_table,
so it's no need to fill this array.
*/
static const char *ext[] = {NullS};
return ext;
}
ulonglong ha_sdb::table_flags() const {
return (HA_REC_NOT_IN_SEQ | HA_NO_AUTO_INCREMENT | HA_NO_READ_LOCAL_LOCK |
HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE |
HA_TABLE_SCAN_ON_INDEX | HA_NULL_IN_KEY | HA_CAN_INDEX_BLOBS);
}
ulong ha_sdb::index_flags(uint inx, uint part, bool all_parts) const {
return (HA_READ_RANGE | HA_DO_INDEX_COND_PUSHDOWN | HA_READ_NEXT |
HA_READ_ORDER | HA_KEYREAD_ONLY);
}
uint ha_sdb::max_supported_record_length() const {
return HA_MAX_REC_LENGTH;
}
uint ha_sdb::max_supported_keys() const {
return MAX_KEY;
}
uint ha_sdb::max_supported_key_length() const {
return 4096;
}
#if MYSQL_VERSION_ID >= 50723
uint ha_sdb::max_supported_key_part_length(
HA_CREATE_INFO *create_info MY_ATTRIBUTE((unused))) const {
return max_supported_key_length();
}
#else
uint ha_sdb::max_supported_key_part_length() const {
return max_supported_key_length();
}
#endif
int ha_sdb::open(const char *name, int mode, uint test_if_locked) {
int rc = 0;
Sdb_conn *connection = NULL;
Sdb_cl cl;
if (!(share = get_sdb_share(name, table))) {
rc = HA_ERR_OUT_OF_MEM;
goto error;
}
rc = sdb_parse_table_name(name, db_name, SDB_CS_NAME_MAX_SIZE, table_name,
SDB_CL_NAME_MAX_SIZE);
if (rc != 0) {
SDB_LOG_ERROR("Table name[%s] can't be parsed. rc: %d", name, rc);
goto error;
}
if (sdb_is_tmp_table(name, table_name)) {
DBUG_ASSERT(table->s->tmp_table);
if (0 != sdb_rebuild_db_name_of_temp_table(db_name, SDB_CS_NAME_MAX_SIZE)) {
rc = HA_ERR_GENERIC;
goto error;
}
}
connection = check_sdb_in_thd(ha_thd(), true);
if (NULL == connection) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(connection->thread_id() == ha_thd()->thread_id());
// Get collection to check if the collection is available.
rc = connection->get_cl(db_name, table_name, cl);
if (0 != rc) {
SDB_LOG_ERROR("Collection[%s.%s] is not available. rc: %d", db_name,
table_name, rc);
goto error;
}
thr_lock_data_init(&share->lock, &lock_data, (void *)this);
ref_length = SDB_OID_LEN; // length of _id
stats.mrr_length_per_rec = ref_length + sizeof(void *);
/* max_data_file_length and max_index_file_length are actually not used in
* cost estimate.
*/
stats.max_data_file_length = 8LL * 1024 * 1024 * 1024 * 1024; // 8TB
stats.max_index_file_length = 8LL * 1024 * 1024 * 1024 * 1024; // 8TB
stats.table_in_mem_estimate = 0;
rc = update_stats(ha_thd(), true);
if (0 != rc) {
goto error;
}
done:
return rc;
error:
if (share) {
free_sdb_share(share);
share = NULL;
}
goto done;
}
int ha_sdb::close(void) {
if (NULL != collection) {
delete collection;
collection = NULL;
}
if (share) {
free_sdb_share(share);
share = NULL;
}
m_bulk_insert_rows.clear();
m_bson_element_cache.release();
return 0;
}
int ha_sdb::reset() {
if (NULL != collection) {
delete collection;
collection = NULL;
}
// don't release bson element cache, so that we can reuse it
m_bulk_insert_rows.clear();
free_root(&blobroot, MYF(0));
m_lock_type = TL_IGNORE;
pushed_condition = SDB_EMPTY_BSON;
m_ignore_dup_key = false;
m_write_can_replace = false;
m_use_bulk_insert = false;
return 0;
}
int ha_sdb::row_to_obj(uchar *buf, bson::BSONObj &obj, bool gen_oid,
bool output_null, bson::BSONObj &null_obj) {
int rc = 0;
bson::BSONObjBuilder obj_builder;
bson::BSONObjBuilder null_obj_builder;
my_bitmap_map *org_bitmap = dbug_tmp_use_all_columns(table, table->read_set);
if (buf != table->record[0]) {
repoint_field_to_record(table, table->record[0], buf);
}
if (gen_oid) {
// Generate and assign an OID for the _id field.
// _id should be the first element for good performance.
obj_builder.genOID();
}
for (Field **field = table->field; *field; field++) {
if ((*field)->is_null()) {
if (output_null) {
null_obj_builder.append((*field)->field_name, "");
}
} else {
rc = field_to_obj(*field, obj_builder);
if (0 != rc) {
goto error;
}
}
}
obj = obj_builder.obj();
null_obj = null_obj_builder.obj();
done:
if (buf != table->record[0]) {
repoint_field_to_record(table, buf, table->record[0]);
}
dbug_tmp_restore_column_map(table->read_set, org_bitmap);
return rc;
error:
goto done;
}
int ha_sdb::field_to_obj(Field *field, bson::BSONObjBuilder &obj_builder) {
int rc = 0;
DBUG_ASSERT(NULL != field);
switch (field->type()) {
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_YEAR: {
// overflow is impossible, store as INT32
DBUG_ASSERT(field->val_int() <= INT_MAX32 &&
field->val_int() >= INT_MIN32);
obj_builder.append(field->field_name, (int)field->val_int());
break;
}
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_LONG: {
longlong value = field->val_int();
if (value > INT_MAX32 || value < INT_MIN32) {
// overflow, so store as INT64
obj_builder.append(field->field_name, (long long)value);
} else {
obj_builder.append(field->field_name, (int)value);
}
break;
}
case MYSQL_TYPE_LONGLONG: {
longlong value = field->val_int();
if (value < 0 && ((Field_num *)field)->unsigned_flag) {
// overflow, so store as DECIMAL
my_decimal tmp_val;
char buff[MAX_FIELD_WIDTH];
String str(buff, sizeof(buff), field->charset());
((Field_num *)field)->val_decimal(&tmp_val);
my_decimal2string(E_DEC_FATAL_ERROR, &tmp_val, 0, 0, 0, &str);
obj_builder.appendDecimal(field->field_name, str.c_ptr());
} else {
obj_builder.append(field->field_name, (long long)value);
}
break;
}
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
case MYSQL_TYPE_TIME: {
obj_builder.append(field->field_name, field->val_real());
break;
}
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB: {
String val_tmp;
field->val_str(&val_tmp);
if (((Field_str *)field)->binary()) {
obj_builder.appendBinData(field->field_name, val_tmp.length(),
bson::BinDataGeneral, val_tmp.ptr());
} else {
String conv_str;
String *str = &val_tmp;
if (!my_charset_same(str->charset(), &SDB_CHARSET)) {
rc = sdb_convert_charset(*str, conv_str, &SDB_CHARSET);
if (rc) {
goto error;
}
str = &conv_str;
}
obj_builder.appendStrWithNoTerminating(field->field_name, str->ptr(),
str->length());
}
break;
}
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_DECIMAL: {
Field_decimal *f = (Field_decimal *)field;
int precision = (int)(f->pack_length());
int scale = (int)(f->decimals());
if (precision < 0 || scale < 0) {
rc = -1;
goto error;
}
char buff[MAX_FIELD_WIDTH];
String str(buff, sizeof(buff), field->charset());
String unused;
f->val_str(&str, &unused);
obj_builder.appendDecimal(field->field_name, str.c_ptr());
break;
}
case MYSQL_TYPE_DATE: {
longlong date_val = 0;
date_val = ((Field_newdate *)field)->val_int();
struct tm tm_val;
tm_val.tm_sec = 0;
tm_val.tm_min = 0;
tm_val.tm_hour = 0;
tm_val.tm_mday = date_val % 100;
date_val = date_val / 100;
tm_val.tm_mon = date_val % 100 - 1;
date_val = date_val / 100;
tm_val.tm_year = date_val - 1900;
tm_val.tm_wday = 0;
tm_val.tm_yday = 0;
tm_val.tm_isdst = 0;
time_t time_tmp = mktime(&tm_val);
bson::Date_t dt((longlong)(time_tmp * 1000));
obj_builder.appendDate(field->field_name, dt);
break;
}
case MYSQL_TYPE_TIMESTAMP2:
case MYSQL_TYPE_TIMESTAMP: {
struct timeval tm;
int warnings = 0;
field->get_timestamp(&tm, &warnings);
obj_builder.appendTimestamp(field->field_name, tm.tv_sec * 1000,
tm.tv_usec);
break;
}
case MYSQL_TYPE_NULL:
// skip the null value
break;
case MYSQL_TYPE_DATETIME: {
char buff[MAX_FIELD_WIDTH];
String str(buff, sizeof(buff), field->charset());
field->val_str(&str);
obj_builder.append(field->field_name, str.c_ptr());
break;
}
case MYSQL_TYPE_JSON: {
Json_wrapper wr;
String buf;
Field_json *field_json = dynamic_cast<Field_json *>(field);
#if MYSQL_VERSION_ID >= 50722
if (field_json->val_json(&wr) || wr.to_binary(&buf)) {
#else
if (field_json->val_json(&wr) || wr.to_value().raw_binary(&buf)) {
#endif
my_error(ER_INVALID_JSON_BINARY_DATA, MYF(0));
rc = ER_INVALID_JSON_BINARY_DATA;
goto error;
}
obj_builder.appendBinData(field->field_name, buf.length(),
bson::BinDataGeneral, buf.ptr());
break;
}
default: {
SDB_PRINT_ERROR(ER_BAD_FIELD_ERROR, ER(ER_BAD_FIELD_ERROR),
field->field_name, table_name);
rc = ER_BAD_FIELD_ERROR;
goto error;
}
}
done:
return rc;
error:
goto done;
}
/*
If table has unique keys, we can match a specific record by the value of
unique key instead of the whole record.
@return false if success
*/
my_bool ha_sdb::get_unique_key_cond(const uchar *rec_row, bson::BSONObj &cond) {
my_bool rc = true;
// force cast to adapt sql layer unreasonable interface.
uchar *row = const_cast<uchar *>(rec_row);
my_bitmap_map *org_bitmap = dbug_tmp_use_all_columns(table, table->read_set);
if (row != table->record[0]) {
repoint_field_to_record(table, table->record[0], row);
}
// 1. match by primary key
uint index_no = table->s->primary_key;
if (index_no < MAX_KEY) {
const KEY *primary_key = table->s->key_info + index_no;
rc = get_cond_from_key(primary_key, cond);
if (!rc) {
goto done;
}
}
// 2. match by other unique index fields.
for (uint i = 0; i < table->s->keys; ++i) {
const KEY *key_info = table->s->key_info + i;
if (key_info->flags & HA_NOSAME) {
rc = get_cond_from_key(key_info, cond);
if (!rc) {
goto done;
}
}
}
done:
if (row != table->record[0]) {
repoint_field_to_record(table, row, table->record[0]);
}
dbug_tmp_restore_column_map(table->read_set, org_bitmap);
return rc;
}
/*
@return false if success
*/
my_bool ha_sdb::get_cond_from_key(const KEY *unique_key, bson::BSONObj &cond) {
my_bool rc = true;
const KEY_PART_INFO *key_part = unique_key->key_part;
const KEY_PART_INFO *key_end = key_part + unique_key->user_defined_key_parts;
my_bool all_field_null = true;
bson::BSONObjBuilder builder;
for (; key_part != key_end; ++key_part) {
Field *field = table->field[key_part->fieldnr - 1];
if (!field->is_null()) {
if (SDB_ERR_OK != field_to_obj(field, builder)) {
rc = true;
goto error;
}
all_field_null = false;
} else {
bson::BSONObjBuilder sub_builder(builder.subobjStart(field->field_name));
sub_builder.append("$isnull", 1);
sub_builder.doneFast();
}
}
// If all fields are NULL, more than one record may be matched!
if (all_field_null) {
rc = true;
goto error;
}
cond = builder.obj();
rc = false;
done:
return rc;
error:
goto done;
}
int ha_sdb::get_update_obj(const uchar *old_data, uchar *new_data,
bson::BSONObj &obj, bson::BSONObj &null_obj) {
int rc = 0;
uint row_offset = (uint)(old_data - new_data);
bson::BSONObjBuilder obj_builder;
bson::BSONObjBuilder null_obj_builder;
my_bitmap_map *org_bitmap = dbug_tmp_use_all_columns(table, table->read_set);
if (new_data != table->record[0]) {
repoint_field_to_record(table, table->record[0], new_data);
}
for (Field **fields = table->field; *fields; fields++) {
Field *field = *fields;
bool is_null = field->is_null();
if (is_null != field->is_null_in_record(old_data)) {
if (is_null) {
null_obj_builder.append(field->field_name, "");
} else {
rc = field_to_obj(field, obj_builder);
if (0 != rc) {
goto error;
}
}
} else if (!is_null) {
if (field->cmp_binary_offset(row_offset) != 0) {
rc = field_to_obj(field, obj_builder);
if (0 != rc) {
goto error;
}
}
}
}
obj = obj_builder.obj();
null_obj = null_obj_builder.obj();
done:
if (new_data != table->record[0]) {
repoint_field_to_record(table, new_data, table->record[0]);
}
dbug_tmp_restore_column_map(table->read_set, org_bitmap);
return rc;
error:
goto done;
}
void ha_sdb::start_bulk_insert(ha_rows rows) {
if (!sdb_use_bulk_insert) {
m_use_bulk_insert = false;
return;
}
m_bulk_insert_rows.clear();
/**
We don't bother with bulk-insert semantics when the estimated rows == 1
The rows value will be 0 if the server does not know how many rows
would be inserted. This can occur when performing INSERT...SELECT
*/
if (rows == 1) {
m_use_bulk_insert = false;
return;
}
m_use_bulk_insert = true;
}
int ha_sdb::flush_bulk_insert() {
DBUG_ASSERT(m_bulk_insert_rows.size() > 0);
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
int flag = 0;
if (m_write_can_replace) {
flag = FLG_INSERT_REPLACEONDUP;
} else if (m_ignore_dup_key) {
flag = FLG_INSERT_CONTONDUP;
}
int rc = collection->bulk_insert(flag, m_bulk_insert_rows);
if (rc != 0) {
if (SDB_IXM_DUP_KEY == get_sdb_code(rc)) {
// convert to MySQL errcode
rc = HA_ERR_FOUND_DUPP_KEY;
}
}
stats.records += m_bulk_insert_rows.size();
m_bulk_insert_rows.clear();
return rc;
}
int ha_sdb::end_bulk_insert() {
int rc = 0;
if (m_use_bulk_insert) {
m_use_bulk_insert = false;
if (m_bulk_insert_rows.size() > 0) {
rc = flush_bulk_insert();
}
}
return rc;
}
int ha_sdb::write_row(uchar *buf) {
int rc = 0;
bson::BSONObj obj;
bson::BSONObj tmp_obj;
ha_statistic_increment(&SSV::ha_write_count);
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
rc = row_to_obj(buf, obj, TRUE, FALSE, tmp_obj);
if (rc != 0) {
goto error;
}
if (m_use_bulk_insert) {
m_bulk_insert_rows.push_back(obj);
if ((int)m_bulk_insert_rows.size() >= sdb_bulk_insert_size) {
rc = flush_bulk_insert();
if (rc != 0) {
goto error;
}
}
} else {
// TODO: SequoiaDB C++ driver currently has no insert() method with a flag,
// we need send FLG_INSERT_CONTONDUP flag to server to ignore duplicate key
// error, so that SequoiaDB will not rollback transaction, here we
// temporarily use bulk_insert() instead, this should be revised when driver
// add new method in new version.
std::vector<bson::BSONObj> row(1, obj);
int flag = 0;
if (m_write_can_replace) {
flag = FLG_INSERT_REPLACEONDUP;
} else if (m_ignore_dup_key) {
flag = FLG_INSERT_CONTONDUP;
}
rc = collection->bulk_insert(flag, row);
if (rc != 0) {
if (SDB_IXM_DUP_KEY == get_sdb_code(rc)) {
// convert to MySQL errcode
rc = HA_ERR_FOUND_DUPP_KEY;
}
goto error;
}
stats.records++;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::update_row(const uchar *old_data, uchar *new_data) {
int rc = 0;
bson::BSONObj cond;
bson::BSONObj new_obj;
bson::BSONObj null_obj;
bson::BSONObj rule_obj;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
ha_statistic_increment(&SSV::ha_update_count);
rc = get_update_obj(old_data, new_data, new_obj, null_obj);
if (rc != 0) {
if (HA_ERR_UNKNOWN_CHARSET == rc && m_ignore_dup_key) {
rc = 0;
} else {
goto error;
}
}
if (null_obj.isEmpty()) {
rule_obj = BSON("$set" << new_obj);
} else {
rule_obj = BSON("$set" << new_obj << "$unset" << null_obj);
}
if (get_unique_key_cond(old_data, cond)) {
cond = cur_rec;
}
rc = collection->update(rule_obj, cond, SDB_EMPTY_BSON,
UPDATE_KEEP_SHARDINGKEY);
if (rc != 0) {
if (SDB_IXM_DUP_KEY == get_sdb_code(rc)) {
// convert to MySQL errcode
rc = HA_ERR_FOUND_DUPP_KEY;
}
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::delete_row(const uchar *buf) {
int rc = 0;
bson::BSONObj cond;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
ha_statistic_increment(&SSV::ha_delete_count);
if (get_unique_key_cond(buf, cond)) {
cond = cur_rec;
}
rc = collection->del(cond);
if (rc != 0) {
goto error;
}
stats.records--;
done:
return rc;
error:
goto done;
}
int ha_sdb::index_next(uchar *buf) {
int rc = 0;
DBUG_ASSERT(idx_order_direction == 1);
ha_statistic_increment(&SSV::ha_read_next_count);
rc = next_row(cur_rec, buf);
if (rc != 0) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::index_prev(uchar *buf) {
int rc = 0;
DBUG_ASSERT(idx_order_direction == -1);
ha_statistic_increment(&SSV::ha_read_prev_count);
rc = next_row(cur_rec, buf);
if (rc != 0) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::index_last(uchar *buf) {
int rc = 0;
ha_statistic_increment(&SSV::ha_read_last_count);
rc = index_read_one(pushed_condition, -1, buf);
if (rc) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::index_first(uchar *buf) {
int rc = 0;
ha_statistic_increment(&SSV::ha_read_first_count);
rc = index_read_one(pushed_condition, 1, buf);
if (rc) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::index_read_map(uchar *buf, const uchar *key_ptr,
key_part_map keypart_map,
enum ha_rkey_function find_flag) {
int rc = 0;
bson::BSONObjBuilder cond_builder;
bson::BSONObj condition = pushed_condition;
bson::BSONObj condition_idx;
int order_direction = 1;
ha_statistic_increment(&SSV::ha_read_key_count);
if (NULL != key_ptr && active_index < MAX_KEY) {
KEY *key_info = table->key_info + active_index;
key_range start_key;
start_key.key = key_ptr;
start_key.length = calculate_key_len(table, active_index, keypart_map);
start_key.keypart_map = keypart_map;
start_key.flag = find_flag;
rc = sdb_create_condition_from_key(table, key_info, &start_key, end_range,
0, (NULL != end_range) ? eq_range : 0,
condition_idx);
if (0 != rc) {
SDB_LOG_ERROR("Fail to build index match object. rc: %d", rc);
goto error;
}
order_direction = sdb_get_key_direction(find_flag);
}
if (!condition.isEmpty()) {
if (!condition_idx.isEmpty()) {
bson::BSONArrayBuilder arr_builder;
arr_builder.append(condition);
arr_builder.append(condition_idx);
condition = BSON("$and" << arr_builder.arr());
}
} else {
condition = condition_idx;
}
rc = index_read_one(condition, order_direction, buf);
if (rc) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::index_read_one(bson::BSONObj condition, int order_direction,
uchar *buf) {
int rc = 0;
bson::BSONObj hint;
bson::BSONObj order_by;
int flag = 0;
KEY *key_info = table->key_info + active_index;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
DBUG_ASSERT(NULL != key_info);
DBUG_ASSERT(NULL != key_info->name);
hint = BSON("" << key_info->name);
idx_order_direction = order_direction;
rc = sdb_get_idx_order(key_info, order_by, order_direction);
if (rc) {
SDB_LOG_ERROR("Fail to get index order. rc: %d", rc);
goto error;
}
flag = get_query_flag(thd_sql_command(ha_thd()), m_lock_type);
rc =
collection->query(condition, SDB_EMPTY_BSON, order_by, hint, 0, -1, flag);
if (rc) {
SDB_LOG_ERROR(
"Collection[%s.%s] failed to query with "
"condition[%s], order[%s], hint[%s]. rc: %d",
collection->get_cs_name(), collection->get_cl_name(),
condition.toString().c_str(), order_by.toString().c_str(),
hint.toString().c_str(), rc);
goto error;
}
rc = (1 == order_direction) ? index_next(buf) : index_prev(buf);
switch (rc) {
case SDB_OK: {
table->status = 0;
break;
}
case SDB_DMS_EOC:
case HA_ERR_END_OF_FILE: {
rc = HA_ERR_KEY_NOT_FOUND;
table->status = STATUS_NOT_FOUND;
break;
}
default: {
table->status = STATUS_NOT_FOUND;
break;
}
}
done:
return rc;
error:
goto done;
}
int ha_sdb::index_init(uint idx, bool sorted) {
active_index = idx;
if (!pushed_cond) {
pushed_condition = SDB_EMPTY_BSON;
}
free_root(&blobroot, MYF(0));
return 0;
}
int ha_sdb::index_end() {
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
collection->close();
active_index = MAX_KEY;
return 0;
}
int ha_sdb::rnd_init(bool scan) {
first_read = true;
if (!pushed_cond) {
pushed_condition = SDB_EMPTY_BSON;
}
free_root(&blobroot, MYF(0));
return 0;
}
int ha_sdb::rnd_end() {
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
collection->close();
return 0;
}
int ha_sdb::obj_to_row(bson::BSONObj &obj, uchar *buf) {
int rc = SDB_ERR_OK;
THD *thd = table->in_use;
my_bool is_select = (SQLCOM_SELECT == thd_sql_command(thd));
memset(buf, 0, table->s->null_bytes);
// allow zero date
sql_mode_t old_sql_mode = thd->variables.sql_mode;
thd->variables.sql_mode &= ~(MODE_NO_ZERO_DATE | MODE_NO_ZERO_IN_DATE);
// ignore field warning
enum_check_fields old_check_fields = thd->count_cuted_fields;
thd->count_cuted_fields = CHECK_FIELD_IGNORE;
/* Avoid asserts in ::store() for columns that are not going to be updated */
my_bitmap_map *org_bitmap = dbug_tmp_use_all_columns(table, table->write_set);
bson::BSONObjIterator iter(obj);
if (is_select && bitmap_is_clear_all(table->read_set)) {
// no field need to read
goto done;
}
rc = m_bson_element_cache.ensure(table->s->fields);
if (SDB_ERR_OK != rc) {
goto error;
}
for (Field **fields = table->field; *fields; fields++) {
Field *field = *fields;
bson::BSONElement elem;
// we only skip non included fields when SELECT.
if (is_select && !bitmap_is_set(table->read_set, field->field_index)) {
continue;
}
if (!m_bson_element_cache[field->field_index].eoo()) {
elem = m_bson_element_cache[field->field_index];
} else {
while (iter.more()) {
bson::BSONElement elem_tmp = iter.next();
if (strcmp(elem_tmp.fieldName(), field->field_name) == 0) {
// current element match the field
elem = elem_tmp;
break;
}
if (strcmp(elem_tmp.fieldName(), SDB_OID_FIELD) == 0) {
// ignore _id
continue;
}
// find matched field to store the element
for (Field **next_fields = fields + 1; *next_fields; next_fields++) {
Field *next_field = *next_fields;
if (strcmp(elem_tmp.fieldName(), next_field->field_name) == 0) {
m_bson_element_cache[next_field->field_index] = elem_tmp;
break;
}
}
}
}
field->reset();
if (elem.eoo() || elem.isNull() || bson::Undefined == elem.type()) {
field->set_null();
continue;
}
rc = bson_element_to_field(elem, field);
if (0 != rc) {
goto error;
}
}
done:
dbug_tmp_restore_column_map(table->write_set, org_bitmap);
thd->count_cuted_fields = old_check_fields;
thd->variables.sql_mode = old_sql_mode;
return rc;
error:
goto done;
}
int ha_sdb::bson_element_to_field(const bson::BSONElement elem, Field *field) {
int rc = SDB_ERR_OK;
DBUG_ASSERT(0 == strcmp(elem.fieldName(), field->field_name));
switch (elem.type()) {
case bson::NumberInt:
case bson::NumberLong: {
longlong nr = elem.numberLong();
field->store(nr, false);
break;
}
case bson::NumberDouble: {
double nr = elem.numberDouble();
field->store(nr);
break;
}
case bson::BinData: {
int lenTmp = 0;
const char *dataTmp = elem.binData(lenTmp);
if (MYSQL_TYPE_JSON != field->type()) {
field->store(dataTmp, lenTmp, &my_charset_bin);
} else {
Field_json *field_json = dynamic_cast<Field_json *>(field);
json_binary::Value v = json_binary::parse_binary(dataTmp, lenTmp);
Json_wrapper wr(v);
field_json->store_json(&wr);
}
break;
}
case bson::String: {
// datetime is stored as string
field->store(elem.valuestr(), elem.valuestrsize() - 1, &SDB_CHARSET);
break;
}
case bson::NumberDecimal: {
bson::bsonDecimal valTmp = elem.numberDecimal();
string strValTmp = valTmp.toString();
field->store(strValTmp.c_str(), strValTmp.length(), &my_charset_bin);
break;
}
case bson::Date: {
MYSQL_TIME time_val;
struct timeval tv;
struct tm tm_val;
longlong millisec = (longlong)(elem.date());
tv.tv_sec = millisec / 1000;
tv.tv_usec = millisec % 1000 * 1000;
localtime_r((const time_t *)(&tv.tv_sec), &tm_val);
time_val.year = tm_val.tm_year + 1900;
time_val.month = tm_val.tm_mon + 1;
time_val.day = tm_val.tm_mday;
time_val.hour = 0;
time_val.minute = 0;
time_val.second = 0;
time_val.second_part = 0;
time_val.neg = 0;
time_val.time_type = MYSQL_TIMESTAMP_DATE;
if ((time_val.month < 1 || time_val.day < 1) ||
(time_val.year > 9999 || time_val.month > 12 || time_val.day > 31)) {
// Invalid date, the field has been reset to zero,
// so no need to store.
} else {
field->store_time(&time_val, 0);
}
break;
}
case bson::Timestamp: {
struct timeval tv;
longlong millisec = (longlong)(elem.timestampTime());
longlong microsec = elem.timestampInc();
tv.tv_sec = millisec / 1000;
tv.tv_usec = millisec % 1000 * 1000 + microsec;
field->store_timestamp(&tv);
break;
}
case bson::Bool: {
bool val = elem.boolean();
field->store(val ? 1 : 0, true);
break;
}
case bson::Object:
default:
rc = SDB_ERR_TYPE_UNSUPPORTED;
goto error;
}
if (field->flags & BLOB_FLAG) {
Field_blob *blob = (Field_blob *)field;
uchar *src, *dst;
uint length, packlength;
packlength = blob->pack_length_no_ptr();
length = blob->get_length(blob->ptr);
memcpy(&src, blob->ptr + packlength, sizeof(char *));
if (src) {
dst = (uchar *)alloc_root(&blobroot, length);
memmove(dst, src, length);
memcpy(blob->ptr + packlength, &dst, sizeof(char *));
}
}
done:
return rc;
error:
goto done;
}
int ha_sdb::cur_row(uchar *buf) {
int rc = 0;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
rc = collection->current(cur_rec);
if (rc != 0) {
goto error;
}
rc = obj_to_row(cur_rec, buf);
if (rc != 0) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::next_row(bson::BSONObj &obj, uchar *buf) {
int rc = 0;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
rc = collection->next(obj);
if (rc != 0) {
if (HA_ERR_END_OF_FILE == rc) {
table->status = STATUS_NOT_FOUND;
}
goto error;
}
rc = obj_to_row(obj, buf);
if (rc != 0) {
goto error;
}
table->status = 0;
done:
return rc;
error:
goto done;
}
int ha_sdb::rnd_next(uchar *buf) {
int rc = 0;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
if (first_read) {
int flag = get_query_flag(thd_sql_command(ha_thd()), m_lock_type);
rc = collection->query(pushed_condition, SDB_EMPTY_BSON, SDB_EMPTY_BSON,
SDB_EMPTY_BSON, 0, -1, flag);
if (rc != 0) {
goto error;
}
first_read = false;
}
ha_statistic_increment(&SSV::ha_read_rnd_next_count);
rc = next_row(cur_rec, buf);
if (rc != 0) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::rnd_pos(uchar *buf, uchar *pos) {
int rc = 0;
bson::BSONObjBuilder objBuilder;
bson::OID oid;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
ha_statistic_increment(&SSV::ha_read_rnd_count);
memcpy((void *)oid.getData(), pos, SDB_OID_LEN);
objBuilder.appendOID(SDB_OID_FIELD, &oid);
bson::BSONObj oidObj = objBuilder.obj();
rc = collection->query_one(cur_rec, oidObj);
if (rc) {
goto error;
}
rc = obj_to_row(cur_rec, buf);
if (rc != 0) {
goto error;
}
done:
return rc;
error:
goto done;
}
void ha_sdb::position(const uchar *record) {
bson::BSONElement beField;
if (cur_rec.getObjectID(beField)) {
bson::OID oid = beField.__oid();
memcpy(ref, oid.getData(), SDB_OID_LEN);
if (beField.type() != bson::jstOID) {
SDB_LOG_ERROR("Unexpected _id's type: %d ", beField.type());
}
}
return;
}
int ha_sdb::info(uint flag) {
int rc = 0;
if (flag & HA_STATUS_VARIABLE) {
if (!(flag & HA_STATUS_NO_LOCK) || stats.records == ~(ha_rows)0) {
rc = update_stats(ha_thd(), !(flag & HA_STATUS_NO_LOCK));
if (0 != rc) {
goto error;
}
}
}
if (flag & HA_STATUS_TIME) {
stats.create_time = 0;
stats.check_time = 0;
stats.update_time = 0;
}
if (flag & HA_STATUS_AUTO) {
stats.auto_increment_value = 0;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::update_stats(THD *thd, bool do_read_stat) {
Sdb_statistics stat;
int rc = 0;
do {
if (share && !do_read_stat) {
share->mutex.lock();
stat = share->stat;
share->mutex.unlock();
DBUG_ASSERT(stat.total_records != ~(int64)0); // should never be invalid
/* Accept shared cached statistics if total_records is valid. */
if (stat.total_records != ~(int64)0) {
break;
}
}
/* Request statistics from SequoiaDB */
Sdb_conn *conn = check_sdb_in_thd(thd, true);
if (NULL == conn) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(conn->thread_id() == thd->thread_id());
rc = conn->get_cl_statistics(db_name, table_name, stat);
if (0 != rc) {
goto done;
}
/* Update shared statistics with fresh data */
if (share) {
Sdb_mutex_guard guard(share->mutex);
share->stat = stat;
}
break;
} while (0);
stats.block_size = (uint)stat.page_size;
stats.data_file_length = (ulonglong)stat.total_data_pages * stat.page_size;
stats.index_file_length = (ulonglong)stat.total_index_pages * stat.page_size;
stats.delete_length = (ulonglong)stat.total_data_free_space;
stats.records = (ha_rows)stat.total_records;
stats.mean_rec_length =
(0 == stats.records)
? 0
: (ulong)((stats.data_file_length - stats.delete_length) /
stats.records);
done:
return rc;
error:
goto done;
}
int ha_sdb::extra(enum ha_extra_function operation) {
switch (operation) {
case HA_EXTRA_IGNORE_DUP_KEY: /* Dup keys don't rollback everything*/
m_ignore_dup_key = true;
break;
case HA_EXTRA_NO_IGNORE_DUP_KEY:
m_ignore_dup_key = false;
break;
case HA_EXTRA_WRITE_CAN_REPLACE:
m_write_can_replace = true;
break;
case HA_EXTRA_WRITE_CANNOT_REPLACE:
m_write_can_replace = false;
break;
default:
break;
}
return 0;
}
int ha_sdb::ensure_collection(THD *thd) {
int rc = 0;
DBUG_ASSERT(NULL != thd);
if (NULL != collection && collection->thread_id() != thd->thread_id()) {
delete collection;
collection = NULL;
}
if (NULL == collection) {
Sdb_conn *conn = check_sdb_in_thd(thd, true);
if (NULL == conn) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(conn->thread_id() == thd->thread_id());
collection = new (std::nothrow) Sdb_cl();
if (NULL == collection) {
rc = HA_ERR_OUT_OF_MEM;
goto error;
}
conn->get_cl(db_name, table_name, *collection);
if (0 != rc) {
delete collection;
collection = NULL;
SDB_LOG_ERROR("Collection[%s.%s] is not available. rc: %d", db_name,
table_name, rc);
goto error;
}
}
done:
return rc;
error:
goto done;
}
int ha_sdb::start_statement(THD *thd, uint table_count) {
int rc = 0;
rc = ensure_collection(thd);
if (0 != rc) {
goto error;
}
if (0 == table_count) {
Sdb_conn *conn = check_sdb_in_thd(thd, true);
if (NULL == conn) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(conn->thread_id() == thd->thread_id());
if (thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) {
if (!conn->is_transaction_on()) {
rc = conn->begin_transaction();
if (rc != 0) {
goto error;
}
trans_register_ha(thd, TRUE, ht, NULL);
}
} else {
// autocommit
if (sdb_use_autocommit && !conn->is_transaction_on()) {
rc = conn->begin_transaction();
if (rc != 0) {
goto error;
}
trans_register_ha(thd, FALSE, ht, NULL);
}
}
} else {
// there is more than one handler involved
}
done:
return rc;
error:
goto done;
}
int ha_sdb::external_lock(THD *thd, int lock_type) {
int rc = 0;
Thd_sdb *thd_sdb = NULL;
if (NULL == check_sdb_in_thd(thd)) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
thd_sdb = thd_get_thd_sdb(thd);
if (F_UNLCK != lock_type) {
rc = start_statement(thd, thd_sdb->lock_count++);
if (0 != rc) {
thd_sdb->lock_count--;
goto error;
}
} else {
if (!--thd_sdb->lock_count) {
if (!(thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) &&
thd_sdb->get_conn()->is_transaction_on()) {
/*
Unlock is done without a transaction commit / rollback.
This happens if the thread didn't update any rows
We must in this case close the transaction to release resources
*/
if (thd->is_error()) {
rc = thd_sdb->get_conn()->rollback_transaction();
} else {
rc = thd_sdb->get_conn()->commit_transaction();
}
if (0 != rc) {
goto error;
}
}
}
}
done:
return rc;
error:
goto done;
}
int ha_sdb::start_stmt(THD *thd, thr_lock_type lock_type) {
int rc = 0;
Thd_sdb *thd_sdb = thd_get_thd_sdb(thd);
m_lock_type = lock_type;
rc = start_statement(thd, thd_sdb->start_stmt_count++);
if (0 != rc) {
thd_sdb->start_stmt_count--;
}
return rc;
}
enum_alter_inplace_result ha_sdb::check_if_supported_inplace_alter(
TABLE *altered_table, Alter_inplace_info *ha_alter_info) {
enum_alter_inplace_result rs;
KEY *keyInfo;
if (ha_alter_info->handler_flags & ~INPLACE_ONLINE_OPERATIONS) {
// include offline-operations
// rs = handler::check_if_supported_inplace_alter(
// altered_table, ha_alter_info ) ;
rs = HA_ALTER_INPLACE_NOT_SUPPORTED;
goto done;
}
keyInfo = ha_alter_info->key_info_buffer;
for (; keyInfo < ha_alter_info->key_info_buffer + ha_alter_info->key_count;
keyInfo++) {
KEY_PART_INFO *keyPart;
KEY_PART_INFO *keyEnd;
/*if ( ( keyInfo->flags & HA_FULLTEXT )
|| ( keyInfo->flags & HA_PACK_KEY )
|| ( keyInfo->flags & HA_BINARY_PACK_KEY ))
{
rs = HA_ALTER_INPLACE_NOT_SUPPORTED ;
goto done ;
}*/
keyPart = keyInfo->key_part;
keyEnd = keyPart + keyInfo->user_defined_key_parts;
for (; keyPart < keyEnd; keyPart++) {
keyPart->field = altered_table->field[keyPart->fieldnr];
keyPart->null_offset = keyPart->field->null_offset();
keyPart->null_bit = keyPart->field->null_bit;
if (keyPart->field->flags & AUTO_INCREMENT_FLAG) {
rs = HA_ALTER_INPLACE_NOT_SUPPORTED;
goto done;
}
}
}
rs = HA_ALTER_INPLACE_NO_LOCK;
done:
return rs;
}
bool ha_sdb::prepare_inplace_alter_table(TABLE *altered_table,
Alter_inplace_info *ha_alter_info) {
/*THD *thd = current_thd ;
bool rs = false ;
switch( thd_sql_command(thd) )
{
case SQLCOM_CREATE_INDEX:
case SQLCOM_DROP_INDEX:
rs = false ;
break ;
default:
rs = true ;
goto error ;
}
done:
return rs ;
error:
goto done ;*/
return false;
}
int ha_sdb::create_index(Sdb_cl &cl, Alter_inplace_info *ha_alter_info,
Bitmap<MAX_INDEXES> &ignored_keys) {
int rc = 0;
const KEY *key_info = NULL;
uint key_nr = 0;
for (uint i = 0; i < ha_alter_info->index_add_count; i++) {
if (ignored_keys.is_set(i)) {
continue;
}
key_nr = ha_alter_info->index_add_buffer[i];
key_info = &ha_alter_info->key_info_buffer[key_nr];
rc = sdb_create_index(key_info, cl);
if (rc) {
goto error;
}
}
done:
return rc;
error:
goto done;
}
int ha_sdb::drop_index(Sdb_cl &cl, Alter_inplace_info *ha_alter_info,
Bitmap<MAX_INDEXES> &ignored_keys) {
int rc = 0;
if (NULL == ha_alter_info->index_drop_buffer) {
goto done;
}
for (uint i = 0; i < ha_alter_info->index_drop_count; i++) {
if (ignored_keys.is_set(i)) {
continue;
}
KEY *key_info = ha_alter_info->index_drop_buffer[i];
rc = cl.drop_index(key_info->name);
if (rc) {
goto error;
}
}
done:
return rc;
error:
goto done;
}
bool ha_sdb::inplace_alter_table(TABLE *altered_table,
Alter_inplace_info *ha_alter_info) {
bool rs = true;
int rc = 0;
THD *thd = current_thd;
Sdb_conn *conn = NULL;
Sdb_cl cl;
Bitmap<MAX_INDEXES> ignored_drop_keys;
Bitmap<MAX_INDEXES> ignored_add_keys;
DBUG_ASSERT(ha_alter_info->handler_flags | INPLACE_ONLINE_OPERATIONS);
conn = check_sdb_in_thd(thd, true);
if (NULL == conn) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
rc = conn->get_cl(db_name, table_name, cl);
if (0 != rc) {
SDB_LOG_ERROR("Collection[%s.%s] is not available. rc: %d", db_name,
table_name, rc);
goto error;
}
if (ha_alter_info->handler_flags & Alter_inplace_info::CHANGE_CREATE_OPTION) {
char *old_comment = table->s->comment.str;
char *new_comment = ha_alter_info->create_info->comment.str;
if (!(old_comment == new_comment ||
strcmp(old_comment, new_comment) == 0)) {
my_error(HA_ERR_WRONG_COMMAND, MYF(0));
goto error;
}
}
// If it's a redefinition of the secondary attributes, such as btree/hash and
// comment, don't recreate the index.
if (ha_alter_info->handler_flags & INPLACE_ONLINE_DROPIDX &&
ha_alter_info->handler_flags & INPLACE_ONLINE_ADDIDX) {
for (uint i = 0; i < ha_alter_info->index_drop_count; i++) {
KEY *drop_key = ha_alter_info->index_drop_buffer[i];
for (uint j = 0; j < ha_alter_info->index_add_count; j++) {
uint key_nr = ha_alter_info->index_add_buffer[j];
KEY *add_key = &ha_alter_info->key_info_buffer[key_nr];
if (sdb_is_same_index(drop_key, add_key)) {
ignored_drop_keys.set_bit(i);
ignored_add_keys.set_bit(j);
}
}
}
}
if (ha_alter_info->handler_flags & INPLACE_ONLINE_DROPIDX) {
rc = drop_index(cl, ha_alter_info, ignored_drop_keys);
if (0 != rc) {
my_error(ER_GET_ERRNO, MYF(0), rc);
goto error;
}
}
if (ha_alter_info->handler_flags & INPLACE_ONLINE_ADDIDX) {
rc = create_index(cl, ha_alter_info, ignored_add_keys);
if (0 != rc) {
my_error(ER_GET_ERRNO, MYF(0), rc);
goto error;
}
}
if (ha_alter_info->handler_flags & Alter_inplace_info::RENAME_INDEX) {
my_error(HA_ERR_UNSUPPORTED, MYF(0), cl.get_cl_name());
goto error;
}
rs = false;
done:
return rs;
error:
goto done;
}
int ha_sdb::delete_all_rows() {
int rc = 0;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
if (collection->is_transaction_on()) {
rc = collection->del();
if (0 == rc) {
stats.records = 0;
}
return rc;
}
return truncate();
}
int ha_sdb::truncate() {
int rc = 0;
DBUG_ASSERT(NULL != collection);
DBUG_ASSERT(collection->thread_id() == ha_thd()->thread_id());
rc = collection->truncate();
if (0 == rc) {
stats.records = 0;
}
return rc;
}
int ha_sdb::analyze(THD *thd, HA_CHECK_OPT *check_opt) {
return update_stats(thd, true);
}
ha_rows ha_sdb::records_in_range(uint inx, key_range *min_key,
key_range *max_key) {
// TODO*********
return 1;
}
int ha_sdb::delete_table(const char *from) {
int rc = 0;
Sdb_conn *conn = NULL;
rc = sdb_parse_table_name(from, db_name, SDB_CS_NAME_MAX_SIZE, table_name,
SDB_CL_NAME_MAX_SIZE);
if (rc != 0) {
goto error;
}
if (sdb_is_tmp_table(from, table_name)) {
if (0 != sdb_rebuild_db_name_of_temp_table(db_name, SDB_CS_NAME_MAX_SIZE)) {
rc = HA_ERR_GENERIC;
goto error;
}
}
conn = check_sdb_in_thd(ha_thd(), true);
if (NULL == conn) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(conn->thread_id() == ha_thd()->thread_id());
rc = conn->drop_cl(db_name, table_name);
if (0 != rc) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::rename_table(const char *from, const char *to) {
Sdb_conn *conn = NULL;
int rc = 0;
char old_db_name[SDB_CS_NAME_MAX_SIZE + 1] = {0};
char old_table_name[SDB_CL_NAME_MAX_SIZE + 1] = {0};
char new_db_name[SDB_CS_NAME_MAX_SIZE + 1] = {0};
char new_table_name[SDB_CL_NAME_MAX_SIZE + 1] = {0};
rc = sdb_parse_table_name(from, old_db_name, SDB_CS_NAME_MAX_SIZE,
old_table_name, SDB_CL_NAME_MAX_SIZE);
if (0 != rc) {
goto error;
}
rc = sdb_parse_table_name(to, new_db_name, SDB_CS_NAME_MAX_SIZE,
new_table_name, SDB_CL_NAME_MAX_SIZE);
if (0 != rc) {
goto error;
}
if (sdb_is_tmp_table(from, old_table_name)) {
rc = sdb_rebuild_db_name_of_temp_table(old_db_name, SDB_CS_NAME_MAX_SIZE);
if (0 != rc) {
goto error;
}
}
if (sdb_is_tmp_table(to, new_table_name)) {
rc = sdb_rebuild_db_name_of_temp_table(new_db_name, SDB_CS_NAME_MAX_SIZE);
if (0 != rc) {
goto error;
}
}
if (strcmp(old_db_name, new_db_name) != 0) {
rc = HA_ERR_NOT_ALLOWED_COMMAND;
goto error;
}
conn = check_sdb_in_thd(ha_thd(), true);
if (NULL == conn) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(conn->thread_id() == ha_thd()->thread_id());
rc = conn->rename_cl(old_db_name, old_table_name, new_table_name);
if (0 != rc) {
goto error;
}
done:
return rc;
error:
goto done;
}
int ha_sdb::get_sharding_key(TABLE *form, bson::BSONObj &sharding_key) {
int rc = 0;
const KEY *shard_idx = NULL;
for (uint i = 0; i < form->s->keys; i++) {
const KEY *key_info = form->s->key_info + i;
if (!strcmp(key_info->name, primary_key_name)) {
shard_idx = key_info;
break;
}
if (NULL == shard_idx && (key_info->flags & HA_NOSAME)) {
shard_idx = key_info;
}
}
if (NULL != shard_idx) {
bson::BSONObjBuilder sharding_key_builder;
const KEY_PART_INFO *key_part;
const KEY_PART_INFO *key_end;
// check unique-idx if include sharding-key
for (uint i = 0; i < form->s->keys; i++) {
const KEY *key_info = form->s->key_info + i;
if ((key_info->flags & HA_NOSAME) && key_info != shard_idx) {
key_part = shard_idx->key_part;
key_end = key_part + shard_idx->user_defined_key_parts;
for (; key_part != key_end; ++key_part) {
const KEY_PART_INFO *key_part_tmp = key_info->key_part;
const KEY_PART_INFO *key_end_tmp =
key_part_tmp + key_info->user_defined_key_parts;
for (; key_part_tmp != key_end_tmp; ++key_part_tmp) {
if (0 == strcmp(key_part->field->field_name,
key_part_tmp->field->field_name)) {
break;
}
}
if (key_part_tmp == key_end_tmp) {
rc = SDB_ERR_INVALID_ARG;
SDB_PRINT_ERROR(
rc,
"The unique index('%-.192s') must include the field: '%-.192s'",
key_info->name, key_part->field->field_name);
goto error;
}
}
}
}
key_part = shard_idx->key_part;
key_end = key_part + shard_idx->user_defined_key_parts;
for (; key_part != key_end; ++key_part) {
sharding_key_builder.append(key_part->field->field_name, 1);
}
sharding_key = sharding_key_builder.obj();
}
done:
return rc;
error:
goto done;
}
int ha_sdb::get_cl_options(TABLE *form, HA_CREATE_INFO *create_info,
bson::BSONObj &options, my_bool use_partition) {
int rc = 0;
bson::BSONObj sharding_key;
if (create_info && create_info->comment.str) {
char *sdb_cmt_pos = NULL;
bson::BSONElement be_options;
bson::BSONObj comments;
if ((sdb_cmt_pos = strstr(create_info->comment.str, SDB_COMMENT)) == NULL) {
goto comment_done;
}
sdb_cmt_pos += strlen(SDB_COMMENT);
while (*sdb_cmt_pos != '\0' && my_isspace(&SDB_CHARSET, *sdb_cmt_pos)) {
sdb_cmt_pos++;
}
if (*sdb_cmt_pos != ':') {
rc = SDB_ERR_INVALID_ARG;
my_printf_error(rc, "Failed to parse comment: '%-.192s'", MYF(0),
create_info->comment.str);
goto error;
}
sdb_cmt_pos += 1;
while (*sdb_cmt_pos != '\0' && my_isspace(&SDB_CHARSET, *sdb_cmt_pos)) {
sdb_cmt_pos++;
}
rc = bson::fromjson(sdb_cmt_pos, comments);
if (0 != rc) {
my_printf_error(rc, "Failed to parse comment: '%-.192s'", MYF(0),
create_info->comment.str);
goto error;
}
be_options = comments.getField("table_options");
if (be_options.type() == bson::Object) {
options = be_options.embeddedObject().copy();
goto done;
} else if (be_options.type() != bson::EOO) {
rc = SDB_ERR_INVALID_ARG;
my_printf_error(rc, "Failed to parse cl_options!", MYF(0));
goto error;
}
}
comment_done:
if (!use_partition) {
options = BSON("Compressed" << true << "CompressionType"
<< "lzw"
<< "ReplSize" << sdb_replica_size);
goto done;
}
rc = get_sharding_key(form, sharding_key);
if (rc != 0) {
goto error;
}
if (!sharding_key.isEmpty()) {
options = BSON("ShardingKey" << sharding_key << "AutoSplit" << true
<< "EnsureShardingIndex" << false
<< "Compressed" << true << "CompressionType"
<< "lzw"
<< "ReplSize" << sdb_replica_size);
} else {
options = BSON("Compressed" << true << "CompressionType"
<< "lzw"
<< "ReplSize" << sdb_replica_size);
}
done:
return rc;
error:
goto done;
}
int ha_sdb::create(const char *name, TABLE *form, HA_CREATE_INFO *create_info) {
int rc = 0;
Sdb_conn *conn = NULL;
Sdb_cl cl;
bool create_temporary = (create_info->options & HA_LEX_CREATE_TMP_TABLE);
bson::BSONObj options;
bool created_cs = false;
bool created_cl = false;
for (Field **fields = form->field; *fields; fields++) {
Field *field = *fields;
if (field->key_length() >= SDB_FIELD_MAX_LEN) {
my_error(ER_TOO_BIG_FIELDLENGTH, MYF(0), field->field_name,
static_cast<ulong>(SDB_FIELD_MAX_LEN));
rc = HA_WRONG_CREATE_OPTION;
goto error;
}
if (strcasecmp(field->field_name, SDB_OID_FIELD) == 0) {
my_error(ER_WRONG_COLUMN_NAME, MYF(0), field->field_name);
rc = HA_WRONG_CREATE_OPTION;
goto error;
}
if (Field::NEXT_NUMBER == field->unireg_check) {
// TODO: support auto-increment field.
// it is auto-increment field if run here.
// the start-value is create_info->auto_increment_value
}
}
rc = sdb_parse_table_name(name, db_name, SDB_CS_NAME_MAX_SIZE, table_name,
SDB_CL_NAME_MAX_SIZE);
if (0 != rc) {
goto error;
}
if (create_temporary) {
if (0 != sdb_rebuild_db_name_of_temp_table(db_name, SDB_CS_NAME_MAX_SIZE)) {
rc = HA_WRONG_CREATE_OPTION;
goto error;
}
}
rc = get_cl_options(form, create_info, options, sdb_use_partition);
if (0 != rc) {
goto error;
}
conn = check_sdb_in_thd(ha_thd(), true);
if (NULL == conn) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(conn->thread_id() == ha_thd()->thread_id());
rc = conn->create_cl(db_name, table_name, options, &created_cs, &created_cl);
if (0 != rc) {
goto error;
}
rc = conn->get_cl(db_name, table_name, cl);
if (0 != rc) {
goto error;
}
for (uint i = 0; i < form->s->keys; i++) {
rc = sdb_create_index(form->s->key_info + i, cl);
if (0 != rc) {
// we disabled sharding index,
// so do not ignore SDB_IXM_EXIST_COVERD_ONE
goto error;
}
}
done:
return rc;
error:
convert_sdb_code(rc);
if (created_cs) {
conn->drop_cs(db_name);
} else if (created_cl) {
conn->drop_cl(db_name, table_name);
}
goto done;
}
THR_LOCK_DATA **ha_sdb::store_lock(THD *thd, THR_LOCK_DATA **to,
enum thr_lock_type lock_type) {
/**
In this function, we can get the MySQL lock by parameter lock_type,
and tell MySQL which lock we can support by return a new THR_LOCK_DATA.
Then, we can change MySQL behavior of mutexes.
*/
m_lock_type = lock_type;
return to;
}
void ha_sdb::unlock_row() {
// TODO: this operation is not supported in sdb.
// unlock by _id or completed-record?
}
int ha_sdb::get_query_flag(const uint sql_command,
enum thr_lock_type lock_type) {
/*
We always add flag QUERY_WITH_RETURNDATA to improve performance,
and we need to add the lock related flag QUERY_FOR_UPDATE in the following
cases:
1. SELECT ... FOR UPDATE
2. doing query in UPDATE ... or DELETE ...
3. SELECT ... LOCK IN SHARE MODE
*/
int query_flag = QUERY_WITH_RETURNDATA;
if ((lock_type >= TL_WRITE_CONCURRENT_INSERT &&
(SQLCOM_UPDATE == sql_command || SQLCOM_DELETE == sql_command ||
SQLCOM_SELECT == sql_command)) ||
TL_READ_WITH_SHARED_LOCKS == lock_type) {
query_flag |= QUERY_FOR_UPDATE;
}
return query_flag;
}
const Item *ha_sdb::cond_push(const Item *cond) {
const Item *remain_cond = cond;
Sdb_cond_ctx sdb_condition;
// we can handle the condition which only involved current table,
// can't handle conditions which involved other tables
if (cond->used_tables() & ~table->pos_in_table_list->map()) {
goto done;
}
try {
sdb_parse_condtion(cond, &sdb_condition);
sdb_condition.to_bson(pushed_condition);
} catch (bson::assertion e) {
SDB_LOG_DEBUG("Exception[%s] occurs when build bson obj.", e.full.c_str());
DBUG_ASSERT(0);
sdb_condition.status = SDB_COND_UNSUPPORTED;
}
if (SDB_COND_SUPPORTED == sdb_condition.status) {
// TODO: build unanalysable condition
remain_cond = NULL;
} else {
if (NULL != ha_thd()) {
SDB_LOG_DEBUG(
"Condition can't be pushed down. db=[%s], table[%s], sql=[%s]",
db_name, table_name, ha_thd()->query().str);
} else {
SDB_LOG_DEBUG(
"Condition can't be pushed down. "
"db=[unknown], table[unknown], sql=[unknown]");
}
pushed_condition = SDB_EMPTY_BSON;
}
done:
return remain_cond;
}
Item *ha_sdb::idx_cond_push(uint keyno, Item *idx_cond) {
return idx_cond;
}
static handler *sdb_create_handler(handlerton *hton, TABLE_SHARE *table,
MEM_ROOT *mem_root) {
return new (mem_root) ha_sdb(hton, table);
}
#ifdef HAVE_PSI_INTERFACE
static PSI_memory_info all_sdb_memory[] = {
{&key_memory_sdb_share, "Sdb_share", PSI_FLAG_GLOBAL},
{&sdb_key_memory_blobroot, "blobroot", 0}};
static PSI_mutex_info all_sdb_mutexes[] = {
{&key_mutex_sdb, "sdb", PSI_FLAG_GLOBAL},
{&key_mutex_SDB_SHARE_mutex, "Sdb_share::mutex", 0}};
static void init_sdb_psi_keys(void) {
const char *category = "sequoiadb";
int count;
count = array_elements(all_sdb_mutexes);
mysql_mutex_register(category, all_sdb_mutexes, count);
count = array_elements(all_sdb_memory);
mysql_memory_register(category, all_sdb_memory, count);
}
#endif
// Commit a transaction started in SequoiaDB.
static int sdb_commit(handlerton *hton, THD *thd, bool all) {
int rc = 0;
Thd_sdb *thd_sdb = thd_get_thd_sdb(thd);
Sdb_conn *connection;
thd_sdb->start_stmt_count = 0;
connection = check_sdb_in_thd(thd, true);
if (NULL == connection) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(connection->thread_id() == thd->thread_id());
if (!connection->is_transaction_on()) {
goto done;
}
if (!all && thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) {
/*
An odditity in the handler interface is that commit on handlerton
is called to indicate end of statement only in cases where
autocommit isn't used and the all flag isn't set.
We also leave quickly when a transaction haven't even been started,
in this case we are safe that no clean up is needed. In this case
the MySQL Server could handle the query without contacting the
SequoiaDB.
*/
thd_sdb->save_point_count++;
goto done;
}
thd_sdb->save_point_count = 0;
rc = connection->commit_transaction();
if (0 != rc) {
goto error;
}
done:
return rc;
error:
goto done;
}
// Rollback a transaction started in SequoiaDB.
static int sdb_rollback(handlerton *hton, THD *thd, bool all) {
int rc = 0;
Thd_sdb *thd_sdb = thd_get_thd_sdb(thd);
Sdb_conn *connection;
thd_sdb->start_stmt_count = 0;
connection = check_sdb_in_thd(thd, true);
if (NULL == connection) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(connection->thread_id() == thd->thread_id());
if (!connection->is_transaction_on()) {
goto done;
}
if (!all && thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) &&
(thd_sdb->save_point_count > 0)) {
/*
Ignore end-of-statement until real rollback or commit is called
as SequoiaDB does not support rollback statement
- mark that rollback was unsuccessful, this will cause full rollback
of the transaction
*/
thd_mark_transaction_to_rollback(thd, 1);
my_error(ER_WARN_ENGINE_TRANSACTION_ROLLBACK, MYF(0), "SequoiaDB");
goto done;
}
thd_sdb->save_point_count = 0;
rc = connection->rollback_transaction();
if (0 != rc) {
goto error;
}
done:
return rc;
error:
goto done;
}
static void sdb_drop_database(handlerton *hton, char *path) {
int rc = 0;
char db_name[SDB_CS_NAME_MAX_SIZE + 1] = {0};
Sdb_conn *connection = NULL;
THD *thd = current_thd;
if (NULL == thd) {
goto error;
}
connection = check_sdb_in_thd(thd, true);
if (NULL == connection) {
rc = HA_ERR_NO_CONNECTION;
goto error;
}
DBUG_ASSERT(connection->thread_id() == thd->thread_id());
rc = sdb_get_db_name_from_path(path, db_name, SDB_CS_NAME_MAX_SIZE);
if (rc != 0) {
goto error;
}
rc = connection->drop_cs(db_name);
if (rc != 0) {
goto error;
}
done:
return;
error:
goto done;
}
static int sdb_close_connection(handlerton *hton, THD *thd) {
Thd_sdb *thd_sdb = thd_get_thd_sdb(thd);
if (NULL != thd_sdb) {
Thd_sdb::release(thd_sdb);
thd_set_thd_sdb(thd, NULL);
}
return 0;
}
static int sdb_init_func(void *p) {
int rc = SDB_ERR_OK;
Sdb_conn_addrs conn_addrs;
#ifdef HAVE_PSI_INTERFACE
init_sdb_psi_keys();
#endif
sdb_hton = (handlerton *)p;
mysql_mutex_init(key_mutex_sdb, &sdb_mutex, MY_MUTEX_INIT_FAST);
(void)my_hash_init(&sdb_open_tables, system_charset_info, 32, 0, 0,
(my_hash_get_key)sdb_get_key, 0, 0, key_memory_sdb_share);
sdb_hton->state = SHOW_OPTION_YES;
sdb_hton->db_type = DB_TYPE_UNKNOWN;
sdb_hton->create = sdb_create_handler;
sdb_hton->flags = (HTON_SUPPORT_LOG_TABLES | HTON_NO_PARTITION);
sdb_hton->commit = sdb_commit;
sdb_hton->rollback = sdb_rollback;
sdb_hton->drop_database = sdb_drop_database;
sdb_hton->close_connection = sdb_close_connection;
if (conn_addrs.parse_conn_addrs(sdb_conn_str)) {
SDB_LOG_ERROR("Invalid value sequoiadb_conn_addr=%s", sdb_conn_str);
return 1;
}
rc = sdb_encrypt_password();
if (SDB_ERR_OK != rc) {
SDB_LOG_ERROR("Failed to encrypt password, rc=%d", rc);
return 1;
}
return 0;
}
static int sdb_done_func(void *p) {
// TODO************
// SHOW_COMP_OPTION state;
my_hash_free(&sdb_open_tables);
mysql_mutex_destroy(&sdb_mutex);
return 0;
}
static struct st_mysql_storage_engine sdb_storage_engine = {
MYSQL_HANDLERTON_INTERFACE_VERSION};
mysql_declare_plugin(sequoiadb){
MYSQL_STORAGE_ENGINE_PLUGIN,
&sdb_storage_engine,
"SequoiaDB",
"SequoiaDB Inc.",
sdb_plugin_info,
PLUGIN_LICENSE_GPL,
sdb_init_func, /* Plugin Init */
sdb_done_func, /* Plugin Deinit */
0x0302, /* version */
NULL, /* status variables */
sdb_sys_vars, /* system variables */
NULL, /* config options */
0, /* flags */
} mysql_declare_plugin_end;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/angelicaiia/sequoiasql-mysql.git
git@gitee.com:angelicaiia/sequoiasql-mysql.git
angelicaiia
sequoiasql-mysql
sequoiasql-mysql
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385