From dbbde385e6339f810288c215c0461f603356e2d4 Mon Sep 17 00:00:00 2001 From: yixinglu <2520865+yixinglu@users.noreply.github.com> Date: Wed, 3 Nov 2021 17:43:34 +0800 Subject: [PATCH] Cleanup useless files --- src/common/utils/MetaKeyUtils.cpp | 1184 ----------------------------- src/common/utils/MetaKeyUtils.h | 392 ---------- 2 files changed, 1576 deletions(-) delete mode 100644 src/common/utils/MetaKeyUtils.cpp delete mode 100644 src/common/utils/MetaKeyUtils.h diff --git a/src/common/utils/MetaKeyUtils.cpp b/src/common/utils/MetaKeyUtils.cpp deleted file mode 100644 index a5ace5827d7..00000000000 --- a/src/common/utils/MetaKeyUtils.cpp +++ /dev/null @@ -1,1184 +0,0 @@ -/* Copyright (c) 2018 vesoft inc. All rights reserved. - * - * This source code is licensed under Apache 2.0 License. - */ - -#include "common/utils/MetaKeyUtils.h" - -#include -#include -#include - -#include - -#include "common/network/NetworkUtils.h" - -namespace nebula { - -// Systemtable means that it does not contain any space information(id).false -// means that the backup should be skipped. -static const std::unordered_map> systemTableMaps = { - {"users", {"__users__", true}}, - {"hosts", {"__hosts__", false}}, - {"snapshots", {"__snapshots__", false}}, - {"configs", {"__configs__", true}}, - {"groups", {"__groups__", true}}, - {"zones", {"__zones__", true}}, - {"ft_service", {"__ft_service__", false}}, - {"sessions", {"__sessions__", true}}}; - -// SystemInfo will always be backuped -static const std::unordered_map> systemInfoMaps{ - {"autoIncrementId", {"__id__", true}}, {"lastUpdateTime", {"__last_update_time__", true}}}; - -// name => {prefix, parseSpaceid}, nullptr means that the backup should be skipped. -static const std::unordered_map< - std::string, - std::pair>> - tableMaps = {{"spaces", {"__spaces__", MetaKeyUtils::spaceId}}, - {"parts", {"__parts__", MetaKeyUtils::parsePartKeySpaceId}}, - {"tags", {"__tags__", MetaKeyUtils::parseTagsKeySpaceID}}, - {"edges", {"__edges__", MetaKeyUtils::parseEdgesKeySpaceID}}, - {"indexes", {"__indexes__", MetaKeyUtils::parseIndexesKeySpaceID}}, - // Index tables are handled separately. - {"index", {"__index__", nullptr}}, - {"index_status", {"__index_status__", MetaKeyUtils::parseIndexStatusKeySpaceID}}, - {"roles", {"__roles__", MetaKeyUtils::parseRoleSpace}}, - {"leaders", {"__leaders__", nullptr}}, - {"leader_terms", {"__leader_terms__", nullptr}}, - {"listener", {"__listener__", nullptr}}, - {"stats", {"__stats__", MetaKeyUtils::parseStatsSpace}}, - {"balance_task", {"__balance_task__", nullptr}}, - {"balance_plan", {"__balance_plan__", nullptr}}, - {"ft_index", {"__ft_index__", nullptr}}, - {"local_id", {"__local_id__", MetaKeyUtils::parseLocalIdSpace}}}; - -// clang-format off -static const std::string kSpacesTable = tableMaps.at("spaces").first; // NOLINT -static const std::string kPartsTable = tableMaps.at("parts").first; // NOLINT -static const std::string kHostsTable = systemTableMaps.at("hosts").first; // NOLINT -static const std::string kTagsTable = tableMaps.at("tags").first; // NOLINT -static const std::string kEdgesTable = tableMaps.at("edges").first; // NOLINT -static const std::string kIndexesTable = tableMaps.at("indexes").first; // NOLINT -static const std::string kIndexTable = tableMaps.at("index").first; // NOLINT -static const std::string kIndexStatusTable = tableMaps.at("index_status").first; // NOLINT -static const std::string kUsersTable = systemTableMaps.at("users").first; // NOLINT -static const std::string kRolesTable = tableMaps.at("roles").first; // NOLINT -static const std::string kConfigsTable = systemTableMaps.at("configs").first; // NOLINT -static const std::string kSnapshotsTable = systemTableMaps.at("snapshots").first; // NOLINT -static const std::string kLeadersTable = tableMaps.at("leaders").first; // NOLINT -static const std::string kLeaderTermsTable = tableMaps.at("leader_terms").first; // NOLINT -static const std::string kGroupsTable = systemTableMaps.at("groups").first; // NOLINT -static const std::string kZonesTable = systemTableMaps.at("zones").first; // NOLINT -static const std::string kListenerTable = tableMaps.at("listener").first; // NOLINT - -// Used to record the number of vertices and edges in the space -// The number of vertices of each tag in the space -// The number of edges of each edgetype in the space -static const std::string kStatsTable = tableMaps.at("stats").first; // NOLINT -static const std::string kBalanceTaskTable = tableMaps.at("balance_task").first; // NOLINT -static const std::string kBalancePlanTable = tableMaps.at("balance_plan").first; // NOLINT -static const std::string kLocalIdTable = tableMaps.at("local_id").first; // NOLINT - -const std::string kFTIndexTable = tableMaps.at("ft_index").first; // NOLINT -const std::string kFTServiceTable = systemTableMaps.at("ft_service").first; // NOLINT -const std::string kSessionsTable = systemTableMaps.at("sessions").first; // NOLINT - -const std::string kIdKey = systemInfoMaps.at("autoIncrementId").first; // NOLINT -const std::string kLastUpdateTimeTable = systemInfoMaps.at("lastUpdateTime").first; // NOLINT - -// clang-format on - -const int kMaxIpAddrLen = 15; // '255.255.255.255' - -std::string MetaKeyUtils::getIndexTable() { return tableMaps.at("index").first; } - -std::unordered_map>> -MetaKeyUtils::getTableMaps() { - return tableMaps; -} - -std::unordered_map> MetaKeyUtils::getSystemInfoMaps() { - return systemInfoMaps; -} - -std::unordered_map> MetaKeyUtils::getSystemTableMaps() { - return systemTableMaps; -} - -std::string MetaKeyUtils::lastUpdateTimeKey() { - std::string key; - key.reserve(kLastUpdateTimeTable.size()); - key.append(kLastUpdateTimeTable.data(), kLastUpdateTimeTable.size()); - return key; -} - -std::string MetaKeyUtils::lastUpdateTimeVal(const int64_t timeInMilliSec) { - std::string val; - val.reserve(sizeof(int64_t)); - val.append(reinterpret_cast(&timeInMilliSec), sizeof(int64_t)); - return val; -} - -std::string MetaKeyUtils::spaceKey(GraphSpaceID spaceId) { - std::string key; - key.reserve(kSpacesTable.size() + sizeof(GraphSpaceID)); - key.append(kSpacesTable.data(), kSpacesTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -std::string MetaKeyUtils::spaceVal(const meta::cpp2::SpaceDesc& spaceDesc) { - std::string val; - apache::thrift::CompactSerializer::serialize(spaceDesc, &val); - return val; -} - -meta::cpp2::SpaceDesc MetaKeyUtils::parseSpace(folly::StringPiece rawData) { - meta::cpp2::SpaceDesc spaceDesc; - apache::thrift::CompactSerializer::deserialize(rawData, spaceDesc); - return spaceDesc; -} - -const std::string& MetaKeyUtils::spacePrefix() { return kSpacesTable; } - -GraphSpaceID MetaKeyUtils::spaceId(folly::StringPiece rawKey) { - return *reinterpret_cast(rawKey.data() + kSpacesTable.size()); -} - -std::string MetaKeyUtils::spaceName(folly::StringPiece rawVal) { - return parseSpace(rawVal).get_space_name(); -} - -std::string MetaKeyUtils::partKey(GraphSpaceID spaceId, PartitionID partId) { - std::string key; - key.reserve(kPartsTable.size() + sizeof(GraphSpaceID) + sizeof(PartitionID)); - key.append(kPartsTable.data(), kPartsTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&partId), sizeof(PartitionID)); - return key; -} - -GraphSpaceID MetaKeyUtils::parsePartKeySpaceId(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kPartsTable.size()); -} - -PartitionID MetaKeyUtils::parsePartKeyPartId(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kPartsTable.size() + - sizeof(GraphSpaceID)); -} - -std::string MetaKeyUtils::partVal(const std::vector& hosts) { return partValV2(hosts); } - -// dataVer(int) + vectorSize(size_t) + vector of (strIp(string) + port(int)) -std::string MetaKeyUtils::partValV2(const std::vector& hosts) { - std::string encodedVal; - int dataVersion = 2; - encodedVal.append(reinterpret_cast(&dataVersion), sizeof(int)) - .append(network::NetworkUtils::toHostsStr(hosts)); - return encodedVal; -} - -std::string MetaKeyUtils::partPrefix() { - std::string prefix; - prefix.reserve(kPartsTable.size() + sizeof(GraphSpaceID)); - prefix.append(kPartsTable.data(), kPartsTable.size()); - return prefix; -} - -std::string MetaKeyUtils::partPrefix(GraphSpaceID spaceId) { - std::string prefix; - prefix.reserve(kPartsTable.size() + sizeof(GraphSpaceID)); - prefix.append(kPartsTable.data(), kPartsTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return prefix; -} - -std::vector MetaKeyUtils::parsePartVal(folly::StringPiece val, int partNum) { - static const size_t unitSizeV1 = sizeof(int64_t); - if (unitSizeV1 * partNum == val.size()) { - return parsePartValV1(val); - } - int dataVer = *reinterpret_cast(val.data()); - UNUSED(dataVer); // currently if not ver1, it must be v2 - val.advance(sizeof(int)); - return parsePartValV2(val); -} - -// partion val is ip(int) + port(int) -std::vector MetaKeyUtils::parsePartValV1(folly::StringPiece val) { - std::vector hosts; - static const size_t unitSize = sizeof(int32_t) * 2; - auto hostsNum = val.size() / unitSize; - hosts.reserve(hostsNum); - VLOG(3) << "Total size:" << val.size() << ", host size:" << unitSize << ", host num:" << hostsNum; - for (decltype(hostsNum) i = 0; i < hostsNum; i++) { - HostAddr h; - uint32_t ip = *reinterpret_cast(val.data() + i * unitSize); - h.host = network::NetworkUtils::intToIPv4(ip); - h.port = *reinterpret_cast(val.data() + i * unitSize + sizeof(int32_t)); - hosts.emplace_back(std::move(h)); - } - return hosts; -} - -// dataVer(int) + vectorSize(size_t) + vector of (strIpV4(string) + port(int)) -std::vector MetaKeyUtils::parsePartValV2(folly::StringPiece val) { - std::vector ret; - auto hostsOrErr = network::NetworkUtils::toHosts(val.str()); - if (hostsOrErr.ok()) { - ret = std::move(hostsOrErr.value()); - } else { - LOG(ERROR) << "invalid input for parsePartValV2()"; - } - return ret; -} - -std::string MetaKeyUtils::hostKey(std::string addr, Port port) { return hostKeyV2(addr, port); } - -std::string MetaKeyUtils::hostKeyV2(std::string addr, Port port) { - std::string key; - HostAddr h(addr, port); - key.append(kHostsTable.data(), kHostsTable.size()).append(MetaKeyUtils::serializeHostAddr(h)); - return key; -} - -const std::string& MetaKeyUtils::hostPrefix() { return kHostsTable; } - -HostAddr MetaKeyUtils::parseHostKey(folly::StringPiece key) { - if (key.size() == kHostsTable.size() + sizeof(int64_t)) { - return parseHostKeyV1(key); - } - return parseHostKeyV2(key); -} - -HostAddr MetaKeyUtils::parseHostKeyV1(folly::StringPiece key) { - HostAddr host; - key.advance(kHostsTable.size()); - uint32_t ip = *reinterpret_cast(key.begin()); - host.host = network::NetworkUtils::intToIPv4(ip); - host.port = *reinterpret_cast(key.begin() + sizeof(uint32_t)); - return host; -} - -HostAddr MetaKeyUtils::parseHostKeyV2(folly::StringPiece key) { - key.advance(kHostsTable.size()); - return MetaKeyUtils::deserializeHostAddr(key); -} - -std::string MetaKeyUtils::leaderKey(std::string addr, Port port) { - LOG(ERROR) << "deprecated function\n" << boost::stacktrace::stacktrace(); - return leaderKeyV2(addr, port); -} - -std::string MetaKeyUtils::leaderKeyV2(std::string addr, Port port) { - LOG(ERROR) << "deprecated function\n" << boost::stacktrace::stacktrace(); - std::string key; - HostAddr h(addr, port); - - key.reserve(kLeadersTable.size() + kMaxIpAddrLen + sizeof(Port)); - key.append(kLeadersTable.data(), kLeadersTable.size()).append(MetaKeyUtils::serializeHostAddr(h)); - return key; -} - -std::string MetaKeyUtils::leaderKey(GraphSpaceID spaceId, PartitionID partId) { - std::string key; - key.reserve(kLeaderTermsTable.size() + sizeof(GraphSpaceID) + sizeof(PartitionID)); - key.append(kLeaderTermsTable.data(), kLeaderTermsTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&partId), sizeof(PartitionID)); - return key; -} - -std::string MetaKeyUtils::leaderVal(const LeaderParts& leaderParts) { - LOG(ERROR) << "deprecated function\n" << boost::stacktrace::stacktrace(); - std::string value; - value.reserve(512); - for (const auto& spaceEntry : leaderParts) { - GraphSpaceID spaceId = spaceEntry.first; - value.append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - size_t leaderCount = spaceEntry.second.size(); - value.append(reinterpret_cast(&leaderCount), sizeof(size_t)); - for (const auto& partId : spaceEntry.second) { - value.append(reinterpret_cast(&partId), sizeof(PartitionID)); - } - } - return value; -} - -// v3: dataVer(int) + lenOfHost(8) + HostAddr(varchar) + term(int64_t) -std::string MetaKeyUtils::leaderValV3(const HostAddr& h, int64_t term) { - std::string leaderVal; - leaderVal.reserve(256); - - int dataVersion = 3; - - auto sHost = serializeHostAddr(h); - auto lenOfHost = sHost.size(); - - leaderVal.append(reinterpret_cast(&dataVersion), sizeof(dataVersion)) - .append(reinterpret_cast(&lenOfHost), sizeof(lenOfHost)) - .append(sHost) - .append(reinterpret_cast(&term), sizeof(term)); - return leaderVal; -} - -// v3: dataVer(int) + lenOfHost(8) + HostAddr(varchar) + term(int64_t) -std::tuple MetaKeyUtils::parseLeaderValV3( - folly::StringPiece val) { - std::tuple ret; - std::get<2>(ret) = nebula::cpp2::ErrorCode::SUCCEEDED; - int dataVer = *reinterpret_cast(val.data()); - if (dataVer != 3) { - std::get<2>(ret) = nebula::cpp2::ErrorCode::E_INVALID_PARM; - return ret; - } - - CHECK_GE(val.size(), sizeof(int)); - val.advance(sizeof(int)); - auto lenOfHost = *reinterpret_cast(val.data()); - - val.advance(sizeof(size_t)); - CHECK_GE(val.size(), lenOfHost); - std::get<0>(ret) = MetaKeyUtils::deserializeHostAddr(val.subpiece(0, lenOfHost)); - - val.advance(lenOfHost); - std::get<1>(ret) = *reinterpret_cast(val.data()); - return ret; -} - -const std::string& MetaKeyUtils::leaderPrefix() { return kLeaderTermsTable; } - -std::string MetaKeyUtils::leaderPrefix(GraphSpaceID spaceId) { - std::string key; - key.reserve(kLeaderTermsTable.size() + sizeof(GraphSpaceID)); - key.append(kLeaderTermsTable.data(), kLeaderTermsTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -HostAddr MetaKeyUtils::parseLeaderKey(folly::StringPiece key) { - LOG(ERROR) << "deprecated function\n" << boost::stacktrace::stacktrace(); - if (key.size() == kLeadersTable.size() + sizeof(int64_t)) { - return parseLeaderKeyV1(key); - } - return parseLeaderKeyV2(key); -} - -// input should be a pair of int32_t -HostAddr MetaKeyUtils::parseLeaderKeyV1(folly::StringPiece key) { - LOG(ERROR) << "deprecated function\n" << boost::stacktrace::stacktrace(); - HostAddr host; - CHECK_EQ(key.size(), kLeadersTable.size() + sizeof(int64_t)); - key.advance(kLeadersTable.size()); - auto ip = *reinterpret_cast(key.begin()); - host.host = network::NetworkUtils::intToIPv4(ip); - host.port = *reinterpret_cast(key.begin() + sizeof(ip)); - return host; -} - -HostAddr MetaKeyUtils::parseLeaderKeyV2(folly::StringPiece key) { - LOG(ERROR) << "deprecated function\n" << boost::stacktrace::stacktrace(); - key.advance(kLeadersTable.size()); - return MetaKeyUtils::deserializeHostAddr(key); -} - -std::pair MetaKeyUtils::parseLeaderKeyV3(folly::StringPiece key) { - std::pair ret; - ret.first = *reinterpret_cast(key.data() + kLeaderTermsTable.size()); - ret.second = *reinterpret_cast(key.data() + kLeaderTermsTable.size() + - sizeof(GraphSpaceID)); - return ret; -} - -LeaderParts MetaKeyUtils::parseLeaderValV1(folly::StringPiece val) { - LOG(ERROR) << "deprecated function\n" << boost::stacktrace::stacktrace(); - LeaderParts leaderParts; - size_t size = val.size(); - // decode leader info - size_t offset = 0; - while (offset + sizeof(GraphSpaceID) + sizeof(size_t) < size) { - GraphSpaceID spaceId = *reinterpret_cast(val.data() + offset); - offset += sizeof(GraphSpaceID); - size_t leaderCount = *reinterpret_cast(val.data() + offset); - offset += sizeof(size_t); - std::vector partIds; - for (size_t i = 0; i < leaderCount && offset < size; i++) { - partIds.emplace_back(*reinterpret_cast(val.data() + offset)); - offset += sizeof(PartitionID); - } - leaderParts.emplace(spaceId, std::move(partIds)); - } - return leaderParts; -} - -std::string MetaKeyUtils::schemaEdgePrefix(GraphSpaceID spaceId, EdgeType edgeType) { - std::string key; - key.reserve(kEdgesTable.size() + sizeof(GraphSpaceID) + sizeof(edgeType)); - key.append(kEdgesTable.data(), kEdgesTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&edgeType), sizeof(edgeType)); - return key; -} - -std::string MetaKeyUtils::schemaEdgesPrefix(GraphSpaceID spaceId) { - std::string key; - key.reserve(kEdgesTable.size() + sizeof(GraphSpaceID)); - key.append(kEdgesTable.data(), kEdgesTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -std::string MetaKeyUtils::schemaEdgeKey(GraphSpaceID spaceId, - EdgeType edgeType, - SchemaVer version) { - auto storageVer = std::numeric_limits::max() - version; - std::string key; - key.reserve(kEdgesTable.size() + sizeof(GraphSpaceID) + sizeof(EdgeType) + sizeof(SchemaVer)); - key.append(kEdgesTable.data(), kEdgesTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&edgeType), sizeof(EdgeType)) - .append(reinterpret_cast(&storageVer), sizeof(SchemaVer)); - return key; -} - -std::string MetaKeyUtils::schemaVal(const std::string& name, const meta::cpp2::Schema& schema) { - auto len = name.size(); - std::string val, sval; - apache::thrift::CompactSerializer::serialize(schema, &sval); - val.reserve(sizeof(int32_t) + name.size() + sval.size()); - val.append(reinterpret_cast(&len), sizeof(int32_t)).append(name).append(sval); - return val; -} - -EdgeType MetaKeyUtils::parseEdgeType(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kEdgesTable.size() + sizeof(GraphSpaceID)); -} - -SchemaVer MetaKeyUtils::parseEdgeVersion(folly::StringPiece key) { - auto offset = kEdgesTable.size() + sizeof(GraphSpaceID) + sizeof(EdgeType); - return std::numeric_limits::max() - - *reinterpret_cast(key.begin() + offset); -} - -GraphSpaceID MetaKeyUtils::parseEdgesKeySpaceID(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kEdgesTable.size()); -} - -std::string MetaKeyUtils::schemaTagKey(GraphSpaceID spaceId, TagID tagId, SchemaVer version) { - auto storageVer = std::numeric_limits::max() - version; - std::string key; - key.reserve(kTagsTable.size() + sizeof(GraphSpaceID) + sizeof(TagID) + sizeof(SchemaVer)); - key.append(kTagsTable.data(), kTagsTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&tagId), sizeof(TagID)) - .append(reinterpret_cast(&storageVer), sizeof(SchemaVer)); - return key; -} - -TagID MetaKeyUtils::parseTagId(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kTagsTable.size() + sizeof(GraphSpaceID)); -} - -SchemaVer MetaKeyUtils::parseTagVersion(folly::StringPiece key) { - auto offset = kTagsTable.size() + sizeof(GraphSpaceID) + sizeof(TagID); - return std::numeric_limits::max() - - *reinterpret_cast(key.begin() + offset); -} - -std::string MetaKeyUtils::schemaTagPrefix(GraphSpaceID spaceId, TagID tagId) { - std::string key; - key.reserve(kTagsTable.size() + sizeof(GraphSpaceID) + sizeof(TagID)); - key.append(kTagsTable.data(), kTagsTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&tagId), sizeof(TagID)); - return key; -} - -std::string MetaKeyUtils::schemaTagsPrefix(GraphSpaceID spaceId) { - std::string key; - key.reserve(kTagsTable.size() + sizeof(GraphSpaceID)); - key.append(kTagsTable.data(), kTagsTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -GraphSpaceID MetaKeyUtils::parseTagsKeySpaceID(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kTagsTable.size()); -} - -meta::cpp2::Schema MetaKeyUtils::parseSchema(folly::StringPiece rawData) { - meta::cpp2::Schema schema; - int32_t offset = sizeof(int32_t) + *reinterpret_cast(rawData.begin()); - auto schval = rawData.subpiece(offset, rawData.size() - offset); - apache::thrift::CompactSerializer::deserialize(schval, schema); - return schema; -} - -std::string MetaKeyUtils::indexKey(GraphSpaceID spaceID, IndexID indexID) { - std::string key; - key.reserve(kIndexesTable.size() + sizeof(GraphSpaceID) + sizeof(IndexID)); - key.append(kIndexesTable.data(), kIndexesTable.size()) - .append(reinterpret_cast(&spaceID), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&indexID), sizeof(IndexID)); - return key; -} - -std::string MetaKeyUtils::indexVal(const nebula::meta::cpp2::IndexItem& item) { - std::string value; - apache::thrift::CompactSerializer::serialize(item, &value); - return value; -} - -std::string MetaKeyUtils::indexPrefix(GraphSpaceID spaceId) { - std::string key; - key.reserve(kIndexesTable.size() + sizeof(GraphSpaceID)); - key.append(kIndexesTable.data(), kIndexesTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -GraphSpaceID MetaKeyUtils::parseIndexesKeySpaceID(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kIndexesTable.size()); -} - -IndexID MetaKeyUtils::parseIndexesKeyIndexID(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kIndexesTable.size() + - sizeof(GraphSpaceID)); -} - -nebula::meta::cpp2::IndexItem MetaKeyUtils::parseIndex(const folly::StringPiece& rawData) { - nebula::meta::cpp2::IndexItem item; - apache::thrift::CompactSerializer::deserialize(rawData, item); - return item; -} - -// This method should replace with JobManager when it ready. -std::string MetaKeyUtils::rebuildIndexStatus(GraphSpaceID space, - char type, - const std::string& indexName) { - std::string key; - key.reserve(64); - key.append(kIndexStatusTable.data(), kIndexStatusTable.size()) - .append(reinterpret_cast(&space), sizeof(GraphSpaceID)) - .append(1, type) - .append(indexName); - return key; -} - -// This method should replace with JobManager when it ready. -std::string MetaKeyUtils::rebuildIndexStatusPrefix(GraphSpaceID space, char type) { - std::string key; - key.reserve(kIndexStatusTable.size() + sizeof(GraphSpaceID) + sizeof(char)); - key.append(kIndexStatusTable.data(), kIndexStatusTable.size()) - .append(reinterpret_cast(&space), sizeof(GraphSpaceID)) - .append(1, type); - return key; -} - -std::string MetaKeyUtils::rebuildIndexStatusPrefix() { - std::string key; - key.reserve(kIndexStatusTable.size()); - key.append(kIndexStatusTable.data(), kIndexStatusTable.size()); - return key; -} - -GraphSpaceID MetaKeyUtils::parseIndexStatusKeySpaceID(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kIndexStatusTable.size()); -} - -std::string MetaKeyUtils::indexSpaceKey(const std::string& name) { - EntryType type = EntryType::SPACE; - std::string key; - key.reserve(64); - key.append(kIndexTable.data(), kIndexTable.size()) - .append(reinterpret_cast(&type), sizeof(type)) - .append(name); - return key; -} - -std::string MetaKeyUtils::indexTagKey(GraphSpaceID spaceId, const std::string& name) { - EntryType type = EntryType::TAG; - std::string key; - key.reserve(128); - key.append(kIndexTable.data(), kIndexTable.size()) - .append(reinterpret_cast(&type), sizeof(type)) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(name); - return key; -} - -std::string MetaKeyUtils::indexEdgeKey(GraphSpaceID spaceId, const std::string& name) { - EntryType type = EntryType::EDGE; - std::string key; - key.reserve(128); - key.append(kIndexTable.data(), kIndexTable.size()) - .append(reinterpret_cast(&type), sizeof(type)) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(name); - return key; -} - -GraphSpaceID MetaKeyUtils::parseIndexKeySpaceID(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kIndexTable.size() + - sizeof(EntryType)); -} - -std::string MetaKeyUtils::indexIndexKey(GraphSpaceID spaceID, const std::string& indexName) { - EntryType type = EntryType::INDEX; - std::string key; - key.reserve(128); - key.append(kIndexTable.data(), kIndexTable.size()) - .append(reinterpret_cast(&type), sizeof(type)) - .append(reinterpret_cast(&spaceID), sizeof(GraphSpaceID)) - .append(indexName); - return key; -} - -std::string MetaKeyUtils::indexGroupKey(const std::string& name) { - EntryType type = EntryType::GROUP; - std::string key; - key.reserve(128); - key.append(kIndexTable.data(), kIndexTable.size()) - .append(reinterpret_cast(&type), sizeof(EntryType)) - .append(name); - return key; -} - -std::string MetaKeyUtils::indexZoneKey(const std::string& name) { - EntryType type = EntryType::ZONE; - std::string key; - key.reserve(128); - key.append(kIndexTable.data(), kIndexTable.size()) - .append(reinterpret_cast(&type), sizeof(type)) - .append(name); - return key; -} - -std::string MetaKeyUtils::assembleSegmentKey(const std::string& segment, const std::string& key) { - std::string segmentKey; - segmentKey.reserve(64); - segmentKey.append(segment).append(key.data(), key.size()); - return segmentKey; -} - -std::string MetaKeyUtils::userPrefix() { return kUsersTable; } - -std::string MetaKeyUtils::userKey(const std::string& account) { - std::string key; - key.reserve(kUsersTable.size() + account.size()); - key.append(kUsersTable.data(), kUsersTable.size()).append(account); - return key; -} - -std::string MetaKeyUtils::userVal(const std::string& val) { - std::string key; - auto pwdLen = val.size(); - key.reserve(sizeof(int64_t) + pwdLen); - key.append(reinterpret_cast(&pwdLen), sizeof(size_t)).append(val); - return key; -} - -std::string MetaKeyUtils::parseUser(folly::StringPiece key) { - return key.subpiece(kUsersTable.size(), key.size() - kUsersTable.size()).str(); -} - -std::string MetaKeyUtils::parseUserPwd(folly::StringPiece val) { - auto len = *reinterpret_cast(val.data()); - return val.subpiece(sizeof(size_t), len).str(); -} - -std::string MetaKeyUtils::roleKey(GraphSpaceID spaceId, const std::string& account) { - std::string key; - key.reserve(kRolesTable.size() + sizeof(GraphSpaceID) + account.size()); - key.append(kRolesTable.data(), kRolesTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(account); - return key; -} - -std::string MetaKeyUtils::roleVal(meta::cpp2::RoleType roleType) { - std::string val; - val.reserve(sizeof(meta::cpp2::RoleType)); - val.append(reinterpret_cast(&roleType), sizeof(meta::cpp2::RoleType)); - return val; -} - -std::string MetaKeyUtils::parseRoleUser(folly::StringPiece key) { - auto offset = kRolesTable.size() + sizeof(GraphSpaceID); - return key.subpiece(offset, key.size() - offset).str(); -} - -GraphSpaceID MetaKeyUtils::parseRoleSpace(folly::StringPiece key) { - return *reinterpret_cast(key.data() + kRolesTable.size()); -} - -std::string MetaKeyUtils::rolesPrefix() { return kRolesTable; } - -std::string MetaKeyUtils::roleSpacePrefix(GraphSpaceID spaceId) { - std::string key; - key.reserve(kRolesTable.size() + sizeof(GraphSpaceID)); - key.append(kRolesTable.data(), kRolesTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -std::string MetaKeyUtils::parseRoleStr(folly::StringPiece key) { - auto* c = reinterpret_cast(&key); - auto type = *reinterpret_cast(c); - std::string role; - switch (type) { - case meta::cpp2::RoleType::GOD: { - role = "GOD"; - break; - } - case meta::cpp2::RoleType::ADMIN: { - role = "ADMIN"; - break; - } - case meta::cpp2::RoleType::DBA: { - role = "DBA"; - break; - } - case meta::cpp2::RoleType::USER: { - role = "USER"; - break; - } - case meta::cpp2::RoleType::GUEST: { - role = "GUEST"; - break; - } - } - return role; -} - -std::string MetaKeyUtils::configKey(const meta::cpp2::ConfigModule& module, - const std::string& name) { - int32_t nSize = name.size(); - std::string key; - key.reserve(128); - key.append(kConfigsTable.data(), kConfigsTable.size()) - .append(reinterpret_cast(&module), sizeof(meta::cpp2::ConfigModule)) - .append(reinterpret_cast(&nSize), sizeof(int32_t)) - .append(name); - return key; -} - -std::string MetaKeyUtils::configKeyPrefix(const meta::cpp2::ConfigModule& module) { - std::string key; - key.reserve(128); - key.append(kConfigsTable.data(), kConfigsTable.size()); - if (module != meta::cpp2::ConfigModule::ALL) { - key.append(reinterpret_cast(&module), sizeof(meta::cpp2::ConfigModule)); - } - return key; -} - -std::string MetaKeyUtils::configValue(const meta::cpp2::ConfigMode& valueMode, const Value& value) { - std::string val, cVal; - apache::thrift::CompactSerializer::serialize(value, &cVal); - val.reserve(sizeof(meta::cpp2::ConfigMode) + cVal.size()); - val.append(reinterpret_cast(&valueMode), sizeof(meta::cpp2::ConfigMode)) - .append(cVal); - return val; -} - -ConfigName MetaKeyUtils::parseConfigKey(folly::StringPiece rawKey) { - std::string key; - auto offset = kConfigsTable.size(); - auto module = *reinterpret_cast(rawKey.data() + offset); - offset += sizeof(meta::cpp2::ConfigModule); - int32_t nSize = *reinterpret_cast(rawKey.data() + offset); - offset += sizeof(int32_t); - auto name = rawKey.subpiece(offset, nSize); - return {module, name.str()}; -} - -meta::cpp2::ConfigItem MetaKeyUtils::parseConfigValue(folly::StringPiece rawData) { - int32_t offset = 0; - meta::cpp2::ConfigMode mode = - *reinterpret_cast(rawData.data() + offset); - offset += sizeof(meta::cpp2::ConfigMode); - Value value; - apache::thrift::CompactSerializer::deserialize(rawData.subpiece(offset, rawData.size() - offset), - value); - - meta::cpp2::ConfigItem item; - item.set_mode(mode); - item.set_value(value); - return item; -} - -std::string MetaKeyUtils::snapshotKey(const std::string& name) { - std::string key; - key.reserve(kSnapshotsTable.size() + name.size()); - key.append(kSnapshotsTable.data(), kSnapshotsTable.size()).append(name); - return key; -} - -std::string MetaKeyUtils::snapshotVal(const meta::cpp2::SnapshotStatus& status, - const std::string& hosts) { - std::string val; - val.reserve(sizeof(meta::cpp2::SnapshotStatus) + sizeof(hosts)); - val.append(reinterpret_cast(&status), sizeof(meta::cpp2::SnapshotStatus)) - .append(hosts); - return val; -} - -meta::cpp2::SnapshotStatus MetaKeyUtils::parseSnapshotStatus(folly::StringPiece rawData) { - return *reinterpret_cast(rawData.data()); -} - -std::string MetaKeyUtils::parseSnapshotHosts(folly::StringPiece rawData) { - return rawData - .subpiece(sizeof(meta::cpp2::SnapshotStatus), - rawData.size() - sizeof(meta::cpp2::SnapshotStatus)) - .str(); -} - -std::string MetaKeyUtils::parseSnapshotName(folly::StringPiece rawData) { - int32_t offset = kSnapshotsTable.size(); - return rawData.subpiece(offset, rawData.size() - offset).str(); -} - -const std::string& MetaKeyUtils::snapshotPrefix() { return kSnapshotsTable; } - -std::string MetaKeyUtils::serializeHostAddr(const HostAddr& host) { - std::string ret; - ret.reserve(sizeof(size_t) + 15 + sizeof(Port)); // 255.255.255.255 - size_t len = host.host.size(); - ret.append(reinterpret_cast(&len), sizeof(size_t)) - .append(host.host.data(), len) - .append(reinterpret_cast(&host.port), sizeof(Port)); - return ret; -} - -HostAddr MetaKeyUtils::deserializeHostAddr(folly::StringPiece raw) { - HostAddr addr; - CHECK_GE(raw.size(), sizeof(size_t) + sizeof(Port)); // host may be "" - size_t offset = 0; - size_t len = *reinterpret_cast(raw.begin() + offset); - offset += sizeof(size_t); - addr.host = std::string(raw.begin() + offset, len); - offset += len; - addr.port = *reinterpret_cast(raw.begin() + offset); - return addr; -} - -std::string MetaKeyUtils::genTimestampStr() { - char ch[60]; - std::time_t t = std::time(nullptr); - std::strftime(ch, sizeof(ch), "%Y_%m_%d_%H_%M_%S", localtime(&t)); - return ch; -} - -std::string MetaKeyUtils::idKey() { return kIdKey; } - -std::string MetaKeyUtils::balanceTaskKey( - BalanceID balanceId, GraphSpaceID spaceId, PartitionID partId, HostAddr src, HostAddr dst) { - std::string str; - str.reserve(64); - str.append(reinterpret_cast(kBalanceTaskTable.data()), kBalanceTaskTable.size()) - .append(reinterpret_cast(&balanceId), sizeof(BalanceID)) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&partId), sizeof(PartitionID)) - .append(serializeHostAddr(src)) - .append(serializeHostAddr(dst)); - return str; -} - -std::string MetaKeyUtils::balanceTaskVal(BalanceTaskStatus status, - BalanceTaskResult retult, - int64_t startTime, - int64_t endTime) { - std::string val; - val.reserve(32); - val.append(reinterpret_cast(&status), sizeof(BalanceTaskStatus)) - .append(reinterpret_cast(&retult), sizeof(BalanceTaskResult)) - .append(reinterpret_cast(&startTime), sizeof(int64_t)) - .append(reinterpret_cast(&endTime), sizeof(int64_t)); - return val; -} - -std::string MetaKeyUtils::balanceTaskPrefix(BalanceID balanceId) { - std::string prefix; - prefix.reserve(32); - prefix.append(reinterpret_cast(kBalanceTaskTable.data()), kBalanceTaskTable.size()) - .append(reinterpret_cast(&balanceId), sizeof(BalanceID)); - return prefix; -} - -std::string MetaKeyUtils::balancePlanKey(BalanceID id) { - CHECK_GE(id, 0); - // make the balance id is stored in decend order - auto encode = folly::Endian::big(std::numeric_limits::max() - id); - std::string key; - key.reserve(sizeof(BalanceID) + kBalancePlanTable.size()); - key.append(reinterpret_cast(kBalancePlanTable.data()), kBalancePlanTable.size()) - .append(reinterpret_cast(&encode), sizeof(BalanceID)); - return key; -} - -std::string MetaKeyUtils::balancePlanVal(BalanceStatus status) { - std::string val; - val.reserve(sizeof(BalanceStatus)); - val.append(reinterpret_cast(&status), sizeof(BalanceStatus)); - return val; -} - -std::string MetaKeyUtils::balancePlanPrefix() { return kBalancePlanTable; } - -std::tuple -MetaKeyUtils::parseBalanceTaskKey(const folly::StringPiece& rawKey) { - uint32_t offset = kBalanceTaskTable.size(); - auto balanceId = *reinterpret_cast(rawKey.begin() + offset); - offset += sizeof(balanceId); - auto spaceId = *reinterpret_cast(rawKey.begin() + offset); - offset += sizeof(GraphSpaceID); - auto partId = *reinterpret_cast(rawKey.begin() + offset); - offset += sizeof(PartitionID); - auto src = MetaKeyUtils::deserializeHostAddr({rawKey, offset}); - offset += src.host.size() + sizeof(size_t) + sizeof(uint32_t); - auto dst = MetaKeyUtils::deserializeHostAddr({rawKey, offset}); - return std::make_tuple(balanceId, spaceId, partId, src, dst); -} - -std::tuple -MetaKeyUtils::parseBalanceTaskVal(const folly::StringPiece& rawVal) { - int32_t offset = 0; - auto status = *reinterpret_cast(rawVal.begin() + offset); - offset += sizeof(BalanceTaskStatus); - auto ret = *reinterpret_cast(rawVal.begin() + offset); - offset += sizeof(BalanceTaskResult); - auto start = *reinterpret_cast(rawVal.begin() + offset); - offset += sizeof(int64_t); - auto end = *reinterpret_cast(rawVal.begin() + offset); - return std::make_tuple(status, ret, start, end); -} - -std::string MetaKeyUtils::groupKey(const std::string& group) { - std::string key; - key.reserve(kGroupsTable.size() + group.size()); - key.append(kGroupsTable.data(), kGroupsTable.size()).append(group); - return key; -} - -BalanceID MetaKeyUtils::parseBalanceID(const folly::StringPiece& rawKey) { - auto decode = *reinterpret_cast(rawKey.begin() + kBalancePlanTable.size()); - auto id = std::numeric_limits::max() - folly::Endian::big(decode); - CHECK_GE(id, 0); - return id; -} - -BalanceStatus MetaKeyUtils::parseBalanceStatus(const folly::StringPiece& rawVal) { - return static_cast(*rawVal.begin()); -} - -std::string MetaKeyUtils::groupVal(const std::vector& zones) { - return folly::join(",", zones); -} - -const std::string& MetaKeyUtils::groupPrefix() { return kGroupsTable; } - -std::string MetaKeyUtils::parseGroupName(folly::StringPiece rawData) { - return rawData.subpiece(kGroupsTable.size(), rawData.size()).toString(); -} - -std::vector MetaKeyUtils::parseZoneNames(folly::StringPiece rawData) { - std::vector zones; - folly::split(',', rawData.str(), zones); - return zones; -} - -std::string MetaKeyUtils::zoneKey(const std::string& zone) { - std::string key; - key.reserve(kZonesTable.size() + zone.size()); - key.append(kZonesTable.data(), kZonesTable.size()).append(zone); - return key; -} - -std::string MetaKeyUtils::zoneVal(const std::vector& hosts) { - std::string value; - value.append(network::NetworkUtils::toHostsStr(hosts)); - return value; -} - -const std::string& MetaKeyUtils::zonePrefix() { return kZonesTable; } - -std::string MetaKeyUtils::parseZoneName(folly::StringPiece rawData) { - return rawData.subpiece(kZonesTable.size(), rawData.size()).toString(); -} - -std::vector MetaKeyUtils::parseZoneHosts(folly::StringPiece rawData) { - std::vector addresses; - auto hostsOrErr = network::NetworkUtils::toHosts(rawData.str()); - if (hostsOrErr.ok()) { - addresses = std::move(hostsOrErr.value()); - } else { - LOG(ERROR) << "invalid input for parseZoneHosts()"; - } - return addresses; -} - -std::string MetaKeyUtils::listenerKey(GraphSpaceID spaceId, - PartitionID partId, - meta::cpp2::ListenerType type) { - std::string key; - key.reserve(kListenerTable.size() + sizeof(GraphSpaceID) + sizeof(meta::cpp2::ListenerType) + - sizeof(PartitionID)); - key.append(kListenerTable.data(), kListenerTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&type), sizeof(meta::cpp2::ListenerType)) - .append(reinterpret_cast(&partId), sizeof(PartitionID)); - return key; -} - -std::string MetaKeyUtils::listenerPrefix(GraphSpaceID spaceId) { - std::string key; - key.reserve(kListenerTable.size() + sizeof(GraphSpaceID)); - key.append(kListenerTable.data(), kListenerTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -std::string MetaKeyUtils::listenerPrefix(GraphSpaceID spaceId, meta::cpp2::ListenerType type) { - std::string key; - key.reserve(kListenerTable.size() + sizeof(GraphSpaceID) + sizeof(meta::cpp2::ListenerType)); - key.append(kListenerTable.data(), kListenerTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)) - .append(reinterpret_cast(&type), sizeof(meta::cpp2::ListenerType)); - return key; -} - -meta::cpp2::ListenerType MetaKeyUtils::parseListenerType(folly::StringPiece rawData) { - auto offset = kListenerTable.size() + sizeof(GraphSpaceID); - return *reinterpret_cast(rawData.data() + offset); -} - -GraphSpaceID MetaKeyUtils::parseListenerSpace(folly::StringPiece rawData) { - auto offset = kListenerTable.size(); - return *reinterpret_cast(rawData.data() + offset); -} - -PartitionID MetaKeyUtils::parseListenerPart(folly::StringPiece rawData) { - auto offset = kListenerTable.size() + sizeof(meta::cpp2::ListenerType) + sizeof(GraphSpaceID); - return *reinterpret_cast(rawData.data() + offset); -} - -std::string MetaKeyUtils::statsKey(GraphSpaceID spaceId) { - std::string key; - key.reserve(kStatsTable.size() + sizeof(GraphSpaceID)); - key.append(kStatsTable.data(), kStatsTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -std::string MetaKeyUtils::statsVal(const meta::cpp2::StatsItem& statsItem) { - std::string val; - apache::thrift::CompactSerializer::serialize(statsItem, &val); - return val; -} - -meta::cpp2::StatsItem MetaKeyUtils::parseStatsVal(folly::StringPiece rawData) { - meta::cpp2::StatsItem statsItem; - apache::thrift::CompactSerializer::deserialize(rawData, statsItem); - return statsItem; -} - -GraphSpaceID MetaKeyUtils::parseStatsSpace(folly::StringPiece rawData) { - auto offset = kStatsTable.size(); - return *reinterpret_cast(rawData.data() + offset); -} - -const std::string& MetaKeyUtils::statsKeyPrefix() { return kStatsTable; } - -std::string MetaKeyUtils::fulltextServiceKey() { - std::string key; - key.reserve(kFTServiceTable.size()); - key.append(kFTServiceTable.data(), kFTServiceTable.size()); - return key; -} - -std::string MetaKeyUtils::fulltextServiceVal(meta::cpp2::FTServiceType type, - const std::vector& clients) { - std::string val, cval; - apache::thrift::CompactSerializer::serialize(clients, &cval); - val.reserve(sizeof(meta::cpp2::FTServiceType) + cval.size()); - val.append(reinterpret_cast(&type), sizeof(meta::cpp2::FTServiceType)).append(cval); - return val; -} - -std::vector MetaKeyUtils::parseFTClients(folly::StringPiece rawData) { - std::vector clients; - int32_t offset = sizeof(meta::cpp2::FTServiceType); - auto clientsRaw = rawData.subpiece(offset, rawData.size() - offset); - apache::thrift::CompactSerializer::deserialize(clientsRaw, clients); - return clients; -} - -const std::string& MetaKeyUtils::sessionPrefix() { return kSessionsTable; } - -std::string MetaKeyUtils::sessionKey(SessionID sessionId) { - std::string key; - key.reserve(kSessionsTable.size() + sizeof(sessionId)); - key.append(kSessionsTable.data(), kSessionsTable.size()) - .append(reinterpret_cast(&sessionId), sizeof(SessionID)); - return key; -} - -std::string MetaKeyUtils::sessionVal(const meta::cpp2::Session& session) { - std::string val; - apache::thrift::CompactSerializer::serialize(session, &val); - return val; -} - -SessionID MetaKeyUtils::getSessionId(const folly::StringPiece& key) { - return *reinterpret_cast(key.data() + kSessionsTable.size()); -} - -meta::cpp2::Session MetaKeyUtils::parseSessionVal(const folly::StringPiece& val) { - meta::cpp2::Session session; - apache::thrift::CompactSerializer::deserialize(val, session); - return session; -} - -std::string MetaKeyUtils::fulltextIndexKey(const std::string& indexName) { - std::string key; - key.reserve(kFTIndexTable.size() + indexName.size()); - key.append(kFTIndexTable.data(), kFTIndexTable.size()).append(indexName); - return key; -} - -std::string MetaKeyUtils::fulltextIndexVal(const meta::cpp2::FTIndex& index) { - std::string val; - apache::thrift::CompactSerializer::serialize(index, &val); - return val; -} - -std::string MetaKeyUtils::parsefulltextIndexName(folly::StringPiece key) { - return key.subpiece(kFTIndexTable.size(), key.size()).toString(); -} - -meta::cpp2::FTIndex MetaKeyUtils::parsefulltextIndex(folly::StringPiece val) { - meta::cpp2::FTIndex ftIndex; - apache::thrift::CompactSerializer::deserialize(val, ftIndex); - return ftIndex; -} - -std::string MetaKeyUtils::fulltextIndexPrefix() { return kFTIndexTable; } - -std::string MetaKeyUtils::localIdKey(GraphSpaceID spaceId) { - std::string key; - key.reserve(kLocalIdTable.size() + sizeof(GraphSpaceID)); - key.append(kLocalIdTable.data(), kLocalIdTable.size()) - .append(reinterpret_cast(&spaceId), sizeof(GraphSpaceID)); - return key; -} - -GraphSpaceID MetaKeyUtils::parseLocalIdSpace(folly::StringPiece rawData) { - auto offset = kLocalIdTable.size(); - return *reinterpret_cast(rawData.data() + offset); -} - -} // namespace nebula diff --git a/src/common/utils/MetaKeyUtils.h b/src/common/utils/MetaKeyUtils.h deleted file mode 100644 index 596280ae0e2..00000000000 --- a/src/common/utils/MetaKeyUtils.h +++ /dev/null @@ -1,392 +0,0 @@ -/* Copyright (c) 2018 vesoft inc. All rights reserved. - * - * This source code is licensed under Apache 2.0 License. - */ - -#ifndef META_KEY_UTILS_H_ -#define META_KEY_UTILS_H_ - -#include "common/base/Base.h" -#include "common/base/Status.h" -#include "common/datatypes/HostAddr.h" -#include "interface/gen-cpp2/meta_types.h" - -namespace nebula { - -static const PartitionID kDefaultPartId = 0; -static const GraphSpaceID kDefaultSpaceId = 0; - -using BalanceID = int64_t; - -enum class BalanceTaskStatus : uint8_t { - START = 0x01, - CHANGE_LEADER = 0x02, - ADD_PART_ON_DST = 0x03, - ADD_LEARNER = 0x04, - CATCH_UP_DATA = 0x05, - MEMBER_CHANGE_ADD = 0x06, - MEMBER_CHANGE_REMOVE = 0x07, - UPDATE_PART_META = 0x08, // After this state, we can't rollback anymore. - REMOVE_PART_ON_SRC = 0x09, - CHECK = 0x0A, - END = 0xFF, -}; - -enum class BalanceTaskResult : uint8_t { - SUCCEEDED = 0x01, - FAILED = 0x02, - IN_PROGRESS = 0x03, - INVALID = 0x04, -}; - -enum class BalanceStatus : uint8_t { - NOT_START = 0x01, - IN_PROGRESS = 0x02, - SUCCEEDED = 0x03, - /** - * TODO(heng): Currently, after the plan failed, we will try to resume it - * when running "balance" again. But in many cases, the plan will be failed - * forever, it this cases, we should rollback the plan. - * */ - FAILED = 0x04, -}; - -enum class EntryType : int8_t { - SPACE = 0x01, - TAG = 0x02, - EDGE = 0x03, - INDEX = 0x04, - CONFIG = 0x05, - GROUP = 0x06, - ZONE = 0x07, -}; - -using ConfigName = std::pair; -using LeaderParts = std::unordered_map>; - -class MetaKeyUtils final { - public: - MetaKeyUtils() = delete; - - static std::string lastUpdateTimeKey(); - - static std::string lastUpdateTimeVal(const int64_t timeInMilliSec); - - static std::string spaceKey(GraphSpaceID spaceId); - - static std::string spaceVal(const meta::cpp2::SpaceDesc& spaceDesc); - - static meta::cpp2::SpaceDesc parseSpace(folly::StringPiece rawData); - - static const std::string& spacePrefix(); - - static GraphSpaceID spaceId(folly::StringPiece rawKey); - - static std::string spaceName(folly::StringPiece rawVal); - - static std::string partKey(GraphSpaceID spaceId, PartitionID partId); - - static GraphSpaceID parsePartKeySpaceId(folly::StringPiece key); - - static PartitionID parsePartKeyPartId(folly::StringPiece key); - - static std::string partVal(const std::vector& hosts); - - static std::string partValV1(const std::vector& hosts); - - static std::string partValV2(const std::vector& hosts); - - static std::string partPrefix(); - - static std::string partPrefix(GraphSpaceID spaceId); - - static std::string encodeHostAddrV2(int ip, int port); - - static HostAddr decodeHostAddrV2(folly::StringPiece val, int& offset); - - static std::vector parsePartVal(folly::StringPiece val, int parNum = 0); - - static std::vector parsePartValV1(folly::StringPiece val); - - static std::vector parsePartValV2(folly::StringPiece val); - - static std::string hostKey(std::string ip, Port port); - - static std::string hostKeyV2(std::string addr, Port port); - - static const std::string& hostPrefix(); - - static HostAddr parseHostKey(folly::StringPiece key); - - static HostAddr parseHostKeyV1(folly::StringPiece key); - - static HostAddr parseHostKeyV2(folly::StringPiece key); - - static std::string leaderKey(std::string ip, Port port); - - static std::string leaderKeyV2(std::string addr, Port port); - - static std::string leaderKey(GraphSpaceID spaceId, PartitionID partId); - - static std::string leaderVal(const LeaderParts& leaderParts); - - static std::string leaderValV3(const HostAddr& h, int64_t term); - - static std::string leaderPrefix(GraphSpaceID spaceId); - - static const std::string& leaderPrefix(); - - static HostAddr parseLeaderKey(folly::StringPiece key); - - static HostAddr parseLeaderKeyV1(folly::StringPiece key); - - static std::pair parseLeaderKeyV3(folly::StringPiece key); - - static HostAddr parseLeaderKeyV2(folly::StringPiece key); - - static LeaderParts parseLeaderValV1(folly::StringPiece val); - - static std::tuple parseLeaderValV3( - folly::StringPiece val); - - static std::string schemaVal(const std::string& name, const meta::cpp2::Schema& schema); - - static std::string schemaEdgePrefix(GraphSpaceID spaceId, EdgeType edgeType); - - static std::string schemaEdgesPrefix(GraphSpaceID spaceId); - - static std::string schemaEdgeKey(GraphSpaceID spaceId, EdgeType edgeType, SchemaVer version); - - static EdgeType parseEdgeType(folly::StringPiece key); - - static SchemaVer parseEdgeVersion(folly::StringPiece key); - - static std::string schemaTagKey(GraphSpaceID spaceId, TagID tagId, SchemaVer version); - - static TagID parseTagId(folly::StringPiece key); - - static SchemaVer parseTagVersion(folly::StringPiece key); - - static std::string schemaTagPrefix(GraphSpaceID spaceId, TagID tagId); - - static std::string schemaTagsPrefix(GraphSpaceID spaceId); - - static meta::cpp2::Schema parseSchema(folly::StringPiece rawData); - - static std::string indexKey(GraphSpaceID spaceId, IndexID indexID); - - static std::string indexVal(const meta::cpp2::IndexItem& item); - - static std::string indexPrefix(GraphSpaceID spaceId); - - static IndexID parseIndexesKeyIndexID(folly::StringPiece key); - - static meta::cpp2::IndexItem parseIndex(const folly::StringPiece& rawData); - - static std::string rebuildIndexStatus(GraphSpaceID space, - char type, - const std::string& indexName); - - static std::string rebuildIndexStatusPrefix(GraphSpaceID spaceId, char type); - - static std::string rebuildIndexStatusPrefix(); - - static std::string rebuildTagIndexStatusPrefix(GraphSpaceID spaceId) { - return rebuildIndexStatusPrefix(spaceId, 'T'); - } - - static std::string rebuildEdgeIndexStatusPrefix(GraphSpaceID spaceId) { - return rebuildIndexStatusPrefix(spaceId, 'E'); - } - - static std::string indexSpaceKey(const std::string& name); - - static std::string indexTagKey(GraphSpaceID spaceId, const std::string& name); - - static std::string indexEdgeKey(GraphSpaceID spaceId, const std::string& name); - - static std::string indexIndexKey(GraphSpaceID spaceId, const std::string& name); - - static std::string indexGroupKey(const std::string& name); - - static std::string indexZoneKey(const std::string& name); - - static std::string assembleSegmentKey(const std::string& segment, const std::string& key); - - static std::string userPrefix(); - - static std::string userKey(const std::string& account); - - static std::string userVal(const std::string& val); - - static std::string parseUser(folly::StringPiece key); - - static std::string parseUserPwd(folly::StringPiece val); - - static std::string roleKey(GraphSpaceID spaceId, const std::string& account); - - static std::string roleVal(meta::cpp2::RoleType roleType); - - static std::string parseRoleUser(folly::StringPiece key); - - static GraphSpaceID parseRoleSpace(folly::StringPiece key); - - static std::string rolesPrefix(); - - static std::string roleSpacePrefix(GraphSpaceID spaceId); - - static std::string parseRoleStr(folly::StringPiece key); - - static std::string configKey(const meta::cpp2::ConfigModule& module, const std::string& name); - - static std::string configKeyPrefix(const meta::cpp2::ConfigModule& module); - - static std::string configValue(const meta::cpp2::ConfigMode& valueMode, const Value& config); - - static ConfigName parseConfigKey(folly::StringPiece rawData); - - static meta::cpp2::ConfigItem parseConfigValue(folly::StringPiece rawData); - - static std::string snapshotKey(const std::string& name); - - static std::string snapshotVal(const meta::cpp2::SnapshotStatus& status, - const std::string& hosts); - - static meta::cpp2::SnapshotStatus parseSnapshotStatus(folly::StringPiece rawData); - - static std::string parseSnapshotHosts(folly::StringPiece rawData); - - static std::string parseSnapshotName(folly::StringPiece rawData); - - static const std::string& snapshotPrefix(); - - static std::string serializeHostAddr(const HostAddr& host); - - static HostAddr deserializeHostAddr(folly::StringPiece str); - - static std::string balanceTaskKey( - BalanceID balanceId, GraphSpaceID spaceId, PartitionID partId, HostAddr src, HostAddr dst); - - static std::string balanceTaskVal(BalanceTaskStatus status, - BalanceTaskResult retult, - int64_t startTime, - int64_t endTime); - - static std::string balanceTaskPrefix(BalanceID balanceId); - - static std::string balancePlanKey(BalanceID id); - - static std::string balancePlanVal(BalanceStatus status); - - static std::string balancePlanPrefix(); - - static BalanceID parseBalanceID(const folly::StringPiece& rawKey); - - static BalanceStatus parseBalanceStatus(const folly::StringPiece& rawVal); - - static std::tuple parseBalanceTaskKey( - const folly::StringPiece& rawKey); - - static std::tuple parseBalanceTaskVal( - const folly::StringPiece& rawVal); - - static std::string groupKey(const std::string& group); - - static std::string groupVal(const std::vector& zones); - - static const std::string& groupPrefix(); - - static std::string parseGroupName(folly::StringPiece rawData); - - static std::vector parseZoneNames(folly::StringPiece rawData); - - static std::string zoneKey(const std::string& zone); - - static std::string zoneVal(const std::vector& hosts); - - static const std::string& zonePrefix(); - - static std::string parseZoneName(folly::StringPiece rawData); - - static std::vector parseZoneHosts(folly::StringPiece rawData); - - static std::string listenerKey(GraphSpaceID spaceId, - PartitionID partId, - meta::cpp2::ListenerType type); - - static std::string listenerPrefix(GraphSpaceID spaceId); - - static std::string listenerPrefix(GraphSpaceID spaceId, meta::cpp2::ListenerType type); - - static meta::cpp2::ListenerType parseListenerType(folly::StringPiece rawData); - - static GraphSpaceID parseListenerSpace(folly::StringPiece rawData); - - static PartitionID parseListenerPart(folly::StringPiece rawData); - - static std::string statsKey(GraphSpaceID spaceId); - - static std::string statsVal(const meta::cpp2::StatsItem& statsItem); - - static meta::cpp2::StatsItem parseStatsVal(folly::StringPiece rawData); - - static const std::string& statsKeyPrefix(); - - static GraphSpaceID parseStatsSpace(folly::StringPiece rawData); - - static std::string fulltextServiceKey(); - - static std::string fulltextServiceVal(meta::cpp2::FTServiceType type, - const std::vector& clients); - - static std::vector parseFTClients(folly::StringPiece rawData); - - static const std::string& sessionPrefix(); - - static std::string sessionKey(SessionID sessionId); - - static std::string sessionVal(const meta::cpp2::Session& session); - - static SessionID getSessionId(const folly::StringPiece& key); - - static meta::cpp2::Session parseSessionVal(const folly::StringPiece& val); - - static std::string fulltextIndexKey(const std::string& indexName); - - static std::string fulltextIndexVal(const meta::cpp2::FTIndex& index); - - static std::string parsefulltextIndexName(folly::StringPiece key); - - static meta::cpp2::FTIndex parsefulltextIndex(folly::StringPiece val); - - static std::string fulltextIndexPrefix(); - - static std::string genTimestampStr(); - - static GraphSpaceID parseEdgesKeySpaceID(folly::StringPiece key); - static GraphSpaceID parseTagsKeySpaceID(folly::StringPiece key); - static GraphSpaceID parseIndexesKeySpaceID(folly::StringPiece key); - static GraphSpaceID parseIndexStatusKeySpaceID(folly::StringPiece key); - static GraphSpaceID parseIndexKeySpaceID(folly::StringPiece key); - static GraphSpaceID parseDefaultKeySpaceID(folly::StringPiece key); - - static std::string idKey(); - - static std::string localIdKey(GraphSpaceID spaceId); - - static GraphSpaceID parseLocalIdSpace(folly::StringPiece rawData); - - static std::string getIndexTable(); - - static std::unordered_map>> - getTableMaps(); - - static std::unordered_map> getSystemInfoMaps(); - - static std::unordered_map> getSystemTableMaps(); -}; - -} // namespace nebula - -#endif // META_KEY_UTILS_H_