forked from mit-pdos/noria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.rs
116 lines (97 loc) · 2.77 KB
/
identity.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
use std::collections::HashMap;
use crate::prelude::*;
/// Applies the identity operation to the view. Since the identity does nothing,
/// it is the simplest possible operation. Primary intended as a reference
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Identity {
src: IndexPair,
}
impl Identity {
/// Construct a new identity operator.
pub fn new(src: NodeIndex) -> Identity {
Identity { src: src.into() }
}
}
impl Ingredient for Identity {
fn take(&mut self) -> NodeOperator {
Clone::clone(self).into()
}
fn ancestors(&self) -> Vec<NodeIndex> {
vec![self.src.as_global()]
}
fn on_connected(&mut self, _: &Graph) {}
fn on_commit(&mut self, _: NodeIndex, remap: &HashMap<NodeIndex, IndexPair>) {
self.src.remap(remap);
}
fn on_input(
&mut self,
_: &mut dyn Executor,
_: LocalNodeIndex,
rs: Records,
_: Option<&[usize]>,
_: &DomainNodes,
_: &StateMap,
) -> ProcessingResult {
ProcessingResult {
results: rs,
..Default::default()
}
}
fn suggest_indexes(&self, _: NodeIndex) -> HashMap<NodeIndex, Vec<usize>> {
HashMap::new()
}
fn resolve(&self, col: usize) -> Option<Vec<(NodeIndex, usize)>> {
Some(vec![(self.src.as_global(), col)])
}
fn description(&self, _: bool) -> String {
"≡".into()
}
fn parent_columns(&self, column: usize) -> Vec<(NodeIndex, Option<usize>)> {
vec![(self.src.as_global(), Some(column))]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ops;
fn setup(materialized: bool) -> ops::test::MockGraph {
let mut g = ops::test::MockGraph::new();
let s = g.add_base("source", &["x", "y", "z"]);
g.set_op(
"identity",
&["x", "y", "z"],
Identity::new(s.as_global()),
materialized,
);
g
}
#[test]
fn it_forwards() {
let mut g = setup(false);
let left: Vec<DataType> = vec![1.into(), "a".into()];
assert_eq!(g.narrow_one_row(left.clone(), false), vec![left].into());
}
#[test]
fn it_suggests_indices() {
let g = setup(false);
let me = 1.into();
let idx = g.node().suggest_indexes(me);
assert_eq!(idx.len(), 0);
}
#[test]
fn it_resolves() {
let g = setup(false);
assert_eq!(
g.node().resolve(0),
Some(vec![(g.narrow_base_id().as_global(), 0)])
);
assert_eq!(
g.node().resolve(1),
Some(vec![(g.narrow_base_id().as_global(), 1)])
);
assert_eq!(
g.node().resolve(2),
Some(vec![(g.narrow_base_id().as_global(), 2)])
);
}
}