1
use crate::{
2
    error::{PRes, PersyError},
3
    id::{PersyId, RecRef},
4
    index::{
5
        config::{ByteVec, IndexType, IndexTypeId},
6
        keeper::Container,
7
        nodes::{Leaf, LeafEntry, Node, NodeRef, Nodes, Value},
8
    },
9
};
10
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
11
use std::io::{Cursor, Read, Write};
12

13
macro_rules! impl_index_type {
14
    ($t:ty, $v:expr,$v1:ident,$w:ty) => {
15
        impl IndexType for $t {
16
            type Wrapper = Container<Self>;
17 0
            fn get_id() -> u8 {
18
                $v
19 1
            }
20
            fn get_type_id() -> IndexTypeId {
21
                IndexTypeId::$v1
22
            }
23
        }
24
    };
25
}
26

27
impl_index_type!(u8, 1, U8, u8);
28
impl_index_type!(u16, 2, U16, u16);
29
impl_index_type!(u32, 3, U32, u32);
30
impl_index_type!(u64, 4, U64, u64);
31
impl_index_type!(u128, 14, U128, u128);
32
impl_index_type!(i8, 5, I8, i8);
33
impl_index_type!(i16, 6, I16, i16);
34
impl_index_type!(i32, 7, I32, i32);
35
impl_index_type!(i64, 8, I64, i64);
36
impl_index_type!(i128, 15, I128, i128);
37
impl_index_type!(f32, 9, F32W, F32W);
38
impl_index_type!(f64, 10, F64W, F64W);
39
impl_index_type!(String, 12, STRING, String);
40
impl_index_type!(PersyId, 13, PERSYID, PersyId);
41
impl_index_type!(ByteVec, 16, BYTEVEC, ByteVec);
42

43
pub trait IndexSerialization: Sized {
44
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()>;
45
    fn deserialize(value: &mut dyn Read) -> PRes<Self>;
46
}
47

48 1
pub fn deserialize<K: IndexType, V: IndexType>(value: &[u8]) -> PRes<Node<K, V>> {
49 1
    let mut reader = Cursor::new(value);
50 1
    let t = reader.read_u8()?;
51 1
    match t {
52 1
        1 => {
53 1
            let prev = if reader.read_u8()? == 1 {
54 1
                Some(K::deserialize(&mut reader)?)
55 0
            } else {
56 1
                None
57
            };
58 1
            let size = reader.read_u32::<BigEndian>()?;
59 1
            let mut entries = Vec::with_capacity(size as usize);
60 1
            for _ in 0..size {
61 1
                let key = K::deserialize(&mut reader)?;
62 1
                let value_type = reader.read_u8()?;
63 1
                if value_type == 1 {
64 1
                    let val_size = reader.read_u32::<BigEndian>()?;
65 1
                    let mut value = Vec::with_capacity(val_size as usize);
66 1
                    for _ in 0..val_size {
67 1
                        value.push(V::deserialize(&mut reader)?);
68
                    }
69 1
                    entries.push(LeafEntry {
70 1
                        key,
71 1
                        value: Value::CLUSTER(value),
72
                    });
73 1
                } else {
74 1
                    let value = V::deserialize(&mut reader)?;
75 1
                    entries.push(LeafEntry {
76 1
                        key,
77 1
                        value: Value::SINGLE(value),
78
                    });
79 0
                }
80 1
            }
81 1
            let next = if reader.read_u8()? == 1 {
82 1
                Some(K::deserialize(&mut reader)?)
83 0
            } else {
84 1
                None
85
            };
86 1
            Ok(Node::LEAF(Leaf { entries, prev, next }))
87 0
        }
88
        2 => {
89 1
            let prev = if reader.read_u8()? == 1 {
90 0
                Some(K::deserialize(&mut reader)?)
91 0
            } else {
92 1
                None
93
            };
94 1
            let size = reader.read_u32::<BigEndian>()?;
95 1
            let mut keys = Vec::with_capacity(size as usize);
96 1
            for _ in 0..size {
97 1
                let key = K::deserialize(&mut reader)?;
98 1
                keys.push(key);
99 0
            }
100 1
            let size = reader.read_u32::<BigEndian>()?;
101 1
            let mut pointers = Vec::with_capacity(size as usize);
102 1
            for _ in 0..size {
103 1
                let page = reader.read_u64::<BigEndian>()?;
104 1
                let pos = reader.read_u32::<BigEndian>()?;
105 1
                pointers.push(RecRef::new(page, pos));
106
            }
107 1
            let next = if reader.read_u8()? == 1 {
108 0
                Some(K::deserialize(&mut reader)?)
109 0
            } else {
110 1
                None
111
            };
112 1
            Ok(Node::NODE(Nodes {
113 1
                keys,
114 1
                pointers,
115 1
                prev,
116 1
                next,
117
            }))
118 0
        }
119 0
        _ => panic!("error on index node deserialization"),
120
    }
121 1
}
122

