Skip to content

Commit

Permalink
feat(tianmu):develop some auto_increment function for tianmu(stoneato…
Browse files Browse the repository at this point in the history
  • Loading branch information
isredstar authored and konghaiya committed Mar 7, 2023
1 parent abd134f commit e734f80
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 13 deletions.
144 changes: 144 additions & 0 deletions mysql-test/suite/tianmu/r/init_auto_increment_value.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
DROP DATABASE IF EXISTS auto_increment_value_db;
CREATE DATABASE auto_increment_value_db;
USE auto_increment_value_db;
CREATE TABLE t_auto_increment_value (
id int NOT NULL AUTO_INCREMENT,
data VARCHAR(64) DEFAULT NULL,
PRIMARY KEY (id)
) engine=tianmu AUTO_INCREMENT=100;
show create table t_auto_increment_value;
Table Create Table
t_auto_increment_value CREATE TABLE `t_auto_increment_value` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=TIANMU AUTO_INCREMENT=100 DEFAULT CHARSET=latin1
insert into t_auto_increment_value (data) values("first"),("middle"),("last");
select * from t_auto_increment_value;
id data
100 first
101 middle
102 last
show create table t_auto_increment_value;
Table Create Table
t_auto_increment_value CREATE TABLE `t_auto_increment_value` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=TIANMU AUTO_INCREMENT=103 DEFAULT CHARSET=latin1
update t_auto_increment_value set id=80 where id=100;
select * from t_auto_increment_value;
id data
80 first
101 middle
102 last
show create table t_auto_increment_value;
Table Create Table
t_auto_increment_value CREATE TABLE `t_auto_increment_value` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=TIANMU AUTO_INCREMENT=103 DEFAULT CHARSET=latin1
insert into t_auto_increment_value (id, data) values(0,"update_lt_max_id");
select * from t_auto_increment_value;
id data
80 first
101 middle
102 last
103 update_lt_max_id
update t_auto_increment_value set id=200 where id=101;
select * from t_auto_increment_value;
id data
80 first
200 middle
102 last
103 update_lt_max_id
show create table t_auto_increment_value;
Table Create Table
t_auto_increment_value CREATE TABLE `t_auto_increment_value` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=TIANMU AUTO_INCREMENT=201 DEFAULT CHARSET=latin1
insert into t_auto_increment_value (id, data) values(0,"update_gt_max_id");
select * from t_auto_increment_value;
id data
80 first
200 middle
102 last
103 update_lt_max_id
201 update_gt_max_id
alter table t_auto_increment_value AUTO_INCREMENT=300;
select * from t_auto_increment_value;
id data
80 first
200 middle
102 last
103 update_lt_max_id
201 update_gt_max_id
show create table t_auto_increment_value;
Table Create Table
t_auto_increment_value CREATE TABLE `t_auto_increment_value` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=TIANMU AUTO_INCREMENT=300 DEFAULT CHARSET=latin1
insert into t_auto_increment_value (id, data) values(0,"alter_gt_max_id");
select * from t_auto_increment_value;
id data
80 first
200 middle
102 last
103 update_lt_max_id
201 update_gt_max_id
300 alter_gt_max_id
alter table t_auto_increment_value AUTO_INCREMENT=50;
select * from t_auto_increment_value;
id data
80 first
200 middle
102 last
103 update_lt_max_id
201 update_gt_max_id
300 alter_gt_max_id
show create table t_auto_increment_value;
Table Create Table
t_auto_increment_value CREATE TABLE `t_auto_increment_value` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=TIANMU AUTO_INCREMENT=301 DEFAULT CHARSET=latin1
insert into t_auto_increment_value (id, data) values(0,"alter_lt_max_id");
select * from t_auto_increment_value;
id data
80 first
200 middle
102 last
103 update_lt_max_id
201 update_gt_max_id
300 alter_gt_max_id
301 alter_lt_max_id
CREATE TABLE load_auto_increment_value (
id int(11) NOT NULL AUTO_INCREMENT,
company varchar(25),
PRIMARY KEY (id)
) ENGINE=TIANMU AUTO_INCREMENT=100;
LOAD DATA LOCAL INFILE 'MYSQL_TEST_DIR/suite/tianmu/std_data/load_auto_increment_value.txt' INTO TABLE load_auto_increment_value;
select * from load_auto_increment_value;
id company
100 "syz100"
101 "syz101"
102 "syz102"
103 "syz103"
104 "syz104"
105 "syz105"
106 "syz106"
107 "syz107"
show create table load_auto_increment_value;
Table Create Table
load_auto_increment_value CREATE TABLE `load_auto_increment_value` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company` varchar(25) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=TIANMU AUTO_INCREMENT=108 DEFAULT CHARSET=latin1
DROP DATABASE auto_increment_value_db;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
0;"syz100"
0;"syz101"
0;"syz102"
0;"syz103"
0;"syz104"
0;"syz105"
0;"syz106"
0;"syz107"
63 changes: 63 additions & 0 deletions mysql-test/suite/tianmu/t/init_auto_increment_value.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--source include/have_tianmu.inc
#
# Test auto_increment_value with TIANMU
#
--disable_warnings
DROP DATABASE IF EXISTS auto_increment_value_db;
--enable_warnings

CREATE DATABASE auto_increment_value_db;
USE auto_increment_value_db;

# test init value
CREATE TABLE t_auto_increment_value (
id int NOT NULL AUTO_INCREMENT,
data VARCHAR(64) DEFAULT NULL,
PRIMARY KEY (id)
) engine=tianmu AUTO_INCREMENT=100;
show create table t_auto_increment_value;
insert into t_auto_increment_value (data) values("first"),("middle"),("last");
select * from t_auto_increment_value;
show create table t_auto_increment_value;

# test update
update t_auto_increment_value set id=80 where id=100;
select * from t_auto_increment_value;
show create table t_auto_increment_value;
insert into t_auto_increment_value (id, data) values(0,"update_lt_max_id");
select * from t_auto_increment_value;

update t_auto_increment_value set id=200 where id=101;
select * from t_auto_increment_value;
show create table t_auto_increment_value;
insert into t_auto_increment_value (id, data) values(0,"update_gt_max_id");
select * from t_auto_increment_value;

# test alter
alter table t_auto_increment_value AUTO_INCREMENT=300;
select * from t_auto_increment_value;
show create table t_auto_increment_value;
insert into t_auto_increment_value (id, data) values(0,"alter_gt_max_id");
select * from t_auto_increment_value;

# when SET AUTO_INCREMENT < max_id, AUTO_INCREMENT value wouldnot be changed.
alter table t_auto_increment_value AUTO_INCREMENT=50;
select * from t_auto_increment_value;
show create table t_auto_increment_value;
insert into t_auto_increment_value (id, data) values(0,"alter_lt_max_id");
select * from t_auto_increment_value;


#test load data with auto_increment value into auto_increment col
CREATE TABLE load_auto_increment_value (
id int(11) NOT NULL AUTO_INCREMENT,
company varchar(25),
PRIMARY KEY (id)
) ENGINE=TIANMU AUTO_INCREMENT=100;
--replace_result $MYSQL_TEST_DIR MYSQL_TEST_DIR
eval LOAD DATA LOCAL INFILE '$MYSQL_TEST_DIR/suite/tianmu/std_data/load_auto_increment_value.txt' INTO TABLE load_auto_increment_value;
select * from load_auto_increment_value;
show create table load_auto_increment_value;

# Clean UP
DROP DATABASE auto_increment_value_db;
8 changes: 6 additions & 2 deletions storage/tianmu/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,8 @@ uint32_t Engine::GetNextTableId() {
return seq;
}

std::shared_ptr<TableOption> Engine::GetTableOption(const std::string &table, TABLE *form) {
std::shared_ptr<TableOption> Engine::GetTableOption(const std::string &table, TABLE *form,
HA_CREATE_INFO *create_info) {
auto opt = std::make_shared<TableOption>();

int power = has_pack(form->s->comment);
Expand All @@ -895,10 +896,13 @@ std::shared_ptr<TableOption> Engine::GetTableOption(const std::string &table, TA

opt->path = table + common::TIANMU_EXT;
opt->name = form->s->table_name.str;
opt->create_info = create_info;
return opt;
}

void Engine::CreateTable(const std::string &table, TABLE *form) { TianmuTable::CreateNew(GetTableOption(table, form)); }
void Engine::CreateTable(const std::string &table, TABLE *form, HA_CREATE_INFO *create_info) {
TianmuTable::CreateNew(GetTableOption(table, form, create_info));
}

AttributeTypeInfo Engine::GetAttrTypeInfo(const Field &field) {
bool auto_inc = field.flags & AUTO_INCREMENT_FLAG;
Expand Down
4 changes: 2 additions & 2 deletions storage/tianmu/core/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Engine final {
~Engine();

int Init(uint engine_slot);
void CreateTable(const std::string &table, TABLE *from);
void CreateTable(const std::string &table, TABLE *from, HA_CREATE_INFO *create_info);
int DeleteTable(const char *table, THD *thd);
void TruncateTable(const std::string &table_path, THD *thd);
int RenameTable(Transaction *trans_, const std::string &from, const std::string &to, THD *thd);
Expand Down Expand Up @@ -182,7 +182,7 @@ class Engine final {
static std::unique_ptr<system::IOParameters> CreateIOParameters(const std::string &path, void *arg);
static std::unique_ptr<system::IOParameters> CreateIOParameters(THD *thd, TABLE *table, void *arg);
void LogStat();
std::shared_ptr<TableOption> GetTableOption(const std::string &table, TABLE *form);
std::shared_ptr<TableOption> GetTableOption(const std::string &table, TABLE *form, HA_CREATE_INFO *create_info);
std::shared_ptr<TableShare> getTableShare(const std::string &table_path);
std::unique_ptr<char[]> GetRecord(size_t &len);

Expand Down
1 change: 1 addition & 0 deletions storage/tianmu/core/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "filter.h"

#include "common/assert.h"
#include "core/tools.h"

Expand Down
6 changes: 3 additions & 3 deletions storage/tianmu/core/table_share.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ TableShare::TableShare(const fs::path &table_path, const TABLE_SHARE *table_shar
for (uint i = 0; i < no_cols; i++) {
common::TX_ID xid;
fv.ReadExact(&xid, sizeof(xid));
m_columns.emplace_back(std::make_unique<ColumnShare>(
this, xid, i, table_path / common::COLUMN_DIR / std::to_string(i), table_share->field[i]));
Field *field = table_share->field[i];
auto colpath = table_path / common::COLUMN_DIR / std::to_string(i);
m_columns.emplace_back(std::make_unique<ColumnShare>(this, xid, i, colpath, field));
}

thr_lock_init(&thr_lock);
}

Expand Down
8 changes: 7 additions & 1 deletion storage/tianmu/core/tianmu_attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ TianmuAttr::TianmuAttr(Transaction *tx, common::TX_ID xid, int a_num, int t_num,
};
}

void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8_t pss, size_t no_rows) {
void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8_t pss, size_t no_rows,
uint64_t auto_inc_value) {
uint32_t no_pack = common::rows2packs(no_rows, pss);

// write meta data(immutable)
Expand Down Expand Up @@ -108,6 +109,11 @@ void TianmuAttr::Create(const fs::path &dir, const AttributeTypeInfo &ati, uint8
dict->SaveData(dir / common::COL_DICT_DIR / std::to_string(1));
}

// auto_increment
if (ati.AutoInc() && auto_inc_value != 0) {
hdr.auto_inc_next = --auto_inc_value;
}

// create version directory
fs::create_directory(dir / common::COL_VERSION_DIR);

Expand Down
3 changes: 2 additions & 1 deletion storage/tianmu/core/tianmu_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class TianmuAttr final : public mm::TraceableObject, public PhysicalColumn, publ
TianmuAttr &operator=(const TianmuAttr &) = delete;
~TianmuAttr() = default;

static void Create(const fs::path &path, const AttributeTypeInfo &ati, uint8_t pss, size_t no_rows);
static void Create(const fs::path &path, const AttributeTypeInfo &ati, uint8_t pss, size_t no_rows,
uint64_t auto_inc_value = 0);

mm::TO_TYPE TraceableType() const override { return mm::TO_TYPE::TO_TEMPORARY; }
void UpdateData(uint64_t row, Value &old_v, Value &new_v);
Expand Down
4 changes: 2 additions & 2 deletions storage/tianmu/core/tianmu_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ void TianmuTable::CreateNew(const std::shared_ptr<TableOption> &opt) {
uint32_t tid = ha_tianmu_engine_->GetNextTableId();
auto &path(opt->path);
uint32_t no_attrs = opt->atis.size();

uint64_t auto_inc_value = opt->create_info->auto_increment_value;
fs::create_directory(path);

TABLE_META meta{common::FILE_MAGIC, common::TABLE_DATA_VERSION, tid, opt->pss};
Expand Down Expand Up @@ -395,7 +395,7 @@ void TianmuTable::CreateNew(const std::shared_ptr<TableOption> &opt) {
auto lnk = column_path / std::to_string(idx);
fs::create_symlink(dir, lnk);

TianmuAttr::Create(lnk, opt->atis[idx], opt->pss, 0);
TianmuAttr::Create(lnk, opt->atis[idx], opt->pss, 0, auto_inc_value);
// TIANMU_LOG(LogCtl_Level::INFO, "Column %zu at %s", idx, dir.c_str());
}
TIANMU_LOG(LogCtl_Level::INFO, "Create table %s, ID = %u", opt->path.c_str(), tid);
Expand Down
1 change: 1 addition & 0 deletions storage/tianmu/core/tianmu_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct TableOption {
std::string name;
int id;
uint8_t pss;
HA_CREATE_INFO *create_info;
};

class DataPackLock : public FunctionExecutor {
Expand Down
Loading

0 comments on commit e734f80

Please sign in to comment.