forked from apache/incubator-graphar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.h
1588 lines (1487 loc) · 54.8 KB
/
graph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Copyright 2022 Alibaba Group Holding Limited.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef GAR_GRAPH_H_
#define GAR_GRAPH_H_
#include <any>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "gar/reader/arrow_chunk_reader.h"
#include "gar/utils/reader_utils.h"
#include "gar/utils/utils.h"
// forward declarations
namespace arrow {
class ChunkedArray;
}
namespace GAR_NAMESPACE_INTERNAL {
/**
* @brief Vertex contains information of certain vertex.
*/
class Vertex {
public:
/**
* Initialize the Vertex.
*
* @param id The vertex id.
* @param readers A set of readers for reading the vertex properties.
*/
explicit Vertex(
IdType id,
std::vector<VertexPropertyArrowChunkReader>& readers); // NOLINT
/**
* @brief Get the id of the vertex.
*
* @return The id of the vertex.
*/
inline IdType id() const noexcept { return id_; }
/**
* @brief Get the property value of the vertex.
*
* @param property The property name.
* @return Result: The property value or error.
*/
template <typename T>
inline Result<T> property(const std::string& property) noexcept {
T ret;
if (properties_.find(property) == properties_.end()) {
return Status::KeyError("The property is not exist.");
}
try {
ret = std::any_cast<T>(properties_[property]);
} catch (const std::bad_any_cast& e) {
return Status::TypeError("The property type is not match.");
}
return ret;
}
private:
IdType id_;
std::map<std::string, std::any> properties_;
};
/**
* @brief Edge contains information of certain edge.
*/
class Edge {
public:
/**
* Initialize the Edge.
*
* @param adj_list_reader The reader for reading the adjList.
* @param property_readers A set of readers for reading the edge properties.
*/
explicit Edge(AdjListArrowChunkReader& adj_list_reader, // NOLINT
std::vector<AdjListPropertyArrowChunkReader>&
property_readers); // NOLINT
/**
* @brief Get source id of the edge.
*
* @return The id of the source vertex.
*/
inline IdType source() const noexcept { return src_id_; }
/**
* @brief Get destination id of the edge.
*
* @return The id of the destination vertex.
*/
inline IdType destination() const noexcept { return dst_id_; }
/**
* @brief Get the property value of the edge.
*
* @param property The property name.
* @return Result: The property value or error.
*/
template <typename T>
inline Result<T> property(const std::string& property) noexcept {
T ret;
if (properties_.find(property) == properties_.end()) {
return Status::KeyError("The property is not exist.");
}
try {
ret = std::any_cast<T>(properties_[property]);
} catch (const std::bad_any_cast& e) {
return Status::TypeError("The property type is not match.");
}
return ret;
}
private:
IdType src_id_, dst_id_;
std::map<std::string, std::any> properties_;
};
/**
* @brief The iterator for traversing a type of vertices.
*
*/
class VertexIter {
public:
/**
* Initialize the iterator.
*
* @param vertex_info The vertex info that describes the vertex type.
* @param prefix The absolute prefix.
* @param offset The current offset of the readers.
*/
explicit VertexIter(const VertexInfo& vertex_info, const std::string& prefix,
IdType offset) noexcept {
for (const auto& pg : vertex_info.GetPropertyGroups()) {
readers_.emplace_back(vertex_info, pg, prefix);
}
cur_offset_ = offset;
}
/** Copy constructor. */
VertexIter(const VertexIter& other)
: readers_(other.readers_), cur_offset_(other.cur_offset_) {}
/** Construct and return the vertex of the current offset. */
Vertex operator*() noexcept {
for (auto& reader : readers_) {
reader.seek(cur_offset_);
}
return Vertex(cur_offset_, readers_);
}
/** Get the vertex id of the current offset. */
IdType id() { return cur_offset_; }
/** Get the value for a property of the current vertex. */
template <typename T>
Result<T> property(const std::string& property) noexcept {
std::shared_ptr<arrow::ChunkedArray> column(nullptr);
for (auto& reader : readers_) {
reader.seek(cur_offset_);
GAR_ASSIGN_OR_RAISE(auto chunk_table, reader.GetChunk());
column = util::GetArrowColumnByName(chunk_table, property);
if (column != nullptr) {
break;
}
}
if (column != nullptr) {
auto array = util::GetArrowArrayByChunkIndex(column, 0);
GAR_ASSIGN_OR_RAISE(auto data, util::GetArrowArrayData(array));
return util::ValueGetter<T>::Value(data, 0);
}
return Status::KeyError("The property is not exist.");
}
/** The prefix increment operator. */
VertexIter& operator++() noexcept {
++cur_offset_;
return *this;
}
/** The postfix increment operator. */
VertexIter operator++(int) {
VertexIter ret(*this);
++cur_offset_;
return ret;
}
/** The add operator. */
VertexIter operator+(IdType offset) {
VertexIter ret(*this);
ret.cur_offset_ += offset;
return ret;
}
/** The equality operator. */
bool operator==(const VertexIter& rhs) const noexcept {
return cur_offset_ == rhs.cur_offset_;
}
/** The inequality operator. */
bool operator!=(const VertexIter& rhs) const noexcept {
return cur_offset_ != rhs.cur_offset_;
}
private:
std::vector<VertexPropertyArrowChunkReader> readers_;
IdType cur_offset_;
};
/**
* @brief VerticesCollection is designed for reading a collection of vertices.
*
*/
class VerticesCollection {
public:
/**
* @brief Initialize the VerticesCollection.
*
* @param vertex_info The vertex info that describes the vertex type.
* @param prefix The absolute prefix.
*/
explicit VerticesCollection(const VertexInfo& vertex_info,
const std::string& prefix)
: vertex_info_(vertex_info), prefix_(prefix) {
// get the vertex num
std::string base_dir;
GAR_ASSIGN_OR_RAISE_ERROR(auto fs,
FileSystemFromUriOrPath(prefix, &base_dir));
GAR_ASSIGN_OR_RAISE_ERROR(auto file_path,
vertex_info.GetVerticesNumFilePath());
std::string vertex_num_path = base_dir + file_path;
GAR_ASSIGN_OR_RAISE_ERROR(vertex_num_,
fs->ReadFileToValue<IdType>(vertex_num_path));
}
/** The iterator pointing to the first vertex. */
VertexIter begin() noexcept { return VertexIter(vertex_info_, prefix_, 0); }
/** The iterator pointing to the past-the-end element. */
VertexIter end() noexcept {
return VertexIter(vertex_info_, prefix_, vertex_num_);
}
/** The iterator pointing to the vertex with specific id. */
VertexIter find(IdType id) { return VertexIter(vertex_info_, prefix_, id); }
/** Get the number of vertices in the collection. */
size_t size() const noexcept { return vertex_num_; }
private:
VertexInfo vertex_info_;
std::string prefix_;
IdType vertex_num_;
};
/**
* @brief EdgesCollection is designed for reading a collection of edges.
*
*/
template <AdjListType adj_list_type>
class EdgesCollection;
/**
* @brief The iterator for traversing a type of edges.
*
*/
class EdgeIter {
public:
/**
* Initialize the iterator.
*
* @param edge_info The edge info that describes the edge type.
* @param prefix The absolute prefix.
* @param adj_list_type The type of adjList.
* @param global_chunk_index The global index of the current edge chunk.
* @param offset The current offset in the current edge chunk.
* @param chunk_begin The index of the first chunk.
* @param chunk_end The index of the last chunk.
* @param offset_of_chunk_begin The begin offset of the first chunk.
* @param offset_of_chunk_end The end offset of the last chunk.
* @param index_converter The converter for transforming the edge chunk
* indices.
*/
explicit EdgeIter(const EdgeInfo& edge_info, const std::string& prefix,
AdjListType adj_list_type, IdType global_chunk_index,
IdType offset, IdType chunk_begin, IdType chunk_end,
IdType offset_of_chunk_begin, IdType offset_of_chunk_end,
std::shared_ptr<util::IndexConverter> index_converter)
: adj_list_reader_(
edge_info, adj_list_type, prefix,
index_converter->GlobalChunkIndexToIndexPair(global_chunk_index)
.first),
global_chunk_index_(global_chunk_index),
cur_offset_(offset),
chunk_size_(edge_info.GetChunkSize()),
src_chunk_size_(edge_info.GetSrcChunkSize()),
dst_chunk_size_(edge_info.GetDstChunkSize()),
num_row_of_chunk_(0),
chunk_begin_(chunk_begin),
chunk_end_(chunk_end),
offset_of_chunk_begin_(offset_of_chunk_begin),
offset_of_chunk_end_(offset_of_chunk_end),
adj_list_type_(adj_list_type),
index_converter_(index_converter) {
vertex_chunk_index_ =
index_converter->GlobalChunkIndexToIndexPair(global_chunk_index).first;
GAR_ASSIGN_OR_RAISE_ERROR(auto& property_groups,
edge_info.GetPropertyGroups(adj_list_type));
for (const auto& pg : property_groups) {
property_readers_.emplace_back(edge_info, pg, adj_list_type, prefix,
vertex_chunk_index_);
}
if (adj_list_type == AdjListType::ordered_by_source ||
adj_list_type == AdjListType::ordered_by_dest) {
offset_reader_ = std::make_shared<AdjListOffsetArrowChunkReader>(
edge_info, adj_list_type, prefix);
}
}
/** Copy constructor. */
EdgeIter(const EdgeIter& other)
: adj_list_reader_(other.adj_list_reader_),
offset_reader_(other.offset_reader_),
property_readers_(other.property_readers_),
global_chunk_index_(other.global_chunk_index_),
vertex_chunk_index_(other.vertex_chunk_index_),
cur_offset_(other.cur_offset_),
chunk_size_(other.chunk_size_),
src_chunk_size_(other.src_chunk_size_),
dst_chunk_size_(other.dst_chunk_size_),
num_row_of_chunk_(other.num_row_of_chunk_),
chunk_begin_(other.chunk_begin_),
chunk_end_(other.chunk_end_),
offset_of_chunk_begin_(other.offset_of_chunk_begin_),
offset_of_chunk_end_(other.offset_of_chunk_end_),
adj_list_type_(other.adj_list_type_),
index_converter_(other.index_converter_) {}
/** Construct and return the edge of the current offset. */
Edge operator*() {
adj_list_reader_.seek(cur_offset_);
for (auto& reader : property_readers_) {
reader.seek(cur_offset_);
}
return Edge(adj_list_reader_, property_readers_);
}
/** Get the source vertex id for the current edge. */
IdType source();
/** Get the destination vertex id for the current edge. */
IdType destination();
/** Get the value of a property for the current edge. */
template <typename T>
Result<T> property(const std::string& property) noexcept {
std::shared_ptr<arrow::ChunkedArray> column(nullptr);
for (auto& reader : property_readers_) {
reader.seek(cur_offset_);
GAR_ASSIGN_OR_RAISE(auto chunk_table, reader.GetChunk());
column = util::GetArrowColumnByName(chunk_table, property);
if (column != nullptr) {
break;
}
}
if (column != nullptr) {
auto array = util::GetArrowArrayByChunkIndex(column, 0);
GAR_ASSIGN_OR_RAISE(auto data, util::GetArrowArrayData(array));
return util::ValueGetter<T>::Value(data, 0);
}
return Status::KeyError("The property is not exist.");
}
/** The prefix increment operator. */
EdgeIter& operator++() {
if (num_row_of_chunk_ == 0) {
adj_list_reader_.seek(cur_offset_);
GAR_ASSIGN_OR_RAISE_ERROR(num_row_of_chunk_,
adj_list_reader_.GetRowNumOfChunk());
}
auto st = adj_list_reader_.seek(++cur_offset_);
if (st.ok() && num_row_of_chunk_ != chunk_size_) {
// check the row offset is overflow
auto row_offset = cur_offset_ % chunk_size_;
if (row_offset >= num_row_of_chunk_) {
cur_offset_ = (cur_offset_ / chunk_size_ + 1) * chunk_size_;
adj_list_reader_.seek(cur_offset_);
st = Status::KeyError();
}
}
if (st.ok() && num_row_of_chunk_ == chunk_size_ &&
cur_offset_ % chunk_size_ == 0) {
GAR_ASSIGN_OR_RAISE_ERROR(num_row_of_chunk_,
adj_list_reader_.GetRowNumOfChunk());
++global_chunk_index_;
}
if (st.IsKeyError()) {
st = adj_list_reader_.next_chunk();
++global_chunk_index_;
++vertex_chunk_index_;
if (!st.IsOutOfRange()) {
GAR_ASSIGN_OR_RAISE_ERROR(num_row_of_chunk_,
adj_list_reader_.GetRowNumOfChunk());
for (auto& reader : property_readers_) {
reader.next_chunk();
}
}
cur_offset_ = 0;
adj_list_reader_.seek(cur_offset_);
}
return *this;
}
/** The postfix increment operator. */
EdgeIter operator++(int) {
EdgeIter ret(*this);
this->operator++();
return ret;
}
/** The copy assignment operator. */
EdgeIter operator=(const EdgeIter& other) {
adj_list_reader_ = other.adj_list_reader_;
offset_reader_ = other.offset_reader_;
property_readers_ = other.property_readers_;
global_chunk_index_ = other.global_chunk_index_;
vertex_chunk_index_ = other.vertex_chunk_index_;
cur_offset_ = other.cur_offset_;
chunk_size_ = other.chunk_size_;
src_chunk_size_ = other.src_chunk_size_;
dst_chunk_size_ = other.dst_chunk_size_;
num_row_of_chunk_ = other.num_row_of_chunk_;
chunk_begin_ = other.chunk_begin_;
chunk_end_ = other.chunk_end_;
offset_of_chunk_begin_ = other.offset_of_chunk_begin_;
offset_of_chunk_end_ = other.offset_of_chunk_end_;
adj_list_type_ = other.adj_list_type_;
index_converter_ = other.index_converter_;
return *this;
}
/** The equality operator. */
bool operator==(const EdgeIter& rhs) const noexcept {
return global_chunk_index_ == rhs.global_chunk_index_ &&
cur_offset_ == rhs.cur_offset_ &&
adj_list_type_ == rhs.adj_list_type_;
}
/** The inequality operator. */
bool operator!=(const EdgeIter& rhs) const noexcept {
return global_chunk_index_ != rhs.global_chunk_index_ ||
cur_offset_ != rhs.cur_offset_ ||
adj_list_type_ != rhs.adj_list_type_;
}
/** Get the global index of the current edge chunk. */
IdType global_chunk_index() const { return global_chunk_index_; }
/** Get the current offset in the current chunk. */
IdType cur_offset() const { return cur_offset_; }
/**
* Let the input iterator to point to the first out-going edge of the
* vertex with specific id after the current position of the iterator.
*
* @param from The input iterator.
* @param id The vertex id.
* @return If such edge is found or not.
*/
bool first_src(const EdgeIter& from, IdType id);
/**
* Let the input iterator to point to the first incoming edge of the
* vertex with specific id after the current position of the iterator.
*
* @param from The input iterator.
* @param id The vertex id.
* @return If such edge is found or not.
*/
bool first_dst(const EdgeIter& from, IdType id);
/** Let the iterator to point to the begin. */
void to_begin() {
global_chunk_index_ = chunk_begin_;
cur_offset_ = offset_of_chunk_begin_;
vertex_chunk_index_ =
index_converter_->GlobalChunkIndexToIndexPair(global_chunk_index_)
.first;
refresh();
}
/** Check if the current position is the end. */
bool is_end() const {
return global_chunk_index_ == chunk_end_ &&
cur_offset_ == offset_of_chunk_end_;
}
/** Point to the next edge with the same source, return false if not found. */
bool next_src() {
if (is_end())
return false;
IdType id = this->source();
IdType pre_vertex_chunk_index = vertex_chunk_index_;
if (adj_list_type_ == AdjListType::ordered_by_source) {
this->operator++();
if (is_end() || this->source() != id)
return false;
else
return true;
}
this->operator++();
while (!is_end()) {
if (this->source() == id) {
return true;
}
if (adj_list_type_ == AdjListType::unordered_by_source) {
if (vertex_chunk_index_ > pre_vertex_chunk_index)
return false;
}
this->operator++();
}
return false;
}
/**
* Point to the next edge with the same destination, return false if not
* found.
*/
bool next_dst() {
if (is_end())
return false;
IdType id = this->destination();
IdType pre_vertex_chunk_index = vertex_chunk_index_;
if (adj_list_type_ == AdjListType::ordered_by_dest) {
this->operator++();
if (is_end() || this->destination() != id)
return false;
else
return true;
}
this->operator++();
while (!is_end()) {
if (this->destination() == id) {
return true;
}
if (adj_list_type_ == AdjListType::unordered_by_dest) {
if (vertex_chunk_index_ > pre_vertex_chunk_index)
return false;
}
this->operator++();
}
return false;
}
/**
* Point to the next edge with the specific source, return false if not
* found.
*/
bool next_src(IdType id) {
if (is_end())
return false;
this->operator++();
return this->first_src(*this, id);
}
/**
* Point to the next edge with the specific destination, return false if
* not found.
*/
bool next_dst(IdType id) {
if (is_end())
return false;
this->operator++();
return this->first_dst(*this, id);
}
private:
// Refresh the readers to point to the current position.
void refresh() {
adj_list_reader_.seek_chunk_index(vertex_chunk_index_);
adj_list_reader_.seek(cur_offset_);
for (auto& reader : property_readers_) {
reader.seek_chunk_index(vertex_chunk_index_);
}
GAR_ASSIGN_OR_RAISE_ERROR(num_row_of_chunk_,
adj_list_reader_.GetRowNumOfChunk());
}
private:
AdjListArrowChunkReader adj_list_reader_;
std::shared_ptr<AdjListOffsetArrowChunkReader> offset_reader_;
std::vector<AdjListPropertyArrowChunkReader> property_readers_;
IdType global_chunk_index_;
IdType vertex_chunk_index_;
IdType cur_offset_;
IdType chunk_size_;
IdType src_chunk_size_;
IdType dst_chunk_size_;
IdType num_row_of_chunk_;
IdType chunk_begin_, chunk_end_;
IdType offset_of_chunk_begin_, offset_of_chunk_end_;
AdjListType adj_list_type_;
std::shared_ptr<util::IndexConverter> index_converter_;
friend class EdgesCollection<AdjListType::ordered_by_source>;
friend class EdgesCollection<AdjListType::ordered_by_dest>;
friend class EdgesCollection<AdjListType::unordered_by_source>;
friend class EdgesCollection<AdjListType::unordered_by_dest>;
};
/**
* @brief The implementation of EdgesCollection when the type of adjList is
* AdjListType::ordered_by_source.
*
*/
template <>
class EdgesCollection<AdjListType::ordered_by_source> {
public:
static const AdjListType adj_list_type_;
/**
* @brief Initialize the EdgesCollection.
*
* @param edge_info The edge info that describes the edge type.
* @param prefix The absolute prefix.
*/
EdgesCollection(const EdgeInfo& edge_info, const std::string& prefix)
: edge_info_(edge_info), prefix_(prefix), chunk_begin_(0) {
std::string base_dir;
GAR_ASSIGN_OR_RAISE_ERROR(auto fs,
FileSystemFromUriOrPath(prefix, &base_dir));
GAR_ASSIGN_OR_RAISE_ERROR(auto adj_list_path_prefix,
edge_info.GetAdjListPathPrefix(adj_list_type_));
base_dir += adj_list_path_prefix;
GAR_ASSIGN_OR_RAISE_ERROR(auto vertex_chunk_num,
fs->GetFileNumOfDir(base_dir));
std::vector<IdType> edge_chunk_nums(vertex_chunk_num, 0);
chunk_end_ = 0;
for (IdType i = 0; i < vertex_chunk_num; ++i) {
std::string chunk_dir = base_dir + "/part" + std::to_string(i);
GAR_ASSIGN_OR_RAISE_ERROR(edge_chunk_nums[i],
fs->GetFileNumOfDir(chunk_dir));
chunk_end_ += edge_chunk_nums[i];
}
index_converter_ =
std::make_shared<util::IndexConverter>(std::move(edge_chunk_nums));
offset_of_chunk_begin_ = 0;
offset_of_chunk_end_ = 0;
}
/**
* @brief Initialize the EdgesCollection with a range of chunks.
*
* @param edge_info The edge info that describes the edge type.
* @param prefix The absolute prefix.
* @param chunk_begin The global index of the begin chunk.
* @param chunk_end The global index of the end chunk.
*/
EdgesCollection(const EdgeInfo& edge_info, const std::string& prefix,
IdType chunk_begin, IdType chunk_end)
: edge_info_(edge_info),
prefix_(prefix),
chunk_begin_(chunk_begin),
chunk_end_(chunk_end) {
std::string base_dir;
GAR_ASSIGN_OR_RAISE_ERROR(auto fs,
FileSystemFromUriOrPath(prefix, &base_dir));
GAR_ASSIGN_OR_RAISE_ERROR(auto adj_list_path_prefix,
edge_info.GetAdjListPathPrefix(adj_list_type_));
base_dir += adj_list_path_prefix;
GAR_ASSIGN_OR_RAISE_ERROR(auto vertex_chunk_num,
fs->GetFileNumOfDir(base_dir));
std::vector<IdType> edge_chunk_nums(vertex_chunk_num, 0);
for (IdType i = 0; i < vertex_chunk_num; ++i) {
std::string chunk_dir = base_dir + "/part" + std::to_string(i);
GAR_ASSIGN_OR_RAISE_ERROR(edge_chunk_nums[i],
fs->GetFileNumOfDir(chunk_dir));
}
index_converter_ =
std::make_shared<util::IndexConverter>(std::move(edge_chunk_nums));
offset_of_chunk_begin_ =
index_converter_->GlobalChunkIndexToIndexPair(chunk_begin).second *
edge_info.GetChunkSize();
offset_of_chunk_end_ =
index_converter_->GlobalChunkIndexToIndexPair(chunk_end).second *
edge_info.GetChunkSize();
}
/**
* @brief Initialize the EdgesCollection for a vertex chunk.
*
* @param edge_info The edge info that describes the edge type.
* @param prefix The absolute prefix.
* @param vertex_chunk_index The index of the vertex chunk.
*/
EdgesCollection(const EdgeInfo& edge_info, const std::string& prefix,
IdType vertex_chunk_index)
: edge_info_(edge_info), prefix_(prefix) {
std::string base_dir;
GAR_ASSIGN_OR_RAISE_ERROR(auto fs,
FileSystemFromUriOrPath(prefix, &base_dir));
GAR_ASSIGN_OR_RAISE_ERROR(auto adj_list_path_prefix,
edge_info.GetAdjListPathPrefix(adj_list_type_));
base_dir += adj_list_path_prefix;
IdType vertex_chunk_num = 0;
GAR_ASSIGN_OR_RAISE_ERROR(vertex_chunk_num, fs->GetFileNumOfDir(base_dir));
std::vector<IdType> edge_chunk_nums(vertex_chunk_num, 0);
chunk_begin_ = 0;
chunk_end_ = 0;
for (IdType i = 0; i < vertex_chunk_num; ++i) {
std::string chunk_dir = base_dir + "/part" + std::to_string(i);
GAR_ASSIGN_OR_RAISE_ERROR(edge_chunk_nums[i],
fs->GetFileNumOfDir(chunk_dir));
if (i < vertex_chunk_index) {
chunk_begin_ += edge_chunk_nums[i];
}
if (i == vertex_chunk_index) {
chunk_end_ = chunk_begin_ + edge_chunk_nums[i];
}
}
index_converter_ =
std::make_shared<util::IndexConverter>(std::move(edge_chunk_nums));
offset_of_chunk_begin_ = 0;
offset_of_chunk_end_ = 0;
}
/** The iterator pointing to the first edge. */
EdgeIter begin() {
if (begin_ == nullptr) {
EdgeIter iter(edge_info_, prefix_, adj_list_type_, chunk_begin_,
offset_of_chunk_begin_, chunk_begin_, chunk_end_,
offset_of_chunk_begin_, offset_of_chunk_end_,
index_converter_);
begin_ = std::make_shared<EdgeIter>(iter);
}
return *begin_;
}
/** The iterator pointing to the past-the-end element. */
EdgeIter end() {
if (end_ == nullptr) {
EdgeIter iter(edge_info_, prefix_, adj_list_type_, chunk_end_,
offset_of_chunk_end_, chunk_begin_, chunk_end_,
offset_of_chunk_begin_, offset_of_chunk_end_,
index_converter_);
end_ = std::make_shared<EdgeIter>(iter);
}
return *end_;
}
/**
* Construct and return the iterator pointing to the first out-going edge of
* the vertex with specific id after the input iterator.
*
* @param id The vertex id.
* @param from The input iterator.
* @return The new constructed iterator.
*/
EdgeIter find_src(IdType id, const EdgeIter& from) {
auto result = utils::GetAdjListOffsetOfVertex(edge_info_, prefix_,
adj_list_type_, id);
if (!result.status().ok()) {
return this->end();
}
auto begin_offset = result.value().first;
auto end_offset = result.value().second;
if (begin_offset >= end_offset) {
return this->end();
}
auto begin_global_chunk_index =
index_converter_->IndexPairToGlobalChunkIndex(
id / edge_info_.GetSrcChunkSize(),
begin_offset / edge_info_.GetChunkSize());
auto end_global_chunk_index = index_converter_->IndexPairToGlobalChunkIndex(
id / edge_info_.GetSrcChunkSize(),
end_offset / edge_info_.GetChunkSize());
if (begin_global_chunk_index > from.global_chunk_index_) {
return EdgeIter(edge_info_, prefix_, adj_list_type_,
begin_global_chunk_index, begin_offset, chunk_begin_,
chunk_end_, offset_of_chunk_begin_, offset_of_chunk_end_,
index_converter_);
} else if (end_global_chunk_index < from.global_chunk_index_) {
return this->end();
} else {
if (begin_offset > from.cur_offset_) {
return EdgeIter(edge_info_, prefix_, adj_list_type_,
begin_global_chunk_index, begin_offset, chunk_begin_,
chunk_end_, offset_of_chunk_begin_,
offset_of_chunk_end_, index_converter_);
} else if (end_offset <= from.cur_offset_) {
return this->end();
} else {
return EdgeIter(edge_info_, prefix_, adj_list_type_,
from.global_chunk_index_, from.cur_offset_,
chunk_begin_, chunk_end_, offset_of_chunk_begin_,
offset_of_chunk_end_, index_converter_);
}
}
return this->end();
}
/**
* Construct and return the iterator pointing to the first incoming edge of
* the vertex with specific id after the input iterator.
*
* @param id The vertex id.
* @param from The input iterator.
* @return The new constructed iterator.
*/
EdgeIter find_dst(IdType id, const EdgeIter& from) {
EdgeIter iter(from);
auto end = this->end();
while (iter != end) {
auto edge = *iter;
if (edge.destination() == id) {
break;
}
++iter;
}
return iter;
}
private:
EdgeInfo edge_info_;
std::string prefix_;
IdType chunk_begin_, chunk_end_;
IdType offset_of_chunk_begin_, offset_of_chunk_end_;
std::shared_ptr<util::IndexConverter> index_converter_;
std::shared_ptr<EdgeIter> begin_, end_;
};
/**
* @brief The implementation of EdgesCollection when the type of adjList is
* AdjListType::ordered_by_dest.
*
*/
template <>
class EdgesCollection<AdjListType::ordered_by_dest> {
public:
static const AdjListType adj_list_type_;
/**
* @brief Initialize the EdgesCollection.
*
* @param edge_info The edge info that describes the edge type.
* @param prefix The absolute prefix.
*/
EdgesCollection(const EdgeInfo& edge_info, const std::string& prefix)
: edge_info_(edge_info), prefix_(prefix), chunk_begin_(0) {
std::string base_dir;
GAR_ASSIGN_OR_RAISE_ERROR(auto fs,
FileSystemFromUriOrPath(prefix, &base_dir));
GAR_ASSIGN_OR_RAISE_ERROR(auto adj_list_path_prefix,
edge_info.GetAdjListPathPrefix(adj_list_type_));
base_dir += adj_list_path_prefix;
GAR_ASSIGN_OR_RAISE_ERROR(auto vertex_chunk_num,
fs->GetFileNumOfDir(base_dir));
std::vector<IdType> edge_chunk_nums(vertex_chunk_num, 0);
chunk_end_ = 0;
for (IdType i = 0; i < vertex_chunk_num; ++i) {
std::string chunk_dir = base_dir + "/part" + std::to_string(i);
GAR_ASSIGN_OR_RAISE_ERROR(edge_chunk_nums[i],
fs->GetFileNumOfDir(chunk_dir));
chunk_end_ += edge_chunk_nums[i];
}
index_converter_ =
std::make_shared<util::IndexConverter>(std::move(edge_chunk_nums));
offset_of_chunk_begin_ = 0;
offset_of_chunk_end_ = 0;
}
/**
* @brief Initialize the EdgesCollection with a range of chunks.
*
* @param edge_info The edge info that describes the edge type.
* @param prefix The absolute prefix.
* @param chunk_begin The global index of the begin chunk.
* @param chunk_end The global index of the end chunk.
*/
EdgesCollection(const EdgeInfo& edge_info, const std::string& prefix,
IdType chunk_begin, IdType chunk_end)
: edge_info_(edge_info),
prefix_(prefix),
chunk_begin_(chunk_begin),
chunk_end_(chunk_end) {
std::string base_dir;
GAR_ASSIGN_OR_RAISE_ERROR(auto fs,
FileSystemFromUriOrPath(prefix, &base_dir));
GAR_ASSIGN_OR_RAISE_ERROR(auto adj_list_path_prefix,
edge_info.GetAdjListPathPrefix(adj_list_type_));
base_dir += adj_list_path_prefix;
GAR_ASSIGN_OR_RAISE_ERROR(auto vertex_chunk_num,
fs->GetFileNumOfDir(base_dir));
std::vector<IdType> edge_chunk_nums(vertex_chunk_num, 0);
for (IdType i = 0; i < vertex_chunk_num; ++i) {
std::string chunk_dir = base_dir + "/part" + std::to_string(i);
GAR_ASSIGN_OR_RAISE_ERROR(edge_chunk_nums[i],
fs->GetFileNumOfDir(chunk_dir));
}
index_converter_ =
std::make_shared<util::IndexConverter>(std::move(edge_chunk_nums));
offset_of_chunk_begin_ =
index_converter_->GlobalChunkIndexToIndexPair(chunk_begin).second *
edge_info.GetChunkSize();
offset_of_chunk_end_ =
index_converter_->GlobalChunkIndexToIndexPair(chunk_end).second *
edge_info.GetChunkSize();
}
/**
* @brief Initialize the EdgesCollection for a vertex chunk.
*
* @param edge_info The edge info that describes the edge type.
* @param prefix The absolute prefix.
* @param vertex_chunk_index The index of the vertex chunk.
*/
EdgesCollection(const EdgeInfo& edge_info, const std::string& prefix,
IdType vertex_chunk_index)
: edge_info_(edge_info), prefix_(prefix) {
std::string base_dir;
GAR_ASSIGN_OR_RAISE_ERROR(auto fs,
FileSystemFromUriOrPath(prefix, &base_dir));
GAR_ASSIGN_OR_RAISE_ERROR(auto adj_list_path_prefix,
edge_info.GetAdjListPathPrefix(adj_list_type_));
base_dir += adj_list_path_prefix;
IdType vertex_chunk_num = 0;
GAR_ASSIGN_OR_RAISE_ERROR(vertex_chunk_num, fs->GetFileNumOfDir(base_dir));
std::vector<IdType> edge_chunk_nums(vertex_chunk_num, 0);
chunk_begin_ = 0;
chunk_end_ = 0;
for (IdType i = 0; i < vertex_chunk_num; ++i) {
std::string chunk_dir = base_dir + "/part" + std::to_string(i);
GAR_ASSIGN_OR_RAISE_ERROR(edge_chunk_nums[i],
fs->GetFileNumOfDir(chunk_dir));
if (i < vertex_chunk_index) {
chunk_begin_ += edge_chunk_nums[i];
}
if (i == vertex_chunk_index) {
chunk_end_ = chunk_begin_ + edge_chunk_nums[i];
}
}
index_converter_ =
std::make_shared<util::IndexConverter>(std::move(edge_chunk_nums));
offset_of_chunk_begin_ = 0;
offset_of_chunk_end_ = 0;
}
/** The iterator pointing to the first edge. */
EdgeIter begin() {
if (begin_ == nullptr) {
EdgeIter iter(edge_info_, prefix_, adj_list_type_, chunk_begin_,
offset_of_chunk_begin_, chunk_begin_, chunk_end_,
offset_of_chunk_begin_, offset_of_chunk_end_,
index_converter_);
begin_ = std::make_shared<EdgeIter>(iter);
}
return *begin_;
}
/** The iterator pointing to the past-the-end element. */
EdgeIter end() {
if (end_ == nullptr) {
EdgeIter iter(edge_info_, prefix_, adj_list_type_, chunk_end_,
offset_of_chunk_end_, chunk_begin_, chunk_end_,
offset_of_chunk_begin_, offset_of_chunk_end_,
index_converter_);
end_ = std::make_shared<EdgeIter>(iter);
}
return *end_;
}
/**
* Construct and return the iterator pointing to the first out-going edge of
* the vertex with specific id after the input iterator.
*
* @param id The vertex id.
* @param from The input iterator.
* @return The new constructed iterator.
*/
EdgeIter find_src(IdType id, const EdgeIter& from) {