123 1
pub fn serialize<K: IndexType, V: IndexType>(node: &Node<K, V>) -> PRes<Vec<u8>> {
124 1
    let mut dest = Vec::new();
125

126 1
    let write_page_and_pos = |dest: &mut Vec<u8>, page, pos| -> PRes<()> {
127 1
        dest.write_u64::<BigEndian>(page)?;
128 1
        dest.write_u32::<BigEndian>(pos)?;
129 1
        Ok(())
130 1
    };
131

132 1
    let write_opt_leafptr = |dest: &mut Vec<u8>, x: Option<&NodeRef>| -> PRes<()> {
133 1
        if let Some(y) = x {
134 1
            write_page_and_pos(dest, y.page, y.pos)?;
135
        } else {
136 0
            write_page_and_pos(dest, 0, 0)?;
137
        }
138 1
        Ok(())
139 1
    };
140

141 1
    match node {
142 1
        Node::LEAF(leaf) => {
143 1
            dest.write_u8(1)?;
144 1
            if let Some(pk) = &leaf.prev {
145 1
                dest.write_u8(1)?;
146 1
                pk.serialize(&mut dest)?;
147
            } else {
148 1
                dest.write_u8(0)?;
149
            }
150 1
            dest.write_u32::<BigEndian>(leaf.entries.len() as u32)?;
151 1
            for entry in &leaf.entries {
152 1
                entry.key.serialize(&mut dest)?;
153 1
                match &entry.value {
154 1
                    Value::CLUSTER(cluster) => {
155 1
                        dest.write_u8(1)?;
156 1
                        dest.write_u32::<BigEndian>(cluster.len() as u32)?;
157 1
                        for val in cluster {
158 1
                            val.serialize(&mut dest)?;
159
                        }
160
                    }
161 1
                    Value::SINGLE(val) => {
162 1
                        dest.write_u8(2)?;
163 1
                        val.serialize(&mut dest)?;
164
                    }
165
                }
166
            }
167 1
            if let Some(pk) = &leaf.next {
168 1
                dest.write_u8(1)?;
169 1
                pk.serialize(&mut dest)?;
170
            } else {
171 1
                dest.write_u8(0)?;
172
            }
173
        }
174 1
        Node::NODE(node) => {
175 1
            dest.write_u8(2)?;
176 1
            if let Some(pk) = &node.prev {
177 0
                dest.write_u8(1)?;
178 0
                pk.serialize(&mut dest)?;
179
            } else {
180 1
                dest.write_u8(0)?;
181
            }
182 1
            dest.write_u32::<BigEndian>(node.keys.len() as u32)?;
183 1
            for k in &node.keys {
184 1
                k.serialize(&mut dest)?;
185
            }
186 1
            dest.write_u32::<BigEndian>(node.pointers.len() as u32)?;
187 1
            for p in &node.pointers {
188 1
                write_opt_leafptr(&mut dest, Some(p))?;
189
            }
190 1
            if let Some(pk) = &node.next {
191 0
                dest.write_u8(1)?;
192 0
                pk.serialize(&mut dest)?;
193
            } else {
194 1
                dest.write_u8(0)?;
195
            }
196
        }
197
    }
198 1
    Ok(dest)
199 1
}
200

201
impl IndexSerialization for u8 {
202 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
203 1
        buffer.write_u8(*self).map_err(PersyError::from)
204 1
    }
205 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
206 1
        value.read_u8().map_err(PersyError::from)
207 1
    }
208
}
209

210
impl IndexSerialization for u16 {
211 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
212 1
        buffer.write_u16::<BigEndian>(*self).map_err(PersyError::from)
213 1
    }
214 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
215 1
        value.read_u16::<BigEndian>().map_err(PersyError::from)
216 1
    }
217
}
218

219
impl IndexSerialization for u32 {
220 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
221 1
        buffer.write_u32::<BigEndian>(*self).map_err(PersyError::from)
222 1
    }
223 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
224 1
        value.read_u32::<BigEndian>().map_err(PersyError::from)
225 1
    }
226
}
227

228
impl IndexSerialization for u64 {
229 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
230 1
        buffer.write_u64::<BigEndian>(*self).map_err(PersyError::from)
231 1
    }
232 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
233 1
        value.read_u64::<BigEndian>().map_err(PersyError::from)
234 1
    }
235
}
236

237
impl IndexSerialization for u128 {
238 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
239 1
        buffer.write_u128::<BigEndian>(*self).map_err(PersyError::from)
240 1
    }
