2011-03-19 06:37:00 +08: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-03 02:01:00 +08:00
|
|
|
#include "db/filename.h"
|
|
|
|
|
2020-04-30 03:59:39 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
2019-05-03 02:01:00 +08:00
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "db/dbformat.h"
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/env.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "util/logging.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2012-01-26 06:56:52 +08:00
|
|
|
// A utility routine: write "data" to the named file and Sync() it.
|
2018-03-13 00:14:44 +08:00
|
|
|
Status WriteStringToFileSync(Env* env, const Slice& data,
|
|
|
|
const std::string& fname);
|
2012-01-26 06:56:52 +08:00
|
|
|
|
2018-03-13 00:14:44 +08:00
|
|
|
static std::string MakeFileName(const std::string& dbname, uint64_t number,
|
2011-03-19 06:37:00 +08:00
|
|
|
const char* suffix) {
|
|
|
|
char buf[100];
|
2020-04-30 06:31:41 +08:00
|
|
|
std::snprintf(buf, sizeof(buf), "/%06llu.%s",
|
|
|
|
static_cast<unsigned long long>(number), suffix);
|
2018-03-13 00:14:44 +08:00
|
|
|
return dbname + buf;
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:14:44 +08:00
|
|
|
std::string LogFileName(const std::string& dbname, uint64_t number) {
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(number > 0);
|
2018-03-13 00:14:44 +08:00
|
|
|
return MakeFileName(dbname, number, "log");
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:14:44 +08:00
|
|
|
std::string TableFileName(const std::string& dbname, uint64_t number) {
|
2013-09-20 04:42:22 +08:00
|
|
|
assert(number > 0);
|
2018-03-13 00:14:44 +08:00
|
|
|
return MakeFileName(dbname, number, "ldb");
|
2013-09-20 04:42:22 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:14:44 +08:00
|
|
|
std::string SSTTableFileName(const std::string& dbname, uint64_t number) {
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(number > 0);
|
2018-03-13 00:14:44 +08:00
|
|
|
return MakeFileName(dbname, number, "sst");
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DescriptorFileName(const std::string& dbname, uint64_t number) {
|
|
|
|
assert(number > 0);
|
|
|
|
char buf[100];
|
2020-04-30 06:31:41 +08:00
|
|
|
std::snprintf(buf, sizeof(buf), "/MANIFEST-%06llu",
|
|
|
|
static_cast<unsigned long long>(number));
|
2011-03-19 06:37:00 +08:00
|
|
|
return dbname + buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CurrentFileName(const std::string& dbname) {
|
|
|
|
return dbname + "/CURRENT";
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
std::string LockFileName(const std::string& dbname) { return dbname + "/LOCK"; }
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
std::string TempFileName(const std::string& dbname, uint64_t number) {
|
|
|
|
assert(number > 0);
|
|
|
|
return MakeFileName(dbname, number, "dbtmp");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string InfoLogFileName(const std::string& dbname) {
|
|
|
|
return dbname + "/LOG";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the name of the old info log file for "dbname".
|
|
|
|
std::string OldInfoLogFileName(const std::string& dbname) {
|
|
|
|
return dbname + "/LOG.old";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Owned filenames have the form:
|
|
|
|
// dbname/CURRENT
|
|
|
|
// dbname/LOCK
|
|
|
|
// dbname/LOG
|
|
|
|
// dbname/LOG.old
|
|
|
|
// dbname/MANIFEST-[0-9]+
|
2013-09-20 04:42:22 +08:00
|
|
|
// dbname/[0-9]+.(log|sst|ldb)
|
2019-05-03 02:01:00 +08:00
|
|
|
bool ParseFileName(const std::string& filename, uint64_t* number,
|
2011-03-19 06:37:00 +08:00
|
|
|
FileType* type) {
|
2018-03-13 00:14:44 +08:00
|
|
|
Slice rest(filename);
|
2011-03-19 06:37:00 +08:00
|
|
|
if (rest == "CURRENT") {
|
|
|
|
*number = 0;
|
|
|
|
*type = kCurrentFile;
|
|
|
|
} else if (rest == "LOCK") {
|
|
|
|
*number = 0;
|
|
|
|
*type = kDBLockFile;
|
|
|
|
} else if (rest == "LOG" || rest == "LOG.old") {
|
|
|
|
*number = 0;
|
|
|
|
*type = kInfoLogFile;
|
|
|
|
} else if (rest.starts_with("MANIFEST-")) {
|
|
|
|
rest.remove_prefix(strlen("MANIFEST-"));
|
|
|
|
uint64_t num;
|
|
|
|
if (!ConsumeDecimalNumber(&rest, &num)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!rest.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*type = kDescriptorFile;
|
|
|
|
*number = num;
|
|
|
|
} else {
|
|
|
|
// Avoid strtoull() to keep filename format independent of the
|
|
|
|
// current locale
|
|
|
|
uint64_t num;
|
|
|
|
if (!ConsumeDecimalNumber(&rest, &num)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Slice suffix = rest;
|
|
|
|
if (suffix == Slice(".log")) {
|
|
|
|
*type = kLogFile;
|
2013-09-20 04:42:22 +08:00
|
|
|
} else if (suffix == Slice(".sst") || suffix == Slice(".ldb")) {
|
2011-03-19 06:37:00 +08:00
|
|
|
*type = kTableFile;
|
|
|
|
} else if (suffix == Slice(".dbtmp")) {
|
|
|
|
*type = kTempFile;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*number = num;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SetCurrentFile(Env* env, const std::string& dbname,
|
|
|
|
uint64_t descriptor_number) {
|
|
|
|
// Remove leading "dbname/" and add newline to manifest file name
|
|
|
|
std::string manifest = DescriptorFileName(dbname, descriptor_number);
|
|
|
|
Slice contents = manifest;
|
|
|
|
assert(contents.starts_with(dbname + "/"));
|
|
|
|
contents.remove_prefix(dbname.size() + 1);
|
|
|
|
std::string tmp = TempFileName(dbname, descriptor_number);
|
2012-01-26 06:56:52 +08:00
|
|
|
Status s = WriteStringToFileSync(env, contents.ToString() + "\n", tmp);
|
2011-03-19 06:37:00 +08:00
|
|
|
if (s.ok()) {
|
|
|
|
s = env->RenameFile(tmp, CurrentFileName(dbname));
|
|
|
|
}
|
|
|
|
if (!s.ok()) {
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
env->RemoveFile(tmp);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|