Skip to content

Commit

Permalink
Implement first PATCH operations (#56)
Browse files Browse the repository at this point in the history
* Prevent update for locked keys + only check mime type for unlocked

* Implement Lock and Unlock PATCH operations

* Implement Increment and Decrement
  • Loading branch information
Clint.Network authored Feb 26, 2020
1 parent 46db892 commit 7791265
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
53 changes: 36 additions & 17 deletions src/kvstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,39 @@ pub struct KvElement {
pub updated_at: DateTime<Utc>,
pub expire_at: DateTime<Utc>,
pub update_count: i32,
pub locked: bool
pub locked: bool,
}

pub struct KvStore {
container: CHashMap<String, KvElement>
container: CHashMap<String, KvElement>,
}

impl KvStore
{
pub fn new() -> KvStore
{
impl KvStore {
pub fn new() -> KvStore {
// TODO: prepare looped persistence
KvStore {
container: CHashMap::new()
container: CHashMap::new(),
}
}

pub fn set(&self, key: String, value: Vec<u8>) -> Option<KvElement> {
// TODO: prepare iterative persistence
let mime_type = tree_magic::from_u8(value.as_ref());
match &mut self.container.get_mut(&key) {
Some(kv_element) => {
kv_element.data = value;
kv_element.mime_type = mime_type;
if !kv_element.locked {
let mime_type = tree_magic::from_u8(value.as_ref());
kv_element.data = value;
kv_element.mime_type = mime_type;
}
kv_element.updated_at = Utc::now();
kv_element.update_count = kv_element.update_count + 1;
kv_element.update_count = kv_element.update_count + 1;
Some(kv_element.to_owned())
},
}
None => {
let mime_type = tree_magic::from_u8(value.as_ref());
let kv_element = KvElement {
data: value,
mime_type: mime_type,
mime_type,
created_at: Utc::now(),
updated_at: Utc::now(),
expire_at: Utc::now(),
Expand All @@ -55,7 +56,7 @@ impl KvStore
pub fn get(&self, key: String) -> Option<KvElement> {
match self.container.get(&key) {
Some(value) => Some(value.clone()),
None => None
None => None,
}
}

Expand All @@ -64,8 +65,26 @@ impl KvStore
Some(kv_element) => {
kv_element.locked = to_lock;
true
},
None => false
}
None => false,
}
}

pub fn increment_or_decrement(&self, key: String, value: f64) -> bool {
match &mut self.container.get_mut(&key) {
Some(kv_element) => {
let byte_to_string = String::from_utf8(kv_element.clone().data).unwrap(); // TODO: handle convert to string error
match byte_to_string.trim().parse::<f64>() {
Ok(initial_value) => {
kv_element.data = (initial_value + value).to_string().into_bytes();
kv_element.updated_at = Utc::now();
kv_element.update_count = kv_element.update_count + 1;
true
}
Err(_) => false,
}
}
None => false,
}
}

Expand All @@ -74,4 +93,4 @@ impl KvStore
pub fn drop(&self, key: String) {
self.container.remove(&key);
}
}
}
39 changes: 32 additions & 7 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ impl Server {
std::process::id()
);
info!("Lucid API Endpoint: https://{}/api/", bind_endpoint);
info!("SSL Certificate: {}", &configuration.general.ssl_certificate);
info!("SSL Private Key: {}", &configuration.general.ssl_certificate_key);
info!(
"SSL Certificate: {}",
&configuration.general.ssl_certificate
);
info!(
"SSL Private Key: {}",
&configuration.general.ssl_certificate_key
);
info!("Use Ctrl+C to stop the server.");
instance
.tls()
Expand Down Expand Up @@ -219,11 +225,30 @@ async fn patch_key(
patch_value: PatchValue,
) -> Result<impl Reply, Rejection> {
if let Some(_) = store.get(key.clone()) {
match patch_value.operation.as_str() {
"lock" | "unlock" => {
let r = store.switch_lock(key.to_string(), true);
println!("{}", r);
Ok("")
match patch_value.operation.to_lowercase().as_str() {
"lock" => {
store.switch_lock(key.to_string(), true);
Ok(warp::reply::json(&JsonMessage {
message: "The specified key was successfully locked.".to_string(),
}))
}
"unlock" => {
store.switch_lock(key.to_string(), false);
Ok(warp::reply::json(&JsonMessage {
message: "The specified key was successfully unlocked.".to_string(),
}))
}
"increment" => {
store.increment_or_decrement(key.to_string(), 1.0);
Ok(warp::reply::json(&JsonMessage {
message: "The specified key was successfully incremented.".to_string(),
}))
}
"decrement" => {
store.increment_or_decrement(key.to_string(), -1.0);
Ok(warp::reply::json(&JsonMessage {
message: "The specified key was successfully decremented.".to_string(),
}))
}
_ => Err(reject::custom(Error::InvalidOperation {
operation: patch_value.operation,
Expand Down

0 comments on commit 7791265

Please sign in to comment.