-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathchild.rs
1209 lines (1040 loc) · 42.8 KB
/
child.rs
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
//! Child layer implementation
//!
//! A child layer stores a reference to a base layer, as well as
//! triple additions and removals, and any new dictionary entries that
//! this layer needs for its additions.
use super::super::builder::*;
use super::super::id_map::*;
use crate::layer::*;
use crate::storage::*;
use rayon::prelude::*;
use tdb_succinct::*;
use std::io;
use std::pin::Pin;
use std::sync::Arc;
use futures::stream::{self, Stream, StreamExt};
use futures::task::{Context, Poll};
/// A child layer.
///
/// This layer type has a parent. It stores triple additions and removals.
#[derive(Clone)]
pub struct ChildLayer {
pub(super) name: [u32; 5],
pub(super) parent: Arc<InternalLayer>,
pub(super) node_dictionary: StringDict,
pub(super) predicate_dictionary: StringDict,
pub(super) value_dictionary: TypedDict,
pub(super) node_value_idmap: IdMap,
pub(super) predicate_idmap: IdMap,
pub(super) parent_node_value_count: usize,
pub(super) parent_predicate_count: usize,
pub(super) pos_subjects: MonotonicLogArray,
pub(super) pos_objects: MonotonicLogArray,
pub(super) pos_s_p_adjacency_list: AdjacencyList,
pub(super) pos_sp_o_adjacency_list: AdjacencyList,
pub(super) pos_o_ps_adjacency_list: AdjacencyList,
pub(super) neg_subjects: MonotonicLogArray,
pub(super) neg_objects: MonotonicLogArray,
pub(super) neg_s_p_adjacency_list: AdjacencyList,
pub(super) neg_sp_o_adjacency_list: AdjacencyList,
pub(super) neg_o_ps_adjacency_list: AdjacencyList,
pub(super) pos_predicate_wavelet_tree: WaveletTree,
pub(super) neg_predicate_wavelet_tree: WaveletTree,
}
impl ChildLayer {
pub async fn load_from_files<F: FileLoad + FileStore + Clone>(
name: [u32; 5],
parent: Arc<InternalLayer>,
files: &ChildLayerFiles<F>,
) -> io::Result<InternalLayer> {
let maps = files.map_all().await?;
Ok(Self::load(name, parent, maps))
}
pub fn load(name: [u32; 5], parent: Arc<InternalLayer>, maps: ChildLayerMaps) -> InternalLayer {
let node_dictionary = StringDict::parse(
maps.node_dictionary_maps.offsets_map,
maps.node_dictionary_maps.blocks_map,
);
let predicate_dictionary = StringDict::parse(
maps.predicate_dictionary_maps.offsets_map,
maps.predicate_dictionary_maps.blocks_map,
);
let value_dictionary = TypedDict::from_parts(
maps.value_dictionary_maps.types_present_map,
maps.value_dictionary_maps.type_offsets_map,
maps.value_dictionary_maps.offsets_map,
maps.value_dictionary_maps.blocks_map,
);
let parent_node_value_count = parent.node_and_value_count();
let parent_predicate_count = parent.predicate_count();
let node_value_idmap = match maps.id_map_maps.node_value_idmap_maps {
None => IdMap::default(),
Some(maps) => IdMap::from_maps(
maps,
util::calculate_width(
(node_dictionary.num_entries() + value_dictionary.num_entries()) as u64,
),
),
};
let predicate_idmap = match maps.id_map_maps.predicate_idmap_maps {
None => IdMap::default(),
Some(map) => IdMap::from_maps(
map,
util::calculate_width(predicate_dictionary.num_entries() as u64),
),
};
let pos_subjects =
MonotonicLogArray::from_logarray(LogArray::parse(maps.pos_subjects_map).unwrap());
let pos_objects =
MonotonicLogArray::from_logarray(LogArray::parse(maps.pos_objects_map).unwrap());
let neg_subjects =
MonotonicLogArray::from_logarray(LogArray::parse(maps.neg_subjects_map).unwrap());
let neg_objects =
MonotonicLogArray::from_logarray(LogArray::parse(maps.neg_objects_map).unwrap());
let pos_s_p_adjacency_list = AdjacencyList::parse(
maps.pos_s_p_adjacency_list_maps.nums_map,
maps.pos_s_p_adjacency_list_maps.bitindex_maps.bits_map,
maps.pos_s_p_adjacency_list_maps.bitindex_maps.blocks_map,
maps.pos_s_p_adjacency_list_maps.bitindex_maps.sblocks_map,
);
let pos_sp_o_adjacency_list = AdjacencyList::parse(
maps.pos_sp_o_adjacency_list_maps.nums_map,
maps.pos_sp_o_adjacency_list_maps.bitindex_maps.bits_map,
maps.pos_sp_o_adjacency_list_maps.bitindex_maps.blocks_map,
maps.pos_sp_o_adjacency_list_maps.bitindex_maps.sblocks_map,
);
let pos_o_ps_adjacency_list = AdjacencyList::parse(
maps.pos_o_ps_adjacency_list_maps.nums_map,
maps.pos_o_ps_adjacency_list_maps.bitindex_maps.bits_map,
maps.pos_o_ps_adjacency_list_maps.bitindex_maps.blocks_map,
maps.pos_o_ps_adjacency_list_maps.bitindex_maps.sblocks_map,
);
let neg_s_p_adjacency_list = AdjacencyList::parse(
maps.neg_s_p_adjacency_list_maps.nums_map,
maps.neg_s_p_adjacency_list_maps.bitindex_maps.bits_map,
maps.neg_s_p_adjacency_list_maps.bitindex_maps.blocks_map,
maps.neg_s_p_adjacency_list_maps.bitindex_maps.sblocks_map,
);
let neg_sp_o_adjacency_list = AdjacencyList::parse(
maps.neg_sp_o_adjacency_list_maps.nums_map,
maps.neg_sp_o_adjacency_list_maps.bitindex_maps.bits_map,
maps.neg_sp_o_adjacency_list_maps.bitindex_maps.blocks_map,
maps.neg_sp_o_adjacency_list_maps.bitindex_maps.sblocks_map,
);
let neg_o_ps_adjacency_list = AdjacencyList::parse(
maps.neg_o_ps_adjacency_list_maps.nums_map,
maps.neg_o_ps_adjacency_list_maps.bitindex_maps.bits_map,
maps.neg_o_ps_adjacency_list_maps.bitindex_maps.blocks_map,
maps.neg_o_ps_adjacency_list_maps.bitindex_maps.sblocks_map,
);
let pos_predicate_wavelet_tree_width = pos_s_p_adjacency_list.nums().width();
let pos_predicate_wavelet_tree = WaveletTree::from_parts(
BitIndex::from_maps(
maps.pos_predicate_wavelet_tree_maps.bits_map,
maps.pos_predicate_wavelet_tree_maps.blocks_map,
maps.pos_predicate_wavelet_tree_maps.sblocks_map,
),
pos_predicate_wavelet_tree_width,
);
let neg_predicate_wavelet_tree_width = neg_s_p_adjacency_list.nums().width();
let neg_predicate_wavelet_tree = WaveletTree::from_parts(
BitIndex::from_maps(
maps.neg_predicate_wavelet_tree_maps.bits_map,
maps.neg_predicate_wavelet_tree_maps.blocks_map,
maps.neg_predicate_wavelet_tree_maps.sblocks_map,
),
neg_predicate_wavelet_tree_width,
);
InternalLayer::Child(ChildLayer {
name,
parent,
node_dictionary,
predicate_dictionary,
value_dictionary,
node_value_idmap,
predicate_idmap,
parent_node_value_count,
parent_predicate_count,
pos_subjects,
pos_objects,
neg_subjects,
neg_objects,
pos_s_p_adjacency_list,
pos_sp_o_adjacency_list,
pos_o_ps_adjacency_list,
neg_s_p_adjacency_list,
neg_sp_o_adjacency_list,
neg_o_ps_adjacency_list,
pos_predicate_wavelet_tree,
neg_predicate_wavelet_tree,
})
}
}
/// A builder for a child layer.
///
/// This builder takes node, predicate and value strings in lexical
/// order through the corresponding `add_<thing>` methods. When
/// they're all added, `into_phase2()` is to be called to turn this
/// builder into a second builder that takes triple data.
pub struct ChildLayerFileBuilder<F: 'static + FileLoad + FileStore + Clone + Send + Sync> {
parent: Arc<dyn Layer>,
files: ChildLayerFiles<F>,
builder: DictionarySetFileBuilder<F>,
}
impl<F: 'static + FileLoad + FileStore + Clone + Send + Sync> ChildLayerFileBuilder<F> {
/// Create the builder from the given files.
pub async fn from_files(
parent: Arc<dyn Layer>,
files: &ChildLayerFiles<F>,
) -> io::Result<Self> {
let builder = DictionarySetFileBuilder::from_files(
files.node_dictionary_files.clone(),
files.predicate_dictionary_files.clone(),
files.value_dictionary_files.clone(),
)
.await?;
Ok(Self {
parent,
files: files.clone(),
builder,
})
}
/// Add a node string.
///
/// Does nothing if the node already exists in the parent, and
/// panics if the given node string is not a lexical successor of
/// the previous node string.
pub fn add_node(&mut self, node: &str) -> u64 {
match self.parent.subject_id(node) {
None => self.builder.add_node(node),
Some(id) => id,
}
}
/// Add a predicate string.
///
/// Does nothing if the predicate already exists in the paretn, and
/// panics if the given predicate string is not a lexical successor of
/// the previous predicate string.
pub fn add_predicate(&mut self, predicate: &str) -> u64 {
match self.parent.predicate_id(predicate) {
None => self.builder.add_predicate(predicate),
Some(id) => id,
}
}
/// Add a value string.
///
/// Does nothing if the value already exists in the paretn, and
/// panics if the given value string is not a lexical successor of
/// the previous value string.
pub fn add_value(&mut self, value: TypedDictEntry) -> u64 {
match self.parent.object_value_id(&value) {
None => self.builder.add_value(value),
Some(id) => id,
}
}
/// Add nodes from an iterable.
///
/// Panics if the nodes are not in lexical order, or if previous
/// added nodes are a lexical succesor of any of these
/// nodes. Skips any nodes that are already part of the base
/// layer.
pub fn add_nodes<I: 'static + IntoIterator<Item = String> + Send>(
&mut self,
nodes: I,
) -> Vec<u64>
where
<I as std::iter::IntoIterator>::IntoIter: Send,
{
// TODO bulk check node existence
let mut result = Vec::new();
for node in nodes {
let id = self.add_node(&node);
result.push(id);
}
result
}
/// Add predicates from an iterable.
///
/// Panics if the predicates are not in lexical order, or if
/// previous added predicates are a lexical succesor of any of
/// these predicates. Skips any predicates that are already part
/// of the base layer.
pub fn add_predicates<I: 'static + IntoIterator<Item = String> + Send>(
&mut self,
predicates: I,
) -> Vec<u64>
where
<I as std::iter::IntoIterator>::IntoIter: Send,
{
// TODO bulk check predicate existence
let mut result = Vec::new();
for predicate in predicates {
let id = self.add_predicate(&predicate);
result.push(id);
}
result
}
/// Add values from an iterable.
///
/// Panics if the values are not in lexical order, or if previous
/// added values are a lexical succesor of any of these
/// values. Skips any nodes that are already part of the base
/// layer.
pub fn add_values<I: 'static + IntoIterator<Item = TypedDictEntry> + Send>(
&mut self,
values: I,
) -> Vec<u64>
where
<I as std::iter::IntoIterator>::IntoIter: Send,
{
// TODO bulk check predicate existence
let mut result = Vec::new();
for value in values {
let id = self.add_value(value);
result.push(id);
}
result
}
/// Turn this builder into a phase 2 builder that will take triple data.
pub async fn into_phase2(self) -> io::Result<ChildLayerFileBuilderPhase2<F>> {
let ChildLayerFileBuilder {
parent,
files,
builder,
} = self;
builder.finalize().await?;
let node_dict_offsets_map = files.node_dictionary_files.offsets_file.map().await?;
let node_dict_blocks_map = files.node_dictionary_files.blocks_file.map().await?;
let predicate_dict_offsets_map =
files.predicate_dictionary_files.offsets_file.map().await?;
let predicate_dict_blocks_map = files.predicate_dictionary_files.blocks_file.map().await?;
let value_dict_types_present_map = files
.value_dictionary_files
.types_present_file
.map()
.await?;
let value_dict_type_offsets_map =
files.value_dictionary_files.type_offsets_file.map().await?;
let value_dict_offsets_map = files.value_dictionary_files.offsets_file.map().await?;
let value_dict_blocks_map = files.value_dictionary_files.blocks_file.map().await?;
let node_dict = StringDict::parse(node_dict_offsets_map, node_dict_blocks_map);
let pred_dict = StringDict::parse(predicate_dict_offsets_map, predicate_dict_blocks_map);
let val_dict = TypedDict::from_parts(
value_dict_types_present_map,
value_dict_type_offsets_map,
value_dict_offsets_map,
value_dict_blocks_map,
);
// TODO: it is a bit silly to parse the dictionaries just for this. surely we can get the counts in an easier way?
let num_nodes = node_dict.num_entries();
let num_predicates = pred_dict.num_entries();
let num_values = val_dict.num_entries();
ChildLayerFileBuilderPhase2::new(parent, files, num_nodes, num_predicates, num_values).await
}
}
/// Second phase of child layer building.
///
/// This builder takes ordered triple additions and removals. When all
/// data has been added, `finalize()` will build a layer.
pub struct ChildLayerFileBuilderPhase2<F: 'static + FileLoad + FileStore + Clone + Send + Sync> {
parent: Arc<dyn Layer>,
files: ChildLayerFiles<F>,
pos_builder: TripleFileBuilder<F>,
neg_builder: TripleFileBuilder<F>,
}
impl<F: 'static + FileLoad + FileStore + Clone + Send + Sync> ChildLayerFileBuilderPhase2<F> {
pub(crate) async fn new(
parent: Arc<dyn Layer>,
files: ChildLayerFiles<F>,
num_nodes: usize,
num_predicates: usize,
num_values: usize,
) -> io::Result<Self> {
let parent_counts = parent.all_counts();
let pos_builder = TripleFileBuilder::new(
files.pos_s_p_adjacency_list_files.clone(),
files.pos_sp_o_adjacency_list_files.clone(),
num_nodes + parent_counts.node_count,
num_predicates + parent_counts.predicate_count,
num_values + parent_counts.value_count,
Some(files.pos_subjects_file.clone()),
)
.await?;
let neg_builder = TripleFileBuilder::new(
files.neg_s_p_adjacency_list_files.clone(),
files.neg_sp_o_adjacency_list_files.clone(),
num_nodes + parent_counts.node_count,
num_predicates + parent_counts.predicate_count,
num_values + parent_counts.value_count,
Some(files.neg_subjects_file.clone()),
)
.await?;
Ok(ChildLayerFileBuilderPhase2 {
parent,
files,
pos_builder,
neg_builder,
})
}
pub(crate) async fn add_triple_unchecked(
&mut self,
subject: u64,
predicate: u64,
object: u64,
) -> io::Result<()> {
self.pos_builder
.add_triple(subject, predicate, object)
.await
}
/// Add the given subject, predicate and object.
///
/// This will panic if a greater triple has already been added,
/// and do nothing if the triple is already part of the parent.
pub async fn add_triple(
&mut self,
subject: u64,
predicate: u64,
object: u64,
) -> io::Result<()> {
if !self.parent.triple_exists(subject, predicate, object) {
self.add_triple_unchecked(subject, predicate, object).await
} else {
Ok(())
}
}
pub(crate) async fn remove_triple_unchecked(
&mut self,
subject: u64,
predicate: u64,
object: u64,
) -> io::Result<()> {
self.neg_builder
.add_triple(subject, predicate, object)
.await
}
/// Remove the given subject, predicate and object.
///
/// This will panic if a greater triple has already been removed,
/// and do nothing if the parent doesn't know aobut this triple.
pub async fn remove_triple(
&mut self,
subject: u64,
predicate: u64,
object: u64,
) -> io::Result<()> {
if self.parent.triple_exists(subject, predicate, object) {
self.remove_triple_unchecked(subject, predicate, object)
.await
} else {
Ok(())
}
}
/// Add the given triple.
///
/// This will panic if a greater triple has already been added,
/// and do nothing if the parent already contains this triple.
pub async fn add_id_triples(&mut self, triples: Vec<IdTriple>) -> io::Result<()> {
let parent = self.parent.clone();
let filtered: Vec<_> = triples
.into_par_iter()
.filter(move |triple| {
!parent.triple_exists(triple.subject, triple.predicate, triple.object)
})
.collect();
for triple in filtered {
self.add_triple_unchecked(triple.subject, triple.predicate, triple.object)
.await?;
}
Ok(())
}
/// Remove the given triple.
///
/// This will panic if a greater triple has already been removed,
/// and do nothing if the parent doesn't know aobut this triple.
pub async fn remove_id_triples(&mut self, triples: Vec<IdTriple>) -> io::Result<()> {
let parent = self.parent.clone();
let filtered: Vec<_> = triples
.into_par_iter()
.filter(move |triple| {
parent.triple_exists(triple.subject, triple.predicate, triple.object)
})
.collect();
for triple in filtered {
self.remove_triple_unchecked(triple.subject, triple.predicate, triple.object)
.await?;
}
Ok(())
}
/// Write the layer data to storage.
pub async fn finalize(self) -> io::Result<()> {
let pos_task = tokio::spawn(self.pos_builder.finalize());
let neg_task = tokio::spawn(self.neg_builder.finalize());
pos_task.await??;
neg_task.await??;
let pos_indexes_task = tokio::spawn(build_indexes(
self.files.pos_s_p_adjacency_list_files,
self.files.pos_sp_o_adjacency_list_files,
self.files.pos_o_ps_adjacency_list_files,
Some(self.files.pos_objects_file),
self.files.pos_predicate_wavelet_tree_files,
));
let neg_indexes_task = tokio::spawn(build_indexes(
self.files.neg_s_p_adjacency_list_files,
self.files.neg_sp_o_adjacency_list_files,
self.files.neg_o_ps_adjacency_list_files,
Some(self.files.neg_objects_file),
self.files.neg_predicate_wavelet_tree_files,
));
pos_indexes_task.await??;
neg_indexes_task.await??;
Ok(())
}
}
pub struct ChildTripleStream<
S1: Stream<Item = io::Result<u64>> + Unpin + Send,
S2: Stream<Item = io::Result<(u64, u64)>> + Unpin + Send,
> {
subjects_stream: stream::Peekable<S1>,
s_p_stream: stream::Peekable<S2>,
sp_o_stream: stream::Peekable<S2>,
last_mapped_s: u64,
last_s_p: (u64, u64),
last_sp: u64,
}
impl<
S1: Stream<Item = io::Result<u64>> + Unpin + Send,
S2: Stream<Item = io::Result<(u64, u64)>> + Unpin + Send,
> ChildTripleStream<S1, S2>
{
fn new(subjects_stream: S1, s_p_stream: S2, sp_o_stream: S2) -> ChildTripleStream<S1, S2> {
ChildTripleStream {
subjects_stream: subjects_stream.peekable(),
s_p_stream: s_p_stream.peekable(),
sp_o_stream: sp_o_stream.peekable(),
last_mapped_s: 0,
last_s_p: (0, 0),
last_sp: 0,
}
}
}
impl<
S1: Stream<Item = io::Result<u64>> + Unpin + Send,
S2: Stream<Item = io::Result<(u64, u64)>> + Unpin + Send,
> Stream for ChildTripleStream<S1, S2>
{
type Item = io::Result<(u64, u64, u64)>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context,
) -> Poll<Option<io::Result<(u64, u64, u64)>>> {
let sp_o = Pin::new(&mut self.sp_o_stream).poll_peek(cx);
match sp_o {
Poll::Ready(Some(Ok((sp, o)))) => {
let sp = *sp;
let o = *o;
if sp > self.last_sp {
let s_p = Pin::new(&mut self.s_p_stream).poll_peek(cx);
match s_p {
Poll::Ready(None) => Poll::Ready(Some(Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"unexpected end of s_p_stream",
)))),
Poll::Ready(Some(Ok((s, p)))) => {
let s = *s;
let p = *p;
if s > self.last_s_p.0 {
let mapped_s = Pin::new(&mut self.subjects_stream).poll_peek(cx);
match mapped_s {
Poll::Ready(None) => Poll::Ready(Some(Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"unexpected end of subjects_stream",
)))),
Poll::Ready(Some(Ok(mapped_s))) => {
let mapped_s = *mapped_s;
util::assert_poll_next(
Pin::new(&mut self.subjects_stream),
cx,
)
.unwrap();
util::assert_poll_next(Pin::new(&mut self.s_p_stream), cx)
.unwrap();
util::assert_poll_next(Pin::new(&mut self.sp_o_stream), cx)
.unwrap();
self.last_mapped_s = mapped_s;
self.last_s_p = (s, p);
self.last_sp = sp;
Poll::Ready(Some(Ok((mapped_s, p, o))))
}
Poll::Ready(Some(Err(_))) => {
Poll::Ready(Some(Err(util::assert_poll_next(
Pin::new(&mut self.subjects_stream),
cx,
)
.err()
.unwrap())))
}
Poll::Pending => Poll::Pending,
}
} else {
util::assert_poll_next(Pin::new(&mut self.s_p_stream), cx).unwrap();
util::assert_poll_next(Pin::new(&mut self.sp_o_stream), cx)
.unwrap();
self.last_s_p = (s, p);
self.last_sp = sp;
Poll::Ready(Some(Ok((self.last_mapped_s, p, o))))
}
}
Poll::Ready(Some(Err(_))) => Poll::Ready(Some(Err(
util::assert_poll_next(Pin::new(&mut self.s_p_stream), cx)
.err()
.unwrap(),
))),
Poll::Pending => Poll::Pending,
}
} else {
util::assert_poll_next(Pin::new(&mut self.sp_o_stream), cx).unwrap();
Poll::Ready(Some(Ok((self.last_mapped_s, self.last_s_p.1, o))))
}
}
Poll::Ready(Some(Err(_))) => Poll::Ready(Some(Err(util::assert_poll_next(
Pin::new(&mut self.sp_o_stream),
cx,
)
.err()
.unwrap()))),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
}
pub async fn open_child_triple_stream<F: 'static + FileLoad + FileStore>(
subjects_file: F,
s_p_files: AdjacencyListFiles<F>,
sp_o_files: AdjacencyListFiles<F>,
) -> io::Result<impl Stream<Item = io::Result<(u64, u64, u64)>> + Unpin + Send> {
let subjects_stream = logarray_stream_entries(subjects_file).await?;
let s_p_stream =
adjacency_list_stream_pairs(s_p_files.bitindex_files.bits_file, s_p_files.nums_file)
.await?;
let sp_o_stream =
adjacency_list_stream_pairs(sp_o_files.bitindex_files.bits_file, sp_o_files.nums_file)
.await?;
Ok(ChildTripleStream::new(
subjects_stream,
s_p_stream,
sp_o_stream,
))
}
#[cfg(test)]
pub mod child_tests {
use super::*;
use crate::layer::base::base_tests::*;
use crate::storage::memory::*;
use futures::stream::TryStreamExt;
pub fn child_layer_files() -> ChildLayerFiles<MemoryBackedStore> {
// TODO inline
child_layer_memory_files()
}
#[tokio::test]
async fn empty_child_layer_equivalent_to_parent() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let child_builder = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await
.unwrap();
let builder = child_builder.into_phase2().await.unwrap();
builder.finalize().await.unwrap();
let child_layer = ChildLayer::load_from_files([5, 4, 3, 2, 1], parent, &child_files)
.await
.unwrap();
assert!(child_layer.triple_exists(1, 1, 1));
assert!(child_layer.triple_exists(2, 1, 1));
assert!(child_layer.triple_exists(2, 1, 3));
assert!(child_layer.triple_exists(2, 3, 6));
assert!(child_layer.triple_exists(3, 2, 5));
assert!(child_layer.triple_exists(3, 3, 6));
assert!(child_layer.triple_exists(4, 3, 6));
assert!(!child_layer.triple_exists(2, 2, 0));
}
#[tokio::test]
async fn child_layer_can_have_inserts() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let child_builder = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await
.unwrap();
let mut b = child_builder.into_phase2().await.unwrap();
b.add_triple(2, 1, 2).await.unwrap();
b.add_triple(3, 3, 3).await.unwrap();
b.finalize().await.unwrap();
let child_layer = ChildLayer::load_from_files([5, 4, 3, 2, 1], parent, &child_files)
.await
.unwrap();
assert!(child_layer.triple_exists(1, 1, 1));
assert!(child_layer.triple_exists(2, 1, 1));
assert!(child_layer.triple_exists(2, 1, 2));
assert!(child_layer.triple_exists(2, 1, 3));
assert!(child_layer.triple_exists(2, 3, 6));
assert!(child_layer.triple_exists(3, 2, 5));
assert!(child_layer.triple_exists(3, 3, 3));
assert!(child_layer.triple_exists(3, 3, 6));
assert!(child_layer.triple_exists(4, 3, 6));
assert!(!child_layer.triple_exists(2, 2, 0));
}
#[tokio::test]
async fn child_layer_can_have_deletes() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let child_builder = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await
.unwrap();
let mut b = child_builder.into_phase2().await.unwrap();
b.remove_triple(2, 1, 1).await.unwrap();
b.remove_triple(3, 2, 5).await.unwrap();
b.finalize().await.unwrap();
let child_layer = ChildLayer::load_from_files([5, 4, 3, 2, 1], parent, &child_files)
.await
.unwrap();
assert!(child_layer.triple_exists(1, 1, 1));
assert!(!child_layer.triple_exists(2, 1, 1));
assert!(child_layer.triple_exists(2, 1, 3));
assert!(child_layer.triple_exists(2, 3, 6));
assert!(!child_layer.triple_exists(3, 2, 5));
assert!(child_layer.triple_exists(3, 3, 6));
assert!(child_layer.triple_exists(4, 3, 6));
assert!(!child_layer.triple_exists(2, 2, 0));
}
#[tokio::test]
async fn child_layer_can_have_inserts_and_deletes() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let child_builder = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await
.unwrap();
let mut b = child_builder.into_phase2().await.unwrap();
b.add_triple(1, 2, 3).await.unwrap();
b.add_triple(2, 3, 4).await.unwrap();
b.remove_triple(3, 2, 5).await.unwrap();
b.finalize().await.unwrap();
let child_layer = ChildLayer::load_from_files([5, 4, 3, 2, 1], parent, &child_files)
.await
.unwrap();
assert!(child_layer.triple_exists(1, 1, 1));
assert!(child_layer.triple_exists(1, 2, 3));
assert!(child_layer.triple_exists(2, 1, 1));
assert!(child_layer.triple_exists(2, 1, 3));
assert!(child_layer.triple_exists(2, 3, 4));
assert!(child_layer.triple_exists(2, 3, 6));
assert!(!child_layer.triple_exists(3, 2, 5));
assert!(child_layer.triple_exists(3, 3, 6));
assert!(child_layer.triple_exists(4, 3, 6));
assert!(!child_layer.triple_exists(2, 2, 0));
}
#[tokio::test]
async fn iterate_child_layer_triples() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let child_builder = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await
.unwrap();
let mut b = child_builder.into_phase2().await.unwrap();
b.add_triple(1, 2, 3).await.unwrap();
b.add_triple(2, 3, 4).await.unwrap();
b.remove_triple(3, 2, 5).await.unwrap();
b.finalize().await.unwrap();
let child_layer = ChildLayer::load_from_files([5, 4, 3, 2, 1], parent, &child_files)
.await
.unwrap();
let subjects: Vec<_> = child_layer
.triples()
.map(|t| (t.subject, t.predicate, t.object))
.collect();
assert_eq!(
vec![
(1, 1, 1),
(1, 2, 3),
(2, 1, 1),
(2, 1, 3),
(2, 3, 4),
(2, 3, 6),
(3, 3, 6),
(4, 3, 6)
],
subjects
);
}
#[tokio::test]
async fn lookup_child_layer_triples_by_predicate() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let child_builder = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await
.unwrap();
let mut b = child_builder.into_phase2().await.unwrap();
b.add_triple(1, 2, 3).await.unwrap();
b.add_triple(2, 3, 4).await.unwrap();
b.remove_triple(3, 2, 5).await.unwrap();
b.finalize().await.unwrap();
let child_layer = ChildLayer::load_from_files([5, 4, 3, 2, 1], parent, &child_files)
.await
.unwrap();
let pairs: Vec<_> = child_layer
.triples_p(1)
.map(|t| (t.subject, t.predicate, t.object))
.collect();
assert_eq!(vec![(1, 1, 1), (2, 1, 1), (2, 1, 3)], pairs);
let pairs: Vec<_> = child_layer
.triples_p(2)
.map(|t| (t.subject, t.predicate, t.object))
.collect();
assert_eq!(vec![(1, 2, 3)], pairs);
let pairs: Vec<_> = child_layer
.triples_p(3)
.map(|t| (t.subject, t.predicate, t.object))
.collect();
assert_eq!(vec![(2, 3, 4), (2, 3, 6), (3, 3, 6), (4, 3, 6)], pairs);
assert!(child_layer.triples_p(4).next().is_none());
}
#[tokio::test]
async fn adding_new_nodes_predicates_and_values_in_child() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let child_builder = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await
.unwrap();
let mut b = child_builder.into_phase2().await.unwrap();
b.add_triple(11, 2, 3).await.unwrap();
b.add_triple(12, 3, 4).await.unwrap();
b.finalize().await.unwrap();
let child_layer = ChildLayer::load_from_files([5, 4, 3, 2, 1], parent, &child_files)
.await
.unwrap();
assert!(child_layer.triple_exists(11, 2, 3));
assert!(child_layer.triple_exists(12, 3, 4));
}
#[tokio::test]
async fn old_dictionary_entries_in_child() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let mut b = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await
.unwrap();
b.add_node("foo");
b.add_predicate("bar");
b.add_value(String::make_entry(&"baz"));
let b = b.into_phase2().await.unwrap();
b.finalize().await.unwrap();
let child_layer = ChildLayer::load_from_files([5, 4, 3, 2, 1], parent, &child_files)
.await
.unwrap();
assert_eq!(3, child_layer.subject_id("bbbbb").unwrap());
assert_eq!(2, child_layer.predicate_id("fghij").unwrap());
assert_eq!(1, child_layer.object_node_id("aaaaa").unwrap());
assert_eq!(
6,
child_layer
.object_value_id(&String::make_entry(&"chicken"))
.unwrap()
);
assert_eq!("bbbbb", child_layer.id_subject(3).unwrap());
assert_eq!("fghij", child_layer.id_predicate(2).unwrap());
assert_eq!(
ObjectType::Node("aaaaa".to_string()),
child_layer.id_object(1).unwrap()
);
assert_eq!(
ObjectType::Value(String::make_entry(&"chicken")),
child_layer.id_object(6).unwrap()
);
}
#[tokio::test]
async fn new_dictionary_entries_in_child() {
let base_layer = example_base_layer().await;
let parent: Arc<InternalLayer> = Arc::new(base_layer.into());
let child_files = child_layer_files();
let mut b = ChildLayerFileBuilder::from_files(parent.clone(), &child_files)
.await