started with using rocksdb to store the data.
- debug binary is no 274MB.... lol - added encoding and decoding function
This commit is contained in:
75
src/server/itemstore.rs
Normal file
75
src/server/itemstore.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
use rocksdb::{SingleThreaded, DBCommon, DB, Options};
|
||||
use std::io;
|
||||
|
||||
pub struct itemstore {
|
||||
db: DB,
|
||||
}
|
||||
|
||||
impl itemstore {
|
||||
pub fn new(path: String) -> itemstore {
|
||||
let db = DB::open_default(&path).expect(&format!("Could not open rocksdb database at {}", &path)[..]);
|
||||
|
||||
match db.get(b"0") {
|
||||
Ok(Some(val)) => (),
|
||||
Ok(None) => {
|
||||
//db.set(b"0", )
|
||||
println!("hi")
|
||||
},
|
||||
Err(e) => println!("Error reading item 0 from db: {}", e),
|
||||
}
|
||||
return itemstore{db:db};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode(bytes: Vec<u8>) -> Vec<String> {
|
||||
let mut count = 0;
|
||||
let mut index: Vec<String> = Vec::new();
|
||||
let mut key: Vec<u8> = Vec::new();
|
||||
loop {
|
||||
if bytes[count] == '$' as u8 {
|
||||
if key.len() != 0 {
|
||||
index.push(String::from_utf8(key.clone()).expect("Error decoding key"));
|
||||
key.clear();
|
||||
}
|
||||
} else if bytes[count] == '!' as u8 {
|
||||
count += 1;
|
||||
key.push(bytes[count]);
|
||||
} else {
|
||||
key.push(bytes[count]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if count == bytes.len() -1 {
|
||||
index.push(String::from_utf8(key.clone()).expect("Error decoding key"));
|
||||
}
|
||||
|
||||
count += 1;
|
||||
if count >= bytes.len() {break};
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
pub fn encode(index: Vec<String>) -> Vec<u8> {
|
||||
let mut bytes: Vec<u8> = Vec::new();
|
||||
for key in index {
|
||||
let mut b = key.into_bytes();
|
||||
let mut count = 0;
|
||||
|
||||
loop {
|
||||
if b[count] == '!' as u8 || b[count] == '$' as u8 {
|
||||
b.insert(count, '!' as u8);
|
||||
count += 1;
|
||||
}
|
||||
|
||||
count += 1;
|
||||
if count >= b.len() {break};
|
||||
}
|
||||
bytes.push('$' as u8);
|
||||
bytes.append(&mut b);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ pub struct Fields{
|
||||
|
||||
pub struct Index{
|
||||
commit_number: u64,
|
||||
fields: Vec<Fields>
|
||||
//fields: Vec<Fields>
|
||||
}
|
||||
|
||||
pub fn import(args: Vec<String>){
|
||||
@@ -24,84 +24,5 @@ pub fn update_item(id: u64, key: String, update_string: String){
|
||||
|
||||
}
|
||||
|
||||
pub fn read_index(id: u64, mize_folder: &str) -> io::Result<Index>{
|
||||
let mut index_folder_num: String = String::new();
|
||||
let mut index_num: String = String::new();
|
||||
let id_len = id.to_string().chars().count();
|
||||
|
||||
if id_len <= 6 {
|
||||
index_folder_num = String::from("0");
|
||||
index_num = id.to_string();
|
||||
} else {
|
||||
index_folder_num = String::from(&id.to_string()[..id_len-6]);
|
||||
index_num = String::from(&id.to_string()[id_len-6..]);
|
||||
}
|
||||
|
||||
let index_file_path = String::from(mize_folder) + "/index" + &index_folder_num + "/" + &index_num;
|
||||
|
||||
let mut file = File::open(index_file_path)?;
|
||||
|
||||
let mut commit_number_buff: [u8; 8];
|
||||
file.read_exact(&mut commit_number_buff[..])?;
|
||||
|
||||
let fields: Vec<Fields> = Vec::new();
|
||||
|
||||
Ok(Index {
|
||||
commit_number: u64::from_le_bytes(commit_number_buff),
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
pub fn init_mize_folder(mize_folder: String) -> Result<(), MizeError>{
|
||||
//##check if the folder resembles a mize folder
|
||||
|
||||
//## check if the index0 file exists and create one if it does not
|
||||
let index_path = mize_folder.clone() + "/index0";
|
||||
if !Path::new(&index_path).exists() {
|
||||
File::create(&index_path).unwrap();
|
||||
}
|
||||
let index_md = fs::metadata(&index_path).unwrap();
|
||||
if index_md.is_dir(){
|
||||
return Err(MizeError{message: String::from("index is a folder and not a file")})
|
||||
}
|
||||
|
||||
let folders: [&str; 5] = ["fields0","keys0","data0","config","other"];
|
||||
for folder in folders {
|
||||
//## check if the folder exists and create one if it does not
|
||||
let path = mize_folder.clone() + "/" + folder;
|
||||
if !Path::new(&path).exists() {
|
||||
fs::create_dir(&path).unwrap();
|
||||
}
|
||||
let index_md = fs::metadata(&path).unwrap();
|
||||
if index_md.is_file(){
|
||||
return Err(MizeError{message: String::from(folder) + " is a file and not a folder"});
|
||||
}
|
||||
}
|
||||
|
||||
let index = read_index(0, &mize_folder);
|
||||
|
||||
//if let Ok(i) = index {
|
||||
|
||||
//}
|
||||
if let Err(i) = index{
|
||||
println!("{:?}", i.kind());
|
||||
match i.kind() {
|
||||
io::ErrorKind::UnexpectedEof => {
|
||||
//create the first item
|
||||
|
||||
//create index for first item
|
||||
File::create(mize_folder.clone() + "/keys0/0");
|
||||
|
||||
//create keyfile in keyfile0
|
||||
File::create(mize_folder.clone() + "/keys0/000_000");
|
||||
},
|
||||
_ => return Err(MizeError{
|
||||
message: String::from("sth went wrong when reading the index of item 0")
|
||||
})
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user