Skip to content

Commit

Permalink
Apply naming conventions from Terrier (#25)
Browse files Browse the repository at this point in the history
* Run the clang-tidy naming convention formatter, but do not enforce the check. We will revisit this in Fall 2020.
  • Loading branch information
lmwnshn committed Sep 26, 2019
1 parent 17f3090 commit 5d2a9d5
Show file tree
Hide file tree
Showing 32 changed files with 470 additions and 458 deletions.
12 changes: 12 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ Checks: '
-modernize-avoid-c-arrays,
-readability-magic-numbers,
'
# TODO(WAN): To be enabled in Fall 2020.
#CheckOptions:
# - { key: readability-identifier-naming.ClassCase, value: CamelCase }
# - { key: readability-identifier-naming.EnumCase, value: CamelCase }
# - { key: readability-identifier-naming.FunctionCase, value: CamelCase }
# - { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE }
# - { key: readability-identifier-naming.MemberCase, value: lower_case }
# - { key: readability-identifier-naming.MemberSuffix, value: _ }
# - { key: readability-identifier-naming.NamespaceCase, value: lower_case }
# - { key: readability-identifier-naming.StructCase, value: CamelCase }
# - { key: readability-identifier-naming.UnionCase, value: CamelCase }
# - { key: readability-identifier-naming.VariableCase, value: lower_case }
WarningsAsErrors: '*'
HeaderFilterRegex: '/(src|test)/include'
AnalyzeTemporaryDtors: true
Expand Down
6 changes: 3 additions & 3 deletions src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

namespace bustub {

std::atomic<bool> ENABLE_LOGGING(false);
std::atomic<bool> enable_logging(false);

std::chrono::duration<int64_t> LOG_TIMEOUT = std::chrono::seconds(1);
std::chrono::duration<int64_t> log_timeout = std::chrono::seconds(1);

std::chrono::milliseconds CYCLE_DETECTION_INTERVAL = std::chrono::milliseconds(50);
std::chrono::milliseconds cycle_detection_interval = std::chrono::milliseconds(50);

} // namespace bustub
26 changes: 13 additions & 13 deletions src/common/util/string_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,31 @@ std::string StringUtil::Prefix(const std::string &str, const std::string &prefix

std::string StringUtil::FormatSize(uint64_t bytes) {
// http://ubuntuforums.org/showpost.php?p=10215516&postcount=5
double BASE = 1024;
double KB = BASE;
double MB = KB * BASE;
double GB = MB * BASE;
double base = 1024;
double kb = base;
double mb = kb * base;
double gb = mb * base;

std::ostringstream os;

if (bytes >= GB) {
os << std::fixed << std::setprecision(2) << (bytes / GB) << " GB";
} else if (bytes >= MB) {
os << std::fixed << std::setprecision(2) << (bytes / MB) << " MB";
} else if (bytes >= KB) {
os << std::fixed << std::setprecision(2) << (bytes / KB) << " KB";
if (bytes >= gb) {
os << std::fixed << std::setprecision(2) << (bytes / gb) << " GB";
} else if (bytes >= mb) {
os << std::fixed << std::setprecision(2) << (bytes / mb) << " MB";
} else if (bytes >= kb) {
os << std::fixed << std::setprecision(2) << (bytes / kb) << " KB";
} else {
os << std::to_string(bytes) + " bytes";
}
return (os.str());
}

std::string StringUtil::Bold(const std::string &str) {
std::string SET_PLAIN_TEXT = "\033[0;0m";
std::string SET_BOLD_TEXT = "\033[0;1m";
std::string set_plain_text = "\033[0;0m";
std::string set_bold_text = "\033[0;1m";

std::ostringstream os;
os << SET_BOLD_TEXT << str << SET_PLAIN_TEXT;
os << set_bold_text << str << set_plain_text;
return (os.str());
}

Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/lock_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ std::vector<std::pair<txn_id_t, txn_id_t>> LockManager::GetEdgeList() {
void LockManager::RunCycleDetection() {
BUSTUB_ASSERT(Detection(), "Detection should be enabled!");
while (enable_cycle_detection_) {
std::this_thread::sleep_for(CYCLE_DETECTION_INTERVAL);
std::this_thread::sleep_for(cycle_detection_interval);
{
std::unique_lock<std::mutex> l(latch_);
// TODO(student): remove the continue and add your cycle detection and abort code here
Expand Down
10 changes: 5 additions & 5 deletions src/concurrency/transaction_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace bustub {

std::unordered_map<txn_id_t, Transaction *> TransactionManager::txn_map_ = {};
std::unordered_map<txn_id_t, Transaction *> TransactionManager::txn_map = {};

Transaction *TransactionManager::Begin(Transaction *txn) {
// Acquire the global transaction latch in shared mode.
Expand All @@ -29,11 +29,11 @@ Transaction *TransactionManager::Begin(Transaction *txn) {
txn = new Transaction(next_txn_id_++);
}

if (ENABLE_LOGGING) {
if (enable_logging) {
// TODO(student): Add logging here.
}

txn_map_[txn->GetTransactionId()] = txn;
txn_map[txn->GetTransactionId()] = txn;
return txn;
}

Expand All @@ -53,7 +53,7 @@ void TransactionManager::Commit(Transaction *txn) {
}
write_set->clear();

if (ENABLE_LOGGING) {
if (enable_logging) {
// TODO(student): add logging here
}

Expand Down Expand Up @@ -83,7 +83,7 @@ void TransactionManager::Abort(Transaction *txn) {
}
write_set->clear();

if (ENABLE_LOGGING) {
if (enable_logging) {
// TODO(student): add logging here
}

Expand Down
6 changes: 3 additions & 3 deletions src/include/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
namespace bustub {

/** Cycle detection is performed every CYCLE_DETECTION_INTERVAL milliseconds. */
extern std::chrono::milliseconds CYCLE_DETECTION_INTERVAL;
extern std::chrono::milliseconds cycle_detection_interval;

/** True if logging should be enabled, false otherwise. */
extern std::atomic<bool> ENABLE_LOGGING;
extern std::atomic<bool> enable_logging;

/** If ENABLE_LOGGING is true, the log should be flushed to disk every LOG_TIMEOUT. */
extern std::chrono::duration<int64_t> LOG_TIMEOUT;
extern std::chrono::duration<int64_t> log_timeout;

static constexpr int INVALID_PAGE_ID = -1; // invalid page id
static constexpr int INVALID_TXN_ID = -1; // invalid transaction id
Expand Down
8 changes: 4 additions & 4 deletions src/include/common/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ enum class ExceptionType {

class Exception : public std::runtime_error {
public:
explicit Exception(const std::string &message) : std::runtime_error(message), type(ExceptionType::INVALID) {
explicit Exception(const std::string &message) : std::runtime_error(message), type_(ExceptionType::INVALID) {
std::string exception_message = "Message :: " + message + "\n";
std::cerr << exception_message;
}

Exception(ExceptionType exception_type, const std::string &message)
: std::runtime_error(message), type(exception_type) {
: std::runtime_error(message), type_(exception_type) {
std::string exception_message =
"\nException Type :: " + ExpectionTypeToString(type) + "\nMessage :: " + message + "\n";
"\nException Type :: " + ExpectionTypeToString(type_) + "\nMessage :: " + message + "\n";
std::cerr << exception_message;
}

Expand Down Expand Up @@ -87,7 +87,7 @@ class Exception : public std::runtime_error {
}

private:
ExceptionType type;
ExceptionType type_;
};

class NotImplementedException : public Exception {
Expand Down
58 changes: 29 additions & 29 deletions src/include/common/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ namespace bustub {
// https://blog.galowicz.de/2016/02/20/short_file_macro/
using cstr = const char *;

static constexpr cstr past_last_slash(cstr a, cstr b) {
return *a == '\0' ? b : *b == '/' ? past_last_slash(a + 1, a + 1) : past_last_slash(a + 1, b);
static constexpr cstr PastLastSlash(cstr a, cstr b) {
return *a == '\0' ? b : *b == '/' ? PastLastSlash(a + 1, a + 1) : PastLastSlash(a + 1, b);
}

static constexpr cstr past_last_slash(cstr a) { return past_last_slash(a, a); }
static constexpr cstr PastLastSlash(cstr a) { return PastLastSlash(a, a); }

#define __SHORT_FILE__ \
({ \
constexpr cstr sf__{past_last_slash(__FILE__)}; \
sf__; \
#define __SHORT_FILE__ \
({ \
constexpr cstr sf__{PastLastSlash(__FILE__)}; \
sf__; \
})

// Log levels.
Expand Down Expand Up @@ -86,7 +86,7 @@ static constexpr int LOG_LEVEL = LOG_LEVEL_INFO;
#define __FUNCTION__ ""
#endif

void outputLogHeader_(const char *file, int line, const char *func, int level);
void OutputLogHeader(const char *file, int line, const char *func, int level);

// Two convenient macros for debugging
// 1. Logging macros.
Expand All @@ -98,10 +98,10 @@ void outputLogHeader_(const char *file, int line, const char *func, int level);
#if LOG_LEVEL <= LOG_LEVEL_ERROR
#define LOG_ERROR_ENABLED
// #pragma message("LOG_ERROR was enabled.")
#define LOG_ERROR(...) \
outputLogHeader_(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_ERROR); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
#define LOG_ERROR(...) \
OutputLogHeader(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_ERROR); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
::fflush(stdout)
#else
#define LOG_ERROR(...) ((void)0)
Expand All @@ -113,10 +113,10 @@ void outputLogHeader_(const char *file, int line, const char *func, int level);
#if LOG_LEVEL <= LOG_LEVEL_WARN
#define LOG_WARN_ENABLED
// #pragma message("LOG_WARN was enabled.")
#define LOG_WARN(...) \
outputLogHeader_(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_WARN); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
#define LOG_WARN(...) \
OutputLogHeader(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_WARN); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
::fflush(stdout)
#else
#define LOG_WARN(...) ((void)0)
Expand All @@ -128,10 +128,10 @@ void outputLogHeader_(const char *file, int line, const char *func, int level);
#if LOG_LEVEL <= LOG_LEVEL_INFO
#define LOG_INFO_ENABLED
// #pragma message("LOG_INFO was enabled.")
#define LOG_INFO(...) \
outputLogHeader_(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_INFO); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
#define LOG_INFO(...) \
OutputLogHeader(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_INFO); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
::fflush(stdout)
#else
#define LOG_INFO(...) ((void)0)
Expand All @@ -143,10 +143,10 @@ void outputLogHeader_(const char *file, int line, const char *func, int level);
#if LOG_LEVEL <= LOG_LEVEL_DEBUG
#define LOG_DEBUG_ENABLED
// #pragma message("LOG_DEBUG was enabled.")
#define LOG_DEBUG(...) \
outputLogHeader_(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_DEBUG); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
#define LOG_DEBUG(...) \
OutputLogHeader(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_DEBUG); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
::fflush(stdout)
#else
#define LOG_DEBUG(...) ((void)0)
Expand All @@ -158,18 +158,18 @@ void outputLogHeader_(const char *file, int line, const char *func, int level);
#if LOG_LEVEL <= LOG_LEVEL_TRACE
#define LOG_TRACE_ENABLED
// #pragma message("LOG_TRACE was enabled.")
#define LOG_TRACE(...) \
outputLogHeader_(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_TRACE); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
#define LOG_TRACE(...) \
OutputLogHeader(__SHORT_FILE__, __LINE__, __FUNCTION__, LOG_LEVEL_TRACE); \
::fprintf(LOG_OUTPUT_STREAM, __VA_ARGS__); \
fprintf(LOG_OUTPUT_STREAM, "\n"); \
::fflush(stdout)
#else
#define LOG_TRACE(...) ((void)0)
#endif

// Output log message header in this format: [type] [file:line:function] time -
// ex: [ERROR] [somefile.cpp:123:doSome()] 2008/07/06 10:00:00 -
inline void outputLogHeader_(const char *file, int line, const char *func, int level) {
inline void OutputLogHeader(const char *file, int line, const char *func, int level) {
time_t t = ::time(nullptr);
tm *curTime = localtime(&t); // NOLINT
char time_str[32]; // FIXME
Expand Down
6 changes: 3 additions & 3 deletions src/include/common/rwlatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace bustub {
class ReaderWriterLatch {
using mutex_t = std::mutex;
using cond_t = std::condition_variable;
static const uint32_t max_readers_ = UINT_MAX;
static const uint32_t MAX_READERS = UINT_MAX;

public:
ReaderWriterLatch() = default;
Expand Down Expand Up @@ -62,7 +62,7 @@ class ReaderWriterLatch {
*/
void RLock() {
std::unique_lock<mutex_t> latch(mutex_);
while (writer_entered_ || reader_count_ == max_readers_) {
while (writer_entered_ || reader_count_ == MAX_READERS) {
reader_.wait(latch);
}
reader_count_++;
Expand All @@ -79,7 +79,7 @@ class ReaderWriterLatch {
writer_.notify_one();
}
} else {
if (reader_count_ == max_readers_ - 1) {
if (reader_count_ == MAX_READERS - 1) {
reader_.notify_one();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/include/concurrency/transaction_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ class TransactionManager {
*/

/** The transaction map is a global list of all the running transactions in the system. */
static std::unordered_map<txn_id_t, Transaction *> txn_map_;
static std::unordered_map<txn_id_t, Transaction *> txn_map;

/**
* Locates and returns the transaction with the given transaction ID.
* @param txn_id the id of the transaction to be found, it must exist!
* @return the transaction with the given transaction id
*/
static Transaction *GetTransaction(txn_id_t txn_id) {
assert(TransactionManager::txn_map_.find(txn_id) != TransactionManager::txn_map_.end());
auto *res = TransactionManager::txn_map_[txn_id];
assert(TransactionManager::txn_map.find(txn_id) != TransactionManager::txn_map.end());
auto *res = TransactionManager::txn_map[txn_id];
assert(res != nullptr);
return res;
}
Expand Down
18 changes: 9 additions & 9 deletions src/include/storage/index/generic_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class GenericKey {
public:
inline void SetFromKey(const Tuple &tuple) {
// intialize to 0
memset(data, 0, KeySize);
memcpy(data, tuple.GetData(), tuple.GetLength());
memset(data_, 0, KeySize);
memcpy(data_, tuple.GetData(), tuple.GetLength());
}

// NOTE: for test purpose only
inline void SetFromInteger(int64_t key) {
memset(data, 0, KeySize);
memcpy(data, &key, sizeof(int64_t));
memset(data_, 0, KeySize);
memcpy(data_, &key, sizeof(int64_t));
}

inline Value ToValue(Schema *schema, uint32_t column_idx) const {
Expand All @@ -47,17 +47,17 @@ class GenericKey {
const TypeId column_type = col.GetType();
const bool is_inlined = col.IsInlined();
if (is_inlined) {
data_ptr = (data + col.GetOffset());
data_ptr = (data_ + col.GetOffset());
} else {
int32_t offset = *reinterpret_cast<int32_t *>(const_cast<char *>(data + col.GetOffset()));
data_ptr = (data + offset);
int32_t offset = *reinterpret_cast<int32_t *>(const_cast<char *>(data_ + col.GetOffset()));
data_ptr = (data_ + offset);
}
return Value::DeserializeFrom(data_ptr, column_type);
}

// NOTE: for test purpose only
// interpret the first 8 bytes as int64_t from data vector
inline int64_t ToString() const { return *reinterpret_cast<int64_t *>(const_cast<char *>(data)); }
inline int64_t ToString() const { return *reinterpret_cast<int64_t *>(const_cast<char *>(data_)); }

// NOTE: for test purpose only
// interpret the first 8 bytes as int64_t from data vector
Expand All @@ -67,7 +67,7 @@ class GenericKey {
}

// actual location of data, extends past the end.
char data[KeySize];
char data_[KeySize];
};

/**
Expand Down
Loading

0 comments on commit 5d2a9d5

Please sign in to comment.