Skip to content

Commit

Permalink
SQL Partition Aware review fixes (#1188)
Browse files Browse the repository at this point in the history
* review fixes

* review fixes
  • Loading branch information
akeles85 authored Apr 5, 2023
1 parent 610be4b commit cc1719d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,43 +73,36 @@ class read_optimized_lru_cache
* @returns Returns the value to which the specified key is cached,
* or default value if this cache contains no mapping for the key.
*/
std::shared_ptr<V> get_or_default(const K& key,
const std::shared_ptr<V>& default_value)
V get_or_default(const K& key, const V& default_value)
{
const auto existing_value = get(key);
return (existing_value != nullptr) ? existing_value : default_value;
return existing_value ? *existing_value : default_value;
}

/**
* @param key the key of the cache entry
* Returns the value to which the specified key is cached,
* or {@code null} if this cache contains no mapping for the key.
* or {@code boost::none} if this cache contains no mapping for the key.
* @returns Returns the value to which the specified key is cached
*/
std::shared_ptr<V> get(const K& key)
boost::optional<V> get(const K& key)
{
auto value_from_cache = cache_.get(key);
if (value_from_cache == nullptr) {
return nullptr;
return boost::none;
}
value_from_cache->touch();
return std::make_shared<int32_t>(value_from_cache->value_);
return boost::make_optional(value_from_cache->value_);
}

/**
* @param key the key of the cache entry
* @param value the value of the cache entry
* @throws exception::illegal_argument if the value equals to nullptr
*/
void put(const K& key, const std::shared_ptr<V>& value)
void put(const K& key, const V& value)
{
if (value == nullptr) {
BOOST_THROW_EXCEPTION(client::exception::illegal_argument(
"Null values are disallowed"));
}

auto old_value =
cache_.put(key, std::make_shared<value_and_timestamp<V>>(*value));
cache_.put(key, std::make_shared<value_and_timestamp<V>>(value));
if (old_value == nullptr && cache_.size() > cleanup_threshold_) {
do_cleanup();
}
Expand All @@ -130,15 +123,15 @@ class read_optimized_lru_cache
{
public:
const T value_;
int64_t timestamp_;
std::atomic<int64_t> timestamp_;

value_and_timestamp(T value)
: value_(value)
{
touch();
}

void touch() { timestamp_ = util::current_time_nanos(); }
void touch() { timestamp_.store(util::current_time_nanos()); }
};

util::SynchronizedMap<K, value_and_timestamp<V>> cache_;
Expand Down
13 changes: 6 additions & 7 deletions hazelcast/src/hazelcast/client/sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ sql_service::execute(const sql_statement& statement)

auto arg_index = statement_par_arg_index != -1
? statement_par_arg_index
: *(partition_argument_index_cache_->get_or_default(
statement.sql(), std::make_shared<int32_t>(-1)));
: (partition_argument_index_cache_->get_or_default(
statement.sql(), -1));

auto partition_id = extract_partition_id(statement, arg_index);
std::shared_ptr<connection::Connection> query_conn =
partition_id != boost::none ? query_connection(partition_id.value())
: query_connection();
partition_id ? query_connection(partition_id.value())
: query_connection();

sql::impl::query_id qid = create_query_id(query_conn);

Expand Down Expand Up @@ -259,8 +259,7 @@ sql_service::handle_execute_response(
original_partition_argument_index) {
if (response.partition_argument_index != -1) {
partition_argument_index_cache_->put(
sql_query,
std::make_shared<int32_t>(response.partition_argument_index));
sql_query, response.partition_argument_index);

if (auto argument_index = statement_par_arg_index_ptr.lock()) {
argument_index->store(response.partition_argument_index);
Expand Down Expand Up @@ -358,7 +357,7 @@ sql_service::extract_partition_id(const sql_statement& statement,
return boost::none;
}

const auto key = statement.serialized_parameters_[arg_index];
const auto& key = statement.serialized_parameters_[arg_index];

return client_context_.get_partition_service().get_partition_id(key);
}
Expand Down
Loading

0 comments on commit cc1719d

Please sign in to comment.