提交 ab22cf34 编写于 作者: D Dmitri Smirnov 提交者: Facebook Github Bot

Implement Env::NumFileLinks (#4221)

Summary:
Although delete scheduler implementation allows for the interface not to be supported, the delete_scheduler_test does not allow for that.
Address compiler warnings
Make sst_dump_test use test directory structure as the current execution directory may not be writiable.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4221

Differential Revision: D9210152

Pulled By: siying

fbshipit-source-id: 381a74511e969ecb8089d5c4b4df87dc30c8df63
上级 de7f423a
...@@ -716,6 +716,34 @@ Status WinEnvIO::LinkFile(const std::string& src, ...@@ -716,6 +716,34 @@ Status WinEnvIO::LinkFile(const std::string& src,
return result; return result;
} }
Status WinEnvIO::NumFileLinks(const std::string& fname,
uint64_t* count) {
Status s;
HANDLE handle = ::CreateFileA(fname.c_str(), 0,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (INVALID_HANDLE_VALUE == handle) {
auto lastError = GetLastError();
s = IOErrorFromWindowsError(
"NumFileLinks: " + fname, lastError);
return s;
}
UniqueCloseHandlePtr handle_guard(handle, CloseHandleFunc);
FILE_STANDARD_INFO standard_info;
if (0 != GetFileInformationByHandleEx(handle, FileStandardInfo,
&standard_info, sizeof(standard_info))) {
*count = standard_info.NumberOfLinks;
} else {
auto lastError = GetLastError();
s = IOErrorFromWindowsError("GetFileInformationByHandleEx: " + fname, lastError);
}
return s;
}
Status WinEnvIO::AreFilesSame(const std::string& first, Status WinEnvIO::AreFilesSame(const std::string& first,
const std::string& second, bool* res) { const std::string& second, bool* res) {
// For MinGW builds // For MinGW builds
...@@ -1325,6 +1353,10 @@ Status WinEnv::LinkFile(const std::string& src, ...@@ -1325,6 +1353,10 @@ Status WinEnv::LinkFile(const std::string& src,
return winenv_io_.LinkFile(src, target); return winenv_io_.LinkFile(src, target);
} }
Status WinEnv::NumFileLinks(const std::string& fname, uint64_t* count) {
return winenv_io_.NumFileLinks(fname, count);
}
Status WinEnv::AreFilesSame(const std::string& first, Status WinEnv::AreFilesSame(const std::string& first,
const std::string& second, bool* res) { const std::string& second, bool* res) {
return winenv_io_.AreFilesSame(first, second, res); return winenv_io_.AreFilesSame(first, second, res);
......
...@@ -144,6 +144,9 @@ public: ...@@ -144,6 +144,9 @@ public:
virtual Status LinkFile(const std::string& src, virtual Status LinkFile(const std::string& src,
const std::string& target); const std::string& target);
virtual Status NumFileLinks(const std::string& /*fname*/,
uint64_t* /*count*/);
virtual Status AreFilesSame(const std::string& first, virtual Status AreFilesSame(const std::string& first,
const std::string& second, bool* res); const std::string& second, bool* res);
...@@ -268,6 +271,9 @@ public: ...@@ -268,6 +271,9 @@ public:
Status LinkFile(const std::string& src, Status LinkFile(const std::string& src,
const std::string& target) override; const std::string& target) override;
Status NumFileLinks(const std::string& fname,
uint64_t* count) override;
Status AreFilesSame(const std::string& first, Status AreFilesSame(const std::string& first,
const std::string& second, bool* res) override; const std::string& second, bool* res) override;
......
...@@ -588,6 +588,11 @@ enum RepFactory StringToRepFactory(const char* ctype) { ...@@ -588,6 +588,11 @@ enum RepFactory StringToRepFactory(const char* ctype) {
return kSkipList; return kSkipList;
} }
#ifdef _MSC_VER
#pragma warning(push)
// truncation of constant value on static_cast
#pragma warning(disable: 4309)
#endif
bool GetNextPrefix(const rocksdb::Slice& src, std::string* v) { bool GetNextPrefix(const rocksdb::Slice& src, std::string* v) {
std::string ret = src.ToString(); std::string ret = src.ToString();
for (int i = static_cast<int>(ret.size()) - 1; i >= 0; i--) { for (int i = static_cast<int>(ret.size()) - 1; i >= 0; i--) {
...@@ -604,6 +609,9 @@ bool GetNextPrefix(const rocksdb::Slice& src, std::string* v) { ...@@ -604,6 +609,9 @@ bool GetNextPrefix(const rocksdb::Slice& src, std::string* v) {
*v = ret; *v = ret;
return true; return true;
} }
#ifdef _MSC_VER
#pragma warning(pop)
#endif
} // namespace } // namespace
static enum RepFactory FLAGS_rep_factory; static enum RepFactory FLAGS_rep_factory;
......
...@@ -53,11 +53,12 @@ void createSST(const std::string& file_name, ...@@ -53,11 +53,12 @@ void createSST(const std::string& file_name,
rocksdb::InternalKeyComparator ikc(opts.comparator); rocksdb::InternalKeyComparator ikc(opts.comparator);
unique_ptr<TableBuilder> tb; unique_ptr<TableBuilder> tb;
env->NewWritableFile(file_name, &file, env_options); ASSERT_OK(env->NewWritableFile(file_name, &file, env_options));
opts.table_factory = tf; opts.table_factory = tf;
std::vector<std::unique_ptr<IntTblPropCollectorFactory> > std::vector<std::unique_ptr<IntTblPropCollectorFactory> >
int_tbl_prop_collector_factories; int_tbl_prop_collector_factories;
unique_ptr<WritableFileWriter> file_writer( std::unique_ptr<WritableFileWriter> file_writer(
new WritableFileWriter(std::move(file), EnvOptions())); new WritableFileWriter(std::move(file), EnvOptions()));
std::string column_family_name; std::string column_family_name;
int unknown_level = -1; int unknown_level = -1;
...@@ -90,30 +91,45 @@ void cleanup(const std::string& file_name) { ...@@ -90,30 +91,45 @@ void cleanup(const std::string& file_name) {
// Test for sst dump tool "raw" mode // Test for sst dump tool "raw" mode
class SSTDumpToolTest : public testing::Test { class SSTDumpToolTest : public testing::Test {
public: std::string testDir_;
public:
BlockBasedTableOptions table_options_; BlockBasedTableOptions table_options_;
SSTDumpToolTest() {} SSTDumpToolTest() {
testDir_ = test::TmpDir();
}
~SSTDumpToolTest() {} ~SSTDumpToolTest() {}
std::string MakeFilePath(const std::string& file_name) const {
std::string path(testDir_);
path.append("/").append(file_name);
return path;
}
template<std::size_t N>
void PopulateCommandArgs(const std::string& file_path, const char* command,
char* (&usage)[N]) const {
for (int i = 0; i < N; ++i) {
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "%s", command);
snprintf(usage[2], optLength, "--file=%s", file_path.c_str());
}
}; };
TEST_F(SSTDumpToolTest, EmptyFilter) { TEST_F(SSTDumpToolTest, EmptyFilter) {
std::string file_name = "rocksdb_sst_test.sst"; std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_name, table_options_); createSST(file_path, table_options_);
char* usage[3]; char* usage[3];
for (int i = 0; i < 3; i++) { PopulateCommandArgs(file_path, "--command=raw", usage);
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--command=raw");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
rocksdb::SSTDumpTool tool; rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage)); ASSERT_TRUE(!tool.Run(3, usage));
cleanup(file_name); cleanup(file_path);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
delete[] usage[i]; delete[] usage[i];
} }
...@@ -121,21 +137,16 @@ TEST_F(SSTDumpToolTest, EmptyFilter) { ...@@ -121,21 +137,16 @@ TEST_F(SSTDumpToolTest, EmptyFilter) {
TEST_F(SSTDumpToolTest, FilterBlock) { TEST_F(SSTDumpToolTest, FilterBlock) {
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true)); table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true));
std::string file_name = "rocksdb_sst_test.sst"; std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_name, table_options_); createSST(file_path, table_options_);
char* usage[3]; char* usage[3];
for (int i = 0; i < 3; i++) { PopulateCommandArgs(file_path, "--command=raw", usage);
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--command=raw");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
rocksdb::SSTDumpTool tool; rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage)); ASSERT_TRUE(!tool.Run(3, usage));
cleanup(file_name); cleanup(file_path);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
delete[] usage[i]; delete[] usage[i];
} }
...@@ -143,21 +154,16 @@ TEST_F(SSTDumpToolTest, FilterBlock) { ...@@ -143,21 +154,16 @@ TEST_F(SSTDumpToolTest, FilterBlock) {
TEST_F(SSTDumpToolTest, FullFilterBlock) { TEST_F(SSTDumpToolTest, FullFilterBlock) {
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
std::string file_name = "rocksdb_sst_test.sst"; std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_name, table_options_); createSST(file_path, table_options_);
char* usage[3]; char* usage[3];
for (int i = 0; i < 3; i++) { PopulateCommandArgs(file_path, "--command=raw", usage);
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--command=raw");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
rocksdb::SSTDumpTool tool; rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage)); ASSERT_TRUE(!tool.Run(3, usage));
cleanup(file_name); cleanup(file_path);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
delete[] usage[i]; delete[] usage[i];
} }
...@@ -165,21 +171,16 @@ TEST_F(SSTDumpToolTest, FullFilterBlock) { ...@@ -165,21 +171,16 @@ TEST_F(SSTDumpToolTest, FullFilterBlock) {
TEST_F(SSTDumpToolTest, GetProperties) { TEST_F(SSTDumpToolTest, GetProperties) {
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
std::string file_name = "rocksdb_sst_test.sst"; std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_name, table_options_); createSST(file_path, table_options_);
char* usage[3]; char* usage[3];
for (int i = 0; i < 3; i++) { PopulateCommandArgs(file_path, "--show_properties", usage);
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--show_properties");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
rocksdb::SSTDumpTool tool; rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage)); ASSERT_TRUE(!tool.Run(3, usage));
cleanup(file_name); cleanup(file_path);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
delete[] usage[i]; delete[] usage[i];
} }
...@@ -187,21 +188,16 @@ TEST_F(SSTDumpToolTest, GetProperties) { ...@@ -187,21 +188,16 @@ TEST_F(SSTDumpToolTest, GetProperties) {
TEST_F(SSTDumpToolTest, CompressedSizes) { TEST_F(SSTDumpToolTest, CompressedSizes) {
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false)); table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
std::string file_name = "rocksdb_sst_test.sst"; std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_name, table_options_); createSST(file_path, table_options_);
char* usage[3]; char* usage[3];
for (int i = 0; i < 3; i++) { PopulateCommandArgs(file_path, "--command=recompress", usage);
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--command=recompress");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
rocksdb::SSTDumpTool tool; rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage)); ASSERT_TRUE(!tool.Run(3, usage));
cleanup(file_name); cleanup(file_path);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
delete[] usage[i]; delete[] usage[i];
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册