Skip to content

Commit

Permalink
Format all C++ files
Browse files Browse the repository at this point in the history
  • Loading branch information
lnikon committed Dec 8, 2024
1 parent 6dc1777 commit 928ff53
Show file tree
Hide file tree
Showing 45 changed files with 993 additions and 1,036 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BasedOnStyle: LLVM
ColumnLimit: 80
ColumnLimit: 120
IndentWidth: 4
TabWidth: 4
UseTab: Never
Expand Down
8 changes: 4 additions & 4 deletions bench/fs/lots_of_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
void fstream_test()
{
const std::string filename("test_stream_3.txt");
std::fstream fs(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
std::fstream fs(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
if (!fs.is_open())
{
std::cerr << "unable to open" << filename << '\n';
exit(EXIT_FAILURE);
}

size_t count{9 * 1024};
size_t count{9 * 1024};
std::string payload("aaa");
while (count-- != 0)
{
Expand All @@ -32,14 +32,14 @@ void fstream_test()
void posix_write_test()
{
const std::string filename("test_stream_2.txt");
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd == -1)
{
std::cerr << "Unable to open " << filename << '\n';
}

std::string payload("bbbbb");
size_t count{9 * 1024};
size_t count{9 * 1024};
while (count-- != 0)
{
write(fd, payload.c_str(), payload.size());
Expand Down
24 changes: 12 additions & 12 deletions bench/fs/open_write_streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
static void BM_BenchmarkFstreamWrite(benchmark::State &state)
{
const std::string filename("test_stream.txt");
std::fstream fs(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
std::fstream fs(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
if (!fs.is_open())
{
std::cerr << "unable to open" << filename << '\n';
Expand All @@ -33,7 +33,7 @@ BENCHMARK(BM_BenchmarkFstreamWrite);
static void BM_BenchmarkFstreamWriteWithFlush(benchmark::State &state)
{
const std::string filename("test_stream.txt");
std::fstream fs(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
std::fstream fs(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
if (!fs.is_open())
{
std::cerr << "unable to open" << filename << '\n';
Expand All @@ -54,7 +54,7 @@ BENCHMARK(BM_BenchmarkFstreamWriteWithFlush);
static void BM_BenchmarkFstreamWriteWithSync(benchmark::State &state)
{
const std::string filename("test_stream.txt");
std::fstream fs(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
std::fstream fs(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
if (!fs.is_open())
{
std::cerr << "unable to open" << filename << '\n';
Expand All @@ -75,7 +75,7 @@ BENCHMARK(BM_BenchmarkFstreamWriteWithSync);
static void BM_BenchmarkFstreamWriteNoBuffering(benchmark::State &state)
{
const std::string filename("test_stream.txt");
std::fstream fs;
std::fstream fs;
fs.rdbuf()->pubsetbuf(nullptr, 0);

fs.open(filename, std::fstream::in | std::fstream::out | std::fstream::app | std::fstream::ate);
Expand All @@ -98,7 +98,7 @@ BENCHMARK(BM_BenchmarkFstreamWriteNoBuffering);
static void BM_BenchmarkPosixWrite(benchmark::State &state)
{
const std::string filename("test_stream_2.txt");
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd == -1)
{
std::cerr << "Unable to open " << filename << '\n';
Expand All @@ -117,7 +117,7 @@ BENCHMARK(BM_BenchmarkPosixWrite);
static void BM_BenchmarkPosixWriteODirect(benchmark::State &state)
{
const std::string filename("test_stream_2.txt");
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT | O_DIRECT, 0644);
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT | O_DIRECT, 0644);
if (fd == -1)
{
std::cerr << "Unable to open " << filename << '\n';
Expand All @@ -136,14 +136,14 @@ BENCHMARK(BM_BenchmarkPosixWriteODirect);
static void BM_BenchmarkPosixScatterWrite(benchmark::State &state)
{
const std::string filename("test_stream_2.txt");
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd == -1)
{
std::cerr << "Unable to open " << filename << '\n';
}

std::vector<iovec> iov;
std::string payload("bbbbb");
std::string payload("bbbbb");
for (auto _ : state)
{
iov.emplace_back(iovec{.iov_base = payload.data(), .iov_len = payload.size()});
Expand All @@ -157,14 +157,14 @@ BENCHMARK(BM_BenchmarkPosixScatterWrite);
static void BM_BenchmarkPosixScatterWriteWithODirect(benchmark::State &state)
{
const std::string filename("test_stream_2.txt");
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT | O_DIRECT, 0644);
int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT | O_DIRECT, 0644);
if (fd == -1)
{
std::cerr << "Unable to open " << filename << '\n';
}

std::vector<iovec> iov;
std::string payload("bbbbb");
std::string payload("bbbbb");
for (auto _ : state)
{
iov.emplace_back(iovec{.iov_base = payload.data(), .iov_len = payload.size()});
Expand All @@ -178,7 +178,7 @@ BENCHMARK(BM_BenchmarkPosixScatterWriteWithODirect);
static void BM_BenchmarkFWrite(benchmark::State &state)
{
const std::string filename("test_stream_3.txt");
FILE *file = fopen(filename.c_str(), "w");
FILE *file = fopen(filename.c_str(), "w");
if (file == nullptr)
{
std::cerr << "Unable to open " << filename << '\n';
Expand All @@ -197,7 +197,7 @@ BENCHMARK(BM_BenchmarkFWrite);
static void BM_BenchmarkFWriteNoBuffering(benchmark::State &state)
{
const std::string filename("test_stream_3.txt");
FILE *file = fopen(filename.c_str(), "w");
FILE *file = fopen(filename.c_str(), "w");
if (file == nullptr)
{
std::cerr << "Unable to open " << filename << '\n';
Expand Down
6 changes: 4 additions & 2 deletions examples/absl/concurrency/thread_safe_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ template <typename TItem> class thread_safe_queue_t
{
/*absl::WriterMutexLock lock(&m_mutex);*/
// if (!m_mutex.AwaitWithTimeout(
// absl::Condition(+[](queue_t *queue) { return !queue->empty(); }, &m_queue), absl::Seconds(1)))
// absl::Condition(+[](queue_t *queue) { return !queue->empty();
// }, &m_queue), absl::Seconds(1)))
//{
// return std::nullopt;
// }

/*spdlog::debug("Popping item from the queue. size={}", m_queue.size());*/
/*spdlog::debug("Popping item from the queue. size={}",
* m_queue.size());*/

std::unique_lock<std::mutex> lk(smut);
cv.wait_for(lk, std::chrono::seconds(1), [this] { return !m_queue.empty(); });
Expand Down
3 changes: 2 additions & 1 deletion lib/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace config
{

/**
* @brief Name of directory inside the database root dir where segments should be stored
* @brief Name of directory inside the database root dir where segments should
* be stored
*/
constexpr const std::string_view SegmentsDirectoryName{"segments"};

Expand Down
2 changes: 1 addition & 1 deletion lib/db/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ auto db_t::get(const structures::lsmtree::key_t &key) -> std::optional<structure
return m_lsmTree.get(key);
}

auto db_t::config() const noexcept -> config::shared_ptr_t
auto db_t::config() const noexcept -> config::shared_ptr_t
{
return m_config;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/db/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class db_t
*/
auto get(const structures::lsmtree::key_t &key) -> std::optional<structures::memtable::memtable_t::record_t>;

auto config() const noexcept -> config::shared_ptr_t ;
auto config() const noexcept -> config::shared_ptr_t;

private:
auto prepare_directory_structure() -> bool;
Expand All @@ -58,7 +58,7 @@ class db_t

using shared_ptr_t = std::shared_ptr<db_t>;

template <typename... Args> auto make_shared(Args&&... args)
template <typename... Args> auto make_shared(Args &&...args)
{
return std::make_shared<db_t>(std::forward<Args>(args)...);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/db/db_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
TEST_CASE("db interface validation", "[db]")
{
config::shared_ptr_t pConfig{config::make_shared()};
auto pSegmentStorage{structures::lsmtree::segments::storage::make_shared()};
auto manifest{db::manifest::make_shared(pConfig)};
auto wal{db::wal::make_shared("wal")};
auto lsmTree{structures::lsmtree::lsmtree_t{pConfig, manifest, wal}};
auto pSegmentStorage{structures::lsmtree::segments::storage::make_shared()};
auto manifest{db::manifest::make_shared(pConfig)};
auto wal{db::wal::make_shared("wal")};
auto lsmTree{structures::lsmtree::lsmtree_t{pConfig, manifest, wal}};

SECTION("fail when db path is empty")
{
Expand Down
3 changes: 2 additions & 1 deletion lib/db/manifest/manifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace db::manifest

static constexpr const std::string_view current_filename("current");

// Manifest file will be periodically rotated to we need a unique filename every time
// Manifest file will be periodically rotated to we need a unique filename every
// time
auto manifest_filename() -> std::string
{
return fmt::format("manifest_{}", structures::lsmtree::segments::helpers::uuid());
Expand Down
5 changes: 3 additions & 2 deletions lib/db/manifest/manifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ struct manifest_t

/**
* @brief Serialize manifest segment record into stream.
* Format: <operation-type><whitespace><segment-name><whitespace><level-index>
* |int |char |string |char |int
* Format:
* <operation-type><whitespace><segment-name><whitespace><level-index>
* |int |char |string |char |int
*
* @tparam stream_gt
* @param os
Expand Down
7 changes: 4 additions & 3 deletions lib/fs/append_only_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct append_only_file_t
auto stream() const noexcept -> std::stringstream;

private:
path_t m_path;
path_t m_path;
std::fstream m_out;
};

Expand Down Expand Up @@ -80,7 +80,8 @@ inline auto append_only_file_t::close() noexcept -> bool
m_out.flush();
m_out.close();

// TODO(lnikon): Do we need to recover when we're unable to close the stream?
// TODO(lnikon): Do we need to recover when we're unable to close the
// stream?
return m_out.bad();
}

Expand All @@ -100,7 +101,7 @@ inline auto append_only_file_t::write(const data_t &data) noexcept -> bool
inline auto append_only_file_t::stream() const noexcept -> std::stringstream
{
std::stringstream fileStringStream;
std::fstream fileStream(m_path);
std::fstream fileStream(m_path);
if (fileStream.is_open())
{
fileStringStream << fileStream.rdbuf();
Expand Down
1 change: 0 additions & 1 deletion lib/fs/random_access_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ class random_access_file_t

} // namespace fs::random_access_file


#endif // ZKV_RANDOM_ACCESS_FILE_H
23 changes: 8 additions & 15 deletions lib/server/grpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ tinykvpp_service_impl_t::tinykvpp_service_impl_t(db::shared_ptr_t db)

auto tinykvpp_service_impl_t::Put(grpc::ServerContext *pContext,
const PutRequest *pRequest,
PutResponse *pResponse) -> grpc::Status
PutResponse *pResponse) -> grpc::Status
{
(void)pContext;

try
{
m_database->put(structures::lsmtree::key_t{pRequest->key()},
structures::lsmtree::value_t{pRequest->value()});
m_database->put(structures::lsmtree::key_t{pRequest->key()}, structures::lsmtree::value_t{pRequest->value()});
pResponse->set_status(std::string("OK"));
return grpc::Status::OK;
}
Expand All @@ -43,13 +42,12 @@ auto tinykvpp_service_impl_t::Put(grpc::ServerContext *pContext,

auto tinykvpp_service_impl_t::Get(grpc::ServerContext *pContext,
const GetRequest *pRequest,
GetResponse *pResponse) -> grpc::Status
GetResponse *pResponse) -> grpc::Status
{
(void)pContext;
try
{
const auto &record =
m_database->get(structures::lsmtree::key_t{pRequest->key()});
const auto &record = m_database->get(structures::lsmtree::key_t{pRequest->key()});
if (record)
{
pResponse->set_value(record.value().m_value.m_value);
Expand Down Expand Up @@ -93,15 +91,12 @@ void grpc_communication_t::start(db::shared_ptr_t database) noexcept
spdlog::info("Starting gRPC communication...");

const auto serverAddress{
fmt::format("{}:{}",
database->config()->ServerConfig.host,
database->config()->ServerConfig.port)};
fmt::format("{}:{}", database->config()->ServerConfig.host, database->config()->ServerConfig.port)};

try
{
grpc::ServerBuilder builder;
builder.AddListeningPort(serverAddress,
grpc::InsecureServerCredentials());
builder.AddListeningPort(serverAddress, grpc::InsecureServerCredentials());

m_service = std::make_unique<tinykvpp_service_impl_t>(database);
builder.RegisterService(m_service.get());
Expand All @@ -118,8 +113,7 @@ void grpc_communication_t::start(db::shared_ptr_t database) noexcept
}
catch (std::exception &e)
{
spdlog::error("Excetion occured while creating gRPC server. {}",
e.what());
spdlog::error("Excetion occured while creating gRPC server. {}", e.what());
exit(EXIT_FAILURE);
}
}
Expand All @@ -133,8 +127,7 @@ void grpc_communication_t::shutdown() noexcept
catch (std::exception &e)
{

spdlog::error("Excetion occured while shutting down gRPC server. {}",
e.what());
spdlog::error("Excetion occured while shutting down gRPC server. {}", e.what());
exit(EXIT_FAILURE);
}
}
Expand Down
10 changes: 4 additions & 6 deletions lib/server/grpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ class tinykvpp_service_impl_t final : public TinyKVPPService::Service
public:
explicit tinykvpp_service_impl_t(db::shared_ptr_t db);

auto Put(grpc::ServerContext *pContext,
const PutRequest *pRequest,
PutResponse *pResponse) -> grpc::Status override;
auto
Put(grpc::ServerContext *pContext, const PutRequest *pRequest, PutResponse *pResponse) -> grpc::Status override;

auto Get(grpc::ServerContext *pContext,
const GetRequest *pRequest,
GetResponse *pResponse) -> grpc::Status override;
auto
Get(grpc::ServerContext *pContext, const GetRequest *pRequest, GetResponse *pResponse) -> grpc::Status override;

private:
db::shared_ptr_t m_database;
Expand Down
3 changes: 1 addition & 2 deletions lib/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ template <communication_strategy_t Strategy> class server_t
Strategy m_impl;
};

template <communication_strategy_kind_k Type>
auto main_server(db::shared_ptr_t &db)
template <communication_strategy_kind_k Type> auto main_server(db::shared_ptr_t &db)
{
auto communicationStrategy = factory<Type>();
server_t server(std::move(communicationStrategy));
Expand Down
Loading

0 comments on commit 928ff53

Please sign in to comment.