Skip to content

Commit

Permalink
Return table id with flag;
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-epsilla committed Nov 6, 2023
1 parent 2e4e9e2 commit 9a890e1
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion engine/bindings/python/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ static PyObject *create_table(PyObject *self, PyObject *args, PyObject *kwargs)
Py_DECREF(tableFieldsListPtr);

// TODO: add auto embedding here
auto status = db->CreateTable(db_name, schema);
size_t table_id;
auto status = db->CreateTable(db_name, schema, table_id);
if (!status.ok()) {
PyErr_SetString(PyExc_Exception, status.message().c_str());
return NULL;
Expand Down
3 changes: 2 additions & 1 deletion engine/db/catalog/basic_meta_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ Status ValidateSchema(TableSchema& table_schema) {
return Status::OK();
}

Status BasicMetaImpl::CreateTable(const std::string& db_name, TableSchema& table_schema) {
Status BasicMetaImpl::CreateTable(const std::string& db_name, TableSchema& table_schema, size_t& table_id) {
// Table name cannot be duplicated.
bool has_table = false;
auto status = HasTable(db_name, table_schema.name_, has_table);
Expand All @@ -367,6 +367,7 @@ Status BasicMetaImpl::CreateTable(const std::string& db_name, TableSchema& table

// TODO: a better way to assign table id.
table_schema.id_ = GetNewTableId(db);
table_id = table_schema.id_;

db.tables_.push_back(table_schema);

Expand Down
2 changes: 1 addition & 1 deletion engine/db/catalog/basic_meta_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BasicMetaImpl : public Meta {

Status DropDatabase(const std::string& db_name) override;

Status CreateTable(const std::string& db_name, TableSchema& table_schema) override;
Status CreateTable(const std::string& db_name, TableSchema& table_schema, size_t& table_id) override;

Status HasTable(const std::string& db_name, const std::string& table_name, bool& response) override;

Expand Down
2 changes: 1 addition & 1 deletion engine/db/catalog/meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Meta {

virtual Status DropDatabase(const std::string& db_name) = 0;

virtual Status CreateTable(const std::string& db_name, TableSchema& table_schema) = 0;
virtual Status CreateTable(const std::string& db_name, TableSchema& table_schema, size_t& table_id) = 0;

virtual Status HasTable(const std::string& db_name, const std::string& table_name, bool& response) = 0;

Expand Down
4 changes: 2 additions & 2 deletions engine/db/db_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ std::shared_ptr<DBMVP> DBServer::GetDB(const std::string& db_name) {
}

Status DBServer::CreateTable(const std::string& db_name,
meta::TableSchema& table_schema) {
meta::TableSchema& table_schema, size_t& table_id) {
// Create table in meta.
vectordb::Status status = meta_->CreateTable(db_name, table_schema);
vectordb::Status status = meta_->CreateTable(db_name, table_schema, table_id);
if (!status.ok()) {
return status;
}
Expand Down
2 changes: 1 addition & 1 deletion engine/db/db_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DBServer {

Status LoadDB(const std::string& db_name, std::string& db_catalog_path, int64_t init_table_scale, bool wal_enabled);
Status UnloadDB(const std::string& db_name);
Status CreateTable(const std::string& db_name, meta::TableSchema& table_schema);
Status CreateTable(const std::string& db_name, meta::TableSchema& table_schema, size_t& table_id);
Status DropTable(const std::string& db_name, const std::string& table_name);
std::shared_ptr<DBMVP> GetDB(const std::string& db_name);
Status ListTables(const std::string& db_name, std::vector<std::string>& table_names);
Expand Down
24 changes: 20 additions & 4 deletions engine/server/web_server/web_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ class WebController : public oatpp::web::server::api::ApiController {
return createDtoResponse(Status::CODE_400, dto);
}

bool return_table_id = false;
if (parsedBody.HasMember("returnTableId")) {
return_table_id = parsedBody.GetBool("returnTableId");
}

vectordb::engine::meta::TableSchema table_schema;
if (!parsedBody.HasMember("name")) {
dto->statusCode = Status::CODE_400.code;
Expand Down Expand Up @@ -223,7 +228,8 @@ class WebController : public oatpp::web::server::api::ApiController {
}
}

vectordb::Status status = db_server->CreateTable(db_name, table_schema);
size_t table_id;
vectordb::Status status = db_server->CreateTable(db_name, table_schema, table_id);

if (!status.ok()) {
auto status_code = Status::CODE_500;
Expand All @@ -236,9 +242,19 @@ class WebController : public oatpp::web::server::api::ApiController {
dto->message = status.message();
return createDtoResponse(status_code, dto);
}
dto->statusCode = Status::CODE_200.code;
dto->message = "Create " + table_schema.name_ + " successfully.";
return createDtoResponse(Status::CODE_200, dto);

if (return_table_id) {
auto res_dto = ObjectRespDto::createShared();
res_dto->statusCode = Status::CODE_200.code;
res_dto->message = "Create " + table_schema.name_ + " successfully.";
oatpp::parser::json::mapping::ObjectMapper mapper;
res_dto->result = mapper.readFromString<oatpp::Any>("{\"tableId\": " + std::to_string(table_id) + "}");
return createDtoResponse(Status::CODE_200, res_dto);
} else {
dto->statusCode = Status::CODE_200.code;
dto->message = "Create " + table_schema.name_ + " successfully.";
return createDtoResponse(Status::CODE_200, dto);
}
}

ADD_CORS(DropTable)
Expand Down

0 comments on commit 9a890e1

Please sign in to comment.