-
Notifications
You must be signed in to change notification settings - Fork 3
feat: implement INC
#2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,31 @@ impl Database { | |
| database_error!(DatabaseErrorType::KeyNotFound(key.to_string())) | ||
| } | ||
|
|
||
| /// Increments a value by its key. Creates the key with a default value of 1 if it does not exist. | ||
| /// | ||
| /// # Arguments: | ||
| /// * `key`: The query key. | ||
| /// | ||
| /// # Returns: | ||
| /// INC_OK enum on success or an error. | ||
| pub fn inc(&mut self, key: &str) -> Result<QueryResponseType, String> { | ||
| if let Some(serialized_value) = self.storage.get(key) { | ||
| let mut deserialized_value: ValueType = deserialize(serialized_value).unwrap(); | ||
|
|
||
| if let ValueType::Int(ref mut value) = deserialized_value { | ||
| *value += 1; | ||
| self.storage.insert(key.to_owned(), serialize(&deserialized_value).unwrap()); | ||
| return Ok(QueryResponseType::INC_OK(deserialized_value)); | ||
| } else { | ||
| return database_error!(DatabaseErrorType::WrongValueType); | ||
| } | ||
| } | ||
|
|
||
| let default_value = ValueType::Int(1); | ||
| self.storage.insert(key.to_owned(), serialize(&default_value).unwrap()); | ||
| return Ok(QueryResponseType::INC_OK(default_value)); | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Two points here:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do I check if the value is int? I thought about it but it's not very clear to me.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I meant check the database-type, so it only works when the database stores INTs, e.g like this: if self.database_type != DatabaseType::Int {
return database_error!(DatabaseErrorType::WrongValueType);
} |
||
|
|
||
| /// Gets values from a range of keys. | ||
| /// | ||
| /// # Arguments: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,6 +161,21 @@ fn parse_get(query: &str) -> Result<QueryRequest, String> { | |
| Ok(QueryRequest::GET(key.to_owned())) | ||
| } | ||
|
|
||
| /// Parses the parameters of a INC query. | ||
| /// | ||
| /// # Arguments: | ||
| /// * `query`: A string containing the parameters of the query, e.g if the query was "INC key" the the parameters are everything after "INC ". | ||
| /// | ||
| /// # Returns: | ||
| /// An instance of `QueryRequest`, variants: INC or ERROR (if the parse failed). | ||
| fn parse_inc(query: &str) -> Result<QueryRequest, String> { | ||
| let key = match validate_key(query) { | ||
| Ok(key) => key, | ||
| Err(error) => return Err(error) | ||
| }; | ||
|
|
||
| Ok(QueryRequest::INC(key.to_owned())) | ||
| } | ||
|
|
||
| /// Parses the parameters of a DEL query. | ||
| /// | ||
|
|
@@ -345,6 +360,9 @@ pub fn parse<'a>(request: &'a str, database_type: &DatabaseType) -> Result<Query | |
| if request.starts_with("GET ") { | ||
| return parse_get(request.strip_prefix("GET ").unwrap()); | ||
| } | ||
| else if request.starts_with("INC ") { | ||
| return parse_inc(request.strip_prefix("INC ").unwrap()); | ||
| } | ||
| else if request.starts_with("DEL ") { | ||
| return parse_del(request.strip_prefix("DEL ").unwrap()); | ||
| } | ||
|
|
@@ -416,6 +434,8 @@ mod tests { | |
| assert_eq!(get_query, parser_error!(ParserErrorType::UnexpectedCharacter)); | ||
| } | ||
|
|
||
| #[test] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some tests here similiar to the others. |
||
|
|
||
| #[test] | ||
| fn test_parse_get_range() { | ||
| let get_range_query = parse_get("RANGE key0 key1"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,9 @@ impl QueryResponse { | |
| QueryResponseType::GET_OK(value) => { | ||
| Self::build_ok_response("GET".to_string(), Some(Self::handle_value_types(&value)), Some(database_type)) | ||
| }, | ||
| QueryResponseType::INC_OK(value) => { | ||
| Self::build_ok_response("INC".to_string(), Some(Self::handle_value_types(&value)), Some(database_type)) | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add tests for inc in this file pls |
||
| QueryResponseType::GET_RANGE_OK(values) => { | ||
| let mut content: String = String::new(); | ||
| for (idx, value) in values.iter().enumerate() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,7 @@ impl State { | |
|
|
||
| match request { | ||
| QueryRequest::GET(key) => self.db.get(&key), | ||
| QueryRequest::INC(key) => self.db.inc(&key), | ||
| QueryRequest::GET_RANGE { key_lower, key_upper } => self.db.get_range(key_lower, key_upper), | ||
| QueryRequest::GET_MANY(keys) => self.db.get_many(keys), | ||
| QueryRequest::DEL(key) => self.db.del(&key), | ||
|
|
@@ -150,6 +151,9 @@ mod tests { | |
| ])); | ||
| assert_eq!(response_set_many, Ok(QueryResponseType::SET_MANY_OK)); | ||
|
|
||
| let response_inc = state.execute_request(client_address, QueryRequest::INC("key_inc".to_string())); | ||
| assert_eq!(response_inc, Ok(QueryResponseType::INC_OK(ValueType::Int(1)))); | ||
|
|
||
|
Comment on lines
+154
to
+156
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coud you add the same query again to test if the value was incremented to 2? |
||
| let response_get = state.execute_request(client_address, QueryRequest::GET("key1".to_string())); | ||
| assert_eq!(response_get, Ok(QueryResponseType::GET_OK(ValueType::Str("value1".to_string())))); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an entry to the
Commandstable in the README for the newINCcommand : )