added get cmd to handle_mize_message function
- and _commit field, that automatically imcrements
This commit is contained in:
@@ -148,6 +148,7 @@ async fn handle_socket_connection(
|
|||||||
// Reading and broadcasting messages
|
// Reading and broadcasting messages
|
||||||
while let Some(result) = socket_rx.next().await {
|
while let Some(result) = socket_rx.next().await {
|
||||||
let msg = result.expect("Error when gettin message from WebSocket");
|
let msg = result.expect("Error when gettin message from WebSocket");
|
||||||
|
let t = msg.clone().into_bytes();
|
||||||
let clients = clients_clone.lock().await;
|
let clients = clients_clone.lock().await;
|
||||||
for client in &clients[..] {
|
for client in &clients[..] {
|
||||||
client.tx.send(Ok(msg.clone()));
|
client.tx.send(Ok(msg.clone()));
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
use std::{io, fmt::format};
|
use std::{io, fmt::format, vec};
|
||||||
use surrealdb::{Datastore, Key, Val};
|
use surrealdb::{Datastore, Key, Val};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
@@ -37,6 +37,13 @@ impl Itemstore {
|
|||||||
.expect("error reading item 0 from db");
|
.expect("error reading item 0 from db");
|
||||||
let mut keys: Vec<Vec<u8>> = Vec::new();
|
let mut keys: Vec<Vec<u8>> = Vec::new();
|
||||||
|
|
||||||
|
//set id:_commit
|
||||||
|
let mut key = id.clone();
|
||||||
|
key.extend(":_commit".to_string().into_bytes());
|
||||||
|
tr.set(key.clone(), vec![0,0,0,0,0,0,0,0]);
|
||||||
|
keys.push(key);
|
||||||
|
|
||||||
|
|
||||||
for field in item {
|
for field in item {
|
||||||
let mut key = id.clone();
|
let mut key = id.clone();
|
||||||
key.push(':' as u8);
|
key.push(':' as u8);
|
||||||
@@ -87,6 +94,21 @@ impl Itemstore {
|
|||||||
tr.set(key, val).await;
|
tr.set(key, val).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//increment commit number
|
||||||
|
let mut commit_key = id.to_be_bytes().to_vec();
|
||||||
|
commit_key.extend(":_commit".to_string().into_bytes());
|
||||||
|
|
||||||
|
let mut commit_num: u64 = match tr.get(&*commit_key).await {
|
||||||
|
Ok(Some(val)) => u64::from_be_bytes(val.try_into().expect("error converting _commit num to u64")),
|
||||||
|
Ok(None) => {
|
||||||
|
panic!("item {} has no _commit", id);
|
||||||
|
},
|
||||||
|
Err(e) => panic!("Error while querying the db: {}", e),
|
||||||
|
};
|
||||||
|
|
||||||
|
commit_num += 1;
|
||||||
|
tr.set(commit_key, commit_num.to_be_bytes());
|
||||||
|
|
||||||
keys = keys.into_iter().unique().collect::<Vec<Vec<u8>>>();
|
keys = keys.into_iter().unique().collect::<Vec<Vec<u8>>>();
|
||||||
tr.set(format!("{}", id).into_bytes(), encode(keys)).await;
|
tr.set(format!("{}", id).into_bytes(), encode(keys)).await;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,61 @@
|
|||||||
|
|
||||||
|
use std::vec;
|
||||||
|
|
||||||
use crate::server::itemstore;
|
use crate::server::itemstore;
|
||||||
|
use crate::server::itemstore::encode;
|
||||||
|
|
||||||
async fn handle_mize_message(message: Vec<u8>){
|
const VERSION: u8 = 1;
|
||||||
|
|
||||||
|
async fn handle_mize_message(
|
||||||
|
message: Vec<u8>,
|
||||||
|
itemstore: itemstore::Itemstore,
|
||||||
|
) -> Option<Vec<u8>>{
|
||||||
|
|
||||||
|
let version = &message[0..1];
|
||||||
|
let cmd = &message[1..2];
|
||||||
|
|
||||||
|
// 1: get
|
||||||
|
// 2: give
|
||||||
|
// 3: get_val
|
||||||
|
// 4: give_val
|
||||||
|
// 5: unauthorized
|
||||||
|
// 6: sub
|
||||||
|
// 7: unsub
|
||||||
|
// 8: update_request
|
||||||
|
// 9: update_deny
|
||||||
|
// 10: update
|
||||||
|
// 11: delete
|
||||||
|
// 12: create
|
||||||
|
match cmd[0] {
|
||||||
|
1 => {
|
||||||
|
let id: u64 = u64::from_be_bytes(message[2..9].try_into().expect("slice with incorrect length"));
|
||||||
|
let id_bytes = id.to_be_bytes();
|
||||||
|
let mut answer: Vec<u8> = vec![VERSION, 2, id_bytes[0], id_bytes[1], id_bytes[2], id_bytes[3]];
|
||||||
|
let item = itemstore.get(id).await;
|
||||||
|
|
||||||
|
let mut temp: Vec<Vec<u8>> = Vec::new();
|
||||||
|
|
||||||
|
for field in item {
|
||||||
|
let encoded = encode(field.to_vec());
|
||||||
|
temp.push(encoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return Some(answer);
|
||||||
|
},
|
||||||
|
2 => {return None;},
|
||||||
|
3 => {return None;},
|
||||||
|
4 => {return None;},
|
||||||
|
5 => {return None;},
|
||||||
|
6 => {return None;},
|
||||||
|
7 => {return None;},
|
||||||
|
8 => {return None;},
|
||||||
|
9 => {return None;},
|
||||||
|
10 => {return None;},
|
||||||
|
11 => {return None;},
|
||||||
|
12 => {return None;},
|
||||||
|
_ => {return None;},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user