creating new items now works

This commit is contained in:
Sebastian Moser
2022-10-13 19:10:07 +02:00
parent f18667b5b3
commit b37250ce43
5 changed files with 79 additions and 38 deletions

View File

@@ -12,63 +12,68 @@ pub type Item = Vec<[Vec<u8>; 2]>;
impl Itemstore {
pub async fn new(path: String) -> Itemstore {
let ds = Datastore::new(&(String::from("file://") + &path[..] + "/db")[..]).await.expect("Could not open database");
println!("in new fn");
let mut tr = ds.transaction(true, false).await.expect("creation of transaction failed");
match tr.get(vec!('0' as u8)).await {
Ok(Some(val)) => println!("item 0 already created"),
Ok(None) => {
let init = vec!("num_of_items".as_bytes().to_vec(), "next_free_id".as_bytes().to_vec());
let init = vec!["num_of_items".as_bytes().to_vec(), "next_free_id".as_bytes().to_vec()];
tr.set("0".to_string().into_bytes(), encode(init)).await;
tr.set("0:num_of_items".to_string().into_bytes(), vec!('1' as u8)).await;
tr.set("0:next_free_id".to_string().into_bytes(), vec!('1' as u8)).await;
tr.commit().await;
let one: u64 = 1;
let zero: u64 = 0;
tr.set("0:num_of_items".to_string().into_bytes(), one.to_be_bytes()).await;
tr.set("0:next_free_id".to_string().into_bytes(), one.to_be_bytes()).await;
tr.set("0:_commit".to_string().into_bytes(), zero.to_be_bytes()).await;
tr.set("0:_type".to_string().into_bytes(), "mize-main".as_bytes().to_vec()).await;
tr.commit().await.expect("error commiting transaction in new function");
},
Err(e) => println!("Error while getting item index with id 0: {}", e)
};
return Itemstore {db:ds};
}
pub async fn create(&self, item: Item) -> u64 {
let mut tr = self.db.transaction(true, true).await.expect("creation of transaction failed");
let mut tr = self.db.transaction(true, false).await.expect("creation of transaction failed");
let id = tr.get("0:next_free_id").await
.expect("no item with id 0 when there definetly should be one")
.expect("error reading item 0 from db");
let id_u64 = u64::from_be_bytes(id.clone().try_into().unwrap());
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);
let key = format!("{}:_commit", id_u64).into_bytes();
tr.set(key.clone(), vec![0,0,0,0,0,0,0,0]).await;
keys.push("_commit".to_owned().into_bytes());
for field in item {
let mut key = id.clone();
key.push(':' as u8);
let mut key = format!("{}:", id_u64).into_bytes();
key.extend(field[0].clone());
let val = field[1].clone();
keys.push(field[0].clone());
let val = field[1].clone();
tr.set(key, val).await;
}
tr.set(id.clone(), encode(keys)).await;
tr.set(format!("{}", id_u64), encode(keys)).await;
//increment 0:number_of_items by 1
let num = tr.get("0:num_of_items".to_string().into_bytes()).await
.expect("error getting \"0:num_of_items\"")
.expect("error reading item 0 from db");
let mut num_u64 = u64::from_be_bytes([num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]]) +1;
tr.set("0:num_of_items".to_string().into_bytes(), num);
tr.set("0:num_of_items".to_string().into_bytes(), num_u64.to_be_bytes()).await;
//imcrement next_free_id by 1
let num = tr.get("0:next_free_id".to_string().into_bytes()).await
.expect("error getting \"0:next_free_id\"")
.expect("error reading item 0 from db");
let mut num_u64 = u64::from_be_bytes([num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]]) +1;
tr.set("0:next_free_id".to_string().into_bytes(), num);
tr.set("0:next_free_id".to_string().into_bytes(), num_u64.to_be_bytes()).await;
tr.commit().await;
let res = tr.commit().await.expect("error commiting transaction in create function");
return u64::from_be_bytes([id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], ])
}
@@ -112,7 +117,7 @@ impl Itemstore {
keys = keys.into_iter().unique().collect::<Vec<Vec<u8>>>();
tr.set(format!("{}", id).into_bytes(), encode(keys)).await;
tr.commit().await;
let res = tr.commit().await.expect("error commiting transaction in update function");
}
pub async fn delete(&self, id: u64){
@@ -130,7 +135,7 @@ impl Itemstore {
tr.del(key).await;
}
tr.del(format!("{}", id)).await;
tr.commit().await;
let res = tr.commit().await.expect("error commiting transaction in delete function");
}
pub async fn get(&self, id: u64) -> Item {
@@ -145,14 +150,11 @@ impl Itemstore {
Err(e) => panic!("Error while querying the db: {}", e),
};
println!("ID: {}", id);
for key in keys {
let mut field: [Vec<u8>; 2] = [Vec::new(), Vec::new()];
field[0] = key.clone();
let mut ke = format!("{}", id).into_bytes();
ke.push(':' as u8);
let mut ke = format!("{}:", id).into_bytes();
ke.extend(key.clone());
println!("Key: {}", String::from_utf8(ke.clone()).unwrap());
field[1] = match tr.get(ke).await {
Ok(Some(val)) => val,
Ok(None) => panic!("a field that should be there wasn't"),