tglman / persy
1 1
mod helpers;
2
use helpers::create_and_drop;
3
use persy::ValueMode;
4

5
#[test]
6 1
fn test_snapshot_after_drop() {
7 1
    create_and_drop("after_drop", |persy| {
8 1
        let mut tx = persy.begin().unwrap();
9 1
        tx.create_segment("test").unwrap();
10 1
        tx.create_index::<u8, u8>("index", ValueMode::Replace).unwrap();
11 1
        let bytes = String::from("something").into_bytes();
12 1
        let id = tx.insert("test", &bytes).unwrap();
13 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
14 1
        let finalizer = tx.prepare().unwrap();
15 1
        finalizer.commit().unwrap();
16 1
        let snapshot = persy.snapshot().unwrap();
17

18 1
        let mut tx = persy.begin().unwrap();
19 1
        tx.drop_segment("test").unwrap();
20 1
        tx.drop_index("index").unwrap();
21 1
        let finalizer = tx.prepare().unwrap();
22 1
        finalizer.commit().unwrap();
23

24 1
        let bytes = String::from("something").into_bytes();
25 1
        let read_after = snapshot.read("test", &id).unwrap();
26 1
        assert_eq!(read_after, Some(bytes));
27 1
        assert_eq!(snapshot.get::<u8, u8>("index", &10).unwrap().next(), Some(20));
28 1
    });
29 1
}
30

31
#[test]
32 1
fn test_double_snapshot_after_drop() {
33 1
    create_and_drop("double_after_drop", |persy| {
34 1
        let mut tx = persy.begin().unwrap();
35 1
        tx.create_segment("test").unwrap();
36 1
        tx.create_index::<u8, u8>("index", ValueMode::Replace).unwrap();
37 1
        let bytes = String::from("something").into_bytes();
38 1
        let id = tx.insert("test", &bytes).unwrap();
39 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
40 1
        let finalizer = tx.prepare().unwrap();
41 1
        finalizer.commit().unwrap();
42 1
        let snapshot = persy.snapshot().unwrap();
43 1
        let snapshot1 = persy.snapshot().unwrap();
44

45 1
        let mut tx = persy.begin().unwrap();
46 1
        tx.drop_segment("test").unwrap();
47 1
        tx.drop_index("index").unwrap();
48 1
        let finalizer = tx.prepare().unwrap();
49 1
        finalizer.commit().unwrap();
50

51 1
        let bytes = String::from("something").into_bytes();
52 1
        let read_after = snapshot.read("test", &id).unwrap();
53 1
        assert_eq!(read_after, Some(bytes));
54 1
        assert_eq!(snapshot.get::<u8, u8>("index", &10).unwrap().next(), Some(20));
55

56 1
        let bytes = String::from("something").into_bytes();
57 1
        let read_after = snapshot1.read("test", &id).unwrap();
58 1
        assert_eq!(read_after, Some(bytes));
59 1
        assert_eq!(snapshot1.get::<u8, u8>("index", &10).unwrap().next(), Some(20));
60 1
        drop(snapshot1);
61

62 1
        let bytes = String::from("something").into_bytes();
63 1
        let read_after = snapshot.read("test", &id).unwrap();
64 1
        assert_eq!(read_after, Some(bytes));
65 1
        assert_eq!(snapshot.get::<u8, u8>("index", &10).unwrap().next(), Some(20));
66 1
    });
67 1
}
68

69
#[test]
70 1
fn test_snapshot_ignore_newers() {
71 1
    create_and_drop("ignore_newers", |persy| {
72 1
        let mut tx = persy.begin().unwrap();
73 1
        tx.create_segment("test").unwrap();
74 1
        tx.create_index::<u8, u8>("index", ValueMode::Replace).unwrap();
75 1
        let bytes = String::from("something").into_bytes();
76 1
        tx.insert("test", &bytes).unwrap();
77 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
78 1
        let finalizer = tx.prepare().unwrap();
79 1
        finalizer.commit().unwrap();
80 1
        let snapshot = persy.snapshot().unwrap();
81

82 1
        let mut tx = persy.begin().unwrap();
83 1
        let bytes = String::from("other").into_bytes();
84 1
        let id2 = tx.insert("test", &bytes).unwrap();
85 1
        tx.put::<u8, u8>("index", 11, 20).unwrap();
86 1
        let finalizer = tx.prepare().unwrap();
87 1
        finalizer.commit().unwrap();
88

89 1
        let read_after = snapshot.read("test", &id2).unwrap();
90 1
        assert_eq!(read_after, None);
91 1
        assert_eq!(snapshot.get::<u8, u8>("index", &11).unwrap().len(), 0);
92 1
    });
93 1
}
94

