Skip to content

Commit e763c6b

Browse files
Pass ColumnFamilyHandle around by reference; wrap Iterator in unique_ptr
In preparation for the range locking patch, clean up some code related to iterators: - Pass rocksdb::ColumnFamilyHandle around and store in classes by references instead of pointers. It is never nullptr, nor is ever reseated in the classes using it. - Wrap the rocksdb::Iterator objects in std::unique_ptr: change the factory method return types and class fields. - In both cases above mark the classes containing reference fields as non-copyable and non-moveable as needed, matching their current usage. - Mark touched methods [[nodiscard]] as applicable. In rdb_index_merge.h, remove many instances of redundant MY_ATTRIBUTE((__nonnull__)) too. - Make touched local vars auto, but with reference or pointer pulled out, sometimes avoiding redundant returned object copies. - Remove redundant instances of const from passed-by-value arguments.
1 parent 55e2d62 commit e763c6b

14 files changed

+264
-269
lines changed

storage/rocksdb/ha_rocksdb.cc

Lines changed: 126 additions & 129 deletions
Large diffs are not rendered by default.

storage/rocksdb/ha_rocksdb.h

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
/* C++ standard header files */
2323
#include <deque>
2424
#include <map>
25+
#include <memory>
2526
#include <string>
2627
#include <string_view>
2728
#include <unordered_map>
@@ -653,11 +654,10 @@ class ha_rocksdb : public my_core::handler, public blob_buffer {
653654
int delete_row(const uchar *const buf) override
654655
MY_ATTRIBUTE((__warn_unused_result__));
655656
void update_table_stats_if_needed();
656-
rocksdb::Status delete_or_singledelete(uint index, Rdb_transaction *const tx,
657-
rocksdb::ColumnFamilyHandle *const cf,
658-
const rocksdb::Slice &key,
659-
TABLE_TYPE table_type)
660-
MY_ATTRIBUTE((__warn_unused_result__));
657+
658+
[[nodiscard]] rocksdb::Status delete_or_singledelete(
659+
uint index, Rdb_transaction *tx, rocksdb::ColumnFamilyHandle &cf,
660+
const rocksdb::Slice &key, TABLE_TYPE table_type);
661661

662662
int index_next(uchar *const buf) override
663663
MY_ATTRIBUTE((__warn_unused_result__));
@@ -1205,18 +1205,17 @@ void remove_tmp_table_handler(THD *const thd, ha_rocksdb *rocksdb_handler);
12051205

12061206
const rocksdb::ReadOptions &rdb_tx_acquire_snapshot(Rdb_transaction *tx);
12071207

1208-
rocksdb::Iterator *rdb_tx_get_iterator(
1209-
THD *thd, rocksdb::ColumnFamilyHandle *const cf, bool skip_bloom_filter,
1208+
[[nodiscard]] std::unique_ptr<rocksdb::Iterator> rdb_tx_get_iterator(
1209+
THD *thd, rocksdb::ColumnFamilyHandle &cf, bool skip_bloom_filter,
12101210
const rocksdb::Slice &eq_cond_lower_bound,
12111211
const rocksdb::Slice &eq_cond_upper_bound,
12121212
const rocksdb::Snapshot **snapshot, TABLE_TYPE table_type,
12131213
bool read_current = false, bool create_snapshot = true);
12141214

1215-
rocksdb::Status rdb_tx_get(Rdb_transaction *tx,
1216-
rocksdb::ColumnFamilyHandle *const column_family,
1217-
const rocksdb::Slice &key,
1218-
rocksdb::PinnableSlice *const value,
1219-
TABLE_TYPE table_type);
1215+
[[nodiscard]] rocksdb::Status rdb_tx_get(
1216+
Rdb_transaction *tx, rocksdb::ColumnFamilyHandle &column_family,
1217+
const rocksdb::Slice &key, rocksdb::PinnableSlice *const value,
1218+
TABLE_TYPE table_type);
12201219

12211220
rocksdb::Status rdb_tx_get_for_update(Rdb_transaction *tx,
12221221
const Rdb_key_def &kd,
@@ -1229,36 +1228,33 @@ void rdb_tx_release_lock(Rdb_transaction *tx, const Rdb_key_def &kd,
12291228
const rocksdb::Slice &key, bool force);
12301229

12311230
void rdb_tx_multi_get(Rdb_transaction *tx,
1232-
rocksdb::ColumnFamilyHandle *const column_family,
1233-
const size_t num_keys, const rocksdb::Slice *keys,
1231+
rocksdb::ColumnFamilyHandle &column_family,
1232+
size_t num_keys, const rocksdb::Slice *keys,
12341233
rocksdb::PinnableSlice *values, TABLE_TYPE table_type,
1235-
rocksdb::Status *statuses, const bool sorted_input);
1234+
rocksdb::Status *statuses, bool sorted_input);
12361235

1237-
inline void rocksdb_smart_seek(bool seek_backward,
1238-
rocksdb::Iterator *const iter,
1236+
inline void rocksdb_smart_seek(bool seek_backward, rocksdb::Iterator &iter,
12391237
const rocksdb::Slice &key_slice) {
12401238
if (seek_backward) {
1241-
iter->SeekForPrev(key_slice);
1239+
iter.SeekForPrev(key_slice);
12421240
} else {
1243-
iter->Seek(key_slice);
1241+
iter.Seek(key_slice);
12441242
}
12451243
}
12461244

1247-
inline void rocksdb_smart_next(bool seek_backward,
1248-
rocksdb::Iterator *const iter) {
1245+
inline void rocksdb_smart_next(bool seek_backward, rocksdb::Iterator &iter) {
12491246
if (seek_backward) {
1250-
iter->Prev();
1247+
iter.Prev();
12511248
} else {
1252-
iter->Next();
1249+
iter.Next();
12531250
}
12541251
}
12551252

1256-
inline void rocksdb_smart_prev(bool seek_backward,
1257-
rocksdb::Iterator *const iter) {
1253+
inline void rocksdb_smart_prev(bool seek_backward, rocksdb::Iterator &iter) {
12581254
if (seek_backward) {
1259-
iter->Next();
1255+
iter.Next();
12601256
} else {
1261-
iter->Prev();
1257+
iter.Prev();
12621258
}
12631259
}
12641260

storage/rocksdb/nosql_access.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,17 +1505,16 @@ class select_exec {
15051505
return false;
15061506
}
15071507

1508-
rocksdb::Iterator *get_iterator(rocksdb::ColumnFamilyHandle *cf,
1509-
bool use_bloom,
1510-
const rocksdb::Slice &lower_bound,
1511-
const rocksdb::Slice &upper_bound) {
1508+
[[nodiscard]] std::unique_ptr<rocksdb::Iterator> get_iterator(
1509+
rocksdb::ColumnFamilyHandle &cf, bool use_bloom,
1510+
const rocksdb::Slice &lower_bound, const rocksdb::Slice &upper_bound) {
15121511
return rdb_tx_get_iterator(m_thd, cf, !use_bloom, lower_bound,
15131512
upper_bound, nullptr, m_table_type);
15141513
}
15151514

1516-
rocksdb::Status get(rocksdb::ColumnFamilyHandle *cf,
1517-
const rocksdb::Slice &key_slice,
1518-
rocksdb::PinnableSlice *value_slice) {
1515+
[[nodiscard]] rocksdb::Status get(rocksdb::ColumnFamilyHandle &cf,
1516+
const rocksdb::Slice &key_slice,
1517+
rocksdb::PinnableSlice *value_slice) {
15191518
rocksdb::Status s;
15201519
return rdb_tx_get(m_tx, cf, key_slice, value_slice, m_table_type);
15211520
}

storage/rocksdb/rdb_cf_manager.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ struct Rdb_cf_scanner : public Rdb_tables_scanner {
374374
for (uint i = 0; i < tdef->m_key_count; i++) {
375375
const Rdb_key_def &kd = *tdef->m_key_descr_arr[i];
376376

377-
if (kd.get_cf()->GetID() == m_cf_id) {
377+
if (kd.get_cf().GetID() == m_cf_id) {
378378
return HA_EXIT_FAILURE;
379379
}
380380
}

storage/rocksdb/rdb_datadic.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ uint Rdb_key_def::setup(const TABLE &tbl, const Rdb_tbl_def &tbl_def,
559559
m_stats.m_distinct_keys_per_prefix.resize(get_key_parts());
560560

561561
/* Cache prefix extractor for bloom filter usage later */
562-
rocksdb::Options opt = rdb_get_rocksdb_db()->GetOptions(get_cf());
562+
const auto opt = rdb_get_rocksdb_db()->GetOptions(&get_cf());
563563
m_prefix_extractor = opt.prefix_extractor;
564564

565565
uint rtn = setup_vector_index(tbl, tbl_def, cmd_srv_helper);
@@ -4205,15 +4205,15 @@ bool Rdb_tbl_def::put_dict(Rdb_dict_manager *const dict,
42054205
for (uint i = 0; i < m_key_count; i++) {
42064206
const Rdb_key_def &kd = *m_key_descr_arr[i];
42074207

4208-
const uint cf_id = kd.get_cf()->GetID();
4208+
const auto cf_id = kd.get_cf().GetID();
42094209
/*
42104210
If cf_id already exists, cf_flags must be the same.
42114211
To prevent race condition, reading/modifying/committing CF flags
42124212
need to be protected by mutex (dict_manager->lock()).
42134213
When RocksDB supports transaction with pessimistic concurrency
42144214
control, we can switch to use it and removing mutex.
42154215
*/
4216-
const std::string cf_name = kd.get_cf()->GetName();
4216+
const auto &cf_name = kd.get_cf().GetName();
42174217

42184218
std::shared_ptr<rocksdb::ColumnFamilyHandle> cfh =
42194219
cf_manager->get_cf(cf_name);
@@ -4366,7 +4366,7 @@ int Rdb_ddl_manager::find_in_uncommitted_keydef(const uint32_t cf_id) {
43664366
for (const auto &pr : m_index_num_to_uncommitted_keydef) {
43674367
const auto &kd = pr.second;
43684368

4369-
if (kd->get_cf()->GetID() == cf_id) {
4369+
if (kd->get_cf().GetID() == cf_id) {
43704370
mysql_rwlock_unlock(&m_rwlock);
43714371
return HA_EXIT_FAILURE;
43724372
}

storage/rocksdb/rdb_datadic.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,10 @@ class Rdb_key_def {
628628
const Rdb_tbl_def &tbl_def_arg, bool &per_part_match_found,
629629
const char *const qualifier);
630630

631-
rocksdb::ColumnFamilyHandle *get_cf() const { return m_cf_handle.get(); }
631+
[[nodiscard]] rocksdb::ColumnFamilyHandle &get_cf() const {
632+
return *m_cf_handle;
633+
}
634+
632635
std::shared_ptr<rocksdb::ColumnFamilyHandle> get_shared_cf() const {
633636
return m_cf_handle;
634637
}

storage/rocksdb/rdb_i_s.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ int Rdb_ddl_scanner::add_table(Rdb_tbl_def *tdef) {
14941494
field[RDB_DDL_FIELD::TTL_DURATION]->store(kd.m_ttl_duration, true);
14951495
field[RDB_DDL_FIELD::INDEX_FLAGS]->store(kd.m_index_flags_bitmap, true);
14961496

1497-
std::string cf_name = kd.get_cf()->GetName();
1497+
const auto &cf_name = kd.get_cf().GetName();
14981498
field[RDB_DDL_FIELD::CF]->store(cf_name.c_str(), cf_name.size(),
14991499
system_charset_info);
15001500
ulonglong auto_incr;

storage/rocksdb/rdb_index_merge.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131

3232
namespace myrocks {
3333

34-
Rdb_index_merge::Rdb_index_merge(const char *const tmpfile_path,
34+
Rdb_index_merge::Rdb_index_merge(const char *tmpfile_path,
3535
const ulonglong merge_buf_size,
3636
const ulonglong merge_combine_read_size,
3737
const ulonglong merge_tmp_file_removal_delay,
38-
rocksdb::ColumnFamilyHandle *cf)
38+
rocksdb::ColumnFamilyHandle &cf)
3939
: m_tmpfile_path(tmpfile_path),
4040
m_merge_buf_size(merge_buf_size),
4141
m_merge_combine_read_size(merge_combine_read_size),
@@ -198,7 +198,7 @@ int Rdb_index_merge::add(const rocksdb::Slice &key, const rocksdb::Slice &val) {
198198
/* Find sort order of the new record */
199199
auto res =
200200
m_offset_tree.emplace(m_rec_buf_unsorted->m_block.get() + rec_offset,
201-
m_cf_handle->GetComparator());
201+
m_cf_handle.GetComparator());
202202
if (!res.second) {
203203
my_printf_error(ER_DUP_ENTRY,
204204
"Failed to insert the record: the key already exists",
@@ -309,7 +309,7 @@ int Rdb_index_merge::merge_heap_prepare() {
309309
/* Allocate buffers for each chunk */
310310
for (ulonglong i = 0; i < m_merge_file.m_num_sort_buffers; i++) {
311311
const auto entry =
312-
std::make_shared<merge_heap_entry>(m_cf_handle->GetComparator());
312+
std::make_shared<merge_heap_entry>(m_cf_handle.GetComparator());
313313

314314
/*
315315
Read chunk_size bytes from each chunk on disk, and place inside

storage/rocksdb/rdb_index_merge.h

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ class Rdb_key_def;
4646
class Rdb_tbl_def;
4747

4848
class Rdb_index_merge {
49-
Rdb_index_merge(const Rdb_index_merge &p) = delete;
50-
Rdb_index_merge &operator=(const Rdb_index_merge &p) = delete;
49+
Rdb_index_merge(const Rdb_index_merge &) = delete;
50+
Rdb_index_merge &operator=(const Rdb_index_merge &) = delete;
51+
Rdb_index_merge(const Rdb_index_merge &&) = delete;
52+
Rdb_index_merge &operator=(Rdb_index_merge &&) = delete;
5153

5254
public:
5355
/* Information about temporary files used in external merge sort */
@@ -67,15 +69,13 @@ class Rdb_index_merge {
6769
ulonglong m_disk_curr_offset; /* current offset on disk */
6870
uint64 m_total_size; /* total # of data bytes in chunk */
6971

70-
void store_key_value(const rocksdb::Slice &key, const rocksdb::Slice &val)
71-
MY_ATTRIBUTE((__nonnull__));
72+
void store_key_value(const rocksdb::Slice &key, const rocksdb::Slice &val);
7273

73-
void store_slice(const rocksdb::Slice &slice) MY_ATTRIBUTE((__nonnull__));
74+
void store_slice(const rocksdb::Slice &slice);
7475

75-
size_t prepare(File fd, ulonglong f_offset) MY_ATTRIBUTE((__nonnull__));
76+
[[nodiscard]] size_t prepare(File fd, ulonglong f_offset);
7677

77-
int read_next_chunk_from_disk(File fd)
78-
MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
78+
[[nodiscard]] int read_next_chunk_from_disk(File fd);
7979

8080
inline bool is_chunk_finished() const {
8181
return m_curr_offset + m_disk_curr_offset - m_disk_start_offset ==
@@ -109,17 +109,16 @@ class Rdb_index_merge {
109109
rocksdb::Slice m_key; /* current key pointed to by block ptr */
110110
rocksdb::Slice m_val;
111111

112-
size_t prepare(File fd, ulonglong f_offset, ulonglong chunk_size)
113-
MY_ATTRIBUTE((__nonnull__));
112+
[[nodiscard]] size_t prepare(File fd, ulonglong f_offset,
113+
ulonglong chunk_size);
114114

115-
int read_next_chunk_from_disk(File fd)
116-
MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
115+
[[nodiscard]] int read_next_chunk_from_disk(File fd);
117116

118-
int read_rec(rocksdb::Slice *const key, rocksdb::Slice *const val)
119-
MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
117+
[[nodiscard]] int read_rec(rocksdb::Slice *const key,
118+
rocksdb::Slice *const val);
120119

121-
int read_slice(rocksdb::Slice *const slice, const uchar **block_ptr)
122-
MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
120+
[[nodiscard]] int read_slice(rocksdb::Slice *const slice,
121+
const uchar **block_ptr);
123122

124123
explicit merge_heap_entry(const rocksdb::Comparator *const comparator)
125124
: m_chunk_info(nullptr), m_block(nullptr), m_comparator(comparator) {}
@@ -152,7 +151,7 @@ class Rdb_index_merge {
152151
const ulonglong m_merge_buf_size;
153152
const ulonglong m_merge_combine_read_size;
154153
const ulonglong m_merge_tmp_file_removal_delay;
155-
rocksdb::ColumnFamilyHandle *m_cf_handle;
154+
rocksdb::ColumnFamilyHandle &m_cf_handle;
156155
struct merge_file_info m_merge_file;
157156
std::shared_ptr<merge_buf_info> m_rec_buf_unsorted;
158157
std::shared_ptr<merge_buf_info> m_output_buf;
@@ -180,9 +179,9 @@ class Rdb_index_merge {
180179
return rocksdb::Slice(reinterpret_cast<const char *>(block), len);
181180
}
182181

183-
static int merge_record_compare(const uchar *a_block, const uchar *b_block,
184-
const rocksdb::Comparator *const comparator)
185-
MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
182+
[[nodiscard]] static int merge_record_compare(
183+
const uchar *a_block, const uchar *b_block,
184+
const rocksdb::Comparator *const comparator) MY_ATTRIBUTE((__nonnull__));
186185

187186
void merge_read_rec(const uchar *const block, rocksdb::Slice *const key,
188187
rocksdb::Slice *const val) MY_ATTRIBUTE((__nonnull__));
@@ -191,37 +190,36 @@ class Rdb_index_merge {
191190
MY_ATTRIBUTE((__nonnull__));
192191

193192
public:
194-
Rdb_index_merge(const char *const tmpfile_path,
195-
const ulonglong merge_buf_size,
196-
const ulonglong merge_combine_read_size,
197-
const ulonglong merge_tmp_file_removal_delay,
198-
rocksdb::ColumnFamilyHandle *cf);
193+
Rdb_index_merge(const char *tmpfile_path, ulonglong merge_buf_size,
194+
ulonglong merge_combine_read_size,
195+
ulonglong merge_tmp_file_removal_delay,
196+
rocksdb::ColumnFamilyHandle &cf);
199197
~Rdb_index_merge();
200198

201-
int init() MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
199+
[[nodiscard]] int init();
202200

203-
int merge_file_create() MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
201+
[[nodiscard]] int merge_file_create();
204202

205-
int add(const rocksdb::Slice &key, const rocksdb::Slice &val)
206-
MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
203+
[[nodiscard]] int add(const rocksdb::Slice &key, const rocksdb::Slice &val);
207204

208-
int merge_buf_write() MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
205+
[[nodiscard]] int merge_buf_write();
209206

210-
int next(rocksdb::Slice *const key, rocksdb::Slice *const val)
211-
MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
207+
[[nodiscard]] int next(rocksdb::Slice *const key, rocksdb::Slice *const val);
212208

213-
int merge_heap_prepare() MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
209+
[[nodiscard]] int merge_heap_prepare();
214210

215211
void merge_heap_top(rocksdb::Slice *key, rocksdb::Slice *val)
216212
MY_ATTRIBUTE((__nonnull__));
217213

218-
int merge_heap_pop_and_get_next(rocksdb::Slice *const key,
219-
rocksdb::Slice *const val)
220-
MY_ATTRIBUTE((__nonnull__, __warn_unused_result__));
214+
[[nodiscard]] int merge_heap_pop_and_get_next(rocksdb::Slice *const key,
215+
rocksdb::Slice *const val)
216+
MY_ATTRIBUTE((__nonnull__));
221217

222218
void merge_reset();
223219

224-
rocksdb::ColumnFamilyHandle *get_cf() const { return m_cf_handle; }
225-
};
220+
[[nodiscard]] rocksdb::ColumnFamilyHandle &get_cf() const {
221+
return m_cf_handle;
222+
}
223+
};
226224

227225
} // namespace myrocks

0 commit comments

Comments
 (0)