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!(
28 1
            snapshot
29
                .get::<u8, u8>("index", &10)
30
                .unwrap()
31
                .unwrap()
32
                .into_iter()
33
                .next(),
34
            Some(20)
35
        );
36 1
    });
37 1
}
38

39
#[test]
40 1
fn test_double_snapshot_after_drop() {
41 1
    create_and_drop("double_after_drop", |persy| {
42 1
        let mut tx = persy.begin().unwrap();
43 1
        tx.create_segment("test").unwrap();
44 1
        tx.create_index::<u8, u8>("index", ValueMode::REPLACE).unwrap();
45 1
        let bytes = String::from("something").into_bytes();
46 1
        let id = tx.insert("test", &bytes).unwrap();
47 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
48 1
        let finalizer = tx.prepare().unwrap();
49 1
        finalizer.commit().unwrap();
50 1
        let snapshot = persy.snapshot().unwrap();
51 1
        let snapshot1 = persy.snapshot().unwrap();
52

53 1
        let mut tx = persy.begin().unwrap();
54 1
        tx.drop_segment("test").unwrap();
55 1
        tx.drop_index("index").unwrap();
56 1
        let finalizer = tx.prepare().unwrap();
57 1
        finalizer.commit().unwrap();
58

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

72 1
        let bytes = String::from("something").into_bytes();
73 1
        let read_after = snapshot1.read("test", &id).unwrap();
74 1
        assert_eq!(read_after, Some(bytes));
75 1
        assert_eq!(
76 1
            snapshot1
77
                .get::<u8, u8>("index", &10)
78
                .unwrap()
79
                .unwrap()
80
                .into_iter()
81
                .next(),
82
            Some(20)
83
        );
84 1
        drop(snapshot1);
85

86 1
        let bytes = String::from("something").into_bytes();
87 1
        let read_after = snapshot.read("test", &id).unwrap();
88 1
        assert_eq!(read_after, Some(bytes));
89 1
        assert_eq!(
90 1
            snapshot
91
                .get::<u8, u8>("index", &10)
92
                .unwrap()
93
                .unwrap()
94
                .into_iter()
95
                .next(),
96
            Some(20)
97
        );
98 1
    });
99 1
}
100

101
#[test]
102 1
fn test_snapshot_ignore_newers() {
103 1
    create_and_drop("ignore_newers", |persy| {
104 1
        let mut tx = persy.begin().unwrap();
105 1
        tx.create_segment("test").unwrap();
106 1
        tx.create_index::<u8, u8>("index", ValueMode::REPLACE).unwrap();
107 1
        let bytes = String::from("something").into_bytes();
108 1
        tx.insert("test", &bytes).unwrap();
109 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
110 1
        let finalizer = tx.prepare().unwrap();
111 1
        finalizer.commit().unwrap();
112 1
        let snapshot = persy.snapshot().unwrap();
113

114 1
        let mut tx = persy.begin().unwrap();
115 1
        let bytes = String::from("other").into_bytes();
116 1
        let id2 = tx.insert("test", &bytes).unwrap();
117 1
        tx.put::<u8, u8>("index", 11, 20).unwrap();
118 1
        let finalizer = tx.prepare().unwrap();
119 1
        finalizer.commit().unwrap();
120

121 1
        let read_after = snapshot.read("test", &id2).unwrap();
122 1
        assert_eq!(read_after, None);
123 1
        assert_eq!(snapshot.get::<u8, u8>("index", &11).unwrap(), None);
124 1
    });
125 1
}
126

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

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

146 1
        let read_after = snapshot.list_segments().unwrap();
147 1
        assert_eq!(read_after.len(), 1);
148 1
        assert_eq!(
149 1
            read_after.into_iter().next().map(|(name, _)| (name)),
150 1
            Some("test".to_string())
151
        );
152 1
        let read_after = snapshot.list_indexes().unwrap();
153 1
        assert_eq!(read_after.len(), 1);
154 1
        assert_eq!(
155 1
            read_after.into_iter().next().map(|(name, _)| (name)),
156 1
            Some("index".to_string())
157
        );
158 1
    });
159 1
}
160

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

174 1
        let mut tx = persy.begin().unwrap();
175 1
        tx.drop_segment("test").unwrap();
176 1
        tx.drop_index("index").unwrap();
177 1
        let finalizer = tx.prepare().unwrap();
178 1
        finalizer.commit().unwrap();
179

180 1
        let read_after = snapshot.scan("test").unwrap();
181 1
        assert_eq!(read_after.into_iter().next().map(|(_, content)| (content)), Some(bytes));
182 1
        let mut read_after = snapshot.range::<u8, u8, _>("index", ..).unwrap();
183 1
        assert_eq!(
184 1
            read_after.next().map(|(_, value)| (value.into_iter().next().unwrap())),
185
            Some(20)
186
        );
187 1
    });
188 1
}
189

190
#[test]
191 1
fn test_snapshot_after_remove_scan() {
192 1
    create_and_drop("after_remove_scan", |persy| {
193 1
        let mut tx = persy.begin().unwrap();
194 1
        tx.create_segment("test").unwrap();
195 1
        tx.create_index::<u8, u8>("index", ValueMode::REPLACE).unwrap();
196 1
        let bytes = String::from("something").into_bytes();
197 1
        let id = tx.insert("test", &bytes).unwrap();
198 1
        tx.put::<u8, u8>("index", 10, 20).unwrap();
199 1
        let finalizer = tx.prepare().unwrap();
200 1
        finalizer.commit().unwrap();
201 1
        let snapshot = persy.snapshot().unwrap();
202

203 1
        let mut tx = persy.begin().unwrap();
204 1
        tx.delete("test", &id).unwrap();
205 1
        tx.remove::<u8, u8>("index", 10, None).unwrap();
206 1
        let finalizer = tx.prepare().unwrap();
207 1
        finalizer.commit().unwrap();
208

209 1
        let read_after = snapshot.read("test", &id).unwrap();
210 1
        assert_eq!(read_after, Some(bytes.clone()));
211 1
        assert_eq!(
212 1
            snapshot
213
                .get::<u8, u8>("index", &10)
214
                .unwrap()
215
                .unwrap()
216
                .into_iter()
217
                .next(),
218
            Some(20)
219
        );
220 1
        let mut read_after = snapshot.scan("test").unwrap();
221 1
        assert_eq!(read_after.next().map(|(_, v)| v), Some(bytes));
222 1
        assert_eq!(
223 1
            snapshot
224
                .range::<u8, u8, _>("index", ..)
225
                .unwrap()
226
                .into_iter()
227
                .next()
228 1
                .map(|(_, v)| v.into_iter().next().unwrap()),
229
            Some(20)
230
        );
231 1
    });
232 1
}

Read our documentation on viewing source code .

Loading