Refactor search algo. Set enum from(u8) functions to implement the From<u8> trait.
This commit is contained in:
parent
049b9a4618
commit
abea01cf9b
3 changed files with 82 additions and 39 deletions
9
.vscode/launch.json
vendored
9
.vscode/launch.json
vendored
|
@ -9,6 +9,15 @@
|
||||||
"args": ["build"]
|
"args": ["build"]
|
||||||
},
|
},
|
||||||
"args": []
|
"args": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Cargo test",
|
||||||
|
"cargo": {
|
||||||
|
"args": ["test"]
|
||||||
|
},
|
||||||
|
"args": []
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ struct LocalStoreMeta {
|
||||||
specifier_count: u32,
|
specifier_count: u32,
|
||||||
index_node_arity: u16,
|
index_node_arity: u16,
|
||||||
data_block_size: u32,
|
data_block_size: u32,
|
||||||
data_offset: u64
|
data_offset: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct EncryptionContext {
|
struct EncryptionContext {
|
||||||
|
@ -231,7 +231,7 @@ fn try_create_store(path: &String, passphrase: &Vec<u8>) -> bool {
|
||||||
specifier_count: 0,
|
specifier_count: 0,
|
||||||
index_node_arity: 64,
|
index_node_arity: 64,
|
||||||
data_block_size: 8092,
|
data_block_size: 8092,
|
||||||
data_offset: 0
|
data_offset: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
superblock_buf.append(&mut serialize_store_meta(&meta));
|
superblock_buf.append(&mut serialize_store_meta(&meta));
|
||||||
|
@ -315,14 +315,12 @@ fn deserialize_store_meta(data: &[u8]) -> LocalStoreMeta {
|
||||||
let data_block_size = u32::from_be_bytes(data[10..14].try_into().unwrap());
|
let data_block_size = u32::from_be_bytes(data[10..14].try_into().unwrap());
|
||||||
let data_offset = u64::from_be_bytes(data[14..22].try_into().unwrap());
|
let data_offset = u64::from_be_bytes(data[14..22].try_into().unwrap());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
LocalStoreMeta {
|
LocalStoreMeta {
|
||||||
record_count: records,
|
record_count: records,
|
||||||
specifier_count: specifiers,
|
specifier_count: specifiers,
|
||||||
index_node_arity,
|
index_node_arity,
|
||||||
data_block_size,
|
data_block_size,
|
||||||
data_offset
|
data_offset,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -519,8 +517,30 @@ impl Store for LocalStore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum IndexNodeType {
|
||||||
|
Internal,
|
||||||
|
Leaf,
|
||||||
|
Invalid,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u8> for IndexNodeType {
|
||||||
|
fn from(value: u8) -> Self {
|
||||||
|
match value {
|
||||||
|
0 => IndexNodeType::Invalid,
|
||||||
|
1 => IndexNodeType::Internal,
|
||||||
|
2 => IndexNodeType::Leaf,
|
||||||
|
_ => panic!("Invalid value {}", value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct IndexNode {
|
||||||
|
node_type: IndexNodeType,
|
||||||
|
children: [u64],
|
||||||
|
}
|
||||||
|
|
||||||
impl LocalStore {
|
impl LocalStore {
|
||||||
fn btree_find_key(&self, key: &String) {
|
fn btree_find_key(&self, key: &String) -> Option<u64> {
|
||||||
let mut file: BufReader<File> =
|
let mut file: BufReader<File> =
|
||||||
BufReader::new(File::open(&self.path).expect("Cannot open file"));
|
BufReader::new(File::open(&self.path).expect("Cannot open file"));
|
||||||
|
|
||||||
|
@ -531,21 +551,35 @@ impl LocalStore {
|
||||||
};
|
};
|
||||||
|
|
||||||
let pos = file.seek(SeekFrom::Start(0x90)).unwrap();
|
let pos = file.seek(SeekFrom::Start(0x90)).unwrap();
|
||||||
|
|
||||||
let mut buf: [u8; 16] = [0; 16];
|
|
||||||
file.read_exact(&mut buf).expect("Read error");
|
|
||||||
|
|
||||||
let key = u64::from_be_bytes(buf[0..8].try_into().unwrap());
|
let mut read_node_type = || {
|
||||||
|
let mut buf: [u8; 2] = [0; 2];
|
||||||
|
file.read_exact(&mut buf).expect("Cannot read node type.");
|
||||||
|
IndexNodeType::from(buf[0])
|
||||||
|
};
|
||||||
|
|
||||||
if hash > key {
|
let mut read_key_pos = || {
|
||||||
//advance
|
let mut buf: [u8; 16] = [0; 16];
|
||||||
todo!();
|
file.read_exact(&mut buf).expect("Read error");
|
||||||
|
|
||||||
|
(
|
||||||
|
u64::from_be_bytes(buf[0..8].try_into().unwrap()),
|
||||||
|
u64::from_be_bytes(buf[8..16].try_into().unwrap()),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut subnode_pos = 0;
|
||||||
|
loop {
|
||||||
|
let (key, offset) = read_key_pos();
|
||||||
|
|
||||||
|
if hash > key {
|
||||||
|
if subnode_pos > self.meta.index_node_arity {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
subnode_pos += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let pointer = u64::from_be_bytes(buf[8..16].try_into().unwrap());
|
|
||||||
let pos2 = file.seek(SeekFrom::Start(pointer)).unwrap();
|
|
||||||
|
|
||||||
println!("Pos: {:x} 2: {:x}", pos, pos2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -672,7 +706,7 @@ mod tests {
|
||||||
key: "meow".to_owned(),
|
key: "meow".to_owned(),
|
||||||
meta: None,
|
meta: None,
|
||||||
specifier: "test".to_owned(),
|
specifier: "test".to_owned(),
|
||||||
r#type: crate::store::RecordType::LOGIN_PASSWORD,
|
r#type: crate::store::RecordType::LoginPassword,
|
||||||
value: vec![1, 2, 3, 4],
|
value: vec![1, 2, 3, 4],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,25 +12,25 @@ pub struct StoreRecord {
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
|
||||||
pub enum RecordType {
|
pub enum RecordType {
|
||||||
LOGIN_PASSWORD = 0,
|
LoginPassword = 0,
|
||||||
PASSWORD = 1,
|
Password = 1,
|
||||||
SSH_KEY = 2,
|
SSHKey = 2,
|
||||||
PGP_KEY = 3,
|
PGPKey = 3,
|
||||||
SYM_KEY = 4,
|
SymKey = 4,
|
||||||
ASYM_KEY_PAIR = 5,
|
AsymKeyPair = 5,
|
||||||
ASYM_PRIV_KEY = 6,
|
AsymPrivKey = 6,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RecordType {
|
impl From<u8> for RecordType {
|
||||||
pub fn from(n: u8) -> Self {
|
fn from(n: u8) -> Self {
|
||||||
match n {
|
match n {
|
||||||
0 => RecordType::LOGIN_PASSWORD,
|
0 => RecordType::LoginPassword,
|
||||||
1 => RecordType::PASSWORD,
|
1 => RecordType::Password,
|
||||||
2 => RecordType::SSH_KEY,
|
2 => RecordType::SSHKey,
|
||||||
3 => RecordType::PGP_KEY,
|
3 => RecordType::PGPKey,
|
||||||
4 => RecordType::SYM_KEY,
|
4 => RecordType::SymKey,
|
||||||
5 => RecordType::ASYM_KEY_PAIR,
|
5 => RecordType::AsymKeyPair,
|
||||||
6 => RecordType::ASYM_PRIV_KEY,
|
6 => RecordType::AsymPrivKey,
|
||||||
_ => panic!("Invalid value '{}'.", n),
|
_ => panic!("Invalid value '{}'.", n),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,8 +56,8 @@ pub enum EncryptionType {
|
||||||
Chacha20Poly1305 = 2,
|
Chacha20Poly1305 = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EncryptionType {
|
impl From<u8> for EncryptionType {
|
||||||
pub fn from(n: u8) -> Self {
|
fn from(n: u8) -> Self {
|
||||||
match n {
|
match n {
|
||||||
0 => EncryptionType::Invalid,
|
0 => EncryptionType::Invalid,
|
||||||
1 => EncryptionType::AesGcm,
|
1 => EncryptionType::AesGcm,
|
||||||
|
@ -75,8 +75,8 @@ pub enum HashType {
|
||||||
Pbkdf2 = 4,
|
Pbkdf2 = 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HashType {
|
impl From<u8> for HashType {
|
||||||
pub fn from(n: u8) -> Self {
|
fn from(n: u8) -> Self {
|
||||||
match n {
|
match n {
|
||||||
0 => HashType::Invalid,
|
0 => HashType::Invalid,
|
||||||
1 => HashType::Argon2,
|
1 => HashType::Argon2,
|
||||||
|
|
Loading…
Reference in a new issue