Skip to content

Commit

Permalink
move the KeyUtils from storage to common/base named NebulaKeyUtils (#402
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zhangguoqing authored and darionyaphet committed May 22, 2019
1 parent 147a280 commit c28a948
Show file tree
Hide file tree
Showing 22 changed files with 129 additions and 142 deletions.
1 change: 1 addition & 0 deletions src/common/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_library(
Status.cpp
SanitizerOptions.cpp
SignalHandler.cpp
NebulaKeyUtils.cpp
)
add_dependencies(base_obj third-party)
IF(${PCHSupport_FOUND})
Expand Down
28 changes: 13 additions & 15 deletions src/storage/KeyUtils.cpp → src/common/base/NebulaKeyUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "storage/KeyUtils.h"
#include "base/NebulaKeyUtils.h"

namespace nebula {
namespace storage {

// static
std::string KeyUtils::vertexKey(PartitionID partId, VertexID vId,
TagID tagId, TagVersion ts) {
std::string NebulaKeyUtils::vertexKey(PartitionID partId, VertexID vId,
TagID tagId, TagVersion ts) {
std::string key;
key.reserve(kVertexLen);
key.append(reinterpret_cast<const char*>(&partId), sizeof(PartitionID))
Expand All @@ -22,12 +21,12 @@ std::string KeyUtils::vertexKey(PartitionID partId, VertexID vId,
}

// static
std::string KeyUtils::edgeKey(PartitionID partId,
VertexID srcId,
EdgeType type,
EdgeRanking rank,
VertexID dstId,
EdgeVersion ts) {
std::string NebulaKeyUtils::edgeKey(PartitionID partId,
VertexID srcId,
EdgeType type,
EdgeRanking rank,
VertexID dstId,
EdgeVersion ts) {
std::string key;
key.reserve(kEdgeLen);
key.append(reinterpret_cast<const char*>(&partId), sizeof(PartitionID))
Expand All @@ -40,7 +39,7 @@ std::string KeyUtils::edgeKey(PartitionID partId,
}

// static
std::string KeyUtils::prefix(PartitionID partId, VertexID srcId, EdgeType type) {
std::string NebulaKeyUtils::prefix(PartitionID partId, VertexID srcId, EdgeType type) {
std::string key;
key.reserve(sizeof(PartitionID) + sizeof(VertexID) + sizeof(EdgeType));
key.append(reinterpret_cast<const char*>(&partId), sizeof(PartitionID))
Expand All @@ -50,7 +49,7 @@ std::string KeyUtils::prefix(PartitionID partId, VertexID srcId, EdgeType type)
}

// static
std::string KeyUtils::prefix(PartitionID partId, VertexID vId) {
std::string NebulaKeyUtils::prefix(PartitionID partId, VertexID vId) {
std::string key;
key.reserve(sizeof(PartitionID) + sizeof(VertexID));
key.append(reinterpret_cast<const char*>(&partId), sizeof(PartitionID))
Expand All @@ -59,8 +58,8 @@ std::string KeyUtils::prefix(PartitionID partId, VertexID vId) {
}

// static
std::string KeyUtils::prefix(PartitionID partId, VertexID src, EdgeType type,
EdgeRanking ranking, VertexID dst) {
std::string NebulaKeyUtils::prefix(PartitionID partId, VertexID src, EdgeType type,
EdgeRanking ranking, VertexID dst) {
std::string key;
key.reserve(sizeof(PartitionID) + sizeof(VertexID) + sizeof(EdgeType)
+ sizeof(VertexID) + sizeof(EdgeRanking));
Expand All @@ -72,6 +71,5 @@ std::string KeyUtils::prefix(PartitionID partId, VertexID src, EdgeType type,
return key;
}

} // namespace storage
} // namespace nebula

21 changes: 12 additions & 9 deletions src/storage/KeyUtils.h → src/common/base/NebulaKeyUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#ifndef STORAGE_KEYUTILS_H_
#define STORAGE_KEYUTILS_H_
#ifndef COMMON_BASE_NEBULAKEYUTILS_H_
#define COMMON_BASE_NEBULAKEYUTILS_H_

#include "base/Base.h"

namespace nebula {
namespace storage {

/**
* VertexKeyUtils:
Expand All @@ -29,9 +28,9 @@ using Edge = std::tuple<VertexID, EdgeType, VertexID, EdgeRanking>;
/**
* This class supply some utils for transition between Vertex/Edge and key in kvstore.
* */
class KeyUtils final {
class NebulaKeyUtils final {
public:
~KeyUtils() = default;
~NebulaKeyUtils() = default;
/**
* Generate vertex key for kv store
* */
Expand Down Expand Up @@ -62,6 +61,12 @@ class KeyUtils final {
return rawKey.size() == kVertexLen;
}

static int32_t getTagId(folly::StringPiece rawKey) {
CHECK_EQ(rawKey.size(), kVertexLen);
auto offset = sizeof(PartitionID) + sizeof(VertexID);
return readInt<int32_t>(rawKey.data() + offset, sizeof(TagID));
}

static bool isEdge(const std::string& rawKey) {
return rawKey.size() == kEdgeLen;
}
Expand All @@ -72,7 +77,6 @@ class KeyUtils final {
rawKey.size() - sizeof(PartitionID));
}


static int64_t getDstId(folly::StringPiece rawKey) {
CHECK_EQ(rawKey.size(), kEdgeLen);
auto offset = kEdgeLen - sizeof(EdgeVersion) - sizeof(VertexID);
Expand Down Expand Up @@ -102,7 +106,7 @@ class KeyUtils final {
}

private:
KeyUtils() = delete;
NebulaKeyUtils() = delete;

private:
static constexpr int32_t kVertexLen = sizeof(PartitionID) + sizeof(VertexID)
Expand All @@ -112,7 +116,6 @@ class KeyUtils final {
+ sizeof(EdgeRanking) + sizeof(EdgeVersion);
};

} // namespace storage
} // namespace nebula
#endif // STORAGE_KEYUTILS_H_
#endif // COMMON_BASE_NEBULAKEYUTILS_H_

12 changes: 12 additions & 0 deletions src/common/base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ nebula_link_libraries(
gtest_main
)
nebula_add_test(signal_handler_test)

add_executable(
nebulakey_utils_test
NebulaKeyUtilsTest.cpp
$<TARGET_OBJECTS:base_obj>
)
nebula_link_libraries(
nebulakey_utils_test
gtest
gtest_main
)
nebula_add_test(nebulakey_utils_test)
44 changes: 44 additions & 0 deletions src/common/base/test/NebulaKeyUtilsTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "base/Base.h"
#include "base/NebulaKeyUtils.h"
#include <gtest/gtest.h>

namespace nebula {

TEST(NebulaKeyUtilsTest, SimpleTest) {
PartitionID partId = 0L;
VertexID srcId = 1001L, dstId = 2001L;
TagID tagId = 1001;
TagVersion tagVersion = 20L;
EdgeType type = 101;
EdgeRanking rank = 10L;
EdgeVersion edgeVersion = 20;

auto vertexKey = NebulaKeyUtils::vertexKey(partId, srcId, tagId, tagVersion);
CHECK(NebulaKeyUtils::isVertex(vertexKey));
CHECK_EQ(tagId, NebulaKeyUtils::getTagId(vertexKey));

auto edgeKey = NebulaKeyUtils::edgeKey(partId, srcId, type, rank, dstId, edgeVersion);
CHECK(NebulaKeyUtils::isEdge(edgeKey));
CHECK_EQ(srcId, NebulaKeyUtils::getSrcId(edgeKey));
CHECK_EQ(dstId, NebulaKeyUtils::getDstId(edgeKey));
CHECK_EQ(type, NebulaKeyUtils::getEdgeType(edgeKey));
CHECK_EQ(rank, NebulaKeyUtils::getRank(edgeKey));
}

} // namespace nebula


int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
folly::init(&argc, &argv, true);
google::SetStderrLogging(google::INFO);

return RUN_ALL_TESTS();
}

6 changes: 3 additions & 3 deletions src/storage/AddEdgesProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
#include "storage/AddEdgesProcessor.h"
#include "base/NebulaKeyUtils.h"
#include <algorithm>
#include <limits>
#include "time/TimeUtils.h"
#include "storage/KeyUtils.h"

namespace nebula {
namespace storage {
Expand All @@ -21,8 +21,8 @@ void AddEdgesProcessor::process(const cpp2::AddEdgesRequest& req) {
auto partId = partEdges.first;
std::vector<kvstore::KV> data;
std::for_each(partEdges.second.begin(), partEdges.second.end(), [&](auto& edge){
auto key = KeyUtils::edgeKey(partId, edge.key.src, edge.key.edge_type,
edge.key.ranking, edge.key.dst, version);
auto key = NebulaKeyUtils::edgeKey(partId, edge.key.src, edge.key.edge_type,
edge.key.ranking, edge.key.dst, version);
data.emplace_back(std::move(key), std::move(edge.get_props()));
});
doPut(spaceId, partId, std::move(data));
Expand Down
6 changes: 3 additions & 3 deletions src/storage/AddVerticesProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/

#include "storage/AddVerticesProcessor.h"
#include "base/NebulaKeyUtils.h"
#include <algorithm>
#include <limits>
#include "time/TimeUtils.h"
#include "storage/KeyUtils.h"

namespace nebula {
namespace storage {
Expand All @@ -27,8 +27,8 @@ void AddVerticesProcessor::process(const cpp2::AddVerticesRequest& req) {
std::for_each(vertices.begin(), vertices.end(), [&](auto& v){
const auto& tags = v.get_tags();
std::for_each(tags.begin(), tags.end(), [&](auto& tag) {
auto key = KeyUtils::vertexKey(partId, v.get_id(),
tag.get_tag_id(), now);
auto key = NebulaKeyUtils::vertexKey(partId, v.get_id(),
tag.get_tag_id(), now);
data.emplace_back(std::move(key), std::move(tag.get_props()));
});
});
Expand Down
1 change: 0 additions & 1 deletion src/storage/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
add_library(
storage_service_handler OBJECT
StorageServiceHandler.cpp
KeyUtils.cpp
StorageHttpHandler.cpp
AddVerticesProcessor.cpp
AddEdgesProcessor.cpp
Expand Down
30 changes: 17 additions & 13 deletions src/storage/QueryBaseProcessor.inl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
#include "storage/QueryBaseProcessor.h"
#include "base/NebulaKeyUtils.h"
#include <algorithm>
#include "storage/KeyUtils.h"
#include "dataman/RowReader.h"
#include "dataman/RowWriter.h"

Expand Down Expand Up @@ -43,20 +43,24 @@ void QueryBaseProcessor<REQ, RESP>::collectProps(RowReader* reader,
case PropContext::PropInKeyType::NONE:
break;
case PropContext::PropInKeyType::SRC:
VLOG(3) << "collect _src, value = " << KeyUtils::getSrcId(key);
collector->collectInt64(ResultType::SUCCEEDED, KeyUtils::getSrcId(key), prop);
VLOG(3) << "collect _src, value = " << NebulaKeyUtils::getSrcId(key);
collector->collectInt64(ResultType::SUCCEEDED,
NebulaKeyUtils::getSrcId(key), prop);
continue;
case PropContext::PropInKeyType::DST:
VLOG(3) << "collect _dst, value = " << KeyUtils::getDstId(key);
collector->collectInt64(ResultType::SUCCEEDED, KeyUtils::getDstId(key), prop);
VLOG(3) << "collect _dst, value = " << NebulaKeyUtils::getDstId(key);
collector->collectInt64(ResultType::SUCCEEDED,
NebulaKeyUtils::getDstId(key), prop);
continue;
case PropContext::PropInKeyType::TYPE:
VLOG(3) << "collect _type, value = " << KeyUtils::getEdgeType(key);
collector->collectInt32(ResultType::SUCCEEDED, KeyUtils::getEdgeType(key), prop);
VLOG(3) << "collect _type, value = " << NebulaKeyUtils::getEdgeType(key);
collector->collectInt32(ResultType::SUCCEEDED,
NebulaKeyUtils::getEdgeType(key), prop);
continue;
case PropContext::PropInKeyType::RANK:
VLOG(3) << "collect _rank, value = " << KeyUtils::getRank(key);
collector->collectInt64(ResultType::SUCCEEDED, KeyUtils::getRank(key), prop);
VLOG(3) << "collect _rank, value = " << NebulaKeyUtils::getRank(key);
collector->collectInt64(ResultType::SUCCEEDED,
NebulaKeyUtils::getRank(key), prop);
continue;
}
if (reader != nullptr) {
Expand Down Expand Up @@ -194,7 +198,7 @@ kvstore::ResultCode QueryBaseProcessor<REQ, RESP>::collectVertexProps(
TagID tagId,
std::vector<PropContext>& props,
Collector* collector) {
auto prefix = KeyUtils::prefix(partId, vId, tagId);
auto prefix = NebulaKeyUtils::prefix(partId, vId, tagId);
std::unique_ptr<kvstore::KVIterator> iter;
auto ret = this->kvstore_->prefix(spaceId_, partId, prefix, &iter);
if (ret != kvstore::ResultCode::SUCCEEDED) {
Expand All @@ -219,7 +223,7 @@ kvstore::ResultCode QueryBaseProcessor<REQ, RESP>::collectEdgeProps(
EdgeType edgeType,
std::vector<PropContext>& props,
EdgeProcessor proc) {
auto prefix = KeyUtils::prefix(partId, vId, edgeType);
auto prefix = NebulaKeyUtils::prefix(partId, vId, edgeType);
std::unique_ptr<kvstore::KVIterator> iter;
auto ret = this->kvstore_->prefix(spaceId_, partId, prefix, &iter);
if (ret != kvstore::ResultCode::SUCCEEDED || !iter) {
Expand All @@ -231,8 +235,8 @@ kvstore::ResultCode QueryBaseProcessor<REQ, RESP>::collectEdgeProps(
for (; iter->valid(); iter->next()) {
auto key = iter->key();
auto val = iter->val();
auto rank = KeyUtils::getRank(key);
auto dstId = KeyUtils::getDstId(key);
auto rank = NebulaKeyUtils::getRank(key);
auto dstId = NebulaKeyUtils::getDstId(key);
if (!firstLoop && rank == lastRank && lastDstId == dstId) {
VLOG(3) << "Only get the latest version for each edge.";
continue;
Expand Down
1 change: 0 additions & 1 deletion src/storage/QueryBoundProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "storage/QueryBoundProcessor.h"
#include <algorithm>
#include "time/Duration.h"
#include "storage/KeyUtils.h"
#include "dataman/RowReader.h"
#include "dataman/RowWriter.h"

Expand Down
6 changes: 3 additions & 3 deletions src/storage/QueryEdgePropsProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

#include "storage/QueryEdgePropsProcessor.h"
#include "base/NebulaKeyUtils.h"
#include <algorithm>
#include "time/Duration.h"
#include "storage/KeyUtils.h"
#include "dataman/RowReader.h"
#include "dataman/RowWriter.h"

Expand All @@ -19,8 +19,8 @@ kvstore::ResultCode QueryEdgePropsProcessor::collectEdgesProps(
const cpp2::EdgeKey& edgeKey,
std::vector<PropContext>& props,
RowSetWriter& rsWriter) {
auto prefix = KeyUtils::prefix(partId, edgeKey.src, edgeKey.edge_type,
edgeKey.ranking, edgeKey.dst);
auto prefix = NebulaKeyUtils::prefix(partId, edgeKey.src, edgeKey.edge_type,
edgeKey.ranking, edgeKey.dst);
std::unique_ptr<kvstore::KVIterator> iter;
auto ret = kvstore_->prefix(spaceId_, partId, prefix, &iter);
// Only use the latest version.
Expand Down
1 change: 0 additions & 1 deletion src/storage/QueryStatsProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "storage/QueryStatsProcessor.h"
#include <algorithm>
#include "time/Duration.h"
#include "storage/KeyUtils.h"
#include "dataman/RowReader.h"
#include "dataman/RowWriter.h"

Expand Down
1 change: 0 additions & 1 deletion src/storage/QueryVertexPropsProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "storage/QueryVertexPropsProcessor.h"
#include <algorithm>
#include "time/Duration.h"
#include "storage/KeyUtils.h"
#include "dataman/RowReader.h"
#include "dataman/RowWriter.h"

Expand Down
Loading

0 comments on commit c28a948

Please sign in to comment.