Print first n entries
This commit is contained in:
parent
eb034592fa
commit
cad4ba8215
5 changed files with 481 additions and 316 deletions
78
storage_engine/src/binary_coding.rs
Normal file
78
storage_engine/src/binary_coding.rs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
use bincode;
|
||||
use bincode::{Decode, Encode};
|
||||
use bincode::config::{BigEndian, Configuration, Fixint};
|
||||
use std::mem::size_of;
|
||||
|
||||
const BIN_CONFIG: Configuration<BigEndian, Fixint> = bincode::config::standard().with_big_endian().with_fixed_int_encoding();
|
||||
|
||||
pub fn encode<T: Encode>(t: &T) -> Result<Vec<u8>, bincode::error::EncodeError> {
|
||||
bincode::encode_to_vec(t, BIN_CONFIG)
|
||||
}
|
||||
|
||||
pub fn decode<T: Decode>(bytes: &[u8]) -> Result<(T, usize), bincode::error::DecodeError> {
|
||||
bincode::decode_from_slice(bytes, BIN_CONFIG)
|
||||
}
|
||||
|
||||
pub fn encode_vector<T: Encode>(ts: &[T]) -> Result<Vec<u8>, bincode::error::EncodeError> {
|
||||
let size: usize = ts.len();
|
||||
let mut result = encode(&size)?;
|
||||
for t in ts {
|
||||
result.append(&mut encode(&t)?);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn decode_vector<T: Decode>(bytes: &[u8]) -> Result<Vec<T>, bincode::error::DecodeError> {
|
||||
let mut offset = size_of::<usize>();
|
||||
let result_len: usize = decode(&bytes[..offset])?.0;
|
||||
let mut result: Vec<T> = Vec::with_capacity(result_len);
|
||||
for _ in 0..result_len {
|
||||
let (x, bytes_consumed) = decode::<T>(&bytes[offset..])?;
|
||||
offset += bytes_consumed;
|
||||
result.push(x);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// We don't care about encoding the length here (since it will be used for a row with known column
|
||||
// size)
|
||||
pub fn encode_sequence<T: Encode>(ts: &[T]) -> Result<Vec<u8>, bincode::error::EncodeError> {
|
||||
let mut result = vec![];
|
||||
for t in ts {
|
||||
result.append(&mut encode(&t)?);
|
||||
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn decode_sequence<T: Decode>(len: usize, bytes: &[u8]) -> Result<Vec<T>, bincode::error::DecodeError> {
|
||||
let mut result: Vec<T> = Vec::with_capacity(len);
|
||||
let mut offset = 0;
|
||||
for _ in 0..len {
|
||||
let (x, bytes_consumed) = decode::<T>(&bytes[offset..])?;
|
||||
offset += bytes_consumed;
|
||||
result.push(x);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
|
||||
fn example_encoding_decoding() {
|
||||
let xs: Vec<u32> = vec![123, 250, 256, 123, 123, 123];
|
||||
let xs: Vec<u32> = vec![];
|
||||
let xs: Vec<u32> = vec![123];
|
||||
let xs: Vec<u32> = vec![123, 250];
|
||||
|
||||
let xs: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
|
||||
|
||||
|
||||
println!("original {:?}", xs);
|
||||
let exs = encode_vector(&xs[..]).unwrap();
|
||||
println!("encoded {:?}", exs);
|
||||
|
||||
// WARNING: Don't forget to specify the type here
|
||||
// let dxs = decode_vector::<u32>(&exs[..]).unwrap();
|
||||
let dxs = decode_vector::<String>(&exs[..]).unwrap();
|
||||
println!("decoded {:?}", dxs);
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue