2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2011-04-19 23:11:15 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2019-05-30 03:44:08 +00:00
|
|
|
#include "file/filename.h"
|
2011-04-19 23:11:15 +00:00
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
2019-06-01 00:19:43 +00:00
|
|
|
#include "logging/logging.h"
|
2011-04-19 23:11:15 +00:00
|
|
|
#include "port/port.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
2011-04-19 23:11:15 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-04-19 23:11:15 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class FileNameTest : public testing::Test {};
|
2011-04-19 23:11:15 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(FileNameTest, Parse) {
|
2011-04-19 23:11:15 +00:00
|
|
|
Slice db;
|
|
|
|
FileType type;
|
|
|
|
uint64_t number;
|
|
|
|
|
2014-08-14 17:05:16 +00:00
|
|
|
char kDefautInfoLogDir = 1;
|
|
|
|
char kDifferentInfoLogDir = 2;
|
|
|
|
char kNoCheckLogDir = 4;
|
|
|
|
char kAllMode = kDefautInfoLogDir | kDifferentInfoLogDir | kNoCheckLogDir;
|
|
|
|
|
2011-04-19 23:11:15 +00:00
|
|
|
// Successful parses
|
|
|
|
static struct {
|
|
|
|
const char* fname;
|
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
2014-08-14 17:05:16 +00:00
|
|
|
char mode;
|
2011-04-19 23:11:15 +00:00
|
|
|
} cases[] = {
|
2014-08-14 17:05:16 +00:00
|
|
|
{"100.log", 100, kLogFile, kAllMode},
|
|
|
|
{"0.log", 0, kLogFile, kAllMode},
|
|
|
|
{"0.sst", 0, kTableFile, kAllMode},
|
|
|
|
{"CURRENT", 0, kCurrentFile, kAllMode},
|
|
|
|
{"LOCK", 0, kDBLockFile, kAllMode},
|
|
|
|
{"MANIFEST-2", 2, kDescriptorFile, kAllMode},
|
|
|
|
{"MANIFEST-7", 7, kDescriptorFile, kAllMode},
|
|
|
|
{"METADB-2", 2, kMetaDatabase, kAllMode},
|
|
|
|
{"METADB-7", 7, kMetaDatabase, kAllMode},
|
|
|
|
{"LOG", 0, kInfoLogFile, kDefautInfoLogDir},
|
|
|
|
{"LOG.old", 0, kInfoLogFile, kDefautInfoLogDir},
|
|
|
|
{"LOG.old.6688", 6688, kInfoLogFile, kDefautInfoLogDir},
|
|
|
|
{"rocksdb_dir_LOG", 0, kInfoLogFile, kDifferentInfoLogDir},
|
|
|
|
{"rocksdb_dir_LOG.old", 0, kInfoLogFile, kDifferentInfoLogDir},
|
|
|
|
{"rocksdb_dir_LOG.old.6688", 6688, kInfoLogFile, kDifferentInfoLogDir},
|
|
|
|
{"18446744073709551615.log", 18446744073709551615ull, kLogFile,
|
|
|
|
kAllMode}, };
|
|
|
|
for (char mode : {kDifferentInfoLogDir, kDefautInfoLogDir, kNoCheckLogDir}) {
|
|
|
|
for (unsigned int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
|
|
|
|
InfoLogPrefix info_log_prefix(mode != kDefautInfoLogDir, "/rocksdb/dir");
|
|
|
|
if (cases[i].mode & mode) {
|
|
|
|
std::string f = cases[i].fname;
|
|
|
|
if (mode == kNoCheckLogDir) {
|
|
|
|
ASSERT_TRUE(ParseFileName(f, &number, &type)) << f;
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(ParseFileName(f, &number, info_log_prefix.prefix, &type))
|
|
|
|
<< f;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(cases[i].type, type) << f;
|
|
|
|
ASSERT_EQ(cases[i].number, number) << f;
|
|
|
|
}
|
|
|
|
}
|
2011-04-19 23:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
static const char* errors[] = {
|
|
|
|
"",
|
|
|
|
"foo",
|
|
|
|
"foo-dx-100.log",
|
|
|
|
".log",
|
|
|
|
"",
|
|
|
|
"manifest",
|
|
|
|
"CURREN",
|
|
|
|
"CURRENTX",
|
|
|
|
"MANIFES",
|
|
|
|
"MANIFEST",
|
|
|
|
"MANIFEST-",
|
|
|
|
"XMANIFEST-3",
|
|
|
|
"MANIFEST-3x",
|
2012-12-17 19:26:59 +00:00
|
|
|
"META",
|
|
|
|
"METADB",
|
|
|
|
"METADB-",
|
|
|
|
"XMETADB-3",
|
|
|
|
"METADB-3x",
|
2011-04-19 23:11:15 +00:00
|
|
|
"LOC",
|
|
|
|
"LOCKx",
|
|
|
|
"LO",
|
|
|
|
"LOGx",
|
|
|
|
"18446744073709551616.log",
|
|
|
|
"184467440737095516150.log",
|
|
|
|
"100",
|
|
|
|
"100.",
|
2011-04-20 22:48:11 +00:00
|
|
|
"100.lop"
|
|
|
|
};
|
2012-11-06 20:02:18 +00:00
|
|
|
for (unsigned int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
|
2011-04-19 23:11:15 +00:00
|
|
|
std::string f = errors[i];
|
2011-04-20 22:48:11 +00:00
|
|
|
ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
|
2011-04-19 23:11:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(FileNameTest, InfoLogFileName) {
|
2014-08-14 17:05:16 +00:00
|
|
|
std::string dbname = ("/data/rocksdb");
|
|
|
|
std::string db_absolute_path;
|
2020-08-07 00:18:25 +00:00
|
|
|
ASSERT_OK(Env::Default()->GetAbsolutePath(dbname, &db_absolute_path));
|
2014-08-14 17:05:16 +00:00
|
|
|
|
|
|
|
ASSERT_EQ("/data/rocksdb/LOG", InfoLogFileName(dbname, db_absolute_path, ""));
|
|
|
|
ASSERT_EQ("/data/rocksdb/LOG.old.666",
|
|
|
|
OldInfoLogFileName(dbname, 666u, db_absolute_path, ""));
|
|
|
|
|
|
|
|
ASSERT_EQ("/data/rocksdb_log/data_rocksdb_LOG",
|
|
|
|
InfoLogFileName(dbname, db_absolute_path, "/data/rocksdb_log"));
|
|
|
|
ASSERT_EQ(
|
|
|
|
"/data/rocksdb_log/data_rocksdb_LOG.old.666",
|
|
|
|
OldInfoLogFileName(dbname, 666u, db_absolute_path, "/data/rocksdb_log"));
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(FileNameTest, Construction) {
|
2011-04-19 23:11:15 +00:00
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
|
|
|
std::string fname;
|
|
|
|
|
|
|
|
fname = CurrentFileName("foo");
|
|
|
|
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
2011-04-20 22:48:11 +00:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0U, number);
|
2011-04-19 23:11:15 +00:00
|
|
|
ASSERT_EQ(kCurrentFile, type);
|
|
|
|
|
|
|
|
fname = LockFileName("foo");
|
|
|
|
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
2011-04-20 22:48:11 +00:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(0U, number);
|
2011-04-19 23:11:15 +00:00
|
|
|
ASSERT_EQ(kDBLockFile, type);
|
|
|
|
|
|
|
|
fname = LogFileName("foo", 192);
|
|
|
|
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
2011-04-20 22:48:11 +00:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(192U, number);
|
2011-04-19 23:11:15 +00:00
|
|
|
ASSERT_EQ(kLogFile, type);
|
|
|
|
|
2014-07-14 22:34:30 +00:00
|
|
|
fname = TableFileName({DbPath("bar", 0)}, 200, 0);
|
|
|
|
std::string fname1 =
|
|
|
|
TableFileName({DbPath("foo", 0), DbPath("bar", 0)}, 200, 1);
|
2014-07-02 16:54:20 +00:00
|
|
|
ASSERT_EQ(fname, fname1);
|
2011-04-19 23:11:15 +00:00
|
|
|
ASSERT_EQ("bar/", std::string(fname.data(), 4));
|
2011-04-20 22:48:11 +00:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(200U, number);
|
2011-04-19 23:11:15 +00:00
|
|
|
ASSERT_EQ(kTableFile, type);
|
|
|
|
|
|
|
|
fname = DescriptorFileName("bar", 100);
|
|
|
|
ASSERT_EQ("bar/", std::string(fname.data(), 4));
|
2011-04-20 22:48:11 +00:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(100U, number);
|
2011-04-19 23:11:15 +00:00
|
|
|
ASSERT_EQ(kDescriptorFile, type);
|
|
|
|
|
|
|
|
fname = TempFileName("tmp", 999);
|
|
|
|
ASSERT_EQ("tmp/", std::string(fname.data(), 4));
|
2011-04-20 22:48:11 +00:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 20:02:18 +00:00
|
|
|
ASSERT_EQ(999U, number);
|
2011-04-19 23:11:15 +00:00
|
|
|
ASSERT_EQ(kTempFile, type);
|
2012-12-17 19:26:59 +00:00
|
|
|
|
|
|
|
fname = MetaDatabaseName("met", 100);
|
|
|
|
ASSERT_EQ("met/", std::string(fname.data(), 4));
|
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
|
|
|
ASSERT_EQ(100U, number);
|
|
|
|
ASSERT_EQ(kMetaDatabase, type);
|
2011-04-19 23:11:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2011-04-19 23:11:15 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2011-04-19 23:11:15 +00:00
|
|
|
}
|