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 ok(&self, v: &T, ps: &mut PageSet) -> bool;
}
pub mod pageset;
pub use pageset::*;
pub mod hashmap;
pub use hashmap::*;
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 {
Int(i64),
String(LString),
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()
}
pub fn list(&mut self) -> &mut LVec<Value>
{
match self {
Value::List(list) => list,
_ => panic!()
}
}
pub fn ilist(&mut self) -> &mut LVec<i64>
{
match self {
Value::IList(list) => list,
_ => panic!()
}
}
pub fn string(&self) -> &LString
{
match self {
Value::String(s) => s,
_ => panic!()
}
}
pub fn int(&self) -> i64
{
match self {
Value::Int(x) => *x,
_ => panic!()
}
}
}
#[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);
}
#[cfg(test)]
#[derive(Hash)]
struct CustEmailKey<'a> {
email: &'a str,
}
#[cfg(test)]
impl <'a> Key<VarValAddr> for CustEmailKey<'a> {
fn ok(&self, v: &VarValAddr, ps: &mut PageSet) -> bool {
let mut list = v.get_value( ps );
let key = list.ilist()[0];
let hms = ps.cust_hashmap;
let mut hm = HashMap::restore( hms, ps );
let addr = hm.get(&key).unwrap();
let mut cr = addr.get_value( ps );
let email = cr.list()[2].string();
*self.email == **email
}
}
#[cfg(test)]
fn init(ps: &mut PageSet)
{
let hm = HashMap::<IdAndVVAddr>::new(ps, 1);
ps.cust_hashmap = hm.save();
let hm = HashMap::<VarValAddr>::new(ps, 1);
ps.cust_by_email = hm.save();
}
#[cfg(test)]
fn make_cust(ps: &mut PageSet, rid: i64, name: &str, email: &str, postal: &str)
{
use pstd::veca;
let cr = Value::List(veca![
Value::Int(rid), Value::String(LString::from(name)),
Value::String(LString::from(email)),
Value::String(LString::from(postal)),
]);
let addr = VarValAddr::new( &cr, ps );
{
let mut hm = HashMap::restore( ps.cust_hashmap, ps );
let val = IdAndVVAddr{ rid, addr };
hm.insert(&rid, val);
if hm.root_changed() { ps.cust_hashmap = hm.save(); }
}
let emkey = CustEmailKey { email };
let addr =
{
let mut hm = HashMap::restore( ps.cust_by_email, ps );
let addr = hm.remove(&emkey);
if hm.root_changed() { ps.cust_by_email = hm.save(); }
addr
};
let addr = if let Some(addr) = addr
{
let mut list = addr.get_value(ps);
list.ilist().push( rid );
addr.update_value(&list, ps)
} else {
let list = Value::IList(veca![rid]);
VarValAddr::new( &list, ps )
};
let mut hm = HashMap::restore( ps.cust_by_email, ps );
hm.insert(&emkey, addr);
if hm.root_changed() { ps.cust_by_email= hm.save(); }
}
#[cfg(test)]
fn list_cust(ps: &mut PageSet, email: &str)
{
let key = CustEmailKey { email };
let mut hm = HashMap::restore( ps.cust_by_email, ps );
if let Some(addr) = hm.get( &key )
{
let list = addr.get_value(ps);
println!( "List of cust ids for {} = {:?}", email, list );
}
else
{
println!("No cust ids found for email {}", email);
}
}
#[cfg(test)]
fn test_cust(ps: &mut PageSet) {
init(ps);
make_cust(ps, 99, "George Barwood", "george@gmail.com", "33 Sandpipe Close, GL2 4LZ" );
make_cust(ps, 100, "George Barwood", "george@gmail.com", "33 Sandpipe Close, GL2 4LZ" );
make_cust(ps, 101, "George Barwood", "george@gmail.com", "33 Sandpipe Close, GL2 4LZ" );
make_cust(ps, 102, "Marilyn Barwood", "maz.barwood@gmail.com", "33 Sandpipe Close, GL2 4LZ" );
list_cust(ps, "george@gmail.com" );
list_cust(ps, "maz.barwood@gmail.com" );
list_cust(ps, "mary@gmail.com" );
}