@@ -55,7 +55,8 @@ void key_copy(uchar *to_key, uchar *from_record, KEY *key_info,
55
55
void * background_thread (void *);
56
56
bool can_use_bloom_filter (RDBSE_KEYDEF *keydef,
57
57
const rocksdb::Slice &eq_cond,
58
- const bool use_all_keys);
58
+ const bool use_all_keys,
59
+ bool is_ascending);
59
60
static int rocksdb_normalize_tablename (const char *tablename,
60
61
StringBuffer<256 > *strbuf);
61
62
@@ -2314,7 +2315,7 @@ int ha_rocksdb::index_read_map(uchar *buf, const uchar *key,
2314
2315
This will open the iterator and position it at a record that's equal or
2315
2316
greater than the lookup tuple.
2316
2317
*/
2317
- setup_index_scan (kd, &slice, use_all_keys);
2318
+ setup_index_scan (kd, &slice, use_all_keys, is_ascending (kd, find_flag) );
2318
2319
bool move_forward= true ;
2319
2320
2320
2321
@@ -3115,7 +3116,8 @@ int ha_rocksdb::write_row(uchar *buf)
3115
3116
3116
3117
void ha_rocksdb::setup_index_scan (RDBSE_KEYDEF *keydef,
3117
3118
rocksdb::Slice *slice,
3118
- bool use_all_keys)
3119
+ bool use_all_keys,
3120
+ bool is_ascending)
3119
3121
{
3120
3122
Rdb_transaction *trx= get_or_create_trx (table->in_use );
3121
3123
@@ -3128,7 +3130,7 @@ void ha_rocksdb::setup_index_scan(RDBSE_KEYDEF *keydef,
3128
3130
rocksdb::ReadOptions options;
3129
3131
if (!lock_rows)
3130
3132
options.snapshot = trx->snapshot ;
3131
- if (!can_use_bloom_filter (keydef, *slice, use_all_keys))
3133
+ if (!can_use_bloom_filter (keydef, *slice, use_all_keys, is_ascending ))
3132
3134
options.total_order_seek = true ;
3133
3135
rocksdb::Iterator* rocksdb_it= rdb->NewIterator (options, keydef->get_cf ());
3134
3136
scan_it= new Apply_changes_iter;
@@ -3910,6 +3912,54 @@ void ha_rocksdb::get_auto_increment(ulonglong offset, ulonglong increment,
3910
3912
mysql_mutex_unlock (&tbl_def->mutex );
3911
3913
}
3912
3914
3915
+ /* *
3916
+ Checking if an index is used for ascending scan or not
3917
+
3918
+ @detail
3919
+ Currently RocksDB does not support bloom filter for
3920
+ prefix lookup + descending scan, but supports bloom filter for
3921
+ prefix lookup + ascending scan. This function returns true if
3922
+ the scan pattern is absolutely ascending.
3923
+ @param keydef
3924
+ @param find_flag
3925
+ */
3926
+ bool ha_rocksdb::is_ascending (RDBSE_KEYDEF *keydef, enum ha_rkey_function find_flag)
3927
+ {
3928
+ bool is_ascending= false ;
3929
+ switch (find_flag) {
3930
+ case HA_READ_KEY_EXACT:
3931
+ case HA_READ_PREFIX:
3932
+ {
3933
+ is_ascending= true ;
3934
+ break ;
3935
+ }
3936
+ case HA_READ_KEY_OR_NEXT:
3937
+ case HA_READ_AFTER_KEY:
3938
+ {
3939
+ if (keydef->is_reverse_cf )
3940
+ is_ascending= false ;
3941
+ else
3942
+ is_ascending= true ;
3943
+ break ;
3944
+ }
3945
+ case HA_READ_KEY_OR_PREV:
3946
+ case HA_READ_BEFORE_KEY:
3947
+ case HA_READ_PREFIX_LAST:
3948
+ case HA_READ_PREFIX_LAST_OR_PREV:
3949
+ {
3950
+ if (keydef->is_reverse_cf )
3951
+ is_ascending= true ;
3952
+ else
3953
+ is_ascending= false ;
3954
+ break ;
3955
+ }
3956
+ default :
3957
+ is_ascending= false ;
3958
+ }
3959
+ return is_ascending;
3960
+ }
3961
+
3962
+
3913
3963
#define SHOW_FNAME (name ) rocksdb_show_##name
3914
3964
3915
3965
#define DEF_SHOW_FUNC (name, key ) \
@@ -4186,28 +4236,36 @@ void* background_thread(void*)
4186
4236
*/
4187
4237
bool can_use_bloom_filter (RDBSE_KEYDEF *keydef,
4188
4238
const rocksdb::Slice &eq_cond,
4189
- const bool use_all_keys)
4239
+ const bool use_all_keys,
4240
+ bool is_ascending)
4190
4241
{
4191
4242
bool can_use= false ;
4192
4243
rocksdb::Options opt = rdb->GetOptions (keydef->get_cf ());
4193
4244
if (opt.prefix_extractor )
4194
4245
{
4195
4246
/*
4196
4247
This is an optimized use case for CappedPrefixTransform.
4197
- If eq_cond length >= prefix extractor length, it is
4198
- always possible to use bloom filter. Keys longer than the
4199
- capped prefix length will be truncated down to the capped length
4200
- and the resulting key is added to the bloom filter.
4248
+ If eq_cond length >= prefix extractor length and if
4249
+ all keys are used for equal lookup, it is
4250
+ always possible to use bloom filter.
4251
+
4252
+ Prefix bloom filter can't be used on descending scan with
4253
+ prefix lookup (i.e. WHERE id1=1 ORDER BY id2 DESC), because of
4254
+ RocksDB's limitation. On ascending (or not sorting) scan,
4255
+ keys longer than the capped prefix length will be truncated down
4256
+ to the capped length and the resulting key is added to the bloom filter.
4201
4257
4202
4258
Keys shorter than the capped prefix length will be added to
4203
4259
the bloom filter. When keys are looked up, key conditionals
4204
4260
longer than the capped length can be used; key conditionals
4205
4261
shorter require all parts of the key to be available
4206
4262
for the short key match.
4207
4263
*/
4208
- if (opt.prefix_extractor ->SameResultWhenAppended (eq_cond))
4264
+ if (use_all_keys && opt.prefix_extractor ->InRange (eq_cond))
4209
4265
can_use= true ;
4210
- else if (use_all_keys && opt.prefix_extractor ->InRange (eq_cond))
4266
+ else if (!is_ascending)
4267
+ can_use= false ;
4268
+ else if (opt.prefix_extractor ->SameResultWhenAppended (eq_cond))
4211
4269
can_use= true ;
4212
4270
else
4213
4271
can_use= false ;
0 commit comments