Skip to content

Commit

Permalink
refactor(interactive): Add method to get edge_num and some code ref…
Browse files Browse the repository at this point in the history
…actor (#4029)

As titled.
  • Loading branch information
zhanglei1949 authored Jul 11, 2024
1 parent 54f4925 commit 4e85eb5
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 51 deletions.
52 changes: 3 additions & 49 deletions flex/engines/hqps_db/database/mutable_csr_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,8 @@ class MutableCSRInterface {
} else {
auto ptr = db_session_.get_vertex_property_column(label_id, prop_name);
if (ptr) {
column = std::dynamic_pointer_cast<TypedRefColumn<T>>(
create_ref_column(ptr));
column =
std::dynamic_pointer_cast<TypedRefColumn<T>>(CreateRefColumn(ptr));
} else {
return nullptr;
}
Expand All @@ -849,7 +849,7 @@ class MutableCSRInterface {
} else if (prop_name == "Label" || prop_name == "LabelKey") {
return std::make_shared<TypedRefColumn<LabelKey>>(label_id);
} else {
return create_ref_column(
return CreateRefColumn(
db_session_.get_vertex_property_column(label_id, prop_name));
}
}
Expand All @@ -862,52 +862,6 @@ class MutableCSRInterface {
}

private:
std::shared_ptr<RefColumnBase> create_ref_column(
std::shared_ptr<ColumnBase> column) const {
auto type = column->type();
if (type == PropertyType::kBool) {
return std::make_shared<TypedRefColumn<bool>>(
*std::dynamic_pointer_cast<TypedColumn<bool>>(column));
} else if (type == PropertyType::kDay) {
return std::make_shared<TypedRefColumn<Day>>(
*std::dynamic_pointer_cast<TypedColumn<Day>>(column));
} else if (type == PropertyType::kDate) {
return std::make_shared<TypedRefColumn<Date>>(
*std::dynamic_pointer_cast<TypedColumn<Date>>(column));
} else if (type == PropertyType::kUInt8) {
return std::make_shared<TypedRefColumn<uint8_t>>(
*std::dynamic_pointer_cast<TypedColumn<uint8_t>>(column));
} else if (type == PropertyType::kUInt16) {
return std::make_shared<TypedRefColumn<uint16_t>>(
*std::dynamic_pointer_cast<TypedColumn<uint16_t>>(column));
} else if (type == PropertyType::kInt32) {
return std::make_shared<TypedRefColumn<int32_t>>(
*std::dynamic_pointer_cast<TypedColumn<int32_t>>(column));
} else if (type == PropertyType::kInt64) {
return std::make_shared<TypedRefColumn<int64_t>>(
*std::dynamic_pointer_cast<TypedColumn<int64_t>>(column));
} else if (type == PropertyType::kUInt32) {
return std::make_shared<TypedRefColumn<uint32_t>>(
*std::dynamic_pointer_cast<TypedColumn<uint32_t>>(column));
} else if (type == PropertyType::kUInt64) {
return std::make_shared<TypedRefColumn<uint64_t>>(
*std::dynamic_pointer_cast<TypedColumn<uint64_t>>(column));
} else if (type == PropertyType::kStringView || type.IsVarchar()) {
return std::make_shared<TypedRefColumn<std::string_view>>(
*std::dynamic_pointer_cast<TypedColumn<std::string_view>>(column));
} else if (type == PropertyType::kFloat) {
return std::make_shared<TypedRefColumn<float>>(
*std::dynamic_pointer_cast<TypedColumn<float>>(column));
} else if (type == PropertyType::kDouble) {
return std::make_shared<TypedRefColumn<double>>(
*std::dynamic_pointer_cast<TypedColumn<double>>(column));
} else {
LOG(FATAL) << "unexpected type to create column, "
<< static_cast<int>(type.type_enum);
return nullptr;
}
}

template <typename PropT>
auto get_single_column_from_graph_with_property(
label_t label, const PropertySelector<PropT>& selector) const {
Expand Down
5 changes: 5 additions & 0 deletions flex/storages/rt_mutable_graph/csr/csr_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class CsrBase {
virtual void resize(vid_t vnum) = 0;
virtual size_t size() const = 0;

// Returns the number of edges in the graph. Note that the returned value is
// exactly the number of edges in this csr. Even if there may be some reserved
// space, the reserved space will count as 0.
virtual size_t edge_num() const = 0;

virtual void close() = 0;

virtual std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const = 0;
Expand Down
28 changes: 28 additions & 0 deletions flex/storages/rt_mutable_graph/csr/immutable_csr.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ class ImmutableCsr : public TypedImmutableCsrBase<EDATA_T> {

size_t size() const override { return adj_lists_.size(); }

size_t edge_num() const override {
size_t ret = 0;
for (size_t i = 0; i < adj_lists_.size(); ++i) {
ret += degree_list_[i];
}
return ret;
}

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<ImmutableCsrConstEdgeIter<EDATA_T>>(get_edges(v));
}
Expand Down Expand Up @@ -452,6 +460,16 @@ class SingleImmutableCsr : public TypedImmutableCsrBase<EDATA_T> {

size_t size() const override { return nbr_list_.size(); }

size_t edge_num() const override {
size_t ret = 0;
for (size_t i = 0; i < nbr_list_.size(); ++i) {
if (nbr_list_[i].neighbor != std::numeric_limits<vid_t>::max()) {
++ret;
}
}
return ret;
}

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<ImmutableCsrConstEdgeIter<EDATA_T>>(get_edges(v));
}
Expand Down Expand Up @@ -629,6 +647,16 @@ class SingleImmutableCsr<std::string_view>

size_t size() const override { return nbr_list_.size(); }

size_t edge_num() const override {
size_t ret = 0;
for (size_t i = 0; i < nbr_list_.size(); ++i) {
if (nbr_list_[i].neighbor != std::numeric_limits<vid_t>::max()) {
++ret;
}
}
return ret;
}

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<ImmutableCsrConstEdgeIter<std::string_view>>(
get_edges(v));
Expand Down
33 changes: 33 additions & 0 deletions flex/storages/rt_mutable_graph/csr/mutable_csr.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,14 @@ class MutableCsr : public TypedMutableCsrBase<EDATA_T> {
}
size_t size() const override { return adj_lists_.size(); }

size_t edge_num() const override {
size_t res = 0;
for (size_t i = 0; i < adj_lists_.size(); ++i) {
res += adj_lists_[i].size();
}
return res;
}

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<MutableCsrConstEdgeIter<EDATA_T>>(get_edges(v));
}
Expand Down Expand Up @@ -573,6 +581,8 @@ class MutableCsr<std::string_view>

size_t size() const override { return csr_.size(); }

size_t edge_num() const override { return csr_.edge_num(); }

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<MutableCsrConstEdgeIter<std::string_view>>(
get_edges(v));
Expand Down Expand Up @@ -657,6 +667,8 @@ class MutableCsr<RecordView> : public TypedMutableCsrBase<RecordView> {

size_t size() const override { return csr_.size(); }

size_t edge_num() const override { return csr_.edge_num(); }

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<MutableCsrConstEdgeIter<RecordView>>(get_edges(v));
}
Expand Down Expand Up @@ -834,6 +846,17 @@ class SingleMutableCsr : public TypedMutableCsrBase<EDATA_T> {

size_t size() const override { return nbr_list_.size(); }

size_t edge_num() const override {
size_t cnt = 0;
for (size_t k = 0; k != nbr_list_.size(); ++k) {
if (nbr_list_[k].timestamp.load() !=
std::numeric_limits<timestamp_t>::max()) {
++cnt;
}
}
return cnt;
}

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<MutableCsrConstEdgeIter<EDATA_T>>(get_edges(v));
}
Expand Down Expand Up @@ -938,6 +961,8 @@ class SingleMutableCsr<std::string_view>

size_t size() const override { return csr_.size(); }

size_t edge_num() const override { return csr_.edge_num(); }

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<MutableCsrConstEdgeIter<std::string_view>>(
get_edges(v));
Expand Down Expand Up @@ -1040,6 +1065,8 @@ class SingleMutableCsr<RecordView> : public TypedMutableCsrBase<RecordView> {

size_t size() const override { return csr_.size(); }

size_t edge_num() const override { return csr_.edge_num(); }

std::shared_ptr<CsrConstEdgeIterBase> edge_iter(vid_t v) const override {
return std::make_shared<MutableCsrConstEdgeIter<RecordView>>(get_edges(v));
}
Expand Down Expand Up @@ -1130,6 +1157,8 @@ class EmptyCsr : public TypedMutableCsrBase<EDATA_T> {

size_t size() const override { return 0; }

size_t edge_num() const override { return 0; }

void batch_put_edge(vid_t src, vid_t dst, const EDATA_T& data,
timestamp_t ts = 0) override {}
void put_edge(vid_t src, vid_t dst, const EDATA_T& data, timestamp_t ts,
Expand Down Expand Up @@ -1192,6 +1221,8 @@ class EmptyCsr<std::string_view>

size_t size() const override { return 0; }

size_t edge_num() const override { return 0; }

void put_edge_with_index(vid_t src, vid_t dst, size_t index, timestamp_t ts,
Allocator& alloc) override {}
void batch_put_edge_with_index(vid_t src, vid_t dst, size_t data,
Expand Down Expand Up @@ -1249,6 +1280,8 @@ class EmptyCsr<RecordView> : public TypedMutableCsrBase<RecordView> {

size_t size() const override { return 0; }

size_t edge_num() const override { return 0; }

void put_edge_with_index(vid_t src, vid_t dst, size_t index, timestamp_t ts,
Allocator& alloc) override {}
void batch_put_edge_with_index(vid_t src, vid_t dst, size_t data,
Expand Down
12 changes: 12 additions & 0 deletions flex/storages/rt_mutable_graph/dual_csr.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ class DualCsrBase {
GetOutCsr()->warmup(thread_num);
}

size_t EdgeNum() const {
const CsrBase* oe_csr = GetOutCsr();
const CsrBase* ie_csr = GetInCsr();
if (oe_csr) {
return oe_csr->edge_num();
} else if (ie_csr) {
return ie_csr->edge_num();
} else {
return 0;
}
}

virtual CsrBase* GetInCsr() = 0;
virtual CsrBase* GetOutCsr() = 0;
virtual const CsrBase* GetInCsr() const = 0;
Expand Down
11 changes: 11 additions & 0 deletions flex/storages/rt_mutable_graph/mutable_property_fragment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ vid_t MutablePropertyFragment::vertex_num(label_t vertex_label) const {
return static_cast<vid_t>(lf_indexers_[vertex_label].size());
}

size_t MutablePropertyFragment::edge_num(label_t src_label, label_t edge_label,
label_t dst_label) const {
size_t index = src_label * vertex_label_num_ * edge_label_num_ +
dst_label * edge_label_num_ + edge_label;
if (dual_csr_list_[index] != NULL) {
return dual_csr_list_[index]->EdgeNum();
} else {
return 0;
}
}

bool MutablePropertyFragment::get_lid(label_t label, const Any& oid,
vid_t& lid) const {
return lf_indexers_[label].get_index(oid, lid);
Expand Down
3 changes: 3 additions & 0 deletions flex/storages/rt_mutable_graph/mutable_property_fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class MutablePropertyFragment {

vid_t vertex_num(label_t vertex_label) const;

size_t edge_num(label_t src_label, label_t edge_label,
label_t dst_label) const;

bool get_lid(label_t label, const Any& oid, vid_t& lid) const;

Any get_oid(label_t label, vid_t lid) const;
Expand Down
14 changes: 12 additions & 2 deletions flex/storages/rt_mutable_graph/schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1335,13 +1335,23 @@ bool Schema::has_edge_label(const std::string& src_label,
const std::string& dst_label,
const std::string& label) const {
label_t edge_label_id;
if (!has_vertex_label(src_label) || !has_vertex_label(dst_label)) {
LOG(ERROR) << "src_label or dst_label not found:" << src_label << ", "
<< dst_label;
return false;
}
auto src_label_id = get_vertex_label_id(src_label);
auto dst_label_id = get_vertex_label_id(dst_label);
if (!elabel_indexer_.get_index(label, edge_label_id)) {
LOG(ERROR) << "edge label not found:" << label;
return false;
}
auto e_label_id =
generate_edge_label(src_label_id, dst_label_id, edge_label_id);
return has_edge_label(src_label_id, dst_label_id, edge_label_id);
}

bool Schema::has_edge_label(label_t src_label, label_t dst_label,
label_t edge_label) const {
uint32_t e_label_id = generate_edge_label(src_label, dst_label, edge_label);
return eprop_names_.find(e_label_id) != eprop_names_.end();
}

Expand Down
3 changes: 3 additions & 0 deletions flex/storages/rt_mutable_graph/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class Schema {
const std::string& dst_label,
const std::string& edge_label) const;

bool has_edge_label(label_t src_label, label_t dst_label,
label_t edge_label) const;

bool valid_edge_property(const std::string& src_label,
const std::string& dst_label,
const std::string& label) const;
Expand Down
46 changes: 46 additions & 0 deletions flex/utils/property/column.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,50 @@ std::shared_ptr<ColumnBase> CreateColumn(PropertyType type,
}
}

std::shared_ptr<RefColumnBase> CreateRefColumn(
std::shared_ptr<ColumnBase> column) {
auto type = column->type();
if (type == PropertyType::kBool) {
return std::make_shared<TypedRefColumn<bool>>(
*std::dynamic_pointer_cast<TypedColumn<bool>>(column));
} else if (type == PropertyType::kDay) {
return std::make_shared<TypedRefColumn<Day>>(
*std::dynamic_pointer_cast<TypedColumn<Day>>(column));
} else if (type == PropertyType::kDate) {
return std::make_shared<TypedRefColumn<Date>>(
*std::dynamic_pointer_cast<TypedColumn<Date>>(column));
} else if (type == PropertyType::kUInt8) {
return std::make_shared<TypedRefColumn<uint8_t>>(
*std::dynamic_pointer_cast<TypedColumn<uint8_t>>(column));
} else if (type == PropertyType::kUInt16) {
return std::make_shared<TypedRefColumn<uint16_t>>(
*std::dynamic_pointer_cast<TypedColumn<uint16_t>>(column));
} else if (type == PropertyType::kInt32) {
return std::make_shared<TypedRefColumn<int32_t>>(
*std::dynamic_pointer_cast<TypedColumn<int32_t>>(column));
} else if (type == PropertyType::kInt64) {
return std::make_shared<TypedRefColumn<int64_t>>(
*std::dynamic_pointer_cast<TypedColumn<int64_t>>(column));
} else if (type == PropertyType::kUInt32) {
return std::make_shared<TypedRefColumn<uint32_t>>(
*std::dynamic_pointer_cast<TypedColumn<uint32_t>>(column));
} else if (type == PropertyType::kUInt64) {
return std::make_shared<TypedRefColumn<uint64_t>>(
*std::dynamic_pointer_cast<TypedColumn<uint64_t>>(column));
} else if (type == PropertyType::kStringView || type.IsVarchar()) {
return std::make_shared<TypedRefColumn<std::string_view>>(
*std::dynamic_pointer_cast<TypedColumn<std::string_view>>(column));
} else if (type == PropertyType::kFloat) {
return std::make_shared<TypedRefColumn<float>>(
*std::dynamic_pointer_cast<TypedColumn<float>>(column));
} else if (type == PropertyType::kDouble) {
return std::make_shared<TypedRefColumn<double>>(
*std::dynamic_pointer_cast<TypedColumn<double>>(column));
} else {
LOG(FATAL) << "unexpected type to create column, "
<< static_cast<int>(type.type_enum);
return nullptr;
}
}

} // namespace gs
6 changes: 6 additions & 0 deletions flex/utils/property/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,12 @@ class TypedRefColumn<GlobalId> : public RefColumnBase {
label_t label_key_;
};

// Create a reference column from a ColumnBase that contains a const reference
// to the actual column storage, offering a column-based store interface for
// vertex properties.
std::shared_ptr<RefColumnBase> CreateRefColumn(
std::shared_ptr<ColumnBase> column);

} // namespace gs

#endif // GRAPHSCOPE_PROPERTY_COLUMN_H_

0 comments on commit 4e85eb5

Please sign in to comment.