pub use atom_file::Data;
use std::hash::Hash;
use std::sync::Arc;
pub trait SmallFixed {
fn size() -> usize;
fn load(bytes: &[u8]) -> Self;
fn save(&self, bytes: &mut [u8]);
}
pub trait Key<T>: Hash
where
T: SmallFixed,
{
fn equal(&self, v: &T, ps: &mut PageSet) -> bool;
}
pub mod hashmap;
pub use hashmap::*;
pub mod pageset;
pub use pageset::*;
pub mod table;
pub use table::*;
pub mod varval;
pub use varval::*;
mod pagetree;
use pagetree::*;
mod treevec;
use treevec::*;
mod bucket;
const PAGE_SIZE: u64 = 4612;
#[test]
fn test_main() {
use page_store::*;
let limits = Limits::default();
let file = atom_file::MultiFileStorage::new("test.db");
let upd = atom_file::FastFileStorage::new("test.upd");
let af = atom_file::AtomicFile::new_with_limits(file, upd, &limits.af_lim);
let ps = BlockPageStg::new(af, &limits);
let is_new = ps.is_new();
let spd = SharedPagedData::new_from_ps(ps);
println!("max page size={}", spd.psi.max_size_page());
let mut ps = PageSet::new(spd.new_writer());
if false {
let (root, len) = if is_new { (ps.new_page(), 0) } else { (3, 410) };
test_tv(root, len, &mut ps);
}
if false {
println!("Calling test_hash");
hashmap::test_hash(&mut ps);
}
if false {
test_table(&mut ps);
}
if false {
test_varval(&mut ps);
}
test_cust(&mut ps);
spd.shutdown();
}
#[cfg(test)]
fn tos(s: &[u8]) -> &str {
str::from_utf8(s).unwrap()
}
#[derive(serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq, Debug)]
pub enum DataType {
Named(u64),
Struct(Vec<(String, DataType)>),
Tuple(Vec<DataType>),
Enum(Vec<(String, DataType)>),
String(u8),
Binary(u8),
Int(u8),
Array(usize, Box<DataType>),
List(Box<DataType>),
Map(Box<DataType>, Box<DataType>),
}
impl DataType {
pub fn to_bytes(&self) -> Vec<u8> {
postcard::to_stdvec(self).unwrap()
}
pub fn from_bytes(b: &[u8]) -> Self {
postcard::from_bytes(b).unwrap()
}
}
use pstd::localalloc::Local;
pub type LString = pstd::StringA<Local>;
pub type LVec<T> = pstd::VecA<T, Local>;
#[derive(serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq, Debug)]
pub enum Value {
String(LString),
Int(i64),
Binary(LVec<u8>),
List(LVec<Value>),
VarVal(u64, u64),
IList(LVec<i64>),
}
impl Value {
pub fn to_bytes(&self) -> Vec<u8> {
postcard::to_stdvec(self).unwrap()
}
pub fn from_bytes(b: &[u8]) -> Self {
postcard::from_bytes(b).unwrap()
}
}
#[test]
fn test_value() {
}
#[test]
fn test_datatype() {
let t1 = DataType::String(20);
let t2 = DataType::Int(3);
let t3 = DataType::Struct(vec![
("name".to_string(), t1.clone()),
("email".to_string(), t1.clone()),
]);
let t4 = DataType::Tuple(vec![t1, t2, t3]);
let bytes = t4.to_bytes();
let t5 = DataType::from_bytes(&bytes);
assert_eq!(t5, t4);
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct IdAndVVAddr {
rid: i64,
id: u64,
len: usize,
}
impl SmallFixed for IdAndVVAddr {
fn size() -> usize {
3 * 8
}
fn load(bytes: &[u8]) -> Self {
let rid = i64::from_le_bytes(bytes[0..8].try_into().unwrap());
let id = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
let len = usize::from_le_bytes(bytes[16..24].try_into().unwrap());
Self { rid, id, len }
}
fn save(&self, bytes: &mut [u8]) {
bytes[0..8].copy_from_slice(&self.rid.to_le_bytes());
bytes[8..16].copy_from_slice(&self.id.to_le_bytes());
bytes[16..24].copy_from_slice(&self.len.to_le_bytes());
}
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct VarValAddr {
id: u64,
len: usize,
}
impl SmallFixed for VarValAddr {
fn size() -> usize {
2 * 8
}
fn load(bytes: &[u8]) -> Self {
let id = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
let len = usize::from_le_bytes(bytes[8..16].try_into().unwrap());
Self { id, len }
}
fn save(&self, bytes: &mut [u8]) {
bytes[0..8].copy_from_slice(&self.id.to_le_bytes());
bytes[8..16].copy_from_slice(&self.len.to_le_bytes());
}
}
#[cfg(test)]
impl Key<IdAndVVAddr> for i64 {
fn equal(&self, v: &IdAndVVAddr, _ps: &mut PageSet) -> bool {
*self == v.rid
}
}
#[cfg(test)]
#[derive(Hash)]
struct EmailKey {
email: String,
}
#[cfg(test)]
impl Key<VarValAddr> for EmailKey {
fn equal(&self, _v: &VarValAddr, _ps: &mut PageSet) -> bool {
true
}
}
#[cfg(test)]
fn test_cust(ps: &mut PageSet) {
use pstd::veca;
let rid: i64 = 99;
let george_email = "george.barwood@gmail.com";
let v1 = Value::List(veca![
Value::Int(rid), Value::String(LString::from("George Barwood")),
Value::String(LString::from(george_email)),
Value::String(LString::from("33 Sandpipe Close, GL2 4LZ")),
]);
let bytes = v1.to_bytes();
let root = ps.new_page();
let mut vv = VarVal::new( (0, root, 0) );
let id = vv.store(&bytes, ps);
{
let mut hm = HashMap::new(ps, 1);
let addr = IdAndVVAddr {
rid,
id,
len: bytes.len(),
};
let key = rid;
hm.insert(&key, addr);
let key = rid;
let x = hm.get(&key);
println!("x={:?}", x);
assert_eq!(x, Some(addr));
let x = x.unwrap();
let mut buf = vec![0; x.len];
vv.get(x.id, x.len, &mut buf, ps);
let v2 = Value::from_bytes(&buf);
println!("v2={:?}", v2);
assert_eq!( v1, v2 );
}
let key2 = EmailKey {
email: george_email.to_string(),
};
let hm2s = {
let mut hm2 = HashMap::new(ps, 1);
let x = hm2.remove(&key2);
assert!(x == None); hm2.save()
};
let list = Value::List(veca![Value::Int(rid)]);
let bytes = list.to_bytes();
let id = vv.store(&bytes, ps);
let val = VarValAddr {
id,
len: bytes.len(),
};
let mut hm2 = HashMap::restore(ps, hm2s);
hm2.insert(&key2, val);
let key3 = EmailKey {
email: george_email.to_string(),
};
let x = hm2.get( &key3 );
println!("x={:?}", x);
let x = x .unwrap();
assert_eq!(x, val );
let mut buf = vec![0; x.len];
vv.get(x.id, x.len, &mut buf, ps);
let list2 = Value::from_bytes(&buf);
println!( "list={:?}", list );
assert_eq!( list, list2 );
}