Skip to content

feat: implement INC - #2

Open
wesleymatosdev wants to merge 1 commit into
theopfr:mainfrom
wesleymatosdev:implement-inc
Open

feat: implement INC#2
wesleymatosdev wants to merge 1 commit into
theopfr:mainfrom
wesleymatosdev:implement-inc

Conversation

@wesleymatosdev

Copy link
Copy Markdown

I added this draft to gather feedback. Lmk what you think @theopfr and I can implement also DECR.

@theopfr

theopfr commented Jul 31, 2025

Copy link
Copy Markdown
Owner

Oh you really revived this project haha, I'll take a look, thanks : )

@theopfr theopfr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Added a couple of small comments but all-in-all nice job : )

Comment thread cachew/src/parser.rs
assert_eq!(get_query, parser_error!(ParserErrorType::UnexpectedCharacter));
}

#[test]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests here similiar to the others.

Comment thread cachew/src/state.rs
Comment on lines +154 to +156
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))));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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?

Comment thread README.md
- [x] add graceful shutdown
- [x] add command line flag handler
- [x] built CLI client
- [x] add INCR/DECR commands for INT type

Copy link
Copy Markdown
Owner

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 Commands table in the README for the new INC command : )

Comment thread cachew/src/database.rs
let default_value = ValueType::Int(1);
self.storage.insert(key.to_owned(), serialize(&default_value).unwrap());
return Ok(QueryResponseType::INC_OK(default_value));
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Two points here:

  1. We need a guard here at the beginning of the function that checks if the value-type is INT. Because if it the type is STR and we do INC key for a non-exisiting key, it will create a key-value pair "key: 1" where 1 is an integer and not a STR.
  2. Could you add also tests here in the database.rs for inc?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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);
}

Comment thread cachew/src/response.rs
},
QueryResponseType::INC_OK(value) => {
Self::build_ok_response("INC".to_string(), Some(Self::handle_value_types(&value)), Some(database_type))
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add tests for inc in this file pls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants