From fd6cc5bef4be2ea52d46624c15ef93ff93bbaf8e Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 30 May 2025 23:46:39 +1200 Subject: [PATCH] :-'Generic Commit' --- src/database.cpp | 28 +++++++++++++++++++++++++--- src/put_handler.cpp | 1 + src/utils.cpp | 11 ++++++++++- src/utils.hpp | 2 ++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/database.cpp b/src/database.cpp index daaec1d..00e42c3 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -1,9 +1,11 @@ #include #include #include +#include #include "database.hpp" #include "sqlite3/sqlite3.h" +#include "utils.hpp" namespace simple_object_storage { @@ -206,14 +208,34 @@ bool Database::run_sql_text(const std::string& sql, const std::string& bind_text return true; } + +bool is_dec_uint64(const std::string& s) { + if (s.empty()) return false; + for (char c : s) { + if (!std::isdigit(static_cast(c))) return false; + } + try { + uint64_t x = std::stoull(s, nullptr, 10); + std::string s2=std::to_string(x); + return s2 == s; + } catch (...) { + return false; + } +} + bool Database::get(const std::string& hash_or_labeltag, dbEntry& entry) { + std::string key=trim(hash_or_labeltag); - if (hash_or_labeltag.find(':') != std::string::npos) + if (hash_or_labeltag.find(':') != std::string::npos) { return (run_sql_text("SELECT hash, labeltags, metadata FROM objects WHERE json_array_length(labeltags) > 0 AND EXISTS (SELECT 1 FROM json_each(labeltags) WHERE value = ?);", hash_or_labeltag, entry)); + } - if (run_sql_text("SELECT hash, labeltags, metadata FROM objects WHERE hash = ?;", hash_or_labeltag, entry)) - return true; + if (is_dec_uint64(hash_or_labeltag)) { + if (run_sql_text("SELECT hash, labeltags, metadata FROM objects WHERE hash = ?;", hash_or_labeltag, entry)) + return true; + } + // try with a :latest tag std::string with_latest = hash_or_labeltag + ":latest"; return (run_sql_text("SELECT hash, labeltags, metadata FROM objects WHERE hash = ?;", with_latest, entry)); } diff --git a/src/put_handler.cpp b/src/put_handler.cpp index 21639c2..c17526b 100644 --- a/src/put_handler.cpp +++ b/src/put_handler.cpp @@ -193,4 +193,5 @@ void PutHandler::add_file_metadata(const std::string& file_path, nlohmann::json& metadata["file_modification_time"] = std::chrono::system_clock::to_time_t(sctp); } + } // namespace simple_object_storage \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp index 353da82..a844620 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -4,7 +4,7 @@ namespace simple_object_storage { ScopeFileDeleter::ScopeFileDeleter(const std::filesystem::path &path) : path_(path), released_(false) {} - + ScopeFileDeleter::~ScopeFileDeleter() { if (!released_) @@ -25,4 +25,13 @@ namespace simple_object_storage void ScopeFileDeleter::release() { released_ = true; } + std::string trim(const std::string &s) + { + size_t start = s.find_first_not_of(" \t\n\r\f\v"); + if (start == std::string::npos) + return ""; + size_t end = s.find_last_not_of(" \t\n\r\f\v"); + return s.substr(start, end - start + 1); + } + } diff --git a/src/utils.hpp b/src/utils.hpp index b9c0a1a..907f1fd 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -16,4 +16,6 @@ private: bool released_; }; +std::string trim(const std::string& s); + } \ No newline at end of file