Skip to content

Commit

Permalink
feat: debug trie_const
Browse files Browse the repository at this point in the history
  • Loading branch information
i007c committed Nov 19, 2024
1 parent b1b2f0e commit dd8d641
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 42 deletions.
35 changes: 3 additions & 32 deletions example-db/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod phone;
mod user;

use crate::note::db::Note;
use crate::phone::db::{PhoneAbc, PhoneDb};
use rand::seq::SliceRandom;
use shah::{db::pond::Origin, error::SystemError, Command, Gene};
use std::io::{stdout, Write};
Expand All @@ -18,7 +17,6 @@ enum Commands {
Help,
Run,
Note,
Trie,
}

fn main() -> Result<(), SystemError> {
Expand Down Expand Up @@ -89,34 +87,6 @@ fn main() -> Result<(), SystemError> {
note_pool.push(note.gene);
}
}
Commands::Trie => {
let db = PhoneDb::new("tests.phone", PhoneAbc);
db.file.set_len(0).expect("file truncate");
let mut db = db.setup().expect("phone setup");

let mock_data = [
// ("223334044", 2233340, [4, 4]),
// ("183937071", 1839370, [7, 1]),
// ("192236504", 1922365, [0, 4]),
("961772969", 9617729, [6, 9]),
("961772970", 9617729, [7, 0]),
];

for (i, (phone, cache, index)) in mock_data.iter().enumerate() {
let i = i as u64;
let a = Gene { id: i + 3, ..Default::default() };
let b = Gene { id: (i + 3) * 2, ..Default::default() };
let k = db.convert_key(phone).expect("convert key");
assert_eq!(k.cache, *cache);
assert_eq!(k.index, *index);

assert_eq!(db.get(&k).expect("get"), None);
assert_eq!(db.set(&k, a).expect("set"), None);
assert_eq!(db.get(&k).expect("get"), Some(a));
assert_eq!(db.set(&k, b).expect("set"), Some(a));
assert_eq!(db.get(&k).expect("get"), Some(b));
}
}
}

Ok(())
Expand All @@ -138,12 +108,13 @@ impl log::Log for SimpleLogger {
log::Level::Error => ["\x1b[31m", "E", "Error"],
};
println!(
"[{}{}\x1b[0m]{{{}{}\x1b[0m}}: {}",
"[{}{}\x1b[0m]{{{}{}\x1b[32m:\x1b[93m{}\x1b[0m}}: {}",
level[0],
level[1],
level[0],
record.target(),
record.args()
record.line().unwrap_or_default(),
record.args(),
);
}

Expand Down
20 changes: 14 additions & 6 deletions shah/src/db/trie_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ where
pub fn get(
&mut self, key: &TrieConstKey<INDEX>,
) -> Result<Option<Val>, SystemError> {
log::debug!("============ \x1b[32mget\x1b[0m =============");
log::warn!("key: {} -> {:?}", key.cache, key.index);
let mut pos = key.cache * Self::PS;
log::info!("phone pos: {pos} = {} * {}", key.cache, Self::PS);
let mut node = [0u64; ABC_LEN];
Expand All @@ -161,7 +163,9 @@ where
self.file.seek(SeekFrom::Start(pos))?;

if i + 1 == INDEX {
log::info!("reading value at: {pos}");
self.file.read_exact(node_value.as_binary_mut())?;
log::info!("node_value: {:?}", node_value);
return Ok(Some(node_value[key.index[i]]));
}

Expand All @@ -179,29 +183,33 @@ where
pub fn set(
&mut self, key: &TrieConstKey<INDEX>, val: Val,
) -> Result<Option<Val>, SystemError> {
log::debug!("============ \x1b[36mset\x1b[0m =============");
log::warn!("key: {} -> {:?}", key.cache, key.index);
let mut pos = key.cache * Self::PS;
let mut node = [0u64; ABC_LEN];
let mut single = 0u64;
let mut node_value = [Val::default(); ABC_LEN];
let mut end_of_file = 0u64;
let mut writing = false;
let mut i = 0usize;

self.file.seek(SeekFrom::Start(pos))?;
self.file.read_exact(node[0].as_binary_mut())?;
if node[0] == 0 {
self.file.read_exact(single.as_binary_mut())?;
if single == 0 {
end_of_file = self.db_size()?;
node[0] = end_of_file;
single = end_of_file;
self.file.seek(SeekFrom::Start(pos))?;
self.file.write_all(node[0].as_binary())?;
self.file.write_all(single.as_binary())?;
self.file.seek(SeekFrom::Start(end_of_file))?;
writing = true;
} else {
pos = node[0];
pos = single;
}

if !writing {
loop {
let ki = key.index[i];
log::info!("ki: {ki}");
self.file.seek(SeekFrom::Start(pos))?;

if i + 1 == INDEX {
Expand All @@ -224,7 +232,7 @@ where
end_of_file = self.db_size()?;
node[ki] = end_of_file;

self.file.seek_relative(-(Self::NODE_SIZE as i64))?;
self.file.seek(SeekFrom::Start(pos))?;
self.file.write_all(node.as_binary())?;
self.file.seek(SeekFrom::Start(end_of_file))?;
break;
Expand Down
6 changes: 2 additions & 4 deletions shah/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[cfg(feature = "serde")]
use crate::{error::SystemError, Binary};
#[cfg(feature = "serde")]
use std::str::FromStr;

pub type GeneId = u64;

Expand All @@ -14,8 +12,8 @@ pub struct Gene {
pub server: u32,
}

#[cfg(feature = "serde")]
impl Gene {
#[cfg(feature = "serde")]
pub fn as_hex(&self) -> String {
let mut dst = [0u8; Gene::S * 2];
let out = faster_hex::hex_encode(self.as_binary(), &mut dst).unwrap();
Expand All @@ -32,7 +30,7 @@ impl Gene {
}

#[cfg(feature = "serde")]
impl FromStr for Gene {
impl std::str::FromStr for Gene {
type Err = SystemError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut gene = Gene::default();
Expand Down

0 comments on commit dd8d641

Please sign in to comment.