This is an automated email from the ASF dual-hosted git repository.
xiaokang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-graphar.git
The following commit(s) were added to refs/heads/main by this push:
new 192a1dc4 feat(c++): Add remove method in c++ library (#783)
192a1dc4 is described below
commit 192a1dc4b8e4ce9daf6175e32520a6001942b492
Author: PinCrimson <[email protected]>
AuthorDate: Thu Oct 9 14:41:35 2025 +0800
feat(c++): Add remove method in c++ library (#783)
* feat(c++): Add delete method in c++ library
In response to issue #766,this commit add remove method in
GraphInfo,EdgeInfo,VertexInfo.
The following methods have been implemented along with their corresponding
unit tests:
GraphInfo::RemoveVertex
GraphInfo::RemoveEdge
EdgeInfo::RemovePropertyGroup
EdgeInfo::RemoveAdjacentList
VertexInfo::RemovePropertyGroup
The implementation status in the documentation has also been updated.
Closes: #766
* format the code style
---
cpp/src/graphar/graph_info.cc | 112 ++++++++++++++++++++++++++++
cpp/src/graphar/graph_info.h | 50 +++++++++++++
cpp/test/test_info.cc | 85 +++++++++++++++++++++
docs/specification/implementation-status.md | 12 +--
4 files changed, 253 insertions(+), 6 deletions(-)
diff --git a/cpp/src/graphar/graph_info.cc b/cpp/src/graphar/graph_info.cc
index 9959f194..fdbac1b3 100644
--- a/cpp/src/graphar/graph_info.cc
+++ b/cpp/src/graphar/graph_info.cc
@@ -71,6 +71,21 @@ std::vector<T> AddVectorElement(const std::vector<T>&
values, T new_element) {
return out;
}
+template <typename T>
+std::vector<T> RemoveVectorElement(const std::vector<T>& values, size_t index)
{
+ if (index >= values.size()) {
+ return values;
+ }
+ std::vector<T> out;
+ out.reserve(values.size() - 1);
+ for (size_t i = 0; i < values.size(); ++i) {
+ if (i != index) {
+ out.push_back(values[i]);
+ }
+ }
+ return out;
+}
+
std::string BuildPath(const std::vector<std::string>& paths) {
std::string path;
for (const auto& p : paths) {
@@ -398,6 +413,27 @@ Result<std::shared_ptr<VertexInfo>>
VertexInfo::AddPropertyGroup(
impl_->prefix_, impl_->version_);
}
+Result<std::shared_ptr<VertexInfo>> VertexInfo::RemovePropertyGroup(
+ std::shared_ptr<PropertyGroup> property_group) const {
+ if (property_group == nullptr) {
+ return Status::Invalid("property group is nullptr");
+ }
+ int idx = -1;
+ for (size_t i = 0; i < impl_->property_groups_.size(); i++) {
+ if (*(impl_->property_groups_[i]) == *property_group) {
+ idx = i;
+ break;
+ }
+ }
+ if (idx == -1) {
+ return Status::Invalid("property group not found");
+ }
+ return std::make_shared<VertexInfo>(
+ impl_->type_, impl_->chunk_size_,
+ RemoveVectorElement(impl_->property_groups_, static_cast<size_t>(idx)),
+ impl_->labels_, impl_->prefix_, impl_->version_);
+}
+
bool VertexInfo::IsValidated() const { return impl_->is_validated(); }
std::shared_ptr<VertexInfo> CreateVertexInfo(
@@ -845,6 +881,28 @@ Result<std::shared_ptr<EdgeInfo>>
EdgeInfo::AddAdjacentList(
impl_->property_groups_, impl_->prefix_, impl_->version_);
}
+Result<std::shared_ptr<EdgeInfo>> EdgeInfo::RemoveAdjacentList(
+ std::shared_ptr<AdjacentList> adj_list) const {
+ if (adj_list == nullptr) {
+ return Status::Invalid("adj list is nullptr");
+ }
+ int idx = -1;
+ for (size_t i = 0; i < impl_->adjacent_lists_.size(); i++) {
+ if (impl_->adjacent_lists_[i]->GetType() == adj_list->GetType()) {
+ idx = i;
+ break;
+ }
+ }
+ if (idx == -1) {
+ return Status::Invalid("adj list not found");
+ }
+ return std::make_shared<EdgeInfo>(
+ impl_->src_type_, impl_->edge_type_, impl_->dst_type_,
impl_->chunk_size_,
+ impl_->src_chunk_size_, impl_->dst_chunk_size_, impl_->directed_,
+ RemoveVectorElement(impl_->adjacent_lists_, static_cast<size_t>(idx)),
+ impl_->property_groups_, impl_->prefix_, impl_->version_);
+}
+
Result<std::shared_ptr<EdgeInfo>> EdgeInfo::AddPropertyGroup(
std::shared_ptr<PropertyGroup> property_group) const {
if (property_group == nullptr) {
@@ -864,6 +922,28 @@ Result<std::shared_ptr<EdgeInfo>>
EdgeInfo::AddPropertyGroup(
impl_->version_);
}
+Result<std::shared_ptr<EdgeInfo>> EdgeInfo::RemovePropertyGroup(
+ std::shared_ptr<PropertyGroup> property_group) const {
+ if (property_group == nullptr) {
+ return Status::Invalid("property group is nullptr");
+ }
+ int idx = -1;
+ for (size_t i = 0; i < impl_->property_groups_.size(); i++) {
+ if (*(impl_->property_groups_[i]) == *property_group) {
+ idx = i;
+ }
+ }
+ if (idx == -1) {
+ return Status::Invalid("property group not found");
+ }
+ return std::make_shared<EdgeInfo>(
+ impl_->src_type_, impl_->edge_type_, impl_->dst_type_,
impl_->chunk_size_,
+ impl_->src_chunk_size_, impl_->dst_chunk_size_, impl_->directed_,
+ impl_->adjacent_lists_,
+ RemoveVectorElement(impl_->property_groups_, static_cast<size_t>(idx)),
+ impl_->prefix_, impl_->version_);
+}
+
bool EdgeInfo::IsValidated() const { return impl_->is_validated(); }
std::shared_ptr<EdgeInfo> CreateEdgeInfo(
@@ -1278,6 +1358,22 @@ Result<std::shared_ptr<GraphInfo>> GraphInfo::AddVertex(
impl_->edge_infos_, impl_->labels_, impl_->prefix_, impl_->version_);
}
+Result<std::shared_ptr<GraphInfo>> GraphInfo::RemoveVertex(
+ std::shared_ptr<VertexInfo> vertex_info) const {
+ if (vertex_info == nullptr) {
+ return Status::Invalid("vertex info is nullptr");
+ }
+ int idx = GetVertexInfoIndex(vertex_info->GetType());
+ if (idx == -1) {
+ return Status::Invalid("vertex info not found");
+ }
+ return std::make_shared<GraphInfo>(
+ impl_->name_,
+ RemoveVectorElement(impl_->vertex_infos_, static_cast<size_t>(idx)),
+ impl_->edge_infos_, impl_->labels_, impl_->prefix_, impl_->version_,
+ impl_->extra_info_);
+}
+
Result<std::shared_ptr<GraphInfo>> GraphInfo::AddEdge(
std::shared_ptr<EdgeInfo> edge_info) const {
if (edge_info == nullptr) {
@@ -1293,6 +1389,22 @@ Result<std::shared_ptr<GraphInfo>> GraphInfo::AddEdge(
impl_->prefix_, impl_->version_);
}
+Result<std::shared_ptr<GraphInfo>> GraphInfo::RemoveEdge(
+ std::shared_ptr<EdgeInfo> edge_info) const {
+ if (edge_info == nullptr) {
+ return Status::Invalid("edge info is nullptr");
+ }
+ int idx = GetEdgeInfoIndex(edge_info->GetSrcType(), edge_info->GetEdgeType(),
+ edge_info->GetDstType());
+ if (idx == -1) {
+ return Status::Invalid("edge info not found");
+ }
+ return std::make_shared<GraphInfo>(
+ impl_->name_, impl_->vertex_infos_,
+ RemoveVectorElement(impl_->edge_infos_, static_cast<size_t>(idx)),
+ impl_->labels_, impl_->prefix_, impl_->version_, impl_->extra_info_);
+}
+
std::shared_ptr<GraphInfo> CreateGraphInfo(
const std::string& name, const VertexInfoVector& vertex_infos,
const EdgeInfoVector& edge_infos, const std::vector<std::string>& labels,
diff --git a/cpp/src/graphar/graph_info.h b/cpp/src/graphar/graph_info.h
index 27a487d4..54d5870d 100644
--- a/cpp/src/graphar/graph_info.h
+++ b/cpp/src/graphar/graph_info.h
@@ -207,6 +207,16 @@ class VertexInfo {
Result<std::shared_ptr<VertexInfo>> AddPropertyGroup(
std::shared_ptr<PropertyGroup> property_group) const;
+ /**
+ * @brief Removes a property group from the VertexInfo instance and returns a
+ * new VertexInfo.
+ * @param property_group The property group to remove.
+ * @return A Status object indicating the success or failure of the
+ * operation. Returns InvalidOperation if the property group is not
contained.
+ */
+ Result<std::shared_ptr<VertexInfo>> RemovePropertyGroup(
+ std::shared_ptr<PropertyGroup> property_group) const;
+
/**
* Get the type of the vertex.
*
@@ -434,6 +444,16 @@ class EdgeInfo {
Result<std::shared_ptr<EdgeInfo>> AddAdjacentList(
std::shared_ptr<AdjacentList> adj_list) const;
+ /**
+ * @brief Removes an adjacency list from the EdgeInfo instance and returns a
+ * new EdgeInfo.
+ * @param adj_list The adjacency list to remove.
+ * @return A Status object indicating the success or failure of the
+ * operation. Returns InvalidOperation if the adjacency list is not
contained.
+ */
+ Result<std::shared_ptr<EdgeInfo>> RemoveAdjacentList(
+ std::shared_ptr<AdjacentList> adj_list) const;
+
/**
* Add a property group to edge info and returns a new EdgeInfo.
*
@@ -442,6 +462,16 @@ class EdgeInfo {
Result<std::shared_ptr<EdgeInfo>> AddPropertyGroup(
std::shared_ptr<PropertyGroup> property_group) const;
+ /**
+ * @brief Removes a property group from the EdgeInfo instance and returns a
+ * new EdgeInfo.
+ * @param property_group The property group to remove.
+ * @return A Status object indicating the success or failure of the
+ * operation. Returns InvalidOperation if the property group is not
contained.
+ */
+ Result<std::shared_ptr<EdgeInfo>> RemovePropertyGroup(
+ std::shared_ptr<PropertyGroup> property_group) const;
+
/**
* Get the type of the source vertex.
* @return The type of the source vertex.
@@ -754,6 +784,16 @@ class GraphInfo {
Result<std::shared_ptr<GraphInfo>> AddVertex(
std::shared_ptr<VertexInfo> vertex_info) const;
+ /**
+ * @brief Removes a vertex info from the GraphInfo instance and returns a new
+ * GraphInfo.
+ * @param vertex_info The vertex info to remove.
+ * @return A Status object indicating the success or failure of the
+ * operation. Returns InvalidOperation if the vertex info is not contained.
+ */
+ Result<std::shared_ptr<GraphInfo>> RemoveVertex(
+ std::shared_ptr<VertexInfo> vertex_info) const;
+
/**
* @brief Adds an edge info to the GraphInfo instance and returns a new
* GraphInfo.
@@ -765,6 +805,16 @@ class GraphInfo {
Result<std::shared_ptr<GraphInfo>> AddEdge(
std::shared_ptr<EdgeInfo> edge_info) const;
+ /**
+ * @brief Removes an edge info from the GraphInfo instance and returns a new
+ * GraphInfo.
+ * @param edge_info The edge info to remove.
+ * @return A Status object indicating the success or failure of the
+ * operation. Returns InvalidOperation if the edge info is not contained.
+ */
+ Result<std::shared_ptr<GraphInfo>> RemoveEdge(
+ std::shared_ptr<EdgeInfo> edge_info) const;
+
/**
* @brief Get the name of the graph.
* @return The name of the graph.
diff --git a/cpp/test/test_info.cc b/cpp/test/test_info.cc
index 9901ea47..cd063cc5 100644
--- a/cpp/test/test_info.cc
+++ b/cpp/test/test_info.cc
@@ -325,6 +325,22 @@ version: gar/v1
auto extend_info3 = extend_info->AddPropertyGroup(pg3);
REQUIRE(!extend_info3.status().ok());
}
+
+ SECTION("RemovePropertyGroup") {
+ auto pg3 = CreatePropertyGroup({Property("p3", int32(), false)},
+ FileType::CSV, "p3/");
+ auto maybe_extend_info = vertex_info->AddPropertyGroup(pg3);
+ REQUIRE(maybe_extend_info.status().ok());
+ auto extend_info = maybe_extend_info.value();
+ REQUIRE(extend_info->PropertyGroupNum() == 2);
+ auto maybe_removed_info = extend_info->RemovePropertyGroup(pg3);
+ REQUIRE(maybe_removed_info.status().ok());
+ auto removed_info = maybe_removed_info.value();
+ REQUIRE(removed_info->PropertyGroupNum() == 1);
+ REQUIRE(removed_info->HasPropertyGroup(pg3) == false);
+ auto maybe_removed_again = removed_info->RemovePropertyGroup(pg3);
+ REQUIRE(!maybe_removed_again.status().ok());
+ }
}
TEST_CASE_METHOD(GlobalFixture, "EdgeInfo") {
@@ -533,6 +549,21 @@ version: gar/v1
REQUIRE(!extend_info2.status().ok());
}
+ SECTION("RemoveAdjacentList") {
+ auto adj_list2 = CreateAdjacentList(AdjListType::ordered_by_dest,
+ FileType::CSV, "ordered_by_dest/");
+ auto maybe_extend_info = edge_info->AddAdjacentList(adj_list2);
+ REQUIRE(maybe_extend_info.status().ok());
+ auto extend_info = maybe_extend_info.value();
+ auto maybe_remove_info = extend_info->RemoveAdjacentList(adj_list2);
+ REQUIRE(maybe_remove_info.status().ok());
+ auto remove_info = maybe_remove_info.value();
+ REQUIRE(remove_info->HasAdjacentListType(AdjListType::ordered_by_dest) ==
+ false);
+ auto remove_info2 = remove_info->RemoveAdjacentList(adj_list2);
+ REQUIRE(!remove_info2.status().ok());
+ }
+
SECTION("AddPropertyGroup") {
auto pg2 = CreatePropertyGroup({Property("p2", int32(), false)},
FileType::CSV, "p2/");
@@ -550,6 +581,23 @@ version: gar/v1
auto extend_info2 = extend_info->AddPropertyGroup(pg2);
REQUIRE(!extend_info2.status().ok());
}
+
+ SECTION("RemovePropertyGroup") {
+ auto pg2 = CreatePropertyGroup({Property("p2", int32(), false)},
+ FileType::CSV, "p2/");
+ auto maybe_extend_info = edge_info->AddPropertyGroup(pg2);
+ REQUIRE(maybe_extend_info.status().ok());
+ auto extend_info = maybe_extend_info.value();
+ auto maybe_remove_info = extend_info->RemovePropertyGroup(pg2);
+ REQUIRE(maybe_remove_info.status().ok());
+ auto remove_info = maybe_remove_info.value();
+ REQUIRE(remove_info->PropertyGroupNum() == 1);
+ REQUIRE(remove_info->HasProperty("p2") == false);
+ REQUIRE(remove_info->HasPropertyGroup(pg2) == false);
+ REQUIRE(remove_info->GetPropertyGroups().size() == 1);
+ auto remove_info2 = remove_info->RemovePropertyGroup(pg2);
+ REQUIRE(!remove_info2.status().ok());
+ }
}
TEST_CASE_METHOD(GlobalFixture, "GraphInfo") {
@@ -693,6 +741,23 @@ vertices:
REQUIRE(!extend_info2.status().ok());
}
+ SECTION("RemoveVertex") {
+ auto vertex_info2 = CreateVertexInfo("test_vertex2", 100, {pg}, {},
+ "test_vertex2/", version);
+ auto maybe_extend_info = graph_info->AddVertex(vertex_info2);
+ REQUIRE(maybe_extend_info.status().ok());
+ auto extend_info = maybe_extend_info.value();
+ auto maybe_remove_info = extend_info->RemoveVertex(vertex_info2);
+ REQUIRE(maybe_remove_info.status().ok());
+ auto remove_info = maybe_remove_info.value();
+ REQUIRE(remove_info->GetVertexInfos().size() == 1);
+ REQUIRE(remove_info->GetVertexInfoByIndex(1) == nullptr);
+ REQUIRE(remove_info->GetVertexInfo("test_vertex2") == nullptr);
+ REQUIRE(remove_info->GetVertexInfoByIndex(1) == nullptr);
+ auto remove_info2 = remove_info->RemoveVertex(vertex_info2);
+ REQUIRE(!remove_info2.status().ok());
+ }
+
SECTION("AddEdge") {
auto edge_info2 =
CreateEdgeInfo("person", "knows2", "person", 1024, 100, 100, true,
@@ -715,6 +780,26 @@ vertices:
auto extend_info2 = extend_info->AddEdge(edge_info2);
REQUIRE(!extend_info2.status().ok());
}
+
+ SECTION("RemoveEdge") {
+ auto edge_info2 =
+ CreateEdgeInfo("person", "knows2", "person", 1024, 100, 100, true,
+ {CreateAdjacentList(AdjListType::ordered_by_source,
+ FileType::CSV, "adj_list/")},
+ {pg}, "test_edge/", version);
+ auto maybe_extend_info = graph_info->AddEdge(edge_info2);
+ REQUIRE(maybe_extend_info.status().ok());
+ auto extend_info = maybe_extend_info.value();
+ auto maybe_remove_info = extend_info->RemoveEdge(edge_info2);
+ REQUIRE(maybe_remove_info.status().ok());
+ auto remove_info = maybe_remove_info.value();
+ REQUIRE(remove_info->EdgeInfoNum() == 1);
+ REQUIRE(remove_info->GetEdgeInfoByIndex(1) == nullptr);
+ REQUIRE(remove_info->GetEdgeInfo("person", "knows2", "person") == nullptr);
+ REQUIRE(remove_info->GetEdgeInfos().size() == 1);
+ auto remove_info2 = remove_info->RemoveEdge(edge_info2);
+ REQUIRE(!remove_info2.status().ok());
+ }
}
TEST_CASE_METHOD(GlobalFixture, "LoadFromYaml") {
diff --git a/docs/specification/implementation-status.md
b/docs/specification/implementation-status.md
index 6d205ae7..d5277017 100644
--- a/docs/specification/implementation-status.md
+++ b/docs/specification/implementation-status.md
@@ -82,7 +82,7 @@ Supported operations in Property:
|-------------------|-------|--------|-------|------------|
| create | ✓ | ✓ | ✓ | ✓ |
| add property | ✓ | ✓ | ✓ | ✓ |
-| remove property | | | | |
+| remove property | ✓ | | | |
| get properties | ✓ | ✓ | ✓ | ✓ |
| check property | ✓ | ✓ | | |
| get file type | ✓ | ✓ | ✓ | ✓ |
@@ -133,7 +133,7 @@ Supported operations in Vertex Info:
|-------------------|-------|--------|-------|------------|
| create | ✓ | ✓ | ✓ | ✓ |
| add group | ✓ | ✓ | ✓ | ✓ |
-| remove group | | | | |
+| remove group | ✓ | | | |
| get label | ✓ | ✓ | ✓ | ✓ |
| get chunk size | ✓ | ✓ | ✓ | ✓ |
| get groups | ✓ | ✓ | ✓ | ✓ |
@@ -161,9 +161,9 @@ Supported operations in Edge Info:
|-------------------|-------|--------|-------|------------|
| create | ✓ | ✓ | ✓ | ✓ |
| add group | ✓ | ✓ | ✓ | ✓ |
-| remove group | | | | |
+| remove group | ✓ | | | |
| add adj list | ✓ | ✓ | ✓ | ✓ |
-| remove adj list | | | | |
+| remove adj list | ✓ | | | |
| get label | ✓ | ✓ | ✓ | ✓ |
| get source label | ✓ | ✓ | ✓ | ✓ |
| get dest label | ✓ | ✓ | ✓ | ✓ |
@@ -200,9 +200,9 @@ Supported operations in Graph Info:
|-------------------|-------|--------|-------|------------|
| create | ✓ | ✓ | ✓ | ✓ |
| add vertex | ✓ | ✓ | ✓ | ✓ |
-| remove vertex | | | | |
+| remove vertex | ✓ | | | |
| add edge | ✓ | ✓ | ✓ | ✓ |
-| remove edge | | | | |
+| remove edge | ✓ | | | |
| get name | ✓ | ✓ | ✓ | ✓ |
| get vertex | ✓ | ✓ | ✓ | ✓ |
| get edge | ✓ | ✓ | ✓ | ✓ |
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]