241 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
242 1
        value.read_u128::<BigEndian>().map_err(PersyError::from)
243 1
    }
244
}
245

246
impl IndexSerialization for i8 {
247 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
248 1
        buffer.write_i8(*self).map_err(PersyError::from)
249 1
    }
250 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
251 1
        value.read_i8().map_err(PersyError::from)
252 1
    }
253
}
254

255
impl IndexSerialization for i16 {
256 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
257 1
        buffer.write_i16::<BigEndian>(*self).map_err(PersyError::from)
258 1
    }
259 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
260 1
        value.read_i16::<BigEndian>().map_err(PersyError::from)
261 1
    }
262
}
263

264
impl IndexSerialization for i32 {
265 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
266 1
        buffer.write_i32::<BigEndian>(*self).map_err(PersyError::from)
267 1
    }
268 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
269 1
        value.read_i32::<BigEndian>().map_err(PersyError::from)
270 1
    }
271
}
272

273
impl IndexSerialization for i64 {
274 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
275 1
        buffer.write_i64::<BigEndian>(*self).map_err(PersyError::from)
276 1
    }
277 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
278 1
        value.read_i64::<BigEndian>().map_err(PersyError::from)
279 1
    }
280
}
281

282
impl IndexSerialization for i128 {
283 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
284 1
        buffer.write_i128::<BigEndian>(*self).map_err(PersyError::from)
285 1
    }
286 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
287 1
        value.read_i128::<BigEndian>().map_err(PersyError::from)
288 1
    }
289
}
290

291
impl IndexSerialization for f32 {
292 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
293 1
        buffer.write_f32::<BigEndian>(*self).map_err(PersyError::from)
294 1
    }
295 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
296 1
        value.read_f32::<BigEndian>().map_err(PersyError::from)
297 1
    }
298
}
299

300
impl IndexSerialization for f64 {
301 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
302 1
        buffer.write_f64::<BigEndian>(*self).map_err(PersyError::from)
303 1
    }
304 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
305 1
        value.read_f64::<BigEndian>().map_err(PersyError::from)
306 1
    }
307
}
308

309
impl IndexSerialization for PersyId {
310 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
311 1
        buffer.write_u64::<BigEndian>(self.0.page)?;
312 1
        buffer.write_u32::<BigEndian>(self.0.pos)?;
313 1
        Ok(())
314 1
    }
315 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
316 1
        let page = value.read_u64::<BigEndian>()?;
317 1
        let pos = value.read_u32::<BigEndian>()?;
318 1
        Ok(PersyId(RecRef::new(page, pos)))
319 1
    }
320
}
321

322
impl IndexSerialization for String {
323 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
324 1
        buffer.write_u16::<BigEndian>(self.len() as u16)?;
325 1
        buffer.write_all(self.as_bytes())?;
326 1
        Ok(())
327 1
    }
328 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
329 1
        let string_size = value.read_u16::<BigEndian>()?;
330 1
        let mut string = String::with_capacity(string_size as usize);
331 1
        value.take(u64::from(string_size)).read_to_string(&mut string)?;
332 1
        Ok(string)
333 1
    }
334
}
335

336
impl IndexSerialization for ByteVec {
337 1
    fn serialize(&self, buffer: &mut dyn Write) -> PRes<()> {
338 1
        buffer.write_u16::<BigEndian>(self.0.len() as u16)?;
339 1
        buffer.write_all(self.0.as_slice())?;
340 1
        Ok(())
341 1
    }
342 1
    fn deserialize(value: &mut dyn Read) -> PRes<Self> {
343 1
        let vec_size = value.read_u16::<BigEndian>()?;
344 1
        let mut slice: Vec<u8> = Vec::new();
345 1
        value.take(u64::from(vec_size)).read_to_end(&mut slice)?;
346 1
        Ok(ByteVec(slice))
347 1
    }
348
}
349

