Skip to content

Commit 5bb4003

Browse files
Sober7135Codex
andauthored
feat(Rust): add GraphInfo support (#883)
* update GraphInfo * refactor(rust): align info bindings with upstream usize APIs * update --------- Co-authored-by: Codex <codex@localhost>
1 parent 83b44b1 commit 5bb4003

7 files changed

Lines changed: 825 additions & 2 deletions

File tree

rust/include/graphar_rs.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "rust/cxx.h"
3333

3434
namespace graphar {
35+
struct MaybeIndex;
36+
37+
using SharedVertexInfo = std::shared_ptr<VertexInfo>;
38+
using SharedEdgeInfo = std::shared_ptr<EdgeInfo>;
3539
using SharedPropertyGroup = std::shared_ptr<PropertyGroup>;
3640
using SharedAdjacentList = std::shared_ptr<AdjacentList>;
3741
using ConstInfoVersion = const InfoVersion;
@@ -89,6 +93,26 @@ std::shared_ptr<graphar::EdgeInfo> create_edge_info(
8993
const std::string& prefix,
9094
std::shared_ptr<graphar::ConstInfoVersion> version);
9195

96+
std::shared_ptr<graphar::GraphInfo> load_graph_info(const std::string& path);
97+
std::shared_ptr<graphar::GraphInfo> create_graph_info(
98+
const std::string& name,
99+
const std::vector<graphar::SharedVertexInfo>& vertex_infos,
100+
const std::vector<graphar::SharedEdgeInfo>& edge_infos,
101+
const rust::Vec<rust::String>& labels, const std::string& prefix,
102+
std::shared_ptr<graphar::ConstInfoVersion> version);
103+
graphar::MaybeIndex graph_info_vertex_info_index(
104+
const graphar::GraphInfo& graph_info, const std::string& type);
105+
graphar::MaybeIndex graph_info_edge_info_index(
106+
const graphar::GraphInfo& graph_info, const std::string& src_type,
107+
const std::string& edge_type, const std::string& dst_type);
108+
109+
void vertex_info_vec_push_vertex_info(
110+
std::vector<graphar::SharedVertexInfo>& vertex_infos,
111+
std::shared_ptr<graphar::VertexInfo> vertex_info);
112+
void edge_info_vec_push_edge_info(
113+
std::vector<graphar::SharedEdgeInfo>& edge_infos,
114+
std::shared_ptr<graphar::EdgeInfo> edge_info);
115+
92116
void vertex_info_save(const graphar::VertexInfo& vertex_info,
93117
const std::string& path);
94118
std::unique_ptr<std::string> vertex_info_dump(
@@ -101,4 +125,8 @@ void push_adjacent_list(graphar::AdjacentListVector& v,
101125
void edge_info_save(const graphar::EdgeInfo& edge_info,
102126
const std::string& path);
103127
std::unique_ptr<std::string> edge_info_dump(const graphar::EdgeInfo& edge_info);
128+
void graph_info_save(const graphar::GraphInfo& graph_info,
129+
const std::string& path);
130+
std::unique_ptr<std::string> graph_info_dump(
131+
const graphar::GraphInfo& graph_info);
104132
} // namespace graphar_rs

rust/src/ffi.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717

1818
use cxx::{ExternType, SharedPtr};
1919

20+
use crate::ffi::graphar::MaybeIndex;
21+
22+
#[repr(transparent)]
23+
#[derive(Clone)]
24+
pub struct SharedVertexInfo(pub(crate) SharedPtr<graphar::VertexInfo>);
25+
26+
unsafe impl ExternType for SharedVertexInfo {
27+
type Id = cxx::type_id!("graphar::SharedVertexInfo");
28+
type Kind = cxx::kind::Opaque;
29+
}
30+
31+
#[repr(transparent)]
32+
#[derive(Clone)]
33+
pub struct SharedEdgeInfo(pub(crate) SharedPtr<graphar::EdgeInfo>);
34+
35+
unsafe impl ExternType for SharedEdgeInfo {
36+
type Id = cxx::type_id!("graphar::SharedEdgeInfo");
37+
type Kind = cxx::kind::Opaque;
38+
}
39+
2040
#[repr(transparent)]
2141
#[derive(Clone)]
2242
pub struct SharedPropertyGroup(pub(crate) SharedPtr<graphar::PropertyGroup>);
@@ -348,6 +368,81 @@ pub(crate) mod graphar {
348368
fn edge_info_dump(edge_info: &EdgeInfo) -> Result<UniquePtr<CxxString>>;
349369
}
350370

371+
struct MaybeIndex {
372+
has_value: bool,
373+
index: usize,
374+
}
375+
376+
// `GraphInfo`
377+
unsafe extern "C++" {
378+
type GraphInfo;
379+
380+
fn GetName(&self) -> &CxxString;
381+
fn GetLabels(&self) -> &CxxVector<CxxString>;
382+
fn GetPrefix(&self) -> &CxxString;
383+
fn version(&self) -> &SharedPtr<ConstInfoVersion>;
384+
fn GetVertexInfo(&self, type_: &CxxString) -> SharedPtr<VertexInfo>;
385+
fn GetEdgeInfo(
386+
&self,
387+
src_type: &CxxString,
388+
edge_type: &CxxString,
389+
dst_type: &CxxString,
390+
) -> SharedPtr<EdgeInfo>;
391+
fn VertexInfoNum(&self) -> usize;
392+
fn EdgeInfoNum(&self) -> usize;
393+
fn GetVertexInfos(&self) -> &CxxVector<SharedVertexInfo>;
394+
fn GetEdgeInfos(&self) -> &CxxVector<SharedEdgeInfo>;
395+
396+
#[namespace = "graphar_rs"]
397+
fn load_graph_info(path: &CxxString) -> Result<SharedPtr<GraphInfo>>;
398+
#[namespace = "graphar_rs"]
399+
fn create_graph_info(
400+
name: &CxxString,
401+
vertex_infos: &CxxVector<SharedVertexInfo>,
402+
edge_infos: &CxxVector<SharedEdgeInfo>,
403+
labels: &Vec<String>,
404+
prefix: &CxxString,
405+
version: SharedPtr<ConstInfoVersion>,
406+
) -> Result<SharedPtr<GraphInfo>>;
407+
#[namespace = "graphar_rs"]
408+
fn graph_info_save(graph_info: &GraphInfo, path: &CxxString) -> Result<()>;
409+
#[namespace = "graphar_rs"]
410+
fn graph_info_dump(graph_info: &GraphInfo) -> Result<UniquePtr<CxxString>>;
411+
#[namespace = "graphar_rs"]
412+
fn graph_info_vertex_info_index(graph_info: &GraphInfo, type_: &CxxString) -> MaybeIndex;
413+
#[namespace = "graphar_rs"]
414+
fn graph_info_edge_info_index(
415+
graph_info: &GraphInfo,
416+
src_type: &CxxString,
417+
edge_type: &CxxString,
418+
dst_type: &CxxString,
419+
) -> MaybeIndex;
420+
}
421+
422+
unsafe extern "C++" {
423+
#[namespace = "graphar_rs"]
424+
fn vertex_info_vec_push_vertex_info(
425+
vertex_infos: Pin<&mut CxxVector<SharedVertexInfo>>,
426+
vertex_info: SharedPtr<VertexInfo>,
427+
);
428+
429+
#[namespace = "graphar_rs"]
430+
fn edge_info_vec_push_edge_info(
431+
edge_infos: Pin<&mut CxxVector<SharedEdgeInfo>>,
432+
edge_info: SharedPtr<EdgeInfo>,
433+
);
434+
}
435+
436+
unsafe extern "C++" {
437+
type SharedVertexInfo = crate::ffi::SharedVertexInfo;
438+
}
439+
impl CxxVector<SharedVertexInfo> {}
440+
441+
unsafe extern "C++" {
442+
type SharedEdgeInfo = crate::ffi::SharedEdgeInfo;
443+
}
444+
impl CxxVector<SharedEdgeInfo> {}
445+
351446
unsafe extern "C++" {
352447
type SharedPropertyGroup = crate::ffi::SharedPropertyGroup;
353448
}
@@ -358,3 +453,13 @@ pub(crate) mod graphar {
358453
}
359454
impl CxxVector<SharedAdjacentList> {}
360455
}
456+
457+
impl From<MaybeIndex> for Option<usize> {
458+
fn from(value: MaybeIndex) -> Self {
459+
if value.has_value {
460+
Some(value.index)
461+
} else {
462+
None
463+
}
464+
}
465+
}

rust/src/graphar_rs.cc

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
* under the License.
1818
*/
1919

20-
#include "graphar_rs.h"
20+
#include "graphar-rs/src/ffi.rs.h"
2121

22+
#include <cstddef>
23+
#include <optional>
2224
#include <stdexcept>
2325
#include <utility>
2426

@@ -157,6 +159,73 @@ std::shared_ptr<graphar::EdgeInfo> create_edge_info(
157159
return edge_info;
158160
}
159161

162+
std::shared_ptr<graphar::GraphInfo> load_graph_info(const std::string& path) {
163+
auto loaded = graphar::GraphInfo::Load(path);
164+
if (!loaded) {
165+
throw std::runtime_error(loaded.error().message());
166+
}
167+
return std::move(loaded).value();
168+
}
169+
170+
std::shared_ptr<graphar::GraphInfo> create_graph_info(
171+
const std::string& name,
172+
const std::vector<graphar::SharedVertexInfo>& vertex_infos,
173+
const std::vector<graphar::SharedEdgeInfo>& edge_infos,
174+
const rust::Vec<rust::String>& labels, const std::string& prefix,
175+
std::shared_ptr<graphar::ConstInfoVersion> version) {
176+
if (name.empty()) {
177+
throw std::runtime_error("CreateGraphInfo: name must not be empty");
178+
}
179+
180+
std::vector<std::string> label_vec;
181+
label_vec.reserve(labels.size());
182+
for (size_t i = 0; i < labels.size(); ++i) {
183+
label_vec.emplace_back(std::string(labels[i]));
184+
}
185+
186+
auto graph_info = graphar::CreateGraphInfo(name, vertex_infos, edge_infos,
187+
label_vec, prefix, version);
188+
if (graph_info == nullptr) {
189+
throw std::runtime_error("CreateGraphInfo: returned nullptr");
190+
}
191+
// if (!graph_info->IsValidated()) {
192+
// throw std::runtime_error("CreateGraphInfo: graph info is not validated");
193+
// }
194+
return graph_info;
195+
}
196+
197+
static graphar::MaybeIndex optional_to_maybe_index(std::optional<size_t> opt) {
198+
if (opt) {
199+
return graphar::MaybeIndex{true, *opt};
200+
} else {
201+
return graphar::MaybeIndex{false, 0};
202+
}
203+
}
204+
205+
graphar::MaybeIndex graph_info_vertex_info_index(
206+
const graphar::GraphInfo& graph_info, const std::string& type) {
207+
return optional_to_maybe_index(graph_info.GetVertexInfoIndex(type));
208+
}
209+
210+
graphar::MaybeIndex graph_info_edge_info_index(
211+
const graphar::GraphInfo& graph_info, const std::string& src_type,
212+
const std::string& edge_type, const std::string& dst_type) {
213+
return optional_to_maybe_index(
214+
graph_info.GetEdgeInfoIndex(src_type, edge_type, dst_type));
215+
}
216+
217+
void vertex_info_vec_push_vertex_info(
218+
std::vector<graphar::SharedVertexInfo>& vertex_infos,
219+
std::shared_ptr<graphar::VertexInfo> vertex_info) {
220+
vertex_infos.emplace_back(std::move(vertex_info));
221+
}
222+
223+
void edge_info_vec_push_edge_info(
224+
std::vector<graphar::SharedEdgeInfo>& edge_infos,
225+
std::shared_ptr<graphar::EdgeInfo> edge_info) {
226+
edge_infos.emplace_back(std::move(edge_info));
227+
}
228+
160229
void vertex_info_save(const graphar::VertexInfo& vertex_info,
161230
const std::string& path) {
162231
auto status = vertex_info.Save(path);
@@ -199,4 +268,21 @@ std::unique_ptr<std::string> edge_info_dump(
199268
}
200269
return std::make_unique<std::string>(std::move(r).value());
201270
}
271+
272+
void graph_info_save(const graphar::GraphInfo& graph_info,
273+
const std::string& path) {
274+
auto status = graph_info.Save(path);
275+
if (!status.ok()) {
276+
throw std::runtime_error(status.message());
277+
}
278+
}
279+
280+
std::unique_ptr<std::string> graph_info_dump(
281+
const graphar::GraphInfo& graph_info) {
282+
auto dumped = graph_info.Dump();
283+
if (!dumped) {
284+
throw std::runtime_error(dumped.error().message());
285+
}
286+
return std::make_unique<std::string>(std::move(dumped).value());
287+
}
202288
} // namespace graphar_rs

rust/src/info/edge_info.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ impl EdgeInfo {
201201

202202
/// Return the number of property groups.
203203
///
204+
/// The upstream C++ API uses `int`; this binding returns `usize` for a
205+
/// Rust-idiomatic count type.
204206
pub fn property_group_num(&self) -> usize {
205207
self.0.PropertyGroupNum()
206208
}

0 commit comments

Comments
 (0)