From 9e32adfc480982d09bc51c0413eba771df01b3f1 Mon Sep 17 00:00:00 2001 From: Ami11111 <691325878@qq.com> Date: Mon, 26 Aug 2024 16:54:13 +0800 Subject: [PATCH] add unit test cases --- src/unit_test/storage/meta/catalog.cpp | 168 +++++++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/src/unit_test/storage/meta/catalog.cpp b/src/unit_test/storage/meta/catalog.cpp index b0c99e0f94..7605324ed0 100644 --- a/src/unit_test/storage/meta/catalog.cpp +++ b/src/unit_test/storage/meta/catalog.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include "unit_test/base_test.h" +#include "type/complex/embedding_type.h" import infinity_context; import infinity_exception; @@ -31,9 +32,17 @@ import txn; import catalog; import status; import extra_ddl_info; +import column_def; +import data_type; +import logical_type; +import embedding_info; +import index_hnsw; +import statement_common; import base_entry; +using namespace infinity; + class CatalogTest : public BaseTestParamStr { void SetUp() override { BaseTestParamStr::SetUp(); @@ -260,3 +269,162 @@ TEST_P(CatalogTest, concurrent_test) { txn_mgr->CommitTxn(txn7); } } + +// txn1: create db1, get db1 info, delete db1, get db1, commit +// txn2: get db1 info, get db1, commit +TEST_P(CatalogTest, get_db_info_test) { + using namespace infinity; + + 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 db")); + + // start txn2 + auto *txn2 = txn_mgr->BeginTxn(MakeUnique("get db")); + + + // create db in empty catalog should be success + { + auto [base_entry, status] = catalog->CreateDatabase("db1", txn1->TxnID(), txn1->BeginTS(), txn_mgr); + EXPECT_TRUE(status.ok()); + } + + { + auto [db_info1, status1] = catalog->GetDatabaseInfo("db1", txn1->TxnID(), txn1->BeginTS()); + // should be visible to same txn + EXPECT_TRUE(status1.ok()); + std::cout<<*(db_info1->db_name_)<db_name_->c_str()); + + // should not be visible to other txn + auto [db_info2, status2] = catalog->GetDatabaseInfo("db1", txn2->TxnID(), txn2->BeginTS()); + EXPECT_TRUE(!status2.ok()); + } + + // drop db should be success + { + auto [base_entry, status] = catalog->DropDatabase("db1", txn1->TxnID(), txn1->BeginTS(), txn_mgr); + EXPECT_TRUE(status.ok()); + + auto [db_info1, status1] = catalog->GetDatabaseInfo("db1", txn1->TxnID(), txn1->BeginTS()); + // should not be visible to same txn + EXPECT_TRUE(!status1.ok()); + + // should not be visible to other txn + auto [db_info2, status2] = catalog->GetDatabaseInfo("db1", txn2->TxnID(), txn2->BeginTS()); + EXPECT_TRUE(!status2.ok()); + } + + txn_mgr->CommitTxn(txn1); + txn_mgr->CommitTxn(txn2); +} + +TEST_P(CatalogTest, get_table_info_test) { + using namespace infinity; + + 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 table")); + + //create table, get table info, drop table + { + Vector> columns; + { + std::set constraints; + constraints.insert(ConstraintType::kNotNull); + i64 column_id = 0; + auto embeddingInfo = MakeShared(EmbeddingDataType::kElemFloat, 128); + auto column_def_ptr = + MakeShared(column_id, MakeShared(LogicalType::kEmbedding, embeddingInfo), "col1", constraints); + 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); + EXPECT_TRUE(status.ok()); + + auto [table_info, status1] = catalog->GetTableInfo("default_db", "tbl1", txn1); + EXPECT_TRUE(status1.ok()); + EXPECT_STREQ("tbl1", table_info->table_name_->c_str()); + + auto [table_entry1, status2] = catalog->DropTableByName("default_db", "tbl1", ConflictType::kError, txn1->TxnID(), txn1->BeginTS(), txn_mgr); + EXPECT_TRUE(status2.ok()); + } + + txn_mgr->CommitTxn(txn1); +} + +TEST_P(CatalogTest, get_table_index_info_test) { + using namespace infinity; + + 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 + { + Vector> columns; + { + std::set constraints; + constraints.insert(ConstraintType::kNotNull); + i64 column_id = 0; + auto embeddingInfo = MakeShared(EmbeddingDataType::kElemFloat, 128); + auto column_def_ptr = + MakeShared(column_id, MakeShared(LogicalType::kEmbedding, embeddingInfo), "col1", constraints); + 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); + EXPECT_TRUE(status.ok()); + } + + // CreateIndex + { + Vector columns1{"col1"}; + Vector parameters1; + parameters1.emplace_back(new InitParameter("metric", "l2")); + parameters1.emplace_back(new InitParameter("encode", "plain")); + parameters1.emplace_back(new InitParameter("m", "16")); + parameters1.emplace_back(new InitParameter("ef_construction", "200")); + + SharedPtr index_name = MakeShared("hnsw_index"); + auto index_base_hnsw = IndexHnsw::Make(index_name, "hnsw_index_test_hnsw", columns1, parameters1); + for (auto *init_parameter : parameters1) { + delete init_parameter; + } + + 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()); + EXPECT_EQ(table_status.ok(), true); + auto [index_entry, index_status] = catalog->CreateIndex(table_entry, index_base_hnsw, conflict_type, txn1->TxnID(), txn1->BeginTS(), txn_mgr); + EXPECT_EQ(index_status.ok(), true); + } + + //get index info + { + auto [index_info, status] = catalog->GetTableIndexInfo("default_db", "tbl1", "hnsw_index", txn1->TxnID(), txn1->BeginTS()); + EXPECT_TRUE(status.ok()); + EXPECT_STREQ("hnsw_index", index_info->index_name_->c_str()); + } + + //drop index + { + auto [index_entry, status] = catalog->DropIndex("default_db", "tbl1", "hnsw_index", ConflictType::kError, txn1->TxnID(), txn1->BeginTS(), txn_mgr); + EXPECT_TRUE(status.ok()); + } + + //drop table + { + auto [table_entry, status] = catalog->DropTableByName("default_db", "tbl1", ConflictType::kError, txn1->TxnID(), txn1->BeginTS(), txn_mgr); + EXPECT_TRUE(status.ok()); + } + + txn_mgr->CommitTxn(txn1); +}