2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-04-20 01:11:15 +02: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.
|
|
|
|
|
|
|
|
#include "db/filename.h"
|
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-04-20 01:11:15 +02:00
|
|
|
|
|
|
|
class FileNameTest { };
|
|
|
|
|
|
|
|
TEST(FileNameTest, Parse) {
|
|
|
|
Slice db;
|
|
|
|
FileType type;
|
|
|
|
uint64_t number;
|
|
|
|
|
|
|
|
// Successful parses
|
|
|
|
static struct {
|
|
|
|
const char* fname;
|
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
|
|
|
} cases[] = {
|
2011-04-21 00:48:11 +02:00
|
|
|
{ "100.log", 100, kLogFile },
|
|
|
|
{ "0.log", 0, kLogFile },
|
|
|
|
{ "0.sst", 0, kTableFile },
|
|
|
|
{ "CURRENT", 0, kCurrentFile },
|
|
|
|
{ "LOCK", 0, kDBLockFile },
|
|
|
|
{ "MANIFEST-2", 2, kDescriptorFile },
|
|
|
|
{ "MANIFEST-7", 7, kDescriptorFile },
|
2012-12-17 20:26:59 +01:00
|
|
|
{ "METADB-2", 2, kMetaDatabase },
|
|
|
|
{ "METADB-7", 7, kMetaDatabase },
|
2011-04-21 00:48:11 +02:00
|
|
|
{ "LOG", 0, kInfoLogFile },
|
|
|
|
{ "LOG.old", 0, kInfoLogFile },
|
|
|
|
{ "18446744073709551615.log", 18446744073709551615ull, kLogFile },
|
2011-04-20 01:11:15 +02:00
|
|
|
};
|
2012-11-06 21:02:18 +01:00
|
|
|
for (unsigned int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
|
2011-04-20 01:11:15 +02:00
|
|
|
std::string f = cases[i].fname;
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_TRUE(ParseFileName(f, &number, &type)) << f;
|
2011-04-20 01:11:15 +02:00
|
|
|
ASSERT_EQ(cases[i].type, type) << f;
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_EQ(cases[i].number, number) << f;
|
2011-04-20 01:11:15 +02: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 20:26:59 +01:00
|
|
|
"META",
|
|
|
|
"METADB",
|
|
|
|
"METADB-",
|
|
|
|
"XMETADB-3",
|
|
|
|
"METADB-3x",
|
2011-04-20 01:11:15 +02:00
|
|
|
"LOC",
|
|
|
|
"LOCKx",
|
|
|
|
"LO",
|
|
|
|
"LOGx",
|
|
|
|
"18446744073709551616.log",
|
|
|
|
"184467440737095516150.log",
|
|
|
|
"100",
|
|
|
|
"100.",
|
2011-04-21 00:48:11 +02:00
|
|
|
"100.lop"
|
|
|
|
};
|
2012-11-06 21:02:18 +01:00
|
|
|
for (unsigned int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
|
2011-04-20 01:11:15 +02:00
|
|
|
std::string f = errors[i];
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
|
2011-04-20 01:11:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileNameTest, Construction) {
|
|
|
|
uint64_t number;
|
|
|
|
FileType type;
|
|
|
|
std::string fname;
|
|
|
|
|
|
|
|
fname = CurrentFileName("foo");
|
|
|
|
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(0U, number);
|
2011-04-20 01:11:15 +02:00
|
|
|
ASSERT_EQ(kCurrentFile, type);
|
|
|
|
|
|
|
|
fname = LockFileName("foo");
|
|
|
|
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(0U, number);
|
2011-04-20 01:11:15 +02:00
|
|
|
ASSERT_EQ(kDBLockFile, type);
|
|
|
|
|
|
|
|
fname = LogFileName("foo", 192);
|
|
|
|
ASSERT_EQ("foo/", std::string(fname.data(), 4));
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(192U, number);
|
2011-04-20 01:11:15 +02:00
|
|
|
ASSERT_EQ(kLogFile, type);
|
|
|
|
|
2014-07-02 18:54:20 +02:00
|
|
|
fname = TableFileName({"bar"}, 200, 0);
|
|
|
|
std::string fname1 = TableFileName({"foo", "bar"}, 200, 1);
|
|
|
|
ASSERT_EQ(fname, fname1);
|
2011-04-20 01:11:15 +02:00
|
|
|
ASSERT_EQ("bar/", std::string(fname.data(), 4));
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(200U, number);
|
2011-04-20 01:11:15 +02:00
|
|
|
ASSERT_EQ(kTableFile, type);
|
|
|
|
|
|
|
|
fname = DescriptorFileName("bar", 100);
|
|
|
|
ASSERT_EQ("bar/", std::string(fname.data(), 4));
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(100U, number);
|
2011-04-20 01:11:15 +02:00
|
|
|
ASSERT_EQ(kDescriptorFile, type);
|
|
|
|
|
|
|
|
fname = TempFileName("tmp", 999);
|
|
|
|
ASSERT_EQ("tmp/", std::string(fname.data(), 4));
|
2011-04-21 00:48:11 +02:00
|
|
|
ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(999U, number);
|
2011-04-20 01:11:15 +02:00
|
|
|
ASSERT_EQ(kTempFile, type);
|
2012-12-17 20:26:59 +01: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-20 01:11:15 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2011-04-20 01:11:15 +02:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2013-10-04 06:49:15 +02:00
|
|
|
return rocksdb::test::RunAllTests();
|
2011-04-20 01:11:15 +02:00
|
|
|
}
|