Skip to content

Commit

Permalink
Issue #25: Make ORDER BY DESC faster
Browse files Browse the repository at this point in the history
fbshipit-source-id: 1151e93
  • Loading branch information
spetrunia authored and facebook-github-bot committed Dec 23, 2019
1 parent 5fc978a commit d363883
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 142 deletions.
251 changes: 196 additions & 55 deletions storage/rocksdb/ha_rocksdb.cc

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions storage/rocksdb/ha_rocksdb.h
Expand Up @@ -169,8 +169,7 @@ class ha_rocksdb: public handler

int create_key_defs(TABLE *table_arg, const char *db_table, uint len);
int secondary_index_read(int keyno, uchar *buf);
void setup_index_scan(rocksdb::ColumnFamilyHandle *cf,
rocksdb::Slice *slice);
void setup_index_scan(RDBSE_KEYDEF *keydef, rocksdb::Slice *slice);
int get_row_by_rowid(uchar *buf, const char *pk_tuple, uint pk_tuple_size);

void update_auto_incr_val();
Expand Down Expand Up @@ -316,6 +315,10 @@ class ha_rocksdb: public handler

int index_first(uchar *buf);
int index_last(uchar *buf);
private:
int index_first_intern(uchar *buf);
int index_last_intern(uchar *buf);
public:
int index_end();

void unlock_row();
Expand Down
9 changes: 7 additions & 2 deletions storage/rocksdb/rdb_applyiter.cc
Expand Up @@ -41,11 +41,13 @@ Apply_changes_iter::~Apply_changes_iter()
}


void Apply_changes_iter::init(Row_table *trx_arg, rocksdb::Iterator *rdb_arg)
void Apply_changes_iter::init(bool is_reverse_arg, Row_table *trx_arg,
rocksdb::Iterator *rdb_arg)
{
delete trx;
delete rdb;
trx= new Row_table_iter(trx_arg);
is_reverse= is_reverse_arg;
trx= new Row_table_iter(trx_arg, is_reverse);
rdb= rdb_arg;
valid= false;
}
Expand Down Expand Up @@ -136,6 +138,9 @@ void Apply_changes_iter::advance(int direction)
int cmp= direction *
compare_mem_comparable_keys((const uchar*)trx_key.data(), trx_key.size(),
(const uchar*)rdb_key.data(), rdb_key.size());
if (is_reverse)
cmp *= -1;

if (!cmp) // keys are equal
{
if (trx->is_tombstone())
Expand Down
8 changes: 7 additions & 1 deletion storage/rocksdb/rdb_applyiter.h
Expand Up @@ -17,6 +17,8 @@
class Row_table;
class Row_table_iter;

class RDBSE_KEYDEF;

/*
A class that looks like RocksDB's iterator, but internally it takes into
account the changes made by the transaction.
Expand All @@ -33,10 +35,14 @@ class Apply_changes_iter
/* These are the iterators we're merging. We own them, so should free them */
Row_table_iter *trx;
rocksdb::Iterator* rdb;

/* If true, we're scanning reverse-ordered data */
bool is_reverse;
public:
Apply_changes_iter();
~Apply_changes_iter();
void init(Row_table *trx_arg, rocksdb::Iterator *rdb_arg);
void init(bool is_reverse_arg, Row_table *trx_arg,
rocksdb::Iterator *rdb_arg);

void Next();
void Prev();
Expand Down
12 changes: 12 additions & 0 deletions storage/rocksdb/rdb_cf_manager.cc
Expand Up @@ -30,6 +30,18 @@
#endif
#include <inttypes.h>


/* Check if ColumnFamily name says it's a reverse-ordered CF */
bool is_cf_name_reverse(const char *name)
{
/* NULL means the default CF is used.. (TODO: can the default CF be reverse?) */
if (name && !strncmp(name, "rev:", 4))
return true;
else
return false;
}


void Column_family_manager::init(std::vector<std::string> *names,
std::vector<rocksdb::ColumnFamilyHandle*> *handles)
{
Expand Down
2 changes: 2 additions & 0 deletions storage/rocksdb/rdb_cf_options.h
Expand Up @@ -21,6 +21,8 @@ namespace rocksdb {
class ColumnFamilyOptions;
}

bool is_cf_name_reverse(const char *name);

/*
Per-column family options configs.
Expand Down
2 changes: 1 addition & 1 deletion storage/rocksdb/rdb_datadic.cc
Expand Up @@ -962,7 +962,7 @@ bool Table_ddl_manager::init(rocksdb::DB *rdb_dict)
look at Field* objects and set max_length and other attributes
*/
tdef->key_descr[keyno]= new RDBSE_KEYDEF(index_number, keyno, NULL,
false);
false, false);

/* Keep track of what was the last index number we saw */
if (max_number < index_number)
Expand Down
10 changes: 9 additions & 1 deletion storage/rocksdb/rdb_datadic.h
Expand Up @@ -199,9 +199,11 @@ class RDBSE_KEYDEF
}

RDBSE_KEYDEF(uint indexnr_arg, uint keyno_arg,
rocksdb::ColumnFamilyHandle* cf_handle_arg, bool is_auto_cf_arg) :
rocksdb::ColumnFamilyHandle* cf_handle_arg,
bool is_reverse_cf_arg, bool is_auto_cf_arg) :
index_number(indexnr_arg),
cf_handle(cf_handle_arg),
is_reverse_cf(is_reverse_cf_arg),
is_auto_cf(is_auto_cf_arg),
pk_part_no(NULL),
pack_info(NULL),
Expand All @@ -219,9 +221,11 @@ class RDBSE_KEYDEF

void setup(TABLE *table);
void set_cf_handle(rocksdb::ColumnFamilyHandle* cf_handle_arg,
bool is_reverse_cf_arg,
bool is_auto_cf_arg)
{
cf_handle= cf_handle_arg;
is_reverse_cf= is_reverse_cf_arg;
is_auto_cf= is_auto_cf_arg;
}

Expand All @@ -235,7 +239,11 @@ class RDBSE_KEYDEF
uchar index_number_storage_form[INDEX_NUMBER_SIZE];

rocksdb::ColumnFamilyHandle* cf_handle;

public:
/* If true, the column family stores data in the reverse order */
bool is_reverse_cf;

bool is_auto_cf;
private:

Expand Down
33 changes: 18 additions & 15 deletions storage/rocksdb/rdb_debug.cc
Expand Up @@ -83,23 +83,26 @@ void dump_value(FILE *out, const rocksdb::Slice &val)

void dump_trx_changes(FILE *out, Row_table &changes)
{
Row_table_iter iter(&changes);
fprintf(out, "TRX %p\n", current_thd);
for (iter.SeekToFirst(); iter.Valid(); iter.Next())
for (int reverse= 0; reverse <= 1; reverse++)
Row_table_iter iter(&changes, (bool)reverse);
{
if (iter.is_tombstone())
fprintf(out, "TRX %p\n", current_thd);
for (iter.SeekToFirst(); iter.Valid(); iter.Next())
{
fprintf(out, "DEL ");
dump_value(out, iter.key());
fputs("\n", out);
}
else
{
fprintf(out, "PUT ");
dump_value(out, iter.key());
fputs(" ", out);
dump_value(out, iter.value());
fputs("\n", out);
if (iter.is_tombstone())
{
fprintf(out, "DEL ");
dump_value(out, iter.key());
fputs("\n", out);
}
else
{
fprintf(out, "PUT ");
dump_value(out, iter.key());
fputs(" ", out);
dump_value(out, iter.value());
fputs("\n", out);
}
}
}
}
Expand Down

0 comments on commit d363883

Please sign in to comment.