fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
396 lines
12 KiB
396 lines
12 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
11 years ago
|
// 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.
|
||
|
//
|
||
14 years ago
|
// 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.
|
||
10 years ago
|
#ifndef __STDC_FORMAT_MACROS
|
||
11 years ago
|
#define __STDC_FORMAT_MACROS
|
||
10 years ago
|
#endif
|
||
|
|
||
8 years ago
|
#include "util/filename.h"
|
||
11 years ago
|
#include <inttypes.h>
|
||
12 years ago
|
|
||
14 years ago
|
#include <ctype.h>
|
||
|
#include <stdio.h>
|
||
11 years ago
|
#include <vector>
|
||
12 years ago
|
#include "rocksdb/env.h"
|
||
10 years ago
|
#include "util/file_reader_writer.h"
|
||
14 years ago
|
#include "util/logging.h"
|
||
10 years ago
|
#include "util/stop_watch.h"
|
||
9 years ago
|
#include "util/string_util.h"
|
||
9 years ago
|
#include "util/sync_point.h"
|
||
14 years ago
|
|
||
11 years ago
|
namespace rocksdb {
|
||
14 years ago
|
|
||
9 years ago
|
static const std::string kRocksDbTFileExt = "sst";
|
||
|
static const std::string kLevelDbTFileExt = "ldb";
|
||
|
|
||
12 years ago
|
// Given a path, flatten the path name by replacing all chars not in
|
||
11 years ago
|
// {[0-9,a-z,A-Z,-,_,.]} with _. And append '_LOG\0' at the end.
|
||
12 years ago
|
// Return the number of chars stored in dest not including the trailing '\0'.
|
||
11 years ago
|
static size_t GetInfoLogPrefix(const std::string& path, char* dest, int len) {
|
||
|
const char suffix[] = "_LOG";
|
||
12 years ago
|
|
||
11 years ago
|
size_t write_idx = 0;
|
||
|
size_t i = 0;
|
||
|
size_t src_len = path.size();
|
||
|
|
||
|
while (i < src_len && write_idx < len - sizeof(suffix)) {
|
||
12 years ago
|
if ((path[i] >= 'a' && path[i] <= 'z') ||
|
||
|
(path[i] >= '0' && path[i] <= '9') ||
|
||
|
(path[i] >= 'A' && path[i] <= 'Z') ||
|
||
|
path[i] == '-' ||
|
||
|
path[i] == '.' ||
|
||
|
path[i] == '_'){
|
||
|
dest[write_idx++] = path[i];
|
||
|
} else {
|
||
9 years ago
|
if (i > 0) {
|
||
12 years ago
|
dest[write_idx++] = '_';
|
||
9 years ago
|
}
|
||
12 years ago
|
}
|
||
|
i++;
|
||
|
}
|
||
11 years ago
|
assert(sizeof(suffix) <= len - write_idx);
|
||
|
// "\0" is automatically added by snprintf
|
||
|
snprintf(dest + write_idx, len - write_idx, suffix);
|
||
|
write_idx += sizeof(suffix) - 1;
|
||
12 years ago
|
return write_idx;
|
||
|
}
|
||
|
|
||
14 years ago
|
static std::string MakeFileName(const std::string& name, uint64_t number,
|
||
|
const char* suffix) {
|
||
|
char buf[100];
|
||
11 years ago
|
snprintf(buf, sizeof(buf), "/%06llu.%s",
|
||
14 years ago
|
static_cast<unsigned long long>(number),
|
||
|
suffix);
|
||
11 years ago
|
return name + buf;
|
||
14 years ago
|
}
|
||
|
|
||
|
std::string LogFileName(const std::string& name, uint64_t number) {
|
||
|
assert(number > 0);
|
||
|
return MakeFileName(name, number, "log");
|
||
|
}
|
||
|
|
||
11 years ago
|
std::string ArchivalDirectory(const std::string& dir) {
|
||
|
return dir + "/" + ARCHIVAL_DIR;
|
||
12 years ago
|
}
|
||
12 years ago
|
std::string ArchivedLogFileName(const std::string& name, uint64_t number) {
|
||
|
assert(number > 0);
|
||
11 years ago
|
return MakeFileName(name + "/" + ARCHIVAL_DIR, number, "log");
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
std::string MakeTableFileName(const std::string& path, uint64_t number) {
|
||
9 years ago
|
return MakeFileName(path, number, kRocksDbTFileExt.c_str());
|
||
|
}
|
||
|
|
||
|
std::string Rocks2LevelTableFileName(const std::string& fullname) {
|
||
|
assert(fullname.size() > kRocksDbTFileExt.size() + 1);
|
||
|
if (fullname.size() <= kRocksDbTFileExt.size() + 1) {
|
||
|
return "";
|
||
|
}
|
||
|
return fullname.substr(0, fullname.size() - kRocksDbTFileExt.size()) +
|
||
|
kLevelDbTFileExt;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
uint64_t TableFileNameToNumber(const std::string& name) {
|
||
|
uint64_t number = 0;
|
||
|
uint64_t base = 1;
|
||
|
int pos = static_cast<int>(name.find_last_of('.'));
|
||
|
while (--pos >= 0 && name[pos] >= '0' && name[pos] <= '9') {
|
||
|
number += (name[pos] - '0') * base;
|
||
|
base *= 10;
|
||
|
}
|
||
|
return number;
|
||
|
}
|
||
|
|
||
11 years ago
|
std::string TableFileName(const std::vector<DbPath>& db_paths, uint64_t number,
|
||
|
uint32_t path_id) {
|
||
14 years ago
|
assert(number > 0);
|
||
11 years ago
|
std::string path;
|
||
|
if (path_id >= db_paths.size()) {
|
||
11 years ago
|
path = db_paths.back().path;
|
||
11 years ago
|
} else {
|
||
11 years ago
|
path = db_paths[path_id].path;
|
||
11 years ago
|
}
|
||
|
return MakeTableFileName(path, number);
|
||
|
}
|
||
|
|
||
11 years ago
|
void FormatFileNumber(uint64_t number, uint32_t path_id, char* out_buf,
|
||
|
size_t out_buf_size) {
|
||
11 years ago
|
if (path_id == 0) {
|
||
11 years ago
|
snprintf(out_buf, out_buf_size, "%" PRIu64, number);
|
||
11 years ago
|
} else {
|
||
11 years ago
|
snprintf(out_buf, out_buf_size, "%" PRIu64
|
||
|
"(path "
|
||
|
"%" PRIu32 ")",
|
||
|
number, path_id);
|
||
11 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
std::string DescriptorFileName(const std::string& dbname, uint64_t number) {
|
||
|
assert(number > 0);
|
||
|
char buf[100];
|
||
|
snprintf(buf, sizeof(buf), "/MANIFEST-%06llu",
|
||
|
static_cast<unsigned long long>(number));
|
||
|
return dbname + buf;
|
||
|
}
|
||
|
|
||
|
std::string CurrentFileName(const std::string& dbname) {
|
||
|
return dbname + "/CURRENT";
|
||
|
}
|
||
|
|
||
|
std::string LockFileName(const std::string& dbname) {
|
||
|
return dbname + "/LOCK";
|
||
|
}
|
||
|
|
||
|
std::string TempFileName(const std::string& dbname, uint64_t number) {
|
||
9 years ago
|
return MakeFileName(dbname, number, kTempFileNameSuffix.c_str());
|
||
14 years ago
|
}
|
||
|
|
||
11 years ago
|
InfoLogPrefix::InfoLogPrefix(bool has_log_dir,
|
||
|
const std::string& db_absolute_path) {
|
||
|
if (!has_log_dir) {
|
||
|
const char kInfoLogPrefix[] = "LOG";
|
||
|
// "\0" is automatically added to the end
|
||
|
snprintf(buf, sizeof(buf), kInfoLogPrefix);
|
||
|
prefix = Slice(buf, sizeof(kInfoLogPrefix) - 1);
|
||
|
} else {
|
||
|
size_t len = GetInfoLogPrefix(db_absolute_path, buf, sizeof(buf));
|
||
|
prefix = Slice(buf, len);
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
std::string InfoLogFileName(const std::string& dbname,
|
||
|
const std::string& db_path, const std::string& log_dir) {
|
||
9 years ago
|
if (log_dir.empty()) {
|
||
12 years ago
|
return dbname + "/LOG";
|
||
9 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
InfoLogPrefix info_log_prefix(true, db_path);
|
||
|
return log_dir + "/" + info_log_prefix.buf;
|
||
14 years ago
|
}
|
||
|
|
||
|
// Return the name of the old info log file for "dbname".
|
||
12 years ago
|
std::string OldInfoLogFileName(const std::string& dbname, uint64_t ts,
|
||
|
const std::string& db_path, const std::string& log_dir) {
|
||
13 years ago
|
char buf[50];
|
||
|
snprintf(buf, sizeof(buf), "%llu", static_cast<unsigned long long>(ts));
|
||
12 years ago
|
|
||
9 years ago
|
if (log_dir.empty()) {
|
||
12 years ago
|
return dbname + "/LOG.old." + buf;
|
||
9 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
InfoLogPrefix info_log_prefix(true, db_path);
|
||
|
return log_dir + "/" + info_log_prefix.buf + ".old." + buf;
|
||
14 years ago
|
}
|
||
|
|
||
9 years ago
|
std::string OptionsFileName(const std::string& dbname, uint64_t file_num) {
|
||
|
char buffer[256];
|
||
|
snprintf(buffer, sizeof(buffer), "%s%06" PRIu64,
|
||
|
kOptionsFileNamePrefix.c_str(), file_num);
|
||
|
return dbname + "/" + buffer;
|
||
|
}
|
||
|
|
||
|
std::string TempOptionsFileName(const std::string& dbname, uint64_t file_num) {
|
||
|
char buffer[256];
|
||
|
snprintf(buffer, sizeof(buffer), "%s%06" PRIu64 ".%s",
|
||
|
kOptionsFileNamePrefix.c_str(), file_num,
|
||
|
kTempFileNameSuffix.c_str());
|
||
|
return dbname + "/" + buffer;
|
||
|
}
|
||
|
|
||
12 years ago
|
std::string MetaDatabaseName(const std::string& dbname, uint64_t number) {
|
||
|
char buf[100];
|
||
|
snprintf(buf, sizeof(buf), "/METADB-%llu",
|
||
|
static_cast<unsigned long long>(number));
|
||
|
return dbname + buf;
|
||
|
}
|
||
14 years ago
|
|
||
11 years ago
|
std::string IdentityFileName(const std::string& dbname) {
|
||
|
return dbname + "/IDENTITY";
|
||
|
}
|
||
|
|
||
14 years ago
|
// Owned filenames have the form:
|
||
11 years ago
|
// dbname/IDENTITY
|
||
14 years ago
|
// dbname/CURRENT
|
||
|
// dbname/LOCK
|
||
11 years ago
|
// dbname/<info_log_name_prefix>
|
||
|
// dbname/<info_log_name_prefix>.old.[0-9]+
|
||
14 years ago
|
// dbname/MANIFEST-[0-9]+
|
||
|
// dbname/[0-9]+.(log|sst)
|
||
12 years ago
|
// dbname/METADB-[0-9]+
|
||
9 years ago
|
// dbname/OPTIONS-[0-9]+
|
||
|
// dbname/OPTIONS-[0-9]+.dbtmp
|
||
11 years ago
|
// Disregards / at the beginning
|
||
14 years ago
|
bool ParseFileName(const std::string& fname,
|
||
|
uint64_t* number,
|
||
11 years ago
|
FileType* type,
|
||
|
WalFileType* log_type) {
|
||
11 years ago
|
return ParseFileName(fname, number, "", type, log_type);
|
||
|
}
|
||
|
|
||
|
bool ParseFileName(const std::string& fname, uint64_t* number,
|
||
|
const Slice& info_log_name_prefix, FileType* type,
|
||
|
WalFileType* log_type) {
|
||
14 years ago
|
Slice rest(fname);
|
||
11 years ago
|
if (fname.length() > 1 && fname[0] == '/') {
|
||
|
rest.remove_prefix(1);
|
||
|
}
|
||
11 years ago
|
if (rest == "IDENTITY") {
|
||
|
*number = 0;
|
||
|
*type = kIdentityFile;
|
||
|
} else if (rest == "CURRENT") {
|
||
14 years ago
|
*number = 0;
|
||
|
*type = kCurrentFile;
|
||
|
} else if (rest == "LOCK") {
|
||
|
*number = 0;
|
||
|
*type = kDBLockFile;
|
||
11 years ago
|
} else if (info_log_name_prefix.size() > 0 &&
|
||
|
rest.starts_with(info_log_name_prefix)) {
|
||
|
rest.remove_prefix(info_log_name_prefix.size());
|
||
|
if (rest == "" || rest == ".old") {
|
||
|
*number = 0;
|
||
|
*type = kInfoLogFile;
|
||
|
} else if (rest.starts_with(".old.")) {
|
||
|
uint64_t ts_suffix;
|
||
|
// sizeof also counts the trailing '\0'.
|
||
|
rest.remove_prefix(sizeof(".old.") - 1);
|
||
|
if (!ConsumeDecimalNumber(&rest, &ts_suffix)) {
|
||
|
return false;
|
||
|
}
|
||
|
*number = ts_suffix;
|
||
|
*type = kInfoLogFile;
|
||
13 years ago
|
}
|
||
14 years ago
|
} 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;
|
||
12 years ago
|
} else if (rest.starts_with("METADB-")) {
|
||
|
rest.remove_prefix(strlen("METADB-"));
|
||
|
uint64_t num;
|
||
|
if (!ConsumeDecimalNumber(&rest, &num)) {
|
||
|
return false;
|
||
|
}
|
||
|
if (!rest.empty()) {
|
||
|
return false;
|
||
|
}
|
||
|
*type = kMetaDatabase;
|
||
|
*number = num;
|
||
9 years ago
|
} else if (rest.starts_with(kOptionsFileNamePrefix)) {
|
||
|
uint64_t ts_suffix;
|
||
|
bool is_temp_file = false;
|
||
|
rest.remove_prefix(kOptionsFileNamePrefix.size());
|
||
|
const std::string kTempFileNameSuffixWithDot =
|
||
|
std::string(".") + kTempFileNameSuffix;
|
||
|
if (rest.ends_with(kTempFileNameSuffixWithDot)) {
|
||
|
rest.remove_suffix(kTempFileNameSuffixWithDot.size());
|
||
|
is_temp_file = true;
|
||
|
}
|
||
|
if (!ConsumeDecimalNumber(&rest, &ts_suffix)) {
|
||
|
return false;
|
||
|
}
|
||
|
*number = ts_suffix;
|
||
|
*type = is_temp_file ? kTempFile : kOptionsFile;
|
||
14 years ago
|
} else {
|
||
|
// Avoid strtoull() to keep filename format independent of the
|
||
|
// current locale
|
||
11 years ago
|
bool archive_dir_found = false;
|
||
|
if (rest.starts_with(ARCHIVAL_DIR)) {
|
||
|
if (rest.size() <= ARCHIVAL_DIR.size()) {
|
||
|
return false;
|
||
|
}
|
||
|
rest.remove_prefix(ARCHIVAL_DIR.size() + 1); // Add 1 to remove / also
|
||
|
if (log_type) {
|
||
|
*log_type = kArchivedLogFile;
|
||
|
}
|
||
|
archive_dir_found = true;
|
||
|
}
|
||
14 years ago
|
uint64_t num;
|
||
|
if (!ConsumeDecimalNumber(&rest, &num)) {
|
||
|
return false;
|
||
|
}
|
||
9 years ago
|
if (rest.size() <= 1 || rest[0] != '.') {
|
||
|
return false;
|
||
|
}
|
||
|
rest.remove_prefix(1);
|
||
|
|
||
14 years ago
|
Slice suffix = rest;
|
||
9 years ago
|
if (suffix == Slice("log")) {
|
||
14 years ago
|
*type = kLogFile;
|
||
11 years ago
|
if (log_type && !archive_dir_found) {
|
||
|
*log_type = kAliveLogFile;
|
||
|
}
|
||
|
} else if (archive_dir_found) {
|
||
|
return false; // Archive dir can contain only log files
|
||
9 years ago
|
} else if (suffix == Slice(kRocksDbTFileExt) ||
|
||
|
suffix == Slice(kLevelDbTFileExt)) {
|
||
14 years ago
|
*type = kTableFile;
|
||
9 years ago
|
} else if (suffix == Slice(kTempFileNameSuffix)) {
|
||
14 years ago
|
*type = kTempFile;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
*number = num;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
Status SetCurrentFile(Env* env, const std::string& dbname,
|
||
11 years ago
|
uint64_t descriptor_number,
|
||
|
Directory* directory_to_fsync) {
|
||
14 years ago
|
// 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);
|
||
11 years ago
|
Status s = WriteStringToFile(env, contents.ToString() + "\n", tmp, true);
|
||
14 years ago
|
if (s.ok()) {
|
||
9 years ago
|
TEST_KILL_RANDOM("SetCurrentFile:0", rocksdb_kill_odds * REDUCE_ODDS2);
|
||
14 years ago
|
s = env->RenameFile(tmp, CurrentFileName(dbname));
|
||
9 years ago
|
TEST_KILL_RANDOM("SetCurrentFile:1", rocksdb_kill_odds * REDUCE_ODDS2);
|
||
14 years ago
|
}
|
||
11 years ago
|
if (s.ok()) {
|
||
|
if (directory_to_fsync != nullptr) {
|
||
|
directory_to_fsync->Fsync();
|
||
|
}
|
||
|
} else {
|
||
14 years ago
|
env->DeleteFile(tmp);
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
11 years ago
|
Status SetIdentityFile(Env* env, const std::string& dbname) {
|
||
|
std::string id = env->GenerateUniqueId();
|
||
|
assert(!id.empty());
|
||
11 years ago
|
// Reserve the filename dbname/000000.dbtmp for the temporary identity file
|
||
|
std::string tmp = TempFileName(dbname, 0);
|
||
11 years ago
|
Status s = WriteStringToFile(env, id, tmp, true);
|
||
11 years ago
|
if (s.ok()) {
|
||
|
s = env->RenameFile(tmp, IdentityFileName(dbname));
|
||
|
}
|
||
|
if (!s.ok()) {
|
||
|
env->DeleteFile(tmp);
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
8 years ago
|
Status SyncManifest(Env* env, const ImmutableDBOptions* db_options,
|
||
10 years ago
|
WritableFileWriter* file) {
|
||
9 years ago
|
TEST_KILL_RANDOM("SyncManifest:0", rocksdb_kill_odds * REDUCE_ODDS2);
|
||
8 years ago
|
StopWatch sw(env, db_options->statistics.get(), MANIFEST_FILE_SYNC_MICROS);
|
||
|
return file->Sync(db_options->use_fsync);
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|