Skip to content

Commit f463d0a

Browse files
committed
use optional and remove empty_
1 parent d535a43 commit f463d0a

4 files changed

Lines changed: 44 additions & 10 deletions

File tree

cpp/src/graphar/high-level/vertices_builder.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cassert>
2424
#include <cstddef>
2525
#include <memory>
26+
#include <optional>
2627
#include <string>
2728
#include <unordered_map>
2829
#include <unordered_set>
@@ -51,21 +52,30 @@ namespace graphar::builder {
5152
*/
5253
class Vertex {
5354
public:
54-
Vertex() : empty_(true) {}
55+
Vertex() = default;
5556

5657
/**
5758
* @brief Initialize the vertex with a given id.
5859
*
5960
* @param id The id of the vertex.
6061
*/
61-
explicit Vertex(IdType id) : id_(id), empty_(false) {}
62+
explicit Vertex(IdType id) : id_(id) {}
6263

6364
/**
6465
* @brief Get id of the vertex.
6566
*
67+
* The id is absent until explicitly set or assigned by VerticesBuilder.
68+
*
6669
* @return The id of the vertex.
6770
*/
68-
IdType GetId() const noexcept { return id_; }
71+
IdType GetId() const { return id_.value(); }
72+
73+
/**
74+
* @brief Check if the vertex id has been initialized.
75+
*
76+
* @return true/false.
77+
*/
78+
bool HasId() const noexcept { return id_.has_value(); }
6979

7080
/**
7181
* @brief Set id of the vertex.
@@ -75,11 +85,11 @@ class Vertex {
7585
void SetId(IdType id) { id_ = id; }
7686

7787
/**
78-
* @brief Check if the vertex is empty.
88+
* @brief Check if the vertex contains no property payload.
7989
*
8090
* @return true/false.
8191
*/
82-
bool Empty() const noexcept { return empty_; }
92+
bool Empty() const noexcept { return properties_.empty(); }
8393

8494
/**
8595
* @brief Add a property to the vertex.
@@ -89,7 +99,6 @@ class Vertex {
8999
*/
90100
// TODO(@acezen): Enable the property to be a vector(list).
91101
void AddProperty(const std::string& name, const std::any& val) {
92-
empty_ = false;
93102
properties_[name] = val;
94103
}
95104

@@ -100,7 +109,6 @@ class Vertex {
100109
AddProperty(name, val);
101110
return;
102111
}
103-
empty_ = false;
104112
if (cardinalities_.find(name) != cardinalities_.end()) {
105113
if (cardinalities_[name] != cardinality) {
106114
throw std::runtime_error("Cardinality mismatch for property: " + name);
@@ -211,8 +219,7 @@ class Vertex {
211219
}
212220

213221
private:
214-
IdType id_;
215-
bool empty_;
222+
std::optional<IdType> id_;
216223
std::unordered_map<std::string, std::any> properties_;
217224
std::unordered_map<std::string, Cardinality> cardinalities_;
218225
};

cpp/test/test_builder.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") {
6464
builder->SetValidateLevel(ValidateLevel::strong_validate);
6565
REQUIRE(builder->GetValidateLevel() == ValidateLevel::strong_validate);
6666

67+
// vertex id and payload state are tracked independently
68+
builder::Vertex empty_vertex;
69+
REQUIRE_FALSE(empty_vertex.HasId());
70+
REQUIRE(empty_vertex.Empty());
71+
REQUIRE_THROWS_AS(empty_vertex.GetId(), std::bad_optional_access);
72+
empty_vertex.SetId(42);
73+
REQUIRE(empty_vertex.HasId());
74+
REQUIRE(empty_vertex.GetId() == 42);
75+
REQUIRE(empty_vertex.Empty());
76+
empty_vertex.AddProperty("id", int64_t{42});
77+
REQUIRE_FALSE(empty_vertex.Empty());
78+
6779
// check different validate levels
6880
builder::Vertex v;
6981
v.AddProperty("id", "id_of_string");
@@ -81,6 +93,11 @@ TEST_CASE_METHOD(GlobalFixture, "Test_vertices_builder") {
8193
builder->Clear();
8294
REQUIRE(builder->GetNum() == 0);
8395

96+
builder::Vertex indexed_vertex(7);
97+
REQUIRE(indexed_vertex.HasId());
98+
REQUIRE(indexed_vertex.GetId() == 7);
99+
REQUIRE(indexed_vertex.Empty());
100+
84101
// add vertices
85102
std::ifstream fp(test_data_dir + "/ldbc_sample/person_0_0.csv");
86103
std::string line;

python/src/bindings/high_level_binding.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,4 @@ extern "C" void bind_high_level_api(pybind11::module_& m) {
415415
py::arg("dst_type"), py::arg("adj_list_type"), py::arg("num_vertices"),
416416
py::arg("writer_options") = nullptr,
417417
py::arg("validate_level") = graphar::ValidateLevel::no_validate);
418-
} // namespace graphar
418+
} // namespace graphar

python/test/test_high_level_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ def test_vertices_builder(sample_graph_vertex):
111111
# Set validate level
112112
builder.SetValidateLevel(ValidateLevel.strong_validate)
113113

114+
empty_vertex = BuilderVertex()
115+
assert empty_vertex.Empty()
116+
with pytest.raises(Exception):
117+
empty_vertex.GetId()
118+
empty_vertex.SetId(42)
119+
assert empty_vertex.GetId() == 42
120+
assert empty_vertex.Empty()
121+
empty_vertex.AddProperty("id", 42)
122+
assert not empty_vertex.Empty()
123+
114124
# Prepare vertex data
115125
vertex_count = 3
116126
property_names = ["id", "firstName", "lastName", "gender"]

0 commit comments

Comments
 (0)