Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions cpp/src/graphar/graph_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
50 changes: 50 additions & 0 deletions cpp/src/graphar/graph_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
85 changes: 85 additions & 0 deletions cpp/test/test_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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/");
Expand All @@ -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") {
Expand Down Expand Up @@ -693,6 +741,23 @@ version: gar/v1
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,
Expand All @@ -715,6 +780,26 @@ version: gar/v1
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") {
Expand Down
Loading
Loading