Start database implementation.

This commit is contained in:
yukirij 2025-05-22 22:17:43 -07:00
parent b92ee28bef
commit 5d4cd09497
5 changed files with 184 additions and 0 deletions

19
src/database/header.rs Normal file
View File

@ -0,0 +1,19 @@
pub struct Header {
pub depth:u8,
pub root:u32,
}
impl Header {
pub fn serialize(&self) -> [u8;16]
{
[0; 16]
}
}
impl Default for Header {
fn default() -> Self
{
Self {
depth:1,
root:1,
}
}
}

82
src/database/mod.rs Normal file
View File

@ -0,0 +1,82 @@
use tokio::{
fs::File,
io::{
Error, SeekFrom,
AsyncWriteExt, AsyncSeekExt
},
};
use std::collections::BTreeSet;
mod header;
mod page;
mod trie;
const PAGE_SIZE :u64 = 4096;
enum Path {
Index(usize),
Key(&u8),
}
pub struct Database {
file:File,
temp:BTreeSet<u64>,
}
impl Database {
async fn init(&mut self) -> Result<(),Error>
{
let mut header = header::Header::default();
header.depth = 1;
header.root = 2;
let buffered = header.serialize();
self.file.set_len(PAGE_SIZE * 3).await?;
self.file.seek(SeekFrom::Start(0)).await?;
self.file.write(&buffered).await?;
self.file.seek(SeekFrom::Start(PAGE_SIZE)).await?;
self.file.write(&buffered).await?;
Ok(())
}
}
impl From<File> for Database {
fn from(value: File) -> Self
{
Self {
file:value,
temp:BTreeSet::new(),
}
}
}
/* Object
** Meta [16]
** [0:3] Type
**
** 00 Blob
** [3:3] Size
**
** 01 List
** []
**
** 10 Trie
**
** Addr [48]
*/
/* Page Table Entry
** <>
*/
/* Allocation Table Entry
** <>
**
*/
/* Header
** <>
**
*/

74
src/database/page.rs Normal file
View File

@ -0,0 +1,74 @@
pub enum Usage {
Blob = 0b00,
List = 0b01,
Trie = 0b10,
Table = 0b11,
}
pub struct Entry {
addr:u64,
res:u64,
}
impl Entry {
pub fn new() -> Self
{
Self {
addr:0,
res:0,
}
}
pub fn blob(address:u64, size:u8) -> Self
{
let addr = address & ((1 << 48) - 1);
let size = (size as u64) << 48;
Self {
addr: size|addr,
res: 0,
}
}
pub fn serialize(&self) -> [u8;16]
{
[
(self.addr & 0xFF) as u8,
((self.addr >> 8) & 0xFF) as u8,
((self.addr >> 16) & 0xFF) as u8,
((self.addr >> 24) & 0xFF) as u8,
((self.addr >> 32) & 0xFF) as u8,
((self.addr >> 40) & 0xFF) as u8,
((self.addr >> 48) & 0xFF) as u8,
((self.addr >> 56) & 0xFF) as u8,
(self.res & 0xFF) as u8,
((self.res >> 8) & 0xFF) as u8,
((self.res >> 16) & 0xFF) as u8,
((self.res >> 24) & 0xFF) as u8,
((self.res >> 32) & 0xFF) as u8,
((self.res >> 40) & 0xFF) as u8,
((self.res >> 48) & 0xFF) as u8,
((self.res >> 56) & 0xFF) as u8,
]
}
pub fn deserialize(&mut self, data:[u8;16])
{
self.addr = data[0] as u64
|(data[1] << 8) as u64
|(data[2] << 16) as u64
|(data[3] << 24) as u64
|(data[4] << 32) as u64
|(data[5] << 40) as u64
|(data[6] << 48) as u64
|(data[7] << 56) as u64;
self.res = data[8] as u64
|(data[9] << 8) as u64
|(data[10] << 16) as u64
|(data[11] << 24) as u64
|(data[12] << 32) as u64
|(data[13] << 40) as u64
|(data[14] << 48) as u64
|(data[15] << 56) as u64;
}
}

8
src/database/trie.rs Normal file
View File

@ -0,0 +1,8 @@
pub struct TrieEntry {
meta:u8,
bytes:[u8; 11],
child:u32,
lesser:u32,
greater:u32,
data:u64,
}

View File

@ -2,3 +2,4 @@
mod blockfile; pub use blockfile::BlockFile;
mod triefile; pub use triefile::TrieFile;
mod database; pub use database::Database;