350
#[cfg(test)]
351
mod tests {
352
    use super::{deserialize, serialize};
353
    use crate::id::{PersyId, RecRef};
354
    use crate::index::{
355
        config::{ByteVec, IndexType, ValueMode},
356
        nodes::{compare, Leaf, Node, NodeRef, Nodes, Value},
357
    };
358
    use rand::random;
359
    use std::{cmp::Ordering, fmt::Debug};
360

361 1
    fn random_pointer() -> NodeRef {
362 1
        RecRef::new(random::<u64>(), random::<u32>())
363 1
    }
364

365
    #[test]
366 1
    fn test_serialization_deserialization_nodes() {
367 1
        let val1 = random_pointer();
368 1
        let val2 = random_pointer();
369 1
        let val3 = random_pointer();
370 1
        let mut node = Nodes::new_from_split(val1, &[(0, val2)]);
371 1
        let pos = node.find(&2).pos;
372 1
        node.add(pos, &2, val3.clone());
373 1
        let value = serialize::<u8, u8>(&Node::NODE(node)).expect("serialization works");
374 1
        let read = deserialize::<u8, u8>(&value).expect("deserialzie successfully");
375
        match read {
376 1
            Node::NODE(n) => {
377 1
                assert_eq!(n.keys.len(), 2);
378 1
                assert_eq!(n.pointers.len(), 3);
379
            }
380 1
            _ => panic!("expected a node"),
381
        }
382 1
    }
383

384 1
    fn single_type_leaf_test<K: IndexType + Debug, V: IndexType + Debug>(key: K, value: V, value1: V) {
385 1
        let mut leaf = Leaf::new();
386 1
        leaf.insert_or_update(&key, &value, ValueMode::REPLACE, "deserialization error")
387
            .expect("insert work");
388 1
        let binary = serialize::<K, V>(&Node::LEAF(leaf)).expect("serialization works");
389 1
        let read = deserialize::<K, V>(&binary).expect("deserialize successfully");
390
        match read {
391 1
            Node::LEAF(n) => {
392 1
                assert_eq!(n.entries.len(), 1);
393 1
                match n.entries[0].value {
394 1
                    Value::SINGLE(ref iv) => assert_eq!(compare(iv, &value), Ordering::Equal),
395 0
                    _ => panic!("expected SINGLE"),
396
                }
397
            }
398 1
            _ => panic!("expected a leaf"),
399
        }
400 1
        let mut leaf_many = Leaf::new();
401 1
        leaf_many
402
            .insert_or_update(&key, &value, ValueMode::CLUSTER, "deserialization error")
403
            .expect("insert work");
404 1
        leaf_many
405
            .insert_or_update(&key, &value1, ValueMode::CLUSTER, "deserialization error")
406
            .expect("insert work");
407 1
        let binary = serialize::<K, V>(&Node::LEAF(leaf_many)).expect("serialization works");
408 1
        let read = deserialize::<K, V>(&binary).expect("deserialize successfully");
409
        match read {
410 1
            Node::LEAF(n) => {
411 1
                assert_eq!(n.entries.len(), 1);
412 1
                match n.entries[0].value {
413 1
                    Value::CLUSTER(ref iv) => {
414 1
                        assert_eq!(compare(&iv[0], &value), Ordering::Equal);
415 1
                        assert_eq!(compare(&iv[1], &value1), Ordering::Equal);
416
                    }
417 0
                    _ => panic!("expected CLUSTER"),
418
                }
419
            }
420 1
            _ => panic!("expected a leaf"),
421
        }
422 1
    }
423

424
    #[test]
425 1
    fn test_serialization_deserialization_leafs() {
426 1
        single_type_leaf_test::<u8, u8>(20, 10, 20);
427 1
        single_type_leaf_test::<u16, u16>(20, 10, 20);
428 1
        single_type_leaf_test::<u32, u32>(20, 10, 20);
429 1
        single_type_leaf_test::<u64, u64>(20, 10, 20);
430 1
        single_type_leaf_test::<u128, u128>(20, 10, 20);
431 1
        single_type_leaf_test::<i8, i8>(20, 10, 20);
432 1
        single_type_leaf_test::<i16, i16>(20, 10, 20);
433 1
        single_type_leaf_test::<i32, i32>(20, 10, 20);
434 1
        single_type_leaf_test::<i64, i64>(20, 10, 20);
435 1
        single_type_leaf_test::<i128, i128>(20, 10, 20);
436 1
        single_type_leaf_test::<f32, f32>(20.0, 10.0, 20.0);
437 1
        single_type_leaf_test::<f64, f64>(20.0, 10.0, 20.0);
438 1
        single_type_leaf_test::<String, String>("o".to_string(), "a".to_string(), "b".to_string());
439 1
        single_type_leaf_test::<i32, String>(10, "a".to_string(), "b".to_string());
440 1
        single_type_leaf_test::<String, i32>("a".to_string(), 10, 20);
441 1
        single_type_leaf_test::<String, ByteVec>("a".to_string(), vec![0, 1].into(), vec![2, 10].into());
442 1
        let id = PersyId(RecRef::new(10, 20));
443 1
        let id1 = PersyId(RecRef::new(20, 20));
444 1
        let id2 = PersyId(RecRef::new(30, 20));
445 1
        single_type_leaf_test::<PersyId, PersyId>(id, id1, id2);
446 1
    }
447
}

Read our documentation on viewing source code .

Loading