Skip to content

Commit

Permalink
Improve coding style for class Column_family_manager
Browse files Browse the repository at this point in the history
Summary:
Rename class Column_family_manager into Rdb_cf_manager
Make member variables to have prefix m_
Make get_ methods const

Reviewed By: jkedgar

Differential Revision: https://reviews.facebook.net/D55815

fbshipit-source-id: b484d764dc7
  • Loading branch information
Vasile Paraschiv authored and facebook-github-bot committed Dec 23, 2019
1 parent ea37c1a commit 8eb4bc2
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 84 deletions.
12 changes: 6 additions & 6 deletions storage/rocksdb/ha_rocksdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ rocksdb::ColumnFamilyOptions default_cf_opts;


Dict_manager dict_manager;
Column_family_manager cf_manager;
Rdb_cf_manager cf_manager;
Table_ddl_manager ddl_manager;
Binlog_info_manager binlog_manager;

Expand Down Expand Up @@ -158,7 +158,7 @@ static const char* const ERRSTR_ROLLBACK_ONLY
static void
rocksdb_flush_all_memtables()
{
Column_family_manager& cf_manager = rocksdb_get_cf_manager();
Rdb_cf_manager& cf_manager = rocksdb_get_cf_manager();
for (auto cf_handle : cf_manager.get_all_cf()) {
rdb->Flush(rocksdb::FlushOptions(), cf_handle);
}
Expand Down Expand Up @@ -2452,7 +2452,7 @@ void get_cf_options(const std::string &cf_name, rocksdb::ColumnFamilyOptions *op
rocksdb_cf_options_map.Get(cf_name, opts);

// Set the comparator according to 'rev:'
if (is_cf_name_reverse(cf_name.c_str()))
if (Rdb_cf_manager::is_cf_name_reverse(cf_name.c_str()))
opts->comparator= &rocksdb_rev_pk_comparator;
else
opts->comparator= &rocksdb_pk_comparator;
Expand Down Expand Up @@ -4011,7 +4011,7 @@ int ha_rocksdb::create_key_defs(TABLE *table_arg, const char *db_table,
goto error;

cf_handles[i]= cf_handle;
is_cf_reverse[i]= is_cf_name_reverse(comment);
is_cf_reverse[i]= Rdb_cf_manager::is_cf_name_reverse(comment);
is_auto_cf[i]= is_auto_cf_flag;
}

Expand Down Expand Up @@ -7557,7 +7557,7 @@ int ha_rocksdb::analyze(THD* thd, HA_CHECK_OPT* check_opt)
}

// for analyze statements, force flush on memtable to get accurate cardinality
Column_family_manager& cf_manager = rocksdb_get_cf_manager();
Rdb_cf_manager& cf_manager = rocksdb_get_cf_manager();
if (thd != nullptr && THDVAR(thd, flush_memtable_on_analyze) &&
!rocksdb_pause_background_work)
{
Expand Down Expand Up @@ -8166,7 +8166,7 @@ rocksdb::DB *rocksdb_get_rdb()
return rdb;
}

Column_family_manager& rocksdb_get_cf_manager()
Rdb_cf_manager& rocksdb_get_cf_manager()
{
return cf_manager;
}
Expand Down
2 changes: 1 addition & 1 deletion storage/rocksdb/ha_rocksdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const char * const DEFAULT_SYSTEM_CF_NAME= "__system__";

/*
Column family name which means "put this index into its own column family".
See get_per_index_cf_name.
See Rdb_cf_manager::get_per_index_cf_name().
*/
const char * const PER_INDEX_CF_NAME = "$per_index_cf";

Expand Down
17 changes: 11 additions & 6 deletions storage/rocksdb/ha_rocksdb_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@

namespace myrocks {

class Column_family_manager;

rocksdb::DB *rocksdb_get_rdb();
Column_family_manager& rocksdb_get_cf_manager();
rocksdb::BlockBasedTableOptions& rocksdb_get_table_options();

void get_cf_options(const std::string &cf_name,
rocksdb::ColumnFamilyOptions *opts) MY_ATTRIBUTE((__nonnull__));
int rocksdb_normalize_tablename(const char *tablename,
Expand All @@ -44,6 +38,17 @@ int rocksdb_get_share_perf_counters(const char *tablename,

void request_save_stats();

/*
Access to singleton objects.
*/

rocksdb::DB *rocksdb_get_rdb();

class Rdb_cf_manager;
Rdb_cf_manager& rocksdb_get_cf_manager();

rocksdb::BlockBasedTableOptions& rocksdb_get_table_options();

class Dict_manager;
Dict_manager *get_dict_manager(void)
MY_ATTRIBUTE((__warn_unused_result__));
Expand Down
89 changes: 46 additions & 43 deletions storage/rocksdb/rdb_cf_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
#include "./rdb_cf_manager.h"

#include "./ha_rocksdb.h"
#include "./ha_rocksdb_proto.h"

namespace myrocks {

/* Check if ColumnFamily name says it's a reverse-ordered CF */
bool is_cf_name_reverse(const char *name)
bool Rdb_cf_manager::is_cf_name_reverse(const char *name)
{
/* nullptr means the default CF is used.. (TODO: can the default CF be
* reverse?) */
Expand All @@ -39,38 +40,39 @@ bool is_cf_name_reverse(const char *name)
static PSI_mutex_key ex_key_cfm;
#endif

void Column_family_manager::init(std::vector<rocksdb::ColumnFamilyHandle*> *handles)
void Rdb_cf_manager::init(std::vector<rocksdb::ColumnFamilyHandle*> *handles)
{
mysql_mutex_init(ex_key_cfm, &cfm_mutex, MY_MUTEX_INIT_FAST);
mysql_mutex_init(ex_key_cfm, &m_mutex, MY_MUTEX_INIT_FAST);

DBUG_ASSERT(handles != nullptr);
DBUG_ASSERT(handles->size() > 0);

for (auto cfh : *handles) {
DBUG_ASSERT(cfh != nullptr);
cf_name_map[cfh->GetName()] = cfh;
cf_id_map[cfh->GetID()] = cfh;
m_cf_name_map[cfh->GetName()] = cfh;
m_cf_id_map[cfh->GetID()] = cfh;
}
}


void Column_family_manager::cleanup()
void Rdb_cf_manager::cleanup()
{
for (auto it : cf_name_map) {
for (auto it : m_cf_name_map) {
delete it.second;
}
mysql_mutex_destroy(&cfm_mutex);
mysql_mutex_destroy(&m_mutex);
}


/*
/**
Generate Column Family name for per-index column families
@param res OUT Column Family name
*/

void get_per_index_cf_name(const char *db_table_name, const char *index_name,
std::string *res)
void Rdb_cf_manager::get_per_index_cf_name(const char *db_table_name,
const char *index_name,
std::string *res)
{
DBUG_ASSERT(db_table_name != nullptr);
DBUG_ASSERT(index_name != nullptr);
Expand All @@ -87,20 +89,21 @@ void get_per_index_cf_name(const char *db_table_name, const char *index_name,
Find column family by name. If it doesn't exist, create it
@detail
See Column_family_manager::get_cf
See Rdb_cf_manager::get_cf
*/
rocksdb::ColumnFamilyHandle*
Column_family_manager::get_or_create_cf(rocksdb::DB *rdb, const char *cf_name,
const char *db_table_name,
const char *index_name,
bool *is_automatic)
Rdb_cf_manager::get_or_create_cf(rocksdb::DB *rdb,
const char *cf_name,
const char *db_table_name,
const char *index_name,
bool *is_automatic)
{
DBUG_ASSERT(rdb != nullptr);
DBUG_ASSERT(is_automatic != nullptr);

rocksdb::ColumnFamilyHandle* cf_handle;

mysql_mutex_lock(&cfm_mutex);
mysql_mutex_lock(&m_mutex);
*is_automatic= false;
if (cf_name == nullptr)
cf_name= DEFAULT_CF_NAME;
Expand All @@ -113,8 +116,8 @@ Column_family_manager::get_or_create_cf(rocksdb::DB *rdb, const char *cf_name,
*is_automatic= true;
}

auto it = cf_name_map.find(cf_name);
if (it != cf_name_map.end())
auto it = m_cf_name_map.find(cf_name);
if (it != m_cf_name_map.end())
cf_handle= it->second;
else
{
Expand All @@ -130,13 +133,13 @@ Column_family_manager::get_or_create_cf(rocksdb::DB *rdb, const char *cf_name,

rocksdb::Status s= rdb->CreateColumnFamily(opts, cf_name_str, &cf_handle);
if (s.ok()) {
cf_name_map[cf_handle->GetName()] = cf_handle;
cf_id_map[cf_handle->GetID()] = cf_handle;
m_cf_name_map[cf_handle->GetName()] = cf_handle;
m_cf_id_map[cf_handle->GetID()] = cf_handle;
} else {
cf_handle= nullptr;
}
}
mysql_mutex_unlock(&cfm_mutex);
mysql_mutex_unlock(&m_mutex);

return cf_handle;
}
Expand All @@ -155,18 +158,18 @@ Column_family_manager::get_or_create_cf(rocksdb::DB *rdb, const char *cf_name,
*/

rocksdb::ColumnFamilyHandle*
Column_family_manager::get_cf(const char *cf_name,
const char *db_table_name,
const char *index_name,
bool *is_automatic)
Rdb_cf_manager::get_cf(const char *cf_name,
const char *db_table_name,
const char *index_name,
bool *is_automatic) const
{
DBUG_ASSERT(cf_name != nullptr);
DBUG_ASSERT(is_automatic != nullptr);

rocksdb::ColumnFamilyHandle* cf_handle;

*is_automatic= false;
mysql_mutex_lock(&cfm_mutex);
mysql_mutex_lock(&m_mutex);
if (cf_name == nullptr)
cf_name= DEFAULT_CF_NAME;

Expand All @@ -178,50 +181,50 @@ Column_family_manager::get_cf(const char *cf_name,
*is_automatic= true;
}

auto it = cf_name_map.find(cf_name);
cf_handle = (it != cf_name_map.end()) ? it->second : nullptr;
auto it = m_cf_name_map.find(cf_name);
cf_handle = (it != m_cf_name_map.end()) ? it->second : nullptr;

mysql_mutex_unlock(&cfm_mutex);
mysql_mutex_unlock(&m_mutex);

return cf_handle;
}

rocksdb::ColumnFamilyHandle* Column_family_manager::get_cf(const uint32_t id)
rocksdb::ColumnFamilyHandle* Rdb_cf_manager::get_cf(const uint32_t id) const
{
rocksdb::ColumnFamilyHandle* cf_handle = nullptr;

mysql_mutex_lock(&cfm_mutex);
auto it = cf_id_map.find(id);
if (it != cf_id_map.end())
mysql_mutex_lock(&m_mutex);
auto it = m_cf_id_map.find(id);
if (it != m_cf_id_map.end())
cf_handle = it->second;
mysql_mutex_unlock(&cfm_mutex);
mysql_mutex_unlock(&m_mutex);

return cf_handle;
}

std::vector<std::string>
Column_family_manager::get_cf_names(void)
Rdb_cf_manager::get_cf_names(void) const
{
std::vector<std::string> names;

mysql_mutex_lock(&cfm_mutex);
for (auto it : cf_name_map) {
mysql_mutex_lock(&m_mutex);
for (auto it : m_cf_name_map) {
names.push_back(it.first);
}
mysql_mutex_unlock(&cfm_mutex);
mysql_mutex_unlock(&m_mutex);
return names;
}

std::vector<rocksdb::ColumnFamilyHandle*>
Column_family_manager::get_all_cf(void)
Rdb_cf_manager::get_all_cf(void) const
{
std::vector<rocksdb::ColumnFamilyHandle*> list;

mysql_mutex_lock(&cfm_mutex);
for (auto it : cf_id_map) {
mysql_mutex_lock(&m_mutex);
for (auto it : m_cf_id_map) {
list.push_back(it.second);
}
mysql_mutex_unlock(&cfm_mutex);
mysql_mutex_unlock(&m_mutex);

return list;
}
Expand Down
35 changes: 18 additions & 17 deletions storage/rocksdb/rdb_cf_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/* C++ system header files */
#include <map>
#include <string>
#include <vector>

/* MySQL header files */
#include "./sql_class.h"
Expand All @@ -28,15 +30,7 @@
namespace myrocks {

/*
Expected from outside: a function that fills CF options for a given name.
*/
void get_cf_options(const std::string &cf_name, rocksdb::ColumnFamilyOptions *opts);

void get_per_index_cf_name(const char *db_table_name, const char *index_name,
std::string *res);

/*
We need a column family manager. Its functions:
We need a Column Family (CF) manager. Its functions:
- create column families (synchronized, don't create the same twice)
- keep count in each column family.
= the count is kept on-disk.
Expand All @@ -49,13 +43,20 @@ void get_per_index_cf_name(const char *db_table_name, const char *index_name,
- CFs are created in a synchronized way. We can't remove them, yet.
*/

class Column_family_manager
class Rdb_cf_manager
{
std::map<std::string, rocksdb::ColumnFamilyHandle*> cf_name_map;
std::map<uint32_t, rocksdb::ColumnFamilyHandle*> cf_id_map;
std::map<std::string, rocksdb::ColumnFamilyHandle*> m_cf_name_map;
std::map<uint32_t, rocksdb::ColumnFamilyHandle*> m_cf_id_map;

mutable mysql_mutex_t m_mutex;

static
void get_per_index_cf_name(const char *db_table_name, const char *index_name,
std::string *res);

mysql_mutex_t cfm_mutex;
public:
static bool is_cf_name_reverse(const char *name);

/*
This is called right after the DB::Open() call. The parameters describe column
families that are present in the database. The first CF is the default CF.
Expand All @@ -78,16 +79,16 @@ class Column_family_manager
rocksdb::ColumnFamilyHandle* get_cf(const char *cf_name,
const char *db_table_name,
const char *index_name,
bool *is_automatic);
bool *is_automatic) const;

/* Look up cf by id; used by datadic */
rocksdb::ColumnFamilyHandle* get_cf(const uint32_t id);
rocksdb::ColumnFamilyHandle* get_cf(const uint32_t id) const;

/* Used to iterate over column families for show status */
std::vector<std::string> get_cf_names(void);
std::vector<std::string> get_cf_names(void) const;

/* Used to iterate over column families */
std::vector<rocksdb::ColumnFamilyHandle*> get_all_cf(void);
std::vector<rocksdb::ColumnFamilyHandle*> get_all_cf(void) const;

// void drop_cf(); -- not implemented so far.
};
Expand Down
2 changes: 0 additions & 2 deletions storage/rocksdb/rdb_cf_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ namespace rocksdb {

namespace myrocks {

bool is_cf_name_reverse(const char *name);

/*
Per-column family options configs.
Expand Down
Loading

0 comments on commit 8eb4bc2

Please sign in to comment.