diff --git a/src/unit_test/storage/meta/entry/table_entry.cpp b/src/unit_test/storage/meta/entry/table_entry.cpp index 60a739eee9..eb133a942b 100644 --- a/src/unit_test/storage/meta/entry/table_entry.cpp +++ b/src/unit_test/storage/meta/entry/table_entry.cpp @@ -29,9 +29,13 @@ import index_full_text; import statement_common; import data_access_state; import txn_store; +import column_vector; +import internal_types; import base_entry; import table_entry; +import segment_entry; +import block_entry; using namespace infinity; @@ -64,6 +68,52 @@ class TableEntryTest : public BaseTestParamStr { } }; +void InsertData(const String& db_name, const String& table_name) { + Vector> datas_ = { + { + R"#(Animalia is an illustrated children's book by anarchism Graeme Base. It was originally published in 1986, followed by a tenth anniversary edition in 1996, and a 25th anniversary edition in 2012. Over three million copies have been sold. A special numbered and signed anniversary edition was also published in 1996, with an embossed gold jacket.)#"}, + { + R"#(The Academy Awards are the oldest awards ceremony for achievements in motion pictures. one of The add test Academy Award for Best Production Design recognizes achievement in art direction on a film. The category's original name was Best Art Direction, but was changed to its current name in 2012 for the 85th Academy Awards. This change resulted from the Art Director's branch of the Academy being renamed the Designer's branch.)#"}, + { + R"#(The American Football Conference (AFC) harm chemical anarchism add test is one of harm chemical the two conferences of the National Football League (NFL). This add test conference and its counterpart, the National Football Conference (NFC), currently contain 16 teams each, making up the 32 teams of the NFL. The current AFC title holder is the New England Patriots.)#"}, + { + R"#(周末我和朋友一起去“电子城”,想挑选一些新的“电脑配件”。那里有各种各样的“hardware”,如“motherboard”、“graphics card”等。我们还看到了一些很“awesome”的“peripheral devices”,像“keyboard”和“mouse”。我朋友说他需要一个新的“power supply”,而我则对那些“high-tech”的“storage devices”比较感兴趣。逛了一会儿后,我们都买到了自己心仪的东西,然后就“happily”回家了。)#"}, + }; + TxnManager *txn_mgr = infinity::InfinityContext::instance().storage()->txn_manager(); + + auto *txn = txn_mgr->BeginTxn(MakeUnique("import data")); + auto [table_entry, status] = txn->GetTableByName(db_name, table_name); + EXPECT_TRUE(status.ok()); + + SegmentID segment_id = Catalog::GetNextSegmentID(table_entry); + SharedPtr segment_entry = SegmentEntry::NewSegmentEntry(table_entry, segment_id, txn); + { + for (BlockID block_id = 0; block_id < datas_.size(); ++block_id) { + UniquePtr block_entry = + BlockEntry::NewBlockEntry(segment_entry.get(), block_id, 0 /*checkpoint_ts*/, table_entry->ColumnCount(), txn); + { + Vector column_vectors; + for (SizeT i = 0; i < table_entry->ColumnCount(); ++i) { + auto *block_column_entry = block_entry->GetColumnBlockEntry(i); + column_vectors.emplace_back(block_column_entry->GetColumnVector(txn->buffer_mgr())); + } + auto& row = datas_[block_id]; + for (SizeT i = 0; i < column_vectors.size(); ++i) { + auto &column = row[i]; + column_vectors[i].AppendByStringView(column); + } + block_entry->IncreaseRowCount(1); + } + segment_entry->AppendBlockEntry(std::move(block_entry)); + } + + } + segment_entry->FlushNewData(); + txn->Import(table_entry, segment_entry); + + txn_mgr->CommitTxn(txn); +} + INSTANTIATE_TEST_SUITE_P(TestWithDifferentParams, TableEntryTest, ::testing::Values(BaseTestParamStr::NULL_CONFIG_PATH, @@ -297,13 +347,10 @@ TEST_P(TableEntryTest, table_indexes_test){ TEST_P(TableEntryTest, get_fulltext_analyzers_test){ TxnManager *txn_mgr = infinity::InfinityContext::instance().storage()->txn_manager(); - Catalog *catalog = infinity::InfinityContext::instance().storage()->catalog(); - - // start txn1 - auto *txn1 = txn_mgr->BeginTxn(MakeUnique("create index")); //create table { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("create table")); Vector> columns; { std::set constraints; @@ -314,12 +361,19 @@ TEST_P(TableEntryTest, get_fulltext_analyzers_test){ columns.emplace_back(column_def_ptr); } auto tbl1_def = MakeUnique(MakeShared("default_db"), MakeShared("tbl1"), columns); - auto [table_entry, status] = catalog->CreateTable("default_db", txn1->TxnID(), txn1->BeginTS(), std::move(tbl1_def), ConflictType::kError, txn_mgr); + auto status = txn1->CreateTable("default_db", std::move(tbl1_def), ConflictType::kError); EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } + + //insert data + { + InsertData("default_db", "tbl1"); } // CreateIndex { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("create index")); Vector columns1{"col1"}; Vector parameters1; SharedPtr index_name = MakeShared("fulltext_index"); @@ -327,11 +381,10 @@ TEST_P(TableEntryTest, get_fulltext_analyzers_test){ const String &db_name = "default_db"; const String &table_name = "tbl1"; - ConflictType conflict_type = ConflictType::kError; - auto [table_entry, table_status] = catalog->GetTableByName(db_name, table_name, txn1->TxnID(), txn1->BeginTS()); + auto [table_entry, table_status] = txn1->GetTableByName(db_name, table_name); EXPECT_EQ(table_status.ok(), true); - auto [index_entry, index_status] = catalog->CreateIndex(table_entry, index_base_fulltext, conflict_type, txn1->TxnID(), txn1->BeginTS(), txn_mgr); + auto [index_entry, index_status] = txn1->CreateIndexDef(table_entry, index_base_fulltext, ConflictType::kInvalid); EXPECT_EQ(index_status.ok(), true); Map column2analyzer; @@ -340,19 +393,269 @@ TEST_P(TableEntryTest, get_fulltext_analyzers_test){ EXPECT_STREQ(pair.first.c_str(), "col1"); EXPECT_STREQ(pair.second.c_str(), "standard"); } + txn_mgr->CommitTxn(txn1); } //drop index { - auto [index_entry, status] = catalog->DropIndex("default_db", "tbl1", "fulltext_index", ConflictType::kError, txn1->TxnID(), txn1->BeginTS(), txn_mgr); + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("drop index")); + auto status = txn1->DropIndexByName("default_db", "tbl1", "fulltext_index", ConflictType::kError); EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); } //drop table { - auto [table_entry, status] = catalog->DropTableByName("default_db", "tbl1", ConflictType::kError, txn1->TxnID(), txn1->BeginTS(), txn_mgr); + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("drop table")); + auto status = txn1->DropTableCollectionByName("default_db", "tbl1", ConflictType::kError); EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); } +} - txn_mgr->CommitTxn(txn1); +TEST_P(TableEntryTest, optimize_fulltext_index_test){ + TxnManager *txn_mgr = infinity::InfinityContext::instance().storage()->txn_manager(); + + //create table + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("create table")); + Vector> columns; + { + std::set constraints; + constraints.insert(ConstraintType::kNotNull); + i64 column_id = 0; + auto column_def_ptr = + MakeShared(column_id, MakeShared(LogicalType::kVarchar), "col1", constraints); + columns.emplace_back(column_def_ptr); + } + auto tbl1_def = MakeUnique(MakeShared("default_db"), MakeShared("tbl1"), columns); + auto status = txn1->CreateTable("default_db", std::move(tbl1_def), ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } + + //insert data + { + InsertData("default_db", "tbl1"); + } + + // CreateIndex + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("create index")); + Vector columns1{"col1"}; + Vector parameters1; + SharedPtr index_name = MakeShared("fulltext_index"); + auto index_base_fulltext = IndexFullText::Make(index_name, "fulltext_index_test_fulltext", columns1, parameters1); + + const String &db_name = "default_db"; + const String &table_name = "tbl1"; + auto [table_entry, table_status] = txn1->GetTableByName(db_name, table_name); + EXPECT_EQ(table_status.ok(), true); + + auto [index_entry, index_status] = txn1->CreateIndexDef(table_entry, index_base_fulltext, ConflictType::kInvalid); + EXPECT_EQ(index_status.ok(), true); + + table_entry->OptimizeIndex(txn1); + txn_mgr->CommitTxn(txn1); + } + + //drop index + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("drop index")); + auto status = txn1->DropIndexByName("default_db", "tbl1", "fulltext_index", ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } + + //drop table + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("drop table")); + auto status = txn1->DropTableCollectionByName("default_db", "tbl1", ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } +} + +TEST_P(TableEntryTest, roll_back_write_test){ + TxnManager *txn_mgr = infinity::InfinityContext::instance().storage()->txn_manager(); + + //create table + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("create table")); + Vector> columns; + { + std::set constraints; + constraints.insert(ConstraintType::kNotNull); + i64 column_id = 0; + auto column_def_ptr = + MakeShared(column_id, MakeShared(LogicalType::kVarchar), "col1", constraints); + columns.emplace_back(column_def_ptr); + } + auto tbl1_def = MakeUnique(MakeShared("default_db"), MakeShared("tbl1"), columns); + auto status = txn1->CreateTable("default_db", std::move(tbl1_def), ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } + + // + { + const String &db_name = "default_db"; + const String &table_name = "tbl1"; + Vector> datas_ = { + { + R"#(Animalia is an illustrated children's book by anarchism Graeme Base. It was originally published in 1986, followed by a tenth anniversary edition in 1996, and a 25th anniversary edition in 2012. Over three million copies have been sold. A special numbered and signed anniversary edition was also published in 1996, with an embossed gold jacket.)#"}, + { + R"#(The Academy Awards are the oldest awards ceremony for achievements in motion pictures. one of The add test Academy Award for Best Production Design recognizes achievement in art direction on a film. The category's original name was Best Art Direction, but was changed to its current name in 2012 for the 85th Academy Awards. This change resulted from the Art Director's branch of the Academy being renamed the Designer's branch.)#"}, + { + R"#(The American Football Conference (AFC) harm chemical anarchism add test is one of harm chemical the two conferences of the National Football League (NFL). This add test conference and its counterpart, the National Football Conference (NFC), currently contain 16 teams each, making up the 32 teams of the NFL. The current AFC title holder is the New England Patriots.)#"}, + { + R"#(周末我和朋友一起去“电子城”,想挑选一些新的“电脑配件”。那里有各种各样的“hardware”,如“motherboard”、“graphics card”等。我们还看到了一些很“awesome”的“peripheral devices”,像“keyboard”和“mouse”。我朋友说他需要一个新的“power supply”,而我则对那些“high-tech”的“storage devices”比较感兴趣。逛了一会儿后,我们都买到了自己心仪的东西,然后就“happily”回家了。)#"}, + }; + TxnManager *txn_mgr = infinity::InfinityContext::instance().storage()->txn_manager(); + + auto *txn = txn_mgr->BeginTxn(MakeUnique("import data")); + auto [table_entry, status] = txn->GetTableByName(db_name, table_name); + EXPECT_TRUE(status.ok()); + + SegmentID segment_id = Catalog::GetNextSegmentID(table_entry); + SharedPtr segment_entry = SegmentEntry::NewSegmentEntry(table_entry, segment_id, txn); + { + for (BlockID block_id = 0; block_id < datas_.size(); ++block_id) { + UniquePtr block_entry = + BlockEntry::NewBlockEntry(segment_entry.get(), block_id, 0 /*checkpoint_ts*/, table_entry->ColumnCount(), txn); + { + Vector column_vectors; + for (SizeT i = 0; i < table_entry->ColumnCount(); ++i) { + auto *block_column_entry = block_entry->GetColumnBlockEntry(i); + column_vectors.emplace_back(block_column_entry->GetColumnVector(txn->buffer_mgr())); + } + auto& row = datas_[block_id]; + for (SizeT i = 0; i < column_vectors.size(); ++i) { + auto &column = row[i]; + column_vectors[i].AppendByStringView(column); + } + block_entry->IncreaseRowCount(1); + } + segment_entry->AppendBlockEntry(std::move(block_entry)); + } + + } + segment_entry->FlushNewData(); + txn->Import(table_entry, segment_entry); + auto txn_store = txn->GetTxnTableStore(table_entry); + txn_store->SetAppendState(MakeUnique(txn_store->GetBlocks())); + table_entry->CommitWrite(txn->TxnID(), txn->CommitTS(), txn_store->txn_segments(), nullptr); + Vector segment_stores; + for(auto pair : txn_store->txn_segments()){ + segment_stores.emplace_back(pair.second); + } + std::cout<RollbackWrite(txn->CommitTS(), segment_stores); + } + + //drop table + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("drop table")); + auto status = txn1->DropTableCollectionByName("default_db", "tbl1", ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } +} + +TEST_P(TableEntryTest, compact_test){ + TxnManager *txn_mgr = infinity::InfinityContext::instance().storage()->txn_manager(); + + //create table + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("create table")); + Vector> columns; + { + std::set constraints; + constraints.insert(ConstraintType::kNotNull); + i64 column_id = 0; + auto column_def_ptr = + MakeShared(column_id, MakeShared(LogicalType::kVarchar), "col1", constraints); + columns.emplace_back(column_def_ptr); + } + auto tbl1_def = MakeUnique(MakeShared("default_db"), MakeShared("tbl1"), columns); + auto status = txn1->CreateTable("default_db", std::move(tbl1_def), ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } + + //insert data + { + InsertData("default_db", "tbl1"); + } + + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("compact")); + const String &db_name = "default_db"; + const String &table_name = "tbl1"; + auto [table_entry, table_status] = txn1->GetTableByName(db_name, table_name); + EXPECT_EQ(table_status.ok(), true); + EXPECT_TRUE(table_entry->CompactPrepare()); + table_entry->CheckCompaction(txn1->TxnID()); + table_entry->SetCompactionAlg(nullptr); + EXPECT_FALSE(table_entry->CompactPrepare()); + table_entry->CheckCompaction(txn1->TxnID()); + } + + //drop table + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("drop table")); + auto status = txn1->DropTableCollectionByName("default_db", "tbl1", ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } +} + +TEST_P(TableEntryTest, check_any_delete_test){ + TxnManager *txn_mgr = infinity::InfinityContext::instance().storage()->txn_manager(); + + //create table + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("create table")); + Vector> columns; + { + std::set constraints; + constraints.insert(ConstraintType::kNotNull); + i64 column_id = 0; + auto column_def_ptr = + MakeShared(column_id, MakeShared(LogicalType::kVarchar), "col1", constraints); + columns.emplace_back(column_def_ptr); + } + auto tbl1_def = MakeUnique(MakeShared("default_db"), MakeShared("tbl1"), columns); + auto status = txn1->CreateTable("default_db", std::move(tbl1_def), ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } + + //insert data + { + InsertData("default_db", "tbl1"); + } + + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("check")); + const String &db_name = "default_db"; + const String &table_name = "tbl1"; + auto [table_entry, table_status] = txn1->GetTableByName(db_name, table_name); + EXPECT_FALSE(table_entry->CheckAnyDelete(txn1->BeginTS())); + Vector delete_row_ids; + delete_row_ids.emplace_back(0, 0); + txn1->Delete(table_entry, delete_row_ids); + txn_mgr->CommitTxn(txn1); + + auto *txn2 = txn_mgr->BeginTxn(MakeUnique("check")); + EXPECT_TRUE(table_entry->CheckAnyDelete(txn2->BeginTS())); + } + + //drop table + { + auto *txn1 = txn_mgr->BeginTxn(MakeUnique("drop table")); + auto status = txn1->DropTableCollectionByName("default_db", "tbl1", ConflictType::kError); + EXPECT_TRUE(status.ok()); + txn_mgr->CommitTxn(txn1); + } } \ No newline at end of file