Skip to content

Commit

Permalink
fix: update taker
Browse files Browse the repository at this point in the history
  • Loading branch information
i007c committed Nov 18, 2024
1 parent 56f63b5 commit b1b2f0e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 39 deletions.
30 changes: 30 additions & 0 deletions example-db/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 @@ -17,6 +18,7 @@ enum Commands {
Help,
Run,
Note,
Trie,
}

fn main() -> Result<(), SystemError> {
Expand Down Expand Up @@ -87,6 +89,34 @@ 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 Down
10 changes: 6 additions & 4 deletions example-db/src/phone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub mod db {
mod tests {
use super::PhoneAbc;
use shah::{db::trie_const::TrieConst, Gene};
type PhoneDb = TrieConst<10, 5, 4, PhoneAbc, Gene>;
type PhoneDb = TrieConst<10, 2, 7, PhoneAbc, Gene>;

#[test]
fn phone_db() {
Expand All @@ -37,9 +37,11 @@ pub mod db {
let mut db = db.setup().expect("phone setup");

let mock_data = [
("223334044", 2233, [3, 4, 0, 4, 4]),
("183937071", 1839, [3, 7, 0, 7, 1]),
("192236504", 1922, [3, 6, 5, 0, 4]),
("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() {
Expand Down
29 changes: 0 additions & 29 deletions shah-macros/tests/model.rs

This file was deleted.

2 changes: 2 additions & 0 deletions shah/src/db/trie_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ where
&mut self, key: &TrieConstKey<INDEX>,
) -> Result<Option<Val>, SystemError> {
let mut pos = key.cache * Self::PS;
log::info!("phone pos: {pos} = {} * {}", key.cache, Self::PS);
let mut node = [0u64; ABC_LEN];
let mut node_value = [Val::default(); ABC_LEN];
let db_size = self.db_size()?;
Expand All @@ -150,6 +151,7 @@ where

self.file.seek(SeekFrom::Start(pos))?;
self.file.read_exact(node[0].as_binary_mut())?;
log::info!("node: {node:?}");
pos = node[0];
if pos == 0 {
return Ok(None);
Expand Down
23 changes: 17 additions & 6 deletions shah/src/taker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{os::unix::net::UnixDatagram, time::Duration};
use std::{
io::ErrorKind, os::unix::net::UnixDatagram, thread::sleep, time::Duration,
};

use crate::{Binary, ErrorCode, ReplyHead};

Expand Down Expand Up @@ -34,12 +36,21 @@ impl Taker {
pub fn take(&mut self, order: &[u8]) -> Result<(), ErrorCode> {
self.reply[0..ReplyHead::S].fill(0);

match self.conn.send(order) {
Err(e) => {
log::error!("send error: {e}");
Err(e)?;
if let Err(e) = self.conn.send(order) {
log::error!("send error: {e:#?}");
match e.kind() {
ErrorKind::NotConnected | ErrorKind::ConnectionRefused => {
for i in 0..3 {
log::info!("reconnect try: {i}");
if self.connect().is_ok() {
self.conn.send(order)?;
break;
}
sleep(Duration::from_secs(2));
}
}
_ => Err(e)?,
}
_ => {}
}
self.conn.recv(&mut self.reply)?;

Expand Down

0 comments on commit b1b2f0e

Please sign in to comment.