Skip to content

Commit

Permalink
Merge branch 'dev-ban'
Browse files Browse the repository at this point in the history
  • Loading branch information
ban-nobuhiro committed Jan 24, 2025
2 parents 37f7ef9 + 064e143 commit 12cfc13
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 33 deletions.
6 changes: 3 additions & 3 deletions include/shirakami/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ Status init(database_options options = {}); // NOLINT
* @return Status::ERR_READ_AREA_VIOLATION error about read area.
*/
Status insert(Token token, Storage storage,
std::string_view key, // NOLINT
std::string_view key,
std::string_view val,
blob_id_type const* blobs_data = nullptr,
std::size_t blobs_size = 0
Expand Down Expand Up @@ -419,7 +419,7 @@ Status tx_begin(transaction_options options = {}); // NOLINT
*/
Status update(Token token, Storage storage, std::string_view key, std::string_view val,
blob_id_type const* blobs_data = nullptr,
std::size_t blobs_size = 0); // NOLINT
std::size_t blobs_size = 0);

/**
* @brief update the record for the given key, or insert the key/value if the
Expand Down Expand Up @@ -449,7 +449,7 @@ Status update(Token token, Storage storage, std::string_view key, std::string_vi
*/
Status upsert(Token token, Storage storage, std::string_view key, std::string_view val,
blob_id_type const* blobs_data = nullptr,
std::size_t blobs_size = 0); // NOLINT
std::size_t blobs_size = 0);


//==========
Expand Down
4 changes: 4 additions & 0 deletions include/shirakami/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>

#include "binary_printer.h"
#include "span_printer.h"

