Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Commit

Permalink
Issue59 rename namespace to database (#227)
Browse files Browse the repository at this point in the history
* Issue59 - rename namespaceInfo to DatabaseInfo

Co-authored-by: Jerry Feng <[email protected]>
  • Loading branch information
jerryhfeng and Jerry Feng authored Apr 5, 2021
1 parent eeba0e4 commit 90303a6
Show file tree
Hide file tree
Showing 23 changed files with 566 additions and 567 deletions.
16 changes: 8 additions & 8 deletions src/k2/connector/entities/entity_ids.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const TableId kPgProcTableId = PgObjectId::GetTableUuid(kTemplate1Oid, kPgProcTa
//-------------------------------------------------------------------------------------------------

namespace {

// TODO: get rid of YugaByte 16 byid UUID, and use PG dboid and tabld oid directly.
// Layout of Postgres database and table 4-byte oids in a YugaByte 16-byte table UUID:
//
// +-----------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -73,11 +73,11 @@ std::string UuidToString(uuid* id) {

} // namespace

std::string PgObjectId::GetNamespaceUuid() const {
return GetNamespaceUuid(database_oid_);
std::string PgObjectId::GetDatabaseUuid() const {
return GetDatabaseUuid(database_oid_);
}

std::string PgObjectId::GetNamespaceUuid(const PgOid& database_oid) {
std::string PgObjectId::GetDatabaseUuid(const PgOid& database_oid) {
uuid id = boost::uuids::nil_uuid();
UuidSetDatabaseId(database_oid, &id);
return UuidToString(&id);
Expand Down Expand Up @@ -123,11 +123,11 @@ bool PgObjectId::IsPgsqlId(const string& uuid) {
return false;
}

Result<uint32_t> PgObjectId::GetDatabaseOidByUuid(const std::string& namespace_uuid) {
DCHECK(IsPgsqlId(namespace_uuid));
Result<uint32_t> PgObjectId::GetDatabaseOidByUuid(const std::string& database_uuid) {
DCHECK(IsPgsqlId(database_uuid));
try {
size_t pos = 0;
const uint32_t oid = stoul(namespace_uuid.substr(0, sizeof(uint32_t) * 2), &pos, 16);
const uint32_t oid = stoul(database_uuid.substr(0, sizeof(uint32_t) * 2), &pos, 16);
if (pos == sizeof(uint32_t) * 2) {
return oid;
}
Expand All @@ -136,7 +136,7 @@ Result<uint32_t> PgObjectId::GetDatabaseOidByUuid(const std::string& namespace_u
// TODO: log the actual exceptions
}
return kPgInvalidOid;
return STATUS(InvalidArgument, "Invalid PostgreSQL namespace uuid", namespace_uuid);
return STATUS(InvalidArgument, "Invalid PostgreSQL database uuid", database_uuid);
}

uint32_t PgObjectId::GetTableOidByTableUuid(const std::string& table_uuid) {
Expand Down
12 changes: 4 additions & 8 deletions src/k2/connector/entities/entity_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,13 @@ namespace sql {
using yb::Result;
using yb::Format;

using NamespaceName = std::string;
using TableName = std::string;
using UDTypeName = std::string;
using RoleName = std::string;

using NamespaceId = std::string;
using TableId = std::string;
using UDTypeId = std::string;

using NamespaceIdTableNamePair = std::pair<NamespaceId, TableName>;

// In addition to regular columns, YB support for postgres also have virtual columns.
// Virtual columns are just expression that is evaluated by DocDB in "doc_expr.cc".
enum class PgSystemAttrNum : int {
Expand Down Expand Up @@ -135,9 +131,9 @@ namespace sql {
}
}

// Get namespace uuid for a Postgres database.
std::string GetNamespaceUuid() const;
static std::string GetNamespaceUuid(const PgOid& database_oid);
// Get database uuid for a Postgres database.
std::string GetDatabaseUuid() const;
static std::string GetDatabaseUuid(const PgOid& database_oid);

// Get table uuid for a Postgres table.
std::string GetTableUuid() const;
Expand All @@ -147,7 +143,7 @@ namespace sql {
std::string GetTableId() const;
static std::string GetTableId(const PgOid& table_oid);

// Is the namespace/table uuid a Postgres database or table uuid?
// Is the database/table uuid a Postgres database or table uuid?
static bool IsPgsqlId(const std::string& uuid);

// Get Postgres database and table oids from a namespace/table uuid.
Expand Down
6 changes: 3 additions & 3 deletions src/k2/connector/entities/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ namespace sql {
return index_map_.FindIndex(index_id);
}

std::shared_ptr<TableInfo> TableInfo::Clone(std::shared_ptr<TableInfo> table_info, std::string namespace_id,
std::string namespace_name, std::string table_uuid, std::string table_name) {
std::shared_ptr<TableInfo> new_table_info = std::make_shared<TableInfo>(namespace_id, namespace_name, table_info->table_oid(), table_name, table_uuid, table_info->schema());
std::shared_ptr<TableInfo> TableInfo::Clone(std::shared_ptr<TableInfo> table_info, std::string database_id,
std::string database_name, std::string table_uuid, std::string table_name) {
std::shared_ptr<TableInfo> new_table_info = std::make_shared<TableInfo>(database_id, database_name, table_info->table_oid(), table_name, table_uuid, table_info->schema());
new_table_info->set_next_column_id(table_info->next_column_id());
new_table_info->set_is_sys_table(table_info->is_sys_table());
if (table_info->has_secondary_indexes()) {
Expand Down
20 changes: 10 additions & 10 deletions src/k2/connector/entities/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ namespace sql {

typedef std::shared_ptr<TableInfo> SharedPtr;

TableInfo(std::string namespace_id, std::string namespace_name, uint32_t table_oid, std::string table_name, std::string table_uuid, Schema schema) :
namespace_id_(namespace_id), namespace_name_(namespace_name), table_oid_(table_oid), table_id_(PgObjectId::GetTableId(table_oid)), table_name_(table_name),
TableInfo(std::string database_id, std::string database_name, uint32_t table_oid, std::string table_name, std::string table_uuid, Schema schema) :
database_id_(database_id), database_name_(database_name), table_oid_(table_oid), table_id_(PgObjectId::GetTableId(table_oid)), table_name_(table_name),
table_uuid_(table_uuid), schema_(std::move(schema)) {
}

const std::string& namespace_id() const {
return namespace_id_;
const std::string& database_id() const {
return database_id_;
}

const std::string& namespace_name() const {
return namespace_name_;
const std::string& database_name() const {
return database_name_;
}

const std::string& table_id() const {
Expand Down Expand Up @@ -133,12 +133,12 @@ namespace sql {
return is_shared_table_;
}

static std::shared_ptr<TableInfo> Clone(std::shared_ptr<TableInfo> table_info, std::string namespace_id,
std::string namespace_name, std::string table_uuid, std::string table_name);
static std::shared_ptr<TableInfo> Clone(std::shared_ptr<TableInfo> table_info, std::string database_id,
std::string database_name, std::string table_uuid, std::string table_name);

private:
std::string namespace_id_;
std::string namespace_name_; // Can be empty, that means the namespace has not been set yet.
std::string database_id_;
std::string database_name_; // Can be empty, that means the database has not been set yet.
// PG internal object id
uint32_t table_oid_;
std::string table_id_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,37 @@ Copyright(c) 2020 Futurewei Cloud
SOFTWARE.
*/

#include "pggate/catalog/namespace_info_handler.h"
#include "pggate/catalog/database_info_handler.h"

#include <glog/logging.h>

namespace k2pg {
namespace sql {
namespace catalog {

NamespaceInfoHandler::NamespaceInfoHandler(std::shared_ptr<K2Adapter> k2_adapter)
DatabaseInfoHandler::DatabaseInfoHandler(std::shared_ptr<K2Adapter> k2_adapter)
: collection_name_(CatalogConsts::skv_collection_name_sql_primary),
schema_name_(CatalogConsts::skv_schema_name_namespace_info) {
schema_name_(CatalogConsts::skv_schema_name_database_info) {
schema_ptr_ = std::make_shared<k2::dto::Schema>(schema_);
k2_adapter_ = k2_adapter;
}

NamespaceInfoHandler::~NamespaceInfoHandler() {
DatabaseInfoHandler::~DatabaseInfoHandler() {
}

// Verify the Namespace(database)_info corresponding SKVSchema in the PG primary SKVCollection doesn't exist and create it
// Verify the database_info corresponding SKVSchema in the PG primary SKVCollection doesn't exist and create it
// Called only once in sql_catalog_manager::InitPrimaryCluster()
InitNamespaceTableResult NamespaceInfoHandler::InitNamespaceTable() {
InitNamespaceTableResult response;
InitDatabaseTableResult DatabaseInfoHandler::InitDatabasTable() {
InitDatabaseTableResult response;

// check to make sure the schema doesn't exists
auto result = k2_adapter_->GetSchema(collection_name_, schema_name_, 1).get();
if (result.status.code != 404) { // expect NotFound
if (result.status.is2xxOK()) {
K2LOG_E(log::catalog, "Unexpected NamespaceInfo SKV schema already exists during init.");
response.status = STATUS(InternalError, "Unexpected NamespaceInfo SKV schema already exists during init.");
K2LOG_E(log::catalog, "Unexpected DatabaseInfo SKV schema already exists during init.");
response.status = STATUS(InternalError, "Unexpected DatabaseInfo SKV schema already exists during init.");
} else { // other read error
K2LOG_E(log::catalog, "Unexpected NamespaceInfo SKV schema read error during init.{}", result.status);
K2LOG_E(log::catalog, "Unexpected DatabaseInfo SKV schema read error during init.{}", result.status);
response.status = K2Adapter::K2StatusToYBStatus(result.status);
}
return response;
Expand All @@ -65,24 +65,24 @@ InitNamespaceTableResult NamespaceInfoHandler::InitNamespaceTable() {
return response;
}

K2LOG_I(log::catalog, "InitNamespaceTable succeeded schema as {} in {}", schema_ptr_->name, collection_name_);
K2LOG_I(log::catalog, "InitDatabasTable succeeded schema as {} in {}", schema_ptr_->name, collection_name_);
response.status = Status(); // OK
return response;
}

AddOrUpdateNamespaceResult NamespaceInfoHandler::AddOrUpdateNamespace(std::shared_ptr<PgTxnHandler> txnHandler, std::shared_ptr<NamespaceInfo> namespace_info) {
AddOrUpdateNamespaceResult response;
AddOrUpdateDatabaseResult DatabaseInfoHandler::UpsertDatabase(std::shared_ptr<PgTxnHandler> txnHandler, std::shared_ptr<DatabaseInfo> database_info) {
AddOrUpdateDatabaseResult response;
k2::dto::SKVRecord record(collection_name_, schema_ptr_);
record.serializeNext<k2::String>(namespace_info->GetNamespaceId());
record.serializeNext<k2::String>(namespace_info->GetNamespaceName());
record.serializeNext<k2::String>(database_info->GetDatabaseId());
record.serializeNext<k2::String>(database_info->GetDatabaseName());
// use int64_t to represent uint32_t since since SKV does not support them
record.serializeNext<int64_t>(namespace_info->GetNamespaceOid());
record.serializeNext<int64_t>(namespace_info->GetNextPgOid());
record.serializeNext<int64_t>(database_info->GetDatabaseOid());
record.serializeNext<int64_t>(database_info->GetNextPgOid());

auto upsertRes = k2_adapter_->UpsertRecord(txnHandler->GetTxn(), record).get();
if (!upsertRes.status.is2xxOK())
{
K2LOG_E(log::catalog, "Failed to upsert namespace record {} due to {}", namespace_info->GetNamespaceId(), upsertRes.status);
K2LOG_E(log::catalog, "Failed to upsert databaseinfo record {} due to {}", database_info->GetDatabaseId(), upsertRes.status);
response.status = K2Adapter::K2StatusToYBStatus(upsertRes.status);
return response;
}
Expand All @@ -91,10 +91,10 @@ AddOrUpdateNamespaceResult NamespaceInfoHandler::AddOrUpdateNamespace(std::share
return response;
}

GetNamespaceResult NamespaceInfoHandler::GetNamespace(std::shared_ptr<PgTxnHandler> txnHandler, const std::string& namespace_id) {
GetNamespaceResult response;
GetDatabaseResult DatabaseInfoHandler::GetDatabase(std::shared_ptr<PgTxnHandler> txnHandler, const std::string& database_id) {
GetDatabaseResult response;
k2::dto::SKVRecord recordKey(collection_name_, schema_ptr_);
recordKey.serializeNext<k2::String>(namespace_id);
recordKey.serializeNext<k2::String>(database_id);

auto result = k2_adapter_->ReadRecord(txnHandler->GetTxn(), recordKey).get();
if (!result.status.is2xxOK()) {
Expand All @@ -103,18 +103,18 @@ GetNamespaceResult NamespaceInfoHandler::GetNamespace(std::shared_ptr<PgTxnHandl
return response;
}

std::shared_ptr<NamespaceInfo> namespace_ptr = std::make_shared<NamespaceInfo>();
namespace_ptr->SetNamespaceId(result.value.deserializeNext<k2::String>().value());
namespace_ptr->SetNamespaceName(result.value.deserializeNext<k2::String>().value());
std::shared_ptr<DatabaseInfo> database_ptr = std::make_shared<DatabaseInfo>();
database_ptr->SetDatabaseId(result.value.deserializeNext<k2::String>().value());
database_ptr->SetDatabaseName(result.value.deserializeNext<k2::String>().value());
// use int64_t to represent uint32_t since since SKV does not support them
namespace_ptr->SetNamespaceOid(result.value.deserializeNext<int64_t>().value());
namespace_ptr->SetNextPgOid(result.value.deserializeNext<int64_t>().value());
response.namespaceInfo = namespace_ptr;
database_ptr->SetDatabaseOid(result.value.deserializeNext<int64_t>().value());
database_ptr->SetNextPgOid(result.value.deserializeNext<int64_t>().value());
response.databaseInfo = database_ptr;
return response;
}

ListNamespacesResult NamespaceInfoHandler::ListNamespaces(std::shared_ptr<PgTxnHandler> txnHandler) {
ListNamespacesResult response;
ListDatabaseResult DatabaseInfoHandler::ListDatabases(std::shared_ptr<PgTxnHandler> txnHandler) {
ListDatabaseResult response;
auto create_result = k2_adapter_->CreateScanRead(collection_name_, schema_name_).get();
if (!create_result.status.is2xxOK()) {
K2LOG_E(log::catalog, "Failed to create scan read due to {}", create_result.status);
Expand All @@ -135,33 +135,33 @@ ListNamespacesResult NamespaceInfoHandler::ListNamespaces(std::shared_ptr<PgTxnH
}

for (auto& record : query_result.records) {
std::shared_ptr<NamespaceInfo> namespace_ptr = std::make_shared<NamespaceInfo>();
namespace_ptr->SetNamespaceId(record.deserializeNext<k2::String>().value());
namespace_ptr->SetNamespaceName(record.deserializeNext<k2::String>().value());
std::shared_ptr<DatabaseInfo> database_ptr = std::make_shared<DatabaseInfo>();
database_ptr->SetDatabaseId(record.deserializeNext<k2::String>().value());
database_ptr->SetDatabaseName(record.deserializeNext<k2::String>().value());
// use int64_t to represent uint32_t since since SKV does not support them
namespace_ptr->SetNamespaceOid(record.deserializeNext<int64_t>().value());
namespace_ptr->SetNextPgOid(record.deserializeNext<int64_t>().value());
response.namespaceInfos.push_back(namespace_ptr);
database_ptr->SetDatabaseOid(record.deserializeNext<int64_t>().value());
database_ptr->SetNextPgOid(record.deserializeNext<int64_t>().value());
response.databaseInfos.push_back(database_ptr);
}
// if the query is not done, the query itself is updated with the pagination token for the next call
} while (!query->isDone());
response.status = Status::OK();
return response;
}

DeleteNamespaceResult NamespaceInfoHandler::DeleteNamespace(std::shared_ptr<PgTxnHandler> txnHandler, std::shared_ptr<NamespaceInfo> namespace_info) {
DeleteNamespaceResult response;
DeleteDataseResult DatabaseInfoHandler::DeleteDatabase(std::shared_ptr<PgTxnHandler> txnHandler, std::shared_ptr<DatabaseInfo> database_info) {
DeleteDataseResult response;
k2::dto::SKVRecord record(collection_name_, schema_ptr_);
record.serializeNext<k2::String>(namespace_info->GetNamespaceId());
record.serializeNext<k2::String>(namespace_info->GetNamespaceName());
record.serializeNext<k2::String>(database_info->GetDatabaseId());
record.serializeNext<k2::String>(database_info->GetDatabaseName());
// use int64_t to represent uint32_t since since SKV does not support them
record.serializeNext<int64_t>(namespace_info->GetNamespaceOid());
record.serializeNext<int64_t>(namespace_info->GetNextPgOid());
record.serializeNext<int64_t>(database_info->GetDatabaseOid());
record.serializeNext<int64_t>(database_info->GetNextPgOid());

auto delResponse = k2_adapter_->DeleteRecord(txnHandler->GetTxn(), record).get();
if (!delResponse.status.is2xxOK()) {
K2LOG_E(log::catalog, "Failed to delete namespace ID {} in Collection {}, due to {}",
namespace_info->GetNamespaceId(), collection_name_, delResponse.status);
K2LOG_E(log::catalog, "Failed to delete database ID {} in Collection {}, due to {}",
database_info->GetDatabaseId(), collection_name_, delResponse.status);
response.status = K2Adapter::K2StatusToYBStatus(delResponse.status);
return response;
}
Expand Down
Loading

0 comments on commit 90303a6

Please sign in to comment.