-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathPropertyGraph.cpp
More file actions
1220 lines (1064 loc) · 42.6 KB
/
PropertyGraph.cpp
File metadata and controls
1220 lines (1064 loc) · 42.6 KB
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
#include "katana/PropertyGraph.h"
#include <sys/mman.h>
#include <utility>
#include "katana/ArrowInterchange.h"
#include "katana/Iterators.h"
#include "katana/Logging.h"
#include "katana/Loops.h"
#include "katana/NUMAArray.h"
#include "katana/PerThreadStorage.h"
#include "katana/Platform.h"
#include "katana/Properties.h"
#include "katana/Result.h"
#include "tsuba/Errors.h"
#include "tsuba/FileFrame.h"
#include "tsuba/RDG.h"
#include "tsuba/tsuba.h"
namespace {
constexpr uint64_t
GetGraphSize(uint64_t num_nodes, uint64_t num_edges) {
/// version, sizeof_edge_data, num_nodes, num_edges
constexpr int mandatory_fields = 4;
return (mandatory_fields + num_nodes) * sizeof(uint64_t) +
(num_edges * sizeof(uint32_t));
}
[[maybe_unused]] bool
CheckTopology(
const uint64_t* out_indices, const uint64_t num_nodes,
const uint32_t* out_dests, const uint64_t num_edges) {
bool has_bad_adj = false;
katana::do_all(
katana::iterate(uint64_t{0}, num_nodes),
[&](auto n) {
if (out_indices[n] > num_edges) {
has_bad_adj = true;
}
},
katana::no_stats());
bool has_bad_dest = false;
katana::do_all(
katana::iterate(uint64_t{0}, num_edges),
[&](auto e) {
if (out_dests[e] >= num_nodes) {
has_bad_dest = true;
}
},
katana::no_stats());
return !has_bad_adj && !has_bad_dest;
}
/// MapTopology takes a file buffer of a topology file and extracts the
/// topology files.
///
/// Format of a topology file (borrowed from the original FileGraph.cpp:
///
/// uint64_t version: 1
/// uint64_t sizeof_edge_data: size of edge data element
/// uint64_t num_nodes: number of nodes
/// uint64_t num_edges: number of edges
/// uint64_t[num_nodes] out_indices: start and end of the edges for a node
/// uint32_t[num_edges] out_dests: destinations (node indexes) of each edge
/// uint32_t padding if num_edges is odd
/// void*[num_edges] edge_data: edge data
///
/// Since property graphs store their edge data separately, we will
/// ignore the size_of_edge_data (data[1]).
katana::Result<katana::GraphTopology>
MapTopology(const tsuba::FileView& file_view) {
const auto* data = file_view.ptr<uint64_t>();
if (file_view.size() < 4) {
return katana::ErrorCode::InvalidArgument;
}
if (data[0] != 1) {
return katana::ErrorCode::InvalidArgument;
}
const uint64_t num_nodes = data[2];
const uint64_t num_edges = data[3];
uint64_t expected_size = GetGraphSize(num_nodes, num_edges);
if (file_view.size() < expected_size) {
return KATANA_ERROR(
katana::ErrorCode::InvalidArgument, "file_view size: {} expected {}",
file_view.size(), expected_size);
}
const uint64_t* out_indices = &data[4];
const uint32_t* out_dests =
reinterpret_cast<const uint32_t*>(out_indices + num_nodes);
KATANA_LOG_DEBUG_ASSERT(
CheckTopology(out_indices, num_nodes, out_dests, num_edges));
return katana::GraphTopology(out_indices, num_nodes, out_dests, num_edges);
}
katana::Result<std::unique_ptr<tsuba::FileFrame>>
WriteTopology(const katana::GraphTopology& topology) {
auto ff = std::make_unique<tsuba::FileFrame>();
if (auto res = ff->Init(); !res) {
return res.error();
}
const uint64_t num_nodes = topology.num_nodes();
const uint64_t num_edges = topology.num_edges();
uint64_t data[4] = {1, 0, num_nodes, num_edges};
arrow::Status aro_sts = ff->Write(&data, 4 * sizeof(uint64_t));
if (!aro_sts.ok()) {
return tsuba::ArrowToTsuba(aro_sts.code());
}
if (num_nodes) {
const auto* raw = topology.adj_data();
static_assert(std::is_same_v<std::decay_t<decltype(*raw)>, uint64_t>);
auto buf = arrow::Buffer::Wrap(raw, num_nodes);
aro_sts = ff->Write(buf);
if (!aro_sts.ok()) {
return tsuba::ArrowToTsuba(aro_sts.code());
}
}
if (num_edges) {
const auto* raw = topology.dest_data();
static_assert(std::is_same_v<std::decay_t<decltype(*raw)>, uint32_t>);
auto buf = arrow::Buffer::Wrap(raw, num_edges);
aro_sts = ff->Write(buf);
if (!aro_sts.ok()) {
return tsuba::ArrowToTsuba(aro_sts.code());
}
}
return std::unique_ptr<tsuba::FileFrame>(std::move(ff));
}
template <typename ArrowType>
struct PropertyColumn {
int field_index;
std::shared_ptr<ArrowType> array;
PropertyColumn(int i, std::shared_ptr<ArrowType>& a)
: field_index(i), array(a) {}
};
/// Assumes all boolean or uint8 properties are types
katana::Result<katana::NUMAArray<katana::EntityTypeID>>
GetEntityTypeIDsFromProperties(
const std::shared_ptr<arrow::Table>& properties,
katana::EntityTypeManager* entity_type_manager) {
// throw an error if each column/property has more than 1 chunk
for (int i = 0, n = properties->num_columns(); i < n; i++) {
std::shared_ptr<arrow::ChunkedArray> property = properties->column(i);
if (property->num_chunks() != 1) {
return KATANA_ERROR(
katana::ErrorCode::NotImplemented,
"property {} has {} chunks (1 chunk expected)",
properties->schema()->field(i)->name(), property->num_chunks());
}
}
// collect the list of types
std::vector<int> type_field_indices;
using BoolPropertyColumn = PropertyColumn<arrow::BooleanArray>;
std::vector<BoolPropertyColumn> bool_properties;
using UInt8PropertyColumn = PropertyColumn<arrow::UInt8Array>;
std::vector<UInt8PropertyColumn> uint8_properties;
const std::shared_ptr<arrow::Schema>& schema = properties->schema();
KATANA_LOG_DEBUG_ASSERT(schema->num_fields() == properties->num_columns());
for (int i = 0, n = schema->num_fields(); i < n; i++) {
const std::shared_ptr<arrow::Field>& current_field = schema->field(i);
// a bool or uint8 property is (always) considered a type
// TODO(roshan) make this customizable by the user
if (current_field->type()->Equals(arrow::boolean())) {
type_field_indices.push_back(i);
std::shared_ptr<arrow::Array> property = properties->column(i)->chunk(0);
auto bool_property =
std::static_pointer_cast<arrow::BooleanArray>(property);
bool_properties.emplace_back(i, bool_property);
} else if (current_field->type()->Equals(arrow::uint8())) {
type_field_indices.push_back(i);
std::shared_ptr<arrow::Array> property = properties->column(i)->chunk(0);
auto uint8_property =
std::static_pointer_cast<arrow::UInt8Array>(property);
uint8_properties.emplace_back(i, uint8_property);
}
}
// assign a new ID to each type
// NB: cannot use unordered_map without defining a hash function for vectors;
// performance is not affected here because the map is very small (<=256)
std::map<katana::gstl::Vector<int>, katana::EntityTypeID>
type_field_indices_to_id;
for (int i : type_field_indices) {
const std::shared_ptr<arrow::Field>& current_field = schema->field(i);
const std::string& field_name = current_field->name();
katana::EntityTypeID new_entity_type_id =
entity_type_manager->AddAtomicEntityType(field_name);
katana::gstl::Vector<int> field_indices = {i};
type_field_indices_to_id.emplace(
std::make_pair(field_indices, new_entity_type_id));
}
// collect the list of unique combination of types
using FieldEntityType = katana::gstl::Vector<int>;
// NB: cannot use unordered_set without defining a hash function for vectors;
// performance is not affected here because the set is very small (<=256)
using FieldEntityTypeSet = katana::gstl::Set<FieldEntityType>;
FieldEntityTypeSet type_combinations;
katana::PerThreadStorage<FieldEntityTypeSet> type_combinations_pts;
katana::do_all(
katana::iterate(int64_t{0}, properties->num_rows()), [&](int64_t row) {
FieldEntityType field_indices;
for (auto bool_property : bool_properties) {
if (bool_property.array->IsValid(row) &&
bool_property.array->Value(row)) {
field_indices.emplace_back(bool_property.field_index);
}
}
for (auto uint8_property : uint8_properties) {
if (uint8_property.array->IsValid(row) &&
uint8_property.array->Value(row)) {
field_indices.emplace_back(uint8_property.field_index);
}
}
if (field_indices.size() > 1) {
FieldEntityTypeSet& local_type_combinations =
*type_combinations_pts.getLocal();
local_type_combinations.emplace(field_indices);
}
});
for (unsigned t = 0, n = katana::activeThreads; t < n; t++) {
FieldEntityTypeSet& remote_type_combinations =
*type_combinations_pts.getRemote(t);
for (auto& type_combination : remote_type_combinations) {
type_combinations.emplace(type_combination);
}
}
// deallocate PerThreadStorage in parallel
katana::on_each([&](unsigned, unsigned) {
FieldEntityTypeSet& local_type_combinations =
*type_combinations_pts.getLocal();
local_type_combinations = FieldEntityTypeSet();
});
// assign a new ID to each unique combination of types
for (const FieldEntityType& field_indices : type_combinations) {
std::vector<std::string> field_names;
for (int i : field_indices) {
const std::shared_ptr<arrow::Field>& current_field = schema->field(i);
const std::string& field_name = current_field->name();
field_names.emplace_back(field_name);
}
katana::EntityTypeID new_entity_type_id =
entity_type_manager->AddNonAtomicEntityType(field_names);
type_field_indices_to_id.emplace(
std::make_pair(field_indices, new_entity_type_id));
}
// assert that all type IDs (including kUnknownEntityType) and
// 1 special type ID (kInvalidEntityType)
// can be stored in 8 bits
if (entity_type_manager->GetNumEntityTypes() >
(std::numeric_limits<katana::EntityTypeID>::max() - size_t{1})) {
return KATANA_ERROR(
katana::ErrorCode::NotImplemented,
"number of unique combination of types is {} "
"but only up to {} is supported currently",
// exclude kUnknownEntityType
entity_type_manager->GetNumEntityTypes() - 1,
// exclude kUnknownEntityType and kInvalidEntityType
std::numeric_limits<katana::EntityTypeID>::max() - 2);
}
// allocate type IDs array
katana::NUMAArray<katana::EntityTypeID> entity_type_ids;
int64_t num_rows = properties->num_rows();
entity_type_ids.allocateInterleaved(num_rows);
// assign the type ID for each row
katana::do_all(katana::iterate(int64_t{0}, num_rows), [&](int64_t row) {
FieldEntityType field_indices;
for (auto bool_property : bool_properties) {
if (bool_property.array->IsValid(row) &&
bool_property.array->Value(row)) {
field_indices.emplace_back(bool_property.field_index);
}
}
for (auto uint8_property : uint8_properties) {
if (uint8_property.array->IsValid(row) &&
uint8_property.array->Value(row)) {
field_indices.emplace_back(uint8_property.field_index);
}
}
if (field_indices.empty()) {
entity_type_ids[row] = katana::kUnknownEntityType;
} else {
katana::EntityTypeID entity_type_id =
type_field_indices_to_id.at(field_indices);
entity_type_ids[row] = entity_type_id;
}
});
return katana::Result<katana::NUMAArray<katana::EntityTypeID>>(
std::move(entity_type_ids));
}
katana::NUMAArray<katana::EntityTypeID>
GetUnknownEntityTypeIDs(uint64_t num_rows) {
katana::NUMAArray<katana::EntityTypeID> entity_type_ids;
entity_type_ids.allocateInterleaved(num_rows);
katana::do_all(katana::iterate(uint64_t{0}, num_rows), [&](uint64_t row) {
entity_type_ids[row] = katana::kUnknownEntityType;
});
return entity_type_ids;
}
} // namespace
katana::Result<std::unique_ptr<katana::PropertyGraph>>
katana::PropertyGraph::Make(
std::unique_ptr<tsuba::RDGFile> rdg_file, tsuba::RDG&& rdg) {
auto topo_result = MapTopology(rdg.topology_file_storage());
if (!topo_result) {
return topo_result.error();
}
auto property_graph = std::make_unique<PropertyGraph>(
std::move(rdg_file), std::move(rdg), std::move(topo_result.value()));
property_graph->recreate_node_property_indexes();
property_graph->recreate_edge_property_indexes();
return property_graph;
}
katana::Result<std::unique_ptr<katana::PropertyGraph>>
MakePropertyGraph(
std::unique_ptr<tsuba::RDGFile> rdg_file,
const tsuba::RDGLoadOptions& opts) {
auto rdg_result = tsuba::RDG::Make(*rdg_file, opts);
if (!rdg_result) {
return rdg_result.error();
}
return katana::PropertyGraph::Make(
std::move(rdg_file), std::move(rdg_result.value()));
}
katana::Result<std::unique_ptr<katana::PropertyGraph>>
katana::PropertyGraph::Make(
const std::string& rdg_name, const tsuba::RDGLoadOptions& opts) {
auto handle = tsuba::Open(rdg_name, tsuba::kReadWrite);
if (!handle) {
return handle.error();
}
return MakePropertyGraph(
std::make_unique<tsuba::RDGFile>(handle.value()), opts);
}
katana::Result<std::unique_ptr<katana::PropertyGraph>>
katana::PropertyGraph::Make(katana::GraphTopology&& topo_to_assign) {
return std::make_unique<katana::PropertyGraph>(std::move(topo_to_assign));
}
katana::Result<std::unique_ptr<katana::PropertyGraph>>
katana::PropertyGraph::Make(
katana::GraphTopology&& topo_to_assign,
NUMAArray<EntityTypeID>&& node_entity_type_id,
NUMAArray<EntityTypeID>&& edge_entity_type_id,
EntityTypeManager&& node_type_manager,
EntityTypeManager&& edge_type_manager) {
return std::make_unique<katana::PropertyGraph>(
std::move(topo_to_assign), std::move(node_entity_type_id),
std::move(edge_entity_type_id), std::move(node_type_manager),
std::move(edge_type_manager));
}
katana::Result<std::unique_ptr<katana::PropertyGraph>>
katana::PropertyGraph::Copy() const {
return Copy(
loaded_node_schema()->field_names(), loaded_edge_schema()->field_names());
}
katana::Result<std::unique_ptr<katana::PropertyGraph>>
katana::PropertyGraph::Copy(
const std::vector<std::string>& node_properties,
const std::vector<std::string>& edge_properties) const {
// TODO(gill): This should copy the RDG in memory without reloading from storage.
tsuba::RDGLoadOptions opts;
opts.partition_id_to_load = partition_id();
opts.node_properties = node_properties;
opts.edge_properties = edge_properties;
return Make(rdg_dir(), opts);
}
katana::Result<void>
katana::PropertyGraph::Validate() {
// TODO (thunt) check that arrow table sizes match topology
// if (topology_.out_dests &&
// topology_.out_dests->length() != table->num_rows()) {
// return ErrorCode::InvalidArgument;
//}
// if (topology_.out_indices &&
// topology_.out_indices->length() != table->num_rows()) {
// return ErrorCode::InvalidArgument;
//}
uint64_t num_node_rows = static_cast<uint64_t>(node_properties()->num_rows());
if (num_node_rows == 0) {
if ((node_properties()->num_columns() != 0) && (num_nodes() != 0)) {
return KATANA_ERROR(
ErrorCode::AssertionFailed,
"number of rows in node properties is 0 but "
"the number of node properties is {} and the number of nodes is {}",
node_properties()->num_columns(), num_nodes());
}
} else if (num_node_rows != num_nodes()) {
return KATANA_ERROR(
ErrorCode::AssertionFailed,
"number of rows in node properties {} differs "
"from the number of nodes {}",
node_properties()->num_rows(), num_nodes());
}
uint64_t num_edge_rows = static_cast<uint64_t>(edge_properties()->num_rows());
if (num_edge_rows == 0) {
if ((edge_properties()->num_columns() != 0) && (num_edges() != 0)) {
return KATANA_ERROR(
ErrorCode::AssertionFailed,
"number of rows in edge properties is 0 but "
"the number of edge properties is {} and the number of edges is {}",
edge_properties()->num_columns(), num_edges());
}
} else if (num_edge_rows != num_edges()) {
return KATANA_ERROR(
ErrorCode::AssertionFailed,
"number of rows in edge properties {} differs "
"from the number of edges {}",
edge_properties()->num_rows(), num_edges());
}
return katana::ResultSuccess();
}
katana::Result<void>
katana::PropertyGraph::ConstructEntityTypeIDs() {
node_entity_type_manager_.Reset();
uint64_t num_node_rows = static_cast<uint64_t>(node_properties()->num_rows());
if (num_node_rows == 0) {
node_entity_type_id_ = GetUnknownEntityTypeIDs(num_nodes());
} else {
auto node_types_res = GetEntityTypeIDsFromProperties(
node_properties(), &node_entity_type_manager_);
if (!node_types_res) {
return node_types_res.error().WithContext("node properties");
}
node_entity_type_id_ = std::move(node_types_res.value());
}
edge_entity_type_manager_.Reset();
uint64_t num_edge_rows = static_cast<uint64_t>(edge_properties()->num_rows());
if (num_edge_rows == 0) {
edge_entity_type_id_ = GetUnknownEntityTypeIDs(num_edges());
} else {
auto edge_types_res = GetEntityTypeIDsFromProperties(
edge_properties(), &edge_entity_type_manager_);
if (!edge_types_res) {
return edge_types_res.error().WithContext("edge properties");
}
edge_entity_type_id_ = std::move(edge_types_res.value());
}
return katana::ResultSuccess();
}
katana::Result<void>
katana::PropertyGraph::DoWrite(
tsuba::RDGHandle handle, const std::string& command_line,
tsuba::RDG::RDGVersioningPolicy versioning_action) {
if (!rdg_.topology_file_storage().Valid()) {
auto result = WriteTopology(topology());
if (!result) {
return result.error();
}
return rdg_.Store(
handle, command_line, versioning_action, std::move(result.value()));
}
return rdg_.Store(handle, command_line, versioning_action);
}
katana::Result<void>
katana::PropertyGraph::ConductWriteOp(
const std::string& uri, const std::string& command_line,
tsuba::RDG::RDGVersioningPolicy versioning_action) {
auto open_res = tsuba::Open(uri, tsuba::kReadWrite);
if (!open_res) {
return open_res.error();
}
auto new_file = std::make_unique<tsuba::RDGFile>(open_res.value());
if (auto res = DoWrite(*new_file, command_line, versioning_action); !res) {
return res.error();
}
file_ = std::move(new_file);
return katana::ResultSuccess();
}
katana::Result<void>
katana::PropertyGraph::WriteView(
const std::string& uri, const std::string& command_line) {
return ConductWriteOp(
uri, command_line, tsuba::RDG::RDGVersioningPolicy::RetainVersion);
}
katana::Result<void>
katana::PropertyGraph::WriteGraph(
const std::string& uri, const std::string& command_line) {
return ConductWriteOp(
uri, command_line, tsuba::RDG::RDGVersioningPolicy::IncrementVersion);
}
katana::Result<void>
katana::PropertyGraph::Commit(const std::string& command_line) {
if (file_ == nullptr) {
if (rdg_.rdg_dir().empty()) {
return KATANA_ERROR(
ErrorCode::InvalidArgument, "RDG commit but rdg_dir_ is empty");
}
return WriteGraph(rdg_.rdg_dir().string(), command_line);
}
return DoWrite(
*file_, command_line, tsuba::RDG::RDGVersioningPolicy::IncrementVersion);
}
katana::Result<void>
katana::PropertyGraph::WriteView(const std::string& command_line) {
// WriteView occurs once, and only before any Commit/Write operation
KATANA_LOG_DEBUG_ASSERT(file_ == nullptr);
return WriteView(rdg_.rdg_dir().string(), command_line);
}
bool
katana::PropertyGraph::Equals(const PropertyGraph* other) const {
if (!topology().Equals(other->topology())) {
return false;
}
const auto& node_props = rdg_.node_properties();
const auto& edge_props = rdg_.edge_properties();
const auto& other_node_props = other->node_properties();
const auto& other_edge_props = other->edge_properties();
if (node_props->num_columns() != other_node_props->num_columns()) {
return false;
}
if (edge_props->num_columns() != other_edge_props->num_columns()) {
return false;
}
for (const auto& prop_name : node_props->ColumnNames()) {
if (!node_props->GetColumnByName(prop_name)->Equals(
other_node_props->GetColumnByName(prop_name))) {
return false;
}
}
for (const auto& prop_name : edge_props->ColumnNames()) {
if (!edge_props->GetColumnByName(prop_name)->Equals(
other_edge_props->GetColumnByName(prop_name))) {
return false;
}
}
return true;
}
std::string
katana::PropertyGraph::ReportDiff(const PropertyGraph* other) const {
fmt::memory_buffer buf;
if (!topology().Equals(other->topology())) {
fmt::format_to(
std::back_inserter(buf),
"Topologies differ nodes/edges {}/{} vs. {}/{}\n",
topology().num_nodes(), topology().num_edges(),
other->topology().num_nodes(), other->topology().num_edges());
} else {
fmt::format_to(std::back_inserter(buf), "Topologies match!\n");
}
const auto& node_props = rdg_.node_properties();
const auto& edge_props = rdg_.edge_properties();
const auto& other_node_props = other->node_properties();
const auto& other_edge_props = other->edge_properties();
if (node_props->num_columns() != other_node_props->num_columns()) {
fmt::format_to(
std::back_inserter(buf), "Number of node properties differ {} vs. {}\n",
node_props->num_columns(), other_node_props->num_columns());
}
if (edge_props->num_columns() != other_edge_props->num_columns()) {
fmt::format_to(
std::back_inserter(buf), "Number of edge properties differ {} vs. {}\n",
edge_props->num_columns(), other_edge_props->num_columns());
}
for (const auto& prop_name : node_props->ColumnNames()) {
auto other_col = other_node_props->GetColumnByName(prop_name);
auto my_col = node_props->GetColumnByName(prop_name);
if (other_col == nullptr) {
fmt::format_to(
std::back_inserter(buf), "Only first has node property {}\n",
prop_name);
} else if (!my_col->Equals(other_col)) {
fmt::format_to(
std::back_inserter(buf), "Node property {:15} {:12} differs\n",
prop_name, fmt::format("({})", my_col->type()->name()));
if (my_col->length() != other_col->length()) {
fmt::format_to(
std::back_inserter(buf), " size {}/{}\n", my_col->length(),
other_col->length());
} else {
DiffFormatTo(buf, my_col, other_col);
}
} else {
fmt::format_to(
std::back_inserter(buf), "Node property {:15} {:12} matches!\n",
prop_name, fmt::format("({})", my_col->type()->name()));
}
}
for (const auto& prop_name : edge_props->ColumnNames()) {
auto other_col = other_edge_props->GetColumnByName(prop_name);
auto my_col = edge_props->GetColumnByName(prop_name);
if (other_col == nullptr) {
fmt::format_to(
std::back_inserter(buf), "Only first has edge property {}\n",
prop_name);
} else if (!edge_props->GetColumnByName(prop_name)->Equals(
other_edge_props->GetColumnByName(prop_name))) {
fmt::format_to(
std::back_inserter(buf), "Edge property {:15} {:12} differs\n",
prop_name, fmt::format("({})", my_col->type()->name()));
if (my_col->length() != other_col->length()) {
fmt::format_to(
std::back_inserter(buf), " size {}/{}\n", my_col->length(),
other_col->length());
} else {
DiffFormatTo(buf, my_col, other_col);
}
} else {
fmt::format_to(
std::back_inserter(buf), "Edge property {:15} {:12} matches!\n",
prop_name, fmt::format("({})", my_col->type()->name()));
}
}
return std::string(buf.begin(), buf.end());
}
katana::Result<void>
katana::PropertyGraph::Write(
const std::string& rdg_name, const std::string& command_line) {
if (auto res = tsuba::Create(rdg_name); !res) {
return res.error();
}
return WriteGraph(rdg_name, command_line);
}
katana::Result<void>
katana::PropertyGraph::AddNodeProperties(
const std::shared_ptr<arrow::Table>& props) {
if (props->num_columns() == 0) {
KATANA_LOG_DEBUG("adding empty node prop table");
return ResultSuccess();
}
if (topology().num_nodes() != static_cast<uint64_t>(props->num_rows())) {
return KATANA_ERROR(
ErrorCode::InvalidArgument, "expected {} rows found {} instead",
topology().num_nodes(), props->num_rows());
}
return rdg_.AddNodeProperties(props);
}
katana::Result<void>
katana::PropertyGraph::UpsertNodeProperties(
const std::shared_ptr<arrow::Table>& props) {
if (props->num_columns() == 0) {
KATANA_LOG_DEBUG("upsert empty node prop table");
return ResultSuccess();
}
if (topology().num_nodes() != static_cast<uint64_t>(props->num_rows())) {
return KATANA_ERROR(
ErrorCode::InvalidArgument, "expected {} rows found {} instead",
topology().num_nodes(), props->num_rows());
}
return rdg_.UpsertNodeProperties(props);
}
katana::Result<void>
katana::PropertyGraph::RemoveNodeProperty(int i) {
return rdg_.RemoveNodeProperty(i);
}
katana::Result<void>
katana::PropertyGraph::RemoveNodeProperty(const std::string& prop_name) {
auto col_names = node_properties()->ColumnNames();
auto pos = std::find(col_names.cbegin(), col_names.cend(), prop_name);
if (pos != col_names.cend()) {
return rdg_.RemoveNodeProperty(std::distance(col_names.cbegin(), pos));
}
return katana::ErrorCode::PropertyNotFound;
}
katana::Result<void>
katana::PropertyGraph::UnloadNodeProperty(int i) {
return rdg_.UnloadNodeProperty(i);
}
katana::Result<void>
katana::PropertyGraph::LoadNodeProperty(const std::string& name, int i) {
return rdg_.LoadNodeProperty(name, i);
}
/// Load a node property by name if it is absent and append its column to
/// the table do nothing otherwise
katana::Result<void>
katana::PropertyGraph::EnsureNodePropertyLoaded(const std::string& name) {
if (HasNodeProperty(name)) {
return katana::ResultSuccess();
}
return LoadNodeProperty(name);
}
std::vector<std::string>
katana::PropertyGraph::ListNodeProperties() const {
return rdg_.ListNodeProperties();
}
std::vector<std::string>
katana::PropertyGraph::ListEdgeProperties() const {
return rdg_.ListEdgeProperties();
}
katana::Result<void>
katana::PropertyGraph::UnloadNodeProperty(const std::string& prop_name) {
auto col_names = node_properties()->ColumnNames();
auto pos = std::find(col_names.cbegin(), col_names.cend(), prop_name);
if (pos != col_names.cend()) {
return rdg_.UnloadNodeProperty(std::distance(col_names.cbegin(), pos));
}
return katana::ErrorCode::PropertyNotFound;
}
katana::Result<void>
katana::PropertyGraph::AddEdgeProperties(
const std::shared_ptr<arrow::Table>& props) {
if (props->num_columns() == 0) {
KATANA_LOG_DEBUG("adding empty edge prop table");
return ResultSuccess();
}
if (topology().num_edges() != static_cast<uint64_t>(props->num_rows())) {
return KATANA_ERROR(
ErrorCode::InvalidArgument, "expected {} rows found {} instead",
topology().num_edges(), props->num_rows());
}
return rdg_.AddEdgeProperties(props);
}
katana::Result<void>
katana::PropertyGraph::UpsertEdgeProperties(
const std::shared_ptr<arrow::Table>& props) {
if (props->num_columns() == 0) {
KATANA_LOG_DEBUG("upsert empty edge prop table");
return ResultSuccess();
}
if (topology().num_edges() != static_cast<uint64_t>(props->num_rows())) {
return KATANA_ERROR(
ErrorCode::InvalidArgument, "expected {} rows found {} instead",
topology().num_edges(), props->num_rows());
}
return rdg_.UpsertEdgeProperties(props);
}
katana::Result<void>
katana::PropertyGraph::RemoveEdgeProperty(int i) {
return rdg_.RemoveEdgeProperty(i);
}
katana::Result<void>
katana::PropertyGraph::RemoveEdgeProperty(const std::string& prop_name) {
auto col_names = edge_properties()->ColumnNames();
auto pos = std::find(col_names.cbegin(), col_names.cend(), prop_name);
if (pos != col_names.cend()) {
return rdg_.RemoveEdgeProperty(std::distance(col_names.cbegin(), pos));
}
return katana::ErrorCode::PropertyNotFound;
}
katana::Result<void>
katana::PropertyGraph::UnloadEdgeProperty(int i) {
return rdg_.UnloadEdgeProperty(i);
}
katana::Result<void>
katana::PropertyGraph::LoadEdgeProperty(const std::string& name, int i) {
return rdg_.LoadEdgeProperty(name, i);
}
/// Load an edge property by name if it is absent and append its column to
/// the table do nothing otherwise
katana::Result<void>
katana::PropertyGraph::EnsureEdgePropertyLoaded(const std::string& name) {
if (HasEdgeProperty(name)) {
return katana::ResultSuccess();
}
return LoadEdgeProperty(name);
}
katana::Result<void>
katana::PropertyGraph::UnloadEdgeProperty(const std::string& prop_name) {
auto col_names = edge_properties()->ColumnNames();
auto pos = std::find(col_names.cbegin(), col_names.cend(), prop_name);
if (pos != col_names.cend()) {
return rdg_.UnloadEdgeProperty(std::distance(col_names.cbegin(), pos));
}
return katana::ErrorCode::PropertyNotFound;
}
katana::Result<void>
katana::PropertyGraph::InformPath(const std::string& input_path) {
if (!rdg_.rdg_dir().empty()) {
KATANA_LOG_DEBUG("rdg dir from {} to {}", rdg_.rdg_dir(), input_path);
}
auto uri_res = katana::Uri::Make(input_path);
if (!uri_res) {
return uri_res.error();
}
rdg_.set_rdg_dir(uri_res.value());
return ResultSuccess();
}
// Build an index over nodes.
katana::Result<void>
katana::PropertyGraph::MakeNodeIndex(const std::string& column_name) {
for (const auto& existing_index : node_indexes_) {
if (existing_index->column_name() == column_name) {
return KATANA_ERROR(
katana::ErrorCode::AlreadyExists,
"Index already exists for column {}", column_name);
}
}
// Get a view of the property.
std::shared_ptr<arrow::ChunkedArray> chunked_property =
GetNodeProperty(column_name);
if (!chunked_property) {
return KATANA_ERROR(
katana::ErrorCode::NotFound, "No such property: {}", column_name);
}
KATANA_LOG_ASSERT(chunked_property->num_chunks() == 1);
std::shared_ptr<arrow::Array> property = chunked_property->chunk(0);
// Create an index based on the type of the field.
std::unique_ptr<katana::PropertyIndex<GraphTopology::Node>> index =
KATANA_CHECKED(katana::MakeTypedIndex<katana::GraphTopology::Node>(
column_name, num_nodes(), property));
KATANA_CHECKED(index->BuildFromProperty());
node_indexes_.push_back(std::move(index));
//save the column name the index was created from for easy assess dudring json load/store
node_property_indexes_column_name.push_back(column_name);
//persist column names to json, index can now can be recreated using recreate_node_property_indexes()
rdg_.set_node_property_indexes_column_name(node_property_indexes_column_name);
return katana::ResultSuccess();
}
// Build an index over edges.
katana::Result<void>
katana::PropertyGraph::MakeEdgeIndex(const std::string& column_name) {
for (const auto& existing_index : edge_indexes_) {
if (existing_index->column_name() == column_name) {
return KATANA_ERROR(
katana::ErrorCode::AlreadyExists,
"Index already exists for column {}", column_name);
}
}
// Get a view of the property.
std::shared_ptr<arrow::ChunkedArray> chunked_property =
GetEdgeProperty(column_name);
if (!chunked_property) {
return KATANA_ERROR(
katana::ErrorCode::NotFound, "No such property: {}", column_name);
}
KATANA_LOG_ASSERT(chunked_property->num_chunks() == 1);
std::shared_ptr<arrow::Array> property = chunked_property->chunk(0);
// Create an index based on the type of the field.
std::unique_ptr<katana::PropertyIndex<katana::GraphTopology::Edge>> index =
KATANA_CHECKED(katana::MakeTypedIndex<katana::GraphTopology::Edge>(
column_name, num_edges(), property));
KATANA_CHECKED(index->BuildFromProperty());
edge_indexes_.push_back(std::move(index));
//save the column name the index was created from for easy assess dudring json load/store
edge_property_indexes_column_name.push_back(column_name);
//persist column names to json, index can now can be recreated using recreate_edge_property_indexes()
rdg_.set_edge_property_indexes_column_name(edge_property_indexes_column_name);
return katana::ResultSuccess();
}
katana::Result<std::unique_ptr<katana::NUMAArray<uint64_t>>>
katana::SortAllEdgesByDest(katana::PropertyGraph* pg) {
// TODO(amber): This function will soon change so that it produces a new sorted
// topology instead of modifying an existing one. The const_cast will go away
const auto& topo = pg->topology();
auto permutation_vec = std::make_unique<katana::NUMAArray<uint64_t>>();
permutation_vec->allocateInterleaved(topo.num_edges());
katana::ParallelSTL::iota(
permutation_vec->begin(), permutation_vec->end(), uint64_t{0});
auto* out_dests_data = const_cast<GraphTopology::Node*>(topo.dest_data());
katana::do_all(
katana::iterate(pg->topology().all_nodes()),
[&](GraphTopology::Node n) {
const auto e_beg = *pg->topology().edges(n).begin();
const auto e_end = *pg->topology().edges(n).end();
auto sort_iter_beg = katana::make_zip_iterator(
out_dests_data + e_beg, permutation_vec->begin() + e_beg);
auto sort_iter_end = katana::make_zip_iterator(
out_dests_data + e_end, permutation_vec->begin() + e_end);
std::sort(
sort_iter_beg, sort_iter_end,
[&](const auto& tup1, const auto& tup2) {
auto d1 = std::get<0>(tup1);
auto d2 = std::get<0>(tup2);
static_assert(std::is_same_v<decltype(d1), GraphTopology::Node>);
static_assert(std::is_same_v<decltype(d1), GraphTopology::Node>);
return d1 < d2;
});
},
katana::steal());
return std::unique_ptr<katana::NUMAArray<uint64_t>>(
std::move(permutation_vec));
}
// TODO(amber): make this a method of a sorted topology class in the near future
// TODO(amber): this method should return an edge_iterator
katana::GraphTopology::Edge
katana::FindEdgeSortedByDest(
const PropertyGraph* graph, const GraphTopology::Node src,
const GraphTopology::Node dst) {
const auto& topo = graph->topology();
auto e_range = topo.edges(src);
constexpr size_t kBinarySearchThreshold = 64;
if (e_range.size() <= kBinarySearchThreshold) {
auto iter = std::find_if(
e_range.begin(), e_range.end(),
[&](const GraphTopology::Edge& e) { return topo.edge_dest(e) == dst; });
return *iter;
} else {
auto iter = std::lower_bound(
e_range.begin(), e_range.end(), dst,
internal::EdgeDestComparator<GraphTopology>{&topo});
return topo.edge_dest(*iter) == dst ? *iter : *e_range.end();
}
}
// TODO(amber): this method should return a new sorted topology
katana::Result<void>
katana::SortNodesByDegree(katana::PropertyGraph* pg) {
const auto& topo = pg->topology();
uint64_t num_nodes = topo.num_nodes();
uint64_t num_edges = topo.num_edges();
using DegreeNodePair = std::pair<uint64_t, uint32_t>;
katana::NUMAArray<DegreeNodePair> dn_pairs;
dn_pairs.allocateInterleaved(num_nodes);