finished statement cache class

This commit is contained in:
2025-08-05 19:30:40 +02:00
parent 31964e55c1
commit 35fad9f47c
13 changed files with 410 additions and 74 deletions
@@ -26,14 +26,14 @@ public:
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(const interface::parameter_binder& bindings) = 0;
template < class Type >
void bind_object(Type &obj, const interface::parameter_binder& bindings) {
void bind_object(Type &obj, interface::parameter_binder& bindings) {
object_parameter_binder object_binder_;
object_binder_.reset(start_index());
object_binder_.bind(obj, bindings);
}
template < class Type >
void bind(const size_t pos, Type &val, const interface::parameter_binder& bindings) {
void bind(const size_t pos, Type &val, interface::parameter_binder& bindings) {
utils::data_type_traits<Type>::bind_value(bindings, adjust_index(pos), val);
}
@@ -16,11 +16,11 @@ public:
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(interface::parameter_binder& bindings) = 0;
template<class Type>
void bind(const Type &obj, const interface::parameter_binder& bindings) {
void bind(const Type &obj, interface::parameter_binder& bindings) {
statement_->bind_object(obj, bindings);
}
template<typename Type>
void bind(size_t pos, Type &value, const interface::parameter_binder& bindings) {
void bind(size_t pos, Type &value, interface::parameter_binder& bindings) {
statement_->bind(pos, value, bindings);
}
void bind(size_t pos, const char *value, size_t size, interface::parameter_binder& bindings) const;
@@ -28,6 +28,8 @@ public:
void reset() const;
[[nodiscard]] std::string sql() const;
[[nodiscard]] std::unique_ptr<utils::attribute_writer> create_binder() const;
protected:
+8 -8
View File
@@ -31,17 +31,17 @@ struct sql_command_info {
struct query_context {
std::string sql;
sql_command command{};
std::string command_name;
std::string command_name{};
sql::table table{""};
std::vector<object::attribute_definition> prototype;
std::vector<std::string> result_vars;
std::vector<std::string> bind_vars;
std::vector<utils::database_type> bind_types;
std::vector<object::attribute_definition> prototype{};
std::vector<std::string> result_vars{};
std::vector<std::string> bind_vars{};
std::vector<utils::database_type> bind_types{};
std::unordered_map<std::string, std::string> column_aliases;
std::unordered_map<std::string, std::string> table_aliases;
std::unordered_map<std::string, std::string> column_aliases{};
std::unordered_map<std::string, std::string> table_aliases{};
std::vector<sql_command_info> additional_commands;
std::vector<sql_command_info> additional_commands{};
};
}
+3 -2
View File
@@ -5,7 +5,6 @@
#include "matador/sql/query_result.hpp"
#include "matador/sql/interface/statement_proxy.hpp"
#include "matador/sql/interface/parameter_binder.hpp"
#include "matador/utils/error.hpp"
#include "matador/utils/result.hpp"
@@ -122,6 +121,8 @@ public:
*/
void reset() const;
[[nodiscard]] std::string sql() const;
private:
template<class Type>
friend class detail::identifier_binder;
@@ -140,7 +141,7 @@ statement &statement::bind(size_t pos, Type &value) {
template<class Type>
statement &statement::bind(const Type &obj) {
statement_proxy_->bind(obj, bindings_);
statement_proxy_->bind(obj, *bindings_);
return *this;
}
+36 -4
View File
@@ -2,8 +2,7 @@
#define STATEMENT_CACHE_HPP
#include "matador/sql/executor.hpp"
#include "matador/sql/interface/statement_proxy.hpp"
#include "matador/sql/statement.hpp"
#include <list>
#include <mutex>
@@ -13,9 +12,36 @@ namespace matador::sql {
class connection_pool;
struct statement_cache_event {
enum class Type { Accessed, Added, Evicted };
Type type;
std::string sql;
std::chrono::steady_clock::time_point timestamp;
};
class statement_cache_observer_interface {
public:
virtual void on_event(const statement_cache_event&) = 0;
virtual ~statement_cache_observer_interface() = default;
};
class statement_cache final {
private:
using list_iterator = std::list<size_t>::iterator;
struct cache_entry {
statement stmt;
std::chrono::steady_clock::time_point last_access;
list_iterator position;
};
public:
explicit statement_cache(connection_pool &pool, size_t max_size = 50);
statement_cache(const statement_cache &) = delete;
statement_cache &operator=(const statement_cache &) = delete;
statement_cache(statement_cache &&) = delete;
statement_cache &operator=(statement_cache &&) = delete;
~statement_cache() = default;
[[nodiscard]] utils::result<statement, utils::error> acquire(const query_context &ctx);
@@ -23,16 +49,22 @@ public:
[[nodiscard]] size_t capacity() const;
[[nodiscard]] bool empty() const;
void subscribe(statement_cache_observer_interface &observer);
private:
void push(statement_cache_event::Type type, const std::string& sql) const;
private:
using list_iterator = std::list<size_t>::iterator;
size_t max_size_{};
std::list<size_t> usage_list_; // LRU: front = most recent, back = least recent
std::unordered_map<size_t, std::pair<statement, list_iterator>> cache_map_;
std::unordered_map<size_t, cache_entry> cache_map_;
std::mutex mutex_;
connection_pool &pool_;
const sql::dialect &dialect_;
std::vector<statement_cache_observer_interface*> observers_;
};
}
#endif //STATEMENT_CACHE_HPP