update on the server works now

This commit is contained in:
Sebastian Moser
2022-10-24 22:29:38 +02:00
parent 202f493eba
commit 2a0a4867f9
3 changed files with 54 additions and 53 deletions

View File

@@ -91,8 +91,7 @@ impl Itemstore {
};
for field in new_item {
let mut key = id.to_be_bytes().to_vec();
key.push(':' as u8);
let mut key = format!("{}:", id).into_bytes();
key.extend(field[0].clone());
let val = field[1].clone();
keys.push(field[0].clone());

View File

@@ -176,16 +176,17 @@ pub async fn handle_mize_message(
let num_of_updates = message.get_u32();
for i in 0..num_of_updates as usize {
for i in 1..num_of_updates as usize {
let key_len = message.get_u32();
let key = message.get_bytes(key_len as usize);
//println!("Key: {}", String::from_utf8(key.clone()).unwrap());
//println!("KEY: {}", String::from_utf8(key.clone()).expect("here utf-8"));
let update_len = message.get_u32();
let update = message.get_bytes(update_len as usize);
let mut found = false;
for a in 0..num_of_updates as usize {
for a in 0..item.len() as usize {
println!("OTHER KEY: {}", String::from_utf8(item[a][0].clone()).expect("here utf-8"));
if item[a][0] == key {
item[a][1] = apply_update(&item[a][1], &update);
found = true;
@@ -193,10 +194,14 @@ pub async fn handle_mize_message(
}
}
if found == false {
println!("should not be here");
let index = item.len() +1;
item[index][1] = apply_update(&item[index][1], &update)
//item[index][1] = apply_update(&item[index][1], &update)
item.push([key.clone(), apply_update(&item[index][1], &update)])
}
}
//println!("ITEM KEY: {}", String::from_utf8(item[1][0].clone()).unwrap());
//println!("ITEM VAL: {}", String::from_utf8(item[1][1].clone()).unwrap());
itemstore.update(id, item).await;
return Response::All(answer);
@@ -252,7 +257,11 @@ pub async fn handle_mize_message(
}
// just like all of this crate, definetly could be done with less clones().... and better error
// handling
pub fn apply_update(val: &Vec<u8>, updates: &Vec<u8>) -> Vec<u8>{
println!("VAL: {:?}", val.clone());
println!("Update: {:?}", updates.clone());
let not_enough_bytes = "not enough bytes in update";
let mut val = val.clone();
let mut update_iter = updates.clone().into_iter();
@@ -263,6 +272,7 @@ pub fn apply_update(val: &Vec<u8>, updates: &Vec<u8>) -> Vec<u8>{
match operation {
//r,start:u32,stop:u32,bytes start..stop
0 => {
//get start and stop
let mut start_bytes: [u8; 4] = [0,0,0,0];
for i in 0..4 {start_bytes[i] = update_iter.next().expect(not_enough_bytes);};
let start = u32::from_be_bytes(start_bytes);
@@ -287,6 +297,7 @@ pub fn apply_update(val: &Vec<u8>, updates: &Vec<u8>) -> Vec<u8>{
},
//i,start:u32, stop:u32, bytes stop-start
1 => {
//get start and stop
let mut start_bytes: [u8; 4] = [0,0,0,0];
for i in 0..4 {start_bytes[i] = update_iter.next().expect(not_enough_bytes);};
let start = u32::from_be_bytes(start_bytes);
@@ -314,50 +325,34 @@ pub fn apply_update(val: &Vec<u8>, updates: &Vec<u8>) -> Vec<u8>{
},
//d,start:u32,stop:u32
2 => {
//get start and stop
let mut start_bytes: [u8; 4] = [0,0,0,0];
for i in 0..4 {start_bytes[i] = update_iter.next().expect(not_enough_bytes);};
let start = u32::from_be_bytes(start_bytes);
let mut stop_bytes: [u8; 4] = [0,0,0,0];
for i in 0..4 {stop_bytes[i] = update_iter.next().expect(not_enough_bytes);};
let stop = u32::from_be_bytes(stop_bytes);
//add the stuff before
for i in 0..start {new_val.push(val_iter.next().expect("not_enough_bytes in val_iter"));};
//skip all the bytes that should be replaced
for i in 0..stop-start {let h = val_iter.next().expect("not_enough_bytes in val_iter"); println!("VAL ITER: {}", h);}
//add stuff after
for byte in val_iter {
new_val.push(byte);
}
},
_ => {panic!("unknown update command")}
}
val = new_val;
} else {println!("here");break;}
} else {break;}
};
return val;
}
//old approach
//pub fn apply_update(val: &Vec<u8>, update: &Vec<u8>) -> Vec<u8>{
//let new_val: Vec<u8> = Vec::new();
//let update_iter = update.into_iter();
//let val_iter = val.into_iter();
//while true {
//if let Some(operation) = update_iter.next(){
//match *operation {
////r,start:u32,stop:u32,bytes start..stop
//0 => {
//let start_bytes: [u8; 4] = [0,0,0,0];
//for i in 0..4 {start_bytes[i] = update_iter.next();};
//let start = u32::from_be_bytes(start_bytes);
//
//let stop_bytes: [u8; 4] = [0,0,0,0];
//for i in 0..4 {stop_bytes[i] = update_iter.next();};
//let start = u32::from_be_bytes(stop_bytes);
//
////add the stuff before
//for i in 0..start {new_val.push(val_iter.next());};
//
////add the new stuff
//for i in start..stop {new_val.push(update_iter.next());};
//},
////i,start:u32, stop:u32, bytes stop-start
//1 => {
//},
////d,start:u32,stop:u32
//2 => {
//},
//}
//} else {break;}
//}
//}