namespace shirakami {

Expand All @@ -21,6 +22,9 @@ static constexpr std::string_view log_location_prefix_timing_event =
#define shirakami_binstring(arg) \
" " #arg "(len=" << (arg).size() << "):\"" << binary_printer((arg)) \
<< "\"" //NOLINT
// receive std::vector only
#define shirakami_vecstring(arg) \
" " #arg "(len=" << (arg).size() << "):" << span_printer((arg))

/**
* @brief logging level constant for errors
Expand Down
59 changes: 59 additions & 0 deletions include/shirakami/span_printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2025-2025 tsurugi project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <ostream>
#include <string_view>
#include <vector>

namespace shirakami {

/**
* @brief debug support to print C++20-std::span-like range
*/
template<typename T>
class span_printer {
public:
constexpr explicit span_printer(const std::vector<T>& vec) noexcept
: data_(vec.data()), size_(vec.size()), sep_(", ") {}
constexpr span_printer(const std::vector<T>& vec, std::string_view sep) noexcept
: data_(vec.data()), size_(vec.size()), sep_(sep) {}
constexpr span_printer(const T* ptr, std::size_t size) noexcept
: data_(ptr), size_(size), sep_(", ") {}
constexpr span_printer(const T* ptr, std::size_t size, std::string_view sep) noexcept
: data_(ptr), size_(size), sep_(sep) {}

friend std::ostream& operator<<(std::ostream& out,
span_printer const& value) {
out << '[';
for (std::size_t i = 0; i < value.size_; i++) {
if (i > 0) {
out << value.sep_;
}
out << value.data_[i]; // NOLINT
}
out << ']';
return out;
}

private:
const T* data_{};
std::size_t size_{};
std::string_view sep_{};
};

} // namespace shirakami
9 changes: 0 additions & 9 deletions src/concurrency_control/include/local_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ static_assert(std::is_nothrow_move_constructible_v<read_set_obj>);
class write_set_obj { // NOLINT
public:
// for update / upsert / insert
write_set_obj(Storage const storage, OP_TYPE const op,
Record* const rec_ptr, std::string_view const val)
: storage_(storage), op_(op), rec_ptr_(rec_ptr), val_(val) {
if (op == OP_TYPE::DELETE) {
LOG_FIRST_N(ERROR, 1) << log_location_prefix << "unreachable path";
}
}

// for upsert / insert
write_set_obj(Storage const storage, OP_TYPE const op,
Record* const rec_ptr, std::string_view const val,
bool const inc_tombstone)
Expand Down
4 changes: 2 additions & 2 deletions src/concurrency_control/interface/insert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static void register_read_if_ltx(session* const ti, Record* const rec_ptr) {
}

Status insert_body(Token const token, Storage const storage, // NOLINT
const std::string_view key, // NOLINT
const std::string_view key,
const std::string_view val) {
// check constraint: key
auto ret = check_constraint_key_length(key);
Expand Down Expand Up @@ -162,7 +162,7 @@ Status insert_body(Token const token, Storage const storage, // NOLINT
}

Status insert(Token const token, Storage const storage, // NOLINT
const std::string_view key, // NOLINT
const std::string_view key,
const std::string_view val,
[[maybe_unused]] blob_id_type const* blobs_data,
[[maybe_unused]] std::size_t blobs_size) {
Expand Down
7 changes: 3 additions & 4 deletions src/concurrency_control/interface/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static void process_before_return_not_found(session* const ti,
}

Status update_body(Token token, Storage storage,
const std::string_view key, // NOLINT
const std::string_view key,
const std::string_view val) {
// check constraint: key
auto ret = check_constraint_key_length(key);
Expand Down Expand Up @@ -82,8 +82,7 @@ Status update_body(Token token, Storage storage,
}

// prepare write
ti->push_to_write_set(
{storage, OP_TYPE::UPDATE, rec_ptr, val}); // NOLINT
ti->push_to_write_set({storage, OP_TYPE::UPDATE, rec_ptr, val, false});
register_read_if_ltx(ti, rec_ptr);
return Status::OK;
}
Expand All @@ -92,7 +91,7 @@ Status update_body(Token token, Storage storage,
}

Status update(Token token, Storage storage,
std::string_view const key, // NOLINT
std::string_view const key,
std::string_view const val,
[[maybe_unused]] blob_id_type const* blobs_data,
[[maybe_unused]] std::size_t blobs_size) {
Expand Down
3 changes: 1 addition & 2 deletions src/concurrency_control/interface/upsert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ Status upsert_body(Token token, Storage storage, const std::string_view key,
}
if (rc == Status::WARN_ALREADY_EXISTS) {
// prepare update
ti->push_to_write_set(
{storage, OP_TYPE::UPSERT, rec_ptr, val}); // NOLINT
ti->push_to_write_set({storage, OP_TYPE::UPSERT, rec_ptr, val, false});
return Status::OK;
}
if (rc == Status::WARN_CONCURRENT_INSERT) { continue; } // else
Expand Down
15 changes: 2 additions & 13 deletions src/concurrency_control/local_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ void local_write_set::push(Token token, write_set_obj&& elem) {
std::lock_guard<std::shared_mutex> lk{get_mtx()};

if (get_for_batch()) {
if (elem.get_op() == OP_TYPE::DELETE) {
cont_for_bt_.insert_or_assign(elem.get_rec_ptr(),
write_set_obj(elem.get_storage(),
elem.get_op(),
elem.get_rec_ptr()));
} else {
cont_for_bt_.insert_or_assign(
elem.get_rec_ptr(),
write_set_obj(elem.get_storage(), elem.get_op(),
elem.get_rec_ptr(), elem.get_value_view(),
elem.get_inc_tombstone()));
}

if (static_cast<session*>(token)->get_tx_type() ==
transaction_options::transaction_type::LONG) {
// update storage map
Expand All @@ -68,6 +55,8 @@ void local_write_set::push(Token token, write_set_obj&& elem) {
}
}
}
auto* rec_ptr = elem.get_rec_ptr();
cont_for_bt_.insert_or_assign(rec_ptr, std::move(elem));
} else {
cont_for_occ_.emplace_back(std::move(elem)); // NOLINT
if (cont_for_occ_.size() > 100) { // NOLINT
Expand Down
3 changes: 3 additions & 0 deletions src/database/include/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ namespace shirakami::logging {

#define shirakami_log_entry \
VLOG(log_trace) << std::boolalpha << log_location_prefix << "-->" // NOLINT
#define shirakami_log_entry_lazy(data) \
if (VLOG_IS_ON(log_trace)) \
LOG(INFO) << std::boolalpha << log_location_prefix << "-->" << data
#define shirakami_log_exit \
VLOG(log_trace) << std::boolalpha << log_location_prefix << "<--" // NOLINT
#define shirakami_ex_log_entry \
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ else ()
"concurrency_control/short_tx/termination/*.cpp"
"concurrency_control/short_tx/tx_state/*.cpp"
"database_options/*.cpp"
"misc_ut/*.cpp"
"result/*.cpp"
"start/*.cpp"
"shirakami_issues/*.cpp"
Expand Down
79 changes: 79 additions & 0 deletions test/misc_ut/span_printer_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

#include <sstream>

#include "glog/logging.h"
#include "gtest/gtest.h"

#include "shirakami/span_printer.h"

namespace shirakami::testing {

using namespace shirakami;

class span_printer_test : public ::testing::Test {
public:
static void call_once_f() {
google::InitGoogleLogging("shirakami-test-misc_ut-span_printer_test");
// FLAGS_stderrthreshold = 0;
}

void SetUp() override { std::call_once(init_google, call_once_f); }

void TearDown() override {}

private:
static inline std::once_flag init_google;
};

template<typename T>
std::string print(const std::vector<T>& v) {
std::stringstream ss{};
ss << span_printer(v);
return ss.str();
}

template<typename T>
std::string print(const std::vector<T>& v, std::string_view s) {
std::stringstream ss{};
ss << span_printer(v, s);
return ss.str();
}

template<typename T>
std::string print(const T* p, std::size_t n) {
std::stringstream ss{};
ss << span_printer(p, n);
return ss.str();
}

template<typename T>
std::string print(const T* p, std::size_t n, std::string_view s) {
std::stringstream ss{};
ss << span_printer(p, n, s);
return ss.str();
}

TEST_F(span_printer_test, charvec) {
EXPECT_EQ(print(std::vector<char>{}), "[]");
EXPECT_EQ(print(std::vector<char>{'a'}), "[a]");
EXPECT_EQ(print(std::vector<char>{'a', 'b'}), "[a, b]");
EXPECT_EQ(print(std::vector<char>{'c', 'h', 'a', 'r'}, ""), "[char]");
}

TEST_F(span_printer_test, uint64vec) {
EXPECT_EQ(print(std::vector<std::uint64_t>{}), "[]");
EXPECT_EQ(print(std::vector<std::uint64_t>{0}), "[0]");
EXPECT_EQ(print(std::vector<std::uint64_t>{100, 200}), "[100, 200]");
EXPECT_EQ(print(std::vector<std::uint64_t>{}, "x"), "[]");
EXPECT_EQ(print(std::vector<std::uint64_t>{100, 200, 30}, "x"), "[100x200x30]");
}

TEST_F(span_printer_test, uint64arr) {
EXPECT_EQ(print((std::uint64_t*)nullptr, 0), "[]");
std::uint64_t p1[] = {0U, 1U, 2U, 3U, 4U, 5U};
EXPECT_EQ(print(p1, 1), "[0]");
EXPECT_EQ(print(p1, 2), "[0, 1]");
EXPECT_EQ(print(&p1[3], 3, "x"), "[3x4x5]");
}

} // namespace shirakami::testing

0 comments on commit 12cfc13

Please sign in to comment.