tablestg 0.1.0

Storage for database tables
Documentation
pub use atom_file::Data;
use std::sync::Arc;

/// Key for Hash Map lookup.
pub trait Key {
    fn hash(&self) -> u64;
    fn equal(&self, r: u64) -> bool;
}

/// [HashMap] - maps keys to 64 bit ids using 64 bit hash.
pub mod hashmap;
pub use hashmap::*;

/// [PageSet] - keeps track of changed pages that need saving.
pub mod pageset;
pub use pageset::*;

/// [Table] -- record storage. Divided into pages, each page stores 64 records.
pub mod table;
pub use table::*;

/// [VarVal] -- storage of variable length values.
pub mod varval;
pub use varval::*;

// Private modules.

/// [PageTree] - list of pages implemented as tree.
mod pagetree;
use pagetree::*;

/// [TreeVec] -- a variable length list of bytes. Used to implement [Table].
mod treevec;
use treevec::*;

/// Hash Map Bucket - maps up to 127 keys to 64 bit ids using 64 bit hash.
mod bucket;

const PAGE_SIZE: u64 = 4612;
// pub const PAGE_SIZE: u64 =  4612;

#[test]
fn test() {
    use page_store::*;

    let limits = Limits::default();
    /*
    let _dt = DataType::String{};
    let _dt = DataType::Binary{};
    let _dt = DataType::Int(4);
    let _dt = DataType::Array( 5, Box::new(DataType::String) );
    let _dt = DataType::Map( Box::new(DataType::String), Box::new(DataType::String) );
    let _dt = DataType::Struct( Vec::new() );
    */

    // Construct BlockPageStg.
    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);
    }

    test_varval(&mut ps);

    // ps.save();

    spd.shutdown();
}

#[cfg(test)]
fn tos(s: &[u8]) -> &str {
    str::from_utf8(s).unwrap()
}

/*
   For a table with very fragmented ids, you can make a new table with new compact ids, and map the old ids to the new ones using a PagedHashMap.

   Access is slightly slower, due to the extra Hash lookup.
*/

pub enum DataType {
    /// e.g. date
    Named(u64), 
    
    /// e.g. `struct{ name: string, email: string, created: date }`
    Struct(Vec<(String, DataType)>),
    
    /// e.g. `( string, string, int )`
    Tuple(Vec<DataType>), 
    
    /// e.g. `enum{ leaf: int, node: [int] }`
    Enum(Vec<(String, DataType)>),

    /// e.g. `string`
    String(u8),
    
    /// e.g. `binary(50)` - 50 is the number of bytes stored inline.
    Binary(u8), 

    /// e.g. `int`
    Int(u8),

    /// e.g. `[string; 5]`
    Array(usize, Box<DataType>), 

    /// e.g. `[string]`
    List(Box<DataType>),

    /// e.g. `[string->int]`
    Map(Box<DataType>, Box<DataType>),
}

impl DataType {}

/* System tables.

   VarVal tables with diffent chunk sizes.
   Schema table
     Name : string
   Table table
     Schema, Name, DataType (varval), root/len, next_id

   DataType table
     Schema, Name, DataType (varval)

   Function table
     Schema, Name, Definition (varval)
*/