Skip to content

Commit

Permalink
Refactoring parts of the code to have a more disciplined and strict d…
Browse files Browse the repository at this point in the history
…esign.
  • Loading branch information
JustAn0therDev committed Jun 29, 2021
1 parent 799155b commit f16d3f9
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions black_marlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ BlackMarlin::~BlackMarlin() {
this->Flush();
}

std::string* BlackMarlin::Get(const std::string& p_key) const {
const std::string* BlackMarlin::Get(const std::string& p_key) const {
auto it = this->m_dict.find(p_key);

if (it != this->m_dict.end()) {
Expand Down Expand Up @@ -71,7 +71,7 @@ void BlackMarlin::Delete(const std::string& p_key) {
this->m_dict.erase(p_key);
}

bool BlackMarlin::Exists(const std::string& p_key) const {
const bool BlackMarlin::Exists(const std::string& p_key) const {
auto it = this->m_dict.find(p_key);

if (it != this->m_dict.end()) {
Expand All @@ -81,7 +81,7 @@ bool BlackMarlin::Exists(const std::string& p_key) const {
return false;
}

size_t BlackMarlin::Count() const {
const size_t BlackMarlin::Count() const {
return this->m_dict.size();
}

Expand Down
10 changes: 5 additions & 5 deletions black_marlin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BlackMarlin {
// BlackMarlin's destructor.
~BlackMarlin();
// Returns the value of a key if it exists. If it does not exist, returns a nullptr.
std::string* Get(const std::string& p_key) const;
const std::string* Get(const std::string& p_key) const;
// Sets the key and the value in the map. If the key already exists and the intention was to overwrite the value, the method Overwrite should be used instead.
void Set(std::string p_key, std::string* p_value);
// Sets the key and the value in the map to be deleted later in the specified amount of seconds.
Expand All @@ -19,16 +19,16 @@ class BlackMarlin {
void Overwrite(std::string p_key, std::string* p_value);
// Deletes the pointer to the string and the "bucket" in the map.
void Delete(const std::string& p_key);
// Deletes the pointer in the specified time in seconds.
void DeleteIn(const std::string& p_key, const uint16_t& p_seconds);
// Returns true if the key exists in the map; false otherwise.
bool Exists(const std::string& p_key) const;
const bool Exists(const std::string& p_key) const;
// Returns the number of items in the map.
size_t Count() const;
const size_t Count() const;
// Frees all pointers and buckets in the map.
void Flush();
private:
// The main std::unordered_map for storing the values (string to string pointer).
std::unordered_map<std::string, std::string*> m_dict;
// Deletes the pointer in the specified time in seconds.
void DeleteIn(const std::string& p_key, const uint16_t& p_seconds);
};
#endif
6 changes: 3 additions & 3 deletions http_request_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void HttpRequestHandler::HandleGet(const BlackMarlin& p_black_marlin, const http
return;
}

auto value = p_black_marlin.Get(p_req.get_param_value("key"));
const auto& value = p_black_marlin.Get(p_req.get_param_value("key"));

if (value == nullptr || std::empty(*value)) {
p_res.status = (int)StatusCode::kNoContent;
Expand Down Expand Up @@ -45,9 +45,9 @@ void HttpRequestHandler::HandlePost(BlackMarlin& p_black_marlin, const httplib::
return;
}

const auto& seconds = Util::TryCastStringToInt(expires_in_seconds);
const auto& seconds_to_expire = Util::TryCastStringToUnsignedShortInt(expires_in_seconds);

p_black_marlin.SetToDeleteLater(key, req_body_ptr, seconds);
p_black_marlin.SetToDeleteLater(key, req_body_ptr, seconds_to_expire);
}
else {
p_black_marlin.Set(key, req_body_ptr);
Expand Down
4 changes: 4 additions & 0 deletions http_request_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#define HTTPREQUESTHANDLERCPP
class HttpRequestHandler {
public:
// The program's address.
const char* m_ip_address = "127.0.0.1";
// The program's running port.
const int m_port = 7000;
// Handles a Get request.
void HandleGet(const BlackMarlin& p_blackMarlin, const httplib::Request& p_req, httplib::Response& p_res) const;
// Handles a Post request.
Expand Down
7 changes: 2 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ int main() {
http_request_handler.HandleGetCount(black_marlin, res);
});

const char* address = "127.0.0.1";
int port = 7000;

std::cout << "Listening at: " << address << ":" << port << "\n";
server.listen(address, port);
std::cout << "Listening at: " << http_request_handler.m_ip_address << ":" << http_request_handler.m_port << "\n";
server.listen(http_request_handler.m_ip_address, http_request_handler.m_port);

return EXIT_SUCCESS;
}
7 changes: 5 additions & 2 deletions status_code.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Enum class with HTTP Status Codes.
enum class StatusCode {
// Resource created in the server.
kCreated = 201,
// Nothing returned in the response body but the headers might be useful (or not).
kNoContent = 204,
kBadRequest = 400,
kNotFound = 404
// Client sent a request with bad syntax.
kBadRequest = 400
};
6 changes: 3 additions & 3 deletions unittests/black_marlin_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ TEST_CASE("Black Marlin Get and Set Tests", "[Get and Set]")
std::string* deletable_val = new std::string("get and set test");
std::string sample_key = "get and set test";
black_marlin.Set(sample_key, deletable_val);
std::string* value = black_marlin.Get(sample_key);

const std::string* value = black_marlin.Get(sample_key);

REQUIRE(*value != "");
REQUIRE(*value == "get and set test");
Expand Down Expand Up @@ -42,7 +43,7 @@ TEST_CASE("Overwrite tests", "[Overwrite]")
std::string* val = new std::string("get and set test two");
black_marlin.Overwrite(sample_key, val);

std::string* value = black_marlin.Get(sample_key);
const std::string* value = black_marlin.Get(sample_key);

REQUIRE(*value == "get and set test two");
}
Expand Down Expand Up @@ -85,4 +86,3 @@ TEST_CASE("Flush Test", "[Flush]")

REQUIRE(black_marlin.Count() == 0);
}

1 change: 0 additions & 1 deletion unittests/run_unittests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/bash

g++ -pthread -std=c++11 ../black_marlin.hpp ../black_marlin.cpp ../util.hpp ../util.cpp catch2.hpp black_marlin_tests.cpp util_tests.cpp -o unittests
valgrind ./unittests
3 changes: 1 addition & 2 deletions util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

#ifndef UTILCPP
#define UTILCPP
class Util {
public:
struct Util {
// Tries casting a string to int. If not successful, returns 0.
static int TryCastStringToInt(std::string& p_str);
// Tries casting a string to an uint16_t. If not successful, returns 0.
Expand Down

0 comments on commit f16d3f9

Please sign in to comment.