|
17 | 17 | * under the License. |
18 | 18 | */ |
19 | 19 |
|
20 | | -#include "graphar_rs.h" |
| 20 | +#include "graphar-rs/src/ffi.rs.h" |
21 | 21 |
|
| 22 | +#include <cstddef> |
| 23 | +#include <optional> |
22 | 24 | #include <stdexcept> |
23 | 25 | #include <utility> |
24 | 26 |
|
@@ -157,6 +159,73 @@ std::shared_ptr<graphar::EdgeInfo> create_edge_info( |
157 | 159 | return edge_info; |
158 | 160 | } |
159 | 161 |
|
| 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 | + |
160 | 229 | void vertex_info_save(const graphar::VertexInfo& vertex_info, |
161 | 230 | const std::string& path) { |
162 | 231 | auto status = vertex_info.Save(path); |
@@ -199,4 +268,21 @@ std::unique_ptr<std::string> edge_info_dump( |
199 | 268 | } |
200 | 269 | return std::make_unique<std::string>(std::move(r).value()); |
201 | 270 | } |
| 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 | +} |
202 | 288 | } // namespace graphar_rs |
0 commit comments