Skip to content

Commit

Permalink
tglogutil-compaction: move a function to a separate file, and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ban-nobuhiro committed Sep 18, 2024
1 parent 6f1e96d commit 1f95216
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 19 deletions.
19 changes: 0 additions & 19 deletions src/limestone/dblogutil/dblogutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,6 @@ void repair(dblog_scan &ds, std::optional<epoch_id_type> epoch) {
}
}

static boost::filesystem::path make_tmp_dir_next_to(const boost::filesystem::path& target_dir, const char* suffix) {
auto canonicalpath = boost::filesystem::canonical(target_dir);
// some versions of boost::filesystem::canonical do not remove trailing directory-separators ('/')
std::string targetdirstring = canonicalpath.string();
std::size_t prev_len{};
do {
prev_len = targetdirstring.size();
canonicalpath.remove_trailing_separator(); // remove only one char
targetdirstring = canonicalpath.string();
} while (targetdirstring.size() < prev_len);

auto tmpdirname = targetdirstring + suffix;
if (::mkdtemp(tmpdirname.data()) == nullptr) {
LOG_LP(ERROR) << "mkdtemp failed, errno = " << errno;
throw std::runtime_error("I/O error");
}
return {tmpdirname};
}

static boost::filesystem::path make_work_dir_next_to(const boost::filesystem::path& target_dir) {
// assume: already checked existence and is_dir
return make_tmp_dir_next_to(target_dir, ".work_XXXXXX");
Expand Down
44 changes: 44 additions & 0 deletions src/limestone/filepath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2024-2024 Project Tsurugi.
*
* 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.
*/

#include <glog/logging.h>
#include <limestone/logging.h>
#include "logging_helper.h"

#include "internal.h"

namespace limestone::internal {

boost::filesystem::path make_tmp_dir_next_to(const boost::filesystem::path& target_dir, const char* suffix) {
auto canonicalpath = boost::filesystem::canonical(target_dir);
// some versions of boost::filesystem::canonical do not remove trailing directory-separators ('/')
std::string targetdirstring = canonicalpath.string();
std::size_t prev_len{};
do {
prev_len = targetdirstring.size();
canonicalpath.remove_trailing_separator(); // remove only one char
targetdirstring = canonicalpath.string();
} while (targetdirstring.size() < prev_len);

auto tmpdirname = targetdirstring + suffix;
if (::mkdtemp(tmpdirname.data()) == nullptr) {
LOG_LP(ERROR) << "mkdtemp failed, errno = " << errno;
throw std::runtime_error("I/O error");
}
return {tmpdirname};
}

}
4 changes: 4 additions & 0 deletions src/limestone/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ void create_compact_pwal(
int num_worker,
const std::set<std::string>& file_names = std::set<std::string>());

// filepath.cpp

boost::filesystem::path make_tmp_dir_next_to(const boost::filesystem::path& target_dir, const char* suffix);

}
58 changes: 58 additions & 0 deletions test/limestone/utils/make_tmp_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

#include <boost/filesystem.hpp>

#include "dblog_scan.h"
#include "internal.h"
#include "log_entry.h"

#include "test_root.h"

namespace limestone::testing {

using namespace std::literals;
using namespace limestone::api;
using namespace limestone::internal;

class make_tmp_test : public ::testing::Test {
public:
static constexpr const char* location = "/tmp/make_tmp_test";

void SetUp() {
boost::filesystem::remove_all(location);
if (!boost::filesystem::create_directory(location)) {
std::cerr << "cannot make directory" << std::endl;
}
}

void TearDown() {
boost::filesystem::remove_all(location);
}

bool starts_with(std::string a, std::string b) { return a.substr(0, b.length()) == b; }

};

TEST_F(make_tmp_test, make_tmp_dir_next_to_0slash) {
std::string p = std::string(location) + "/test0";
boost::filesystem::create_directory(p);
auto tmp = make_tmp_dir_next_to({p}, ".suffix_XXXXXX");
ASSERT_TRUE(starts_with(tmp.filename().string(), "test0.suffix_"));
}

// check removing trailig slashes

TEST_F(make_tmp_test, make_tmp_dir_next_to_1slash) {
std::string p = std::string(location) + "/test1/";
boost::filesystem::create_directory(p);
auto tmp = make_tmp_dir_next_to({p}, ".suffix_XXXXXX");
ASSERT_TRUE(starts_with(tmp.filename().string(), "test1.suffix_"));
}

TEST_F(make_tmp_test, make_tmp_dir_next_to_2slash) {
std::string p = std::string(location) + "/test2//";
boost::filesystem::create_directory(p);
auto tmp = make_tmp_dir_next_to({p}, ".suffix_XXXXXX");
ASSERT_TRUE(starts_with(tmp.filename().string(), "test2.suffix_"));
}

}

0 comments on commit 1f95216

Please sign in to comment.