Skip to content

Commit cc1719d

Browse files
authored
SQL Partition Aware review fixes (#1188)
* review fixes * review fixes
1 parent 610be4b commit cc1719d

File tree

3 files changed

+66
-78
lines changed

3 files changed

+66
-78
lines changed

hazelcast/include/hazelcast/client/sql/impl/read_optimized_lru_cache.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,43 +73,36 @@ class read_optimized_lru_cache
7373
* @returns Returns the value to which the specified key is cached,
7474
* or default value if this cache contains no mapping for the key.
7575
*/
76-
std::shared_ptr<V> get_or_default(const K& key,
77-
const std::shared_ptr<V>& default_value)
76+
V get_or_default(const K& key, const V& default_value)
7877
{
7978
const auto existing_value = get(key);
80-
return (existing_value != nullptr) ? existing_value : default_value;
79+
return existing_value ? *existing_value : default_value;
8180
}
8281

8382
/**
8483
* @param key the key of the cache entry
8584
* Returns the value to which the specified key is cached,
86-
* or {@code null} if this cache contains no mapping for the key.
85+
* or {@code boost::none} if this cache contains no mapping for the key.
8786
* @returns Returns the value to which the specified key is cached
8887
*/
89-
std::shared_ptr<V> get(const K& key)
88+
boost::optional<V> get(const K& key)
9089
{
9190
auto value_from_cache = cache_.get(key);
9291
if (value_from_cache == nullptr) {
93-
return nullptr;
92+
return boost::none;
9493
}
9594
value_from_cache->touch();
96-
return std::make_shared<int32_t>(value_from_cache->value_);
95+
return boost::make_optional(value_from_cache->value_);
9796
}
9897

9998
/**
10099
* @param key the key of the cache entry
101100
* @param value the value of the cache entry
102-
* @throws exception::illegal_argument if the value equals to nullptr
103101
*/
104-
void put(const K& key, const std::shared_ptr<V>& value)
102+
void put(const K& key, const V& value)
105103
{
106-
if (value == nullptr) {
107-
BOOST_THROW_EXCEPTION(client::exception::illegal_argument(
108-
"Null values are disallowed"));
109-
}
110-
111104
auto old_value =
112-
cache_.put(key, std::make_shared<value_and_timestamp<V>>(*value));
105+
cache_.put(key, std::make_shared<value_and_timestamp<V>>(value));
113106
if (old_value == nullptr && cache_.size() > cleanup_threshold_) {
114107
do_cleanup();
115108
}
@@ -130,15 +123,15 @@ class read_optimized_lru_cache
130123
{
131124
public:
132125
const T value_;
133-
int64_t timestamp_;
126+
std::atomic<int64_t> timestamp_;
134127

135128
value_and_timestamp(T value)
136129
: value_(value)
137130
{
138131
touch();
139132
}
140133

141-
void touch() { timestamp_ = util::current_time_nanos(); }
134+
void touch() { timestamp_.store(util::current_time_nanos()); }
142135
};
143136

144137
util::SynchronizedMap<K, value_and_timestamp<V>> cache_;

hazelcast/src/hazelcast/client/sql.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ sql_service::execute(const sql_statement& statement)
7777

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

8383
auto partition_id = extract_partition_id(statement, arg_index);
8484
std::shared_ptr<connection::Connection> query_conn =
85-
partition_id != boost::none ? query_connection(partition_id.value())
86-
: query_connection();
85+
partition_id ? query_connection(partition_id.value())
86+
: query_connection();
8787

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

@@ -259,8 +259,7 @@ sql_service::handle_execute_response(
259259
original_partition_argument_index) {
260260
if (response.partition_argument_index != -1) {
261261
partition_argument_index_cache_->put(
262-
sql_query,
263-
std::make_shared<int32_t>(response.partition_argument_index));
262+
sql_query, response.partition_argument_index);
264263

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

361-
const auto key = statement.serialized_parameters_[arg_index];
360+
const auto& key = statement.serialized_parameters_[arg_index];
362361

363362
return client_context_.get_partition_service().get_partition_id(key);
364363
}

0 commit comments

Comments
 (0)