95
#[test]
96 1
fn test_snapshot_list_after_drop() {
97 1
    create_and_drop("after_drop_list", |persy| {
98 1
        let mut tx = persy.begin().unwrap();
99 1
        tx.create_segment("test").unwrap();
100 1
        tx.create_index::<u8, u8>("index", ValueMode::Replace).unwrap();
101 1
        let bytes = String::from("something").into_bytes();
102 1
        tx.insert("test", &bytes).unwrap();
103 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
104 1
        let finalizer = tx.prepare().unwrap();
105 1
        finalizer.commit().unwrap();
106 1
        let snapshot = persy.snapshot().unwrap();
107

108 1
        let mut tx = persy.begin().unwrap();
109 1
        tx.drop_segment("test").unwrap();
110 1
        tx.drop_index("index").unwrap();
111 1
        let finalizer = tx.prepare().unwrap();
112 1
        finalizer.commit().unwrap();
113

114 1
        let read_after = snapshot.list_segments().unwrap();
115 1
        assert_eq!(read_after.len(), 1);
116 1
        assert_eq!(
117 1
            read_after.into_iter().next().map(|(name, _)| (name)),
118 1
            Some("test".to_string())
119
        );
120 1
        let read_after = snapshot.list_indexes().unwrap();
121 1
        assert_eq!(read_after.len(), 1);
122 1
        assert_eq!(
123 1
            read_after.into_iter().next().map(|(name, _)| (name)),
124 1
            Some("index".to_string())
125
        );
126 1
    });
127 1
}
128

129
#[test]
130 1
fn test_snapshot_scan_after_drop() {
131 1
    create_and_drop("after_drop_scan", |persy| {
132 1
        let mut tx = persy.begin().unwrap();
133 1
        tx.create_segment("test").unwrap();
134 1
        tx.create_index::<u8, u8>("index", ValueMode::Replace).unwrap();
135 1
        let bytes = String::from("something").into_bytes();
136 1
        tx.insert("test", &bytes).unwrap();
137 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
138 1
        let finalizer = tx.prepare().unwrap();
139 1
        finalizer.commit().unwrap();
140 1
        let snapshot = persy.snapshot().unwrap();
141

142 1
        let mut tx = persy.begin().unwrap();
143 1
        tx.drop_segment("test").unwrap();
144 1
        tx.drop_index("index").unwrap();
145 1
        let finalizer = tx.prepare().unwrap();
146 1
        finalizer.commit().unwrap();
147

148 1
        let read_after = snapshot.scan("test").unwrap();
149 1
        assert_eq!(read_after.into_iter().next().map(|(_, content)| (content)), Some(bytes));
150 1
        let mut read_after = snapshot.range::<u8, u8, _>("index", ..).unwrap();
151 1
        assert_eq!(
152 1
            read_after.next().map(|(_, value)| (value.into_iter().next().unwrap())),
153
            Some(20)
154
        );
155 1
    });
156 1
}
157

158
#[test]
159 1
fn test_snapshot_after_remove_scan() {
160 1
    create_and_drop("after_remove_scan", |persy| {
161 1
        let mut tx = persy.begin().unwrap();
162 1
        tx.create_segment("test").unwrap();
163 1
        tx.create_index::<u8, u8>("index", ValueMode::Replace).unwrap();
164 1
        let bytes = String::from("something").into_bytes();
165 1
        let id = tx.insert("test", &bytes).unwrap();
166 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
167 1
        let finalizer = tx.prepare().unwrap();
168 1
        finalizer.commit().unwrap();
169 1
        let snapshot = persy.snapshot().unwrap();
170

171 1
        let mut tx = persy.begin().unwrap();
172 1
        tx.delete("test", &id).unwrap();
173 1
        tx.remove::<u8, u8>("index", 10, None).unwrap();
174 1
        let finalizer = tx.prepare().unwrap();
175 1
        finalizer.commit().unwrap();
176

177 1
        let read_after = snapshot.read("test", &id).unwrap();
178 1
        assert_eq!(read_after, Some(bytes.clone()));
179 1
        assert_eq!(snapshot.get::<u8, u8>("index", &10).unwrap().next(), Some(20));
180 1
        let mut read_after = snapshot.scan("test").unwrap();
181 1
        assert_eq!(read_after.next().map(|(_, v)| v), Some(bytes));
182 1
        assert_eq!(
183 1
            snapshot
184
                .range::<u8, u8, _>("index", ..)
185
                .unwrap()
186
                .into_iter()
187
                .next()
188 1
                .map(|(_, v)| v.into_iter().next().unwrap()),
189
            Some(20)
190
        );
191 1
    });
192 1
}

Read our documentation on viewing source code .

Loading