1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
10 // File names used by DB code
16 #include <unordered_map>
19 #include "options/db_options.h"
20 #include "port/port.h"
21 #include "rocksdb/file_system.h"
22 #include "rocksdb/options.h"
23 #include "rocksdb/slice.h"
24 #include "rocksdb/status.h"
25 #include "rocksdb/transaction_log.h"
27 namespace ROCKSDB_NAMESPACE
{
32 class WritableFileWriter
;
35 constexpr char kFilePathSeparator
= '\\';
37 constexpr char kFilePathSeparator
= '/';
40 // Return the name of the log file with the specified number
41 // in the db named by "dbname". The result will be prefixed with
43 extern std::string
LogFileName(const std::string
& dbname
, uint64_t number
);
45 extern std::string
LogFileName(uint64_t number
);
47 extern std::string
BlobFileName(uint64_t number
);
49 extern std::string
BlobFileName(const std::string
& bdirname
, uint64_t number
);
51 extern std::string
BlobFileName(const std::string
& dbname
,
52 const std::string
& blob_dir
, uint64_t number
);
54 extern std::string
ArchivalDirectory(const std::string
& dbname
);
56 // Return the name of the archived log file with the specified number
57 // in the db named by "dbname". The result will be prefixed with "dbname".
58 extern std::string
ArchivedLogFileName(const std::string
& dbname
, uint64_t num
);
60 extern std::string
MakeTableFileName(const std::string
& name
, uint64_t number
);
62 extern std::string
MakeTableFileName(uint64_t number
);
64 // Return the name of sstable with LevelDB suffix
65 // created from RocksDB sstable suffixed name
66 extern std::string
Rocks2LevelTableFileName(const std::string
& fullname
);
68 // the reverse function of MakeTableFileName
69 // TODO(yhchiang): could merge this function with ParseFileName()
70 extern uint64_t TableFileNameToNumber(const std::string
& name
);
72 // Return the name of the sstable with the specified number
73 // in the db named by "dbname". The result will be prefixed with
75 extern std::string
TableFileName(const std::vector
<DbPath
>& db_paths
,
76 uint64_t number
, uint32_t path_id
);
78 // Sufficient buffer size for FormatFileNumber.
79 const size_t kFormatFileNumberBufSize
= 38;
81 extern void FormatFileNumber(uint64_t number
, uint32_t path_id
, char* out_buf
,
84 // Return the name of the descriptor file for the db named by
85 // "dbname" and the specified incarnation number. The result will be
86 // prefixed with "dbname".
87 extern std::string
DescriptorFileName(const std::string
& dbname
,
90 extern std::string
DescriptorFileName(uint64_t number
);
92 extern const std::string kCurrentFileName
; // = "CURRENT"
94 // Return the name of the current file. This file contains the name
95 // of the current manifest file. The result will be prefixed with
97 extern std::string
CurrentFileName(const std::string
& dbname
);
99 // Return the name of the lock file for the db named by
100 // "dbname". The result will be prefixed with "dbname".
101 extern std::string
LockFileName(const std::string
& dbname
);
103 // Return the name of a temporary file owned by the db named "dbname".
104 // The result will be prefixed with "dbname".
105 extern std::string
TempFileName(const std::string
& dbname
, uint64_t number
);
107 // A helper structure for prefix of info log names.
108 struct InfoLogPrefix
{
111 // Prefix with DB absolute path encoded
112 explicit InfoLogPrefix(bool has_log_dir
, const std::string
& db_absolute_path
);
114 explicit InfoLogPrefix();
117 // Return the name of the info log file for "dbname".
118 extern std::string
InfoLogFileName(const std::string
& dbname
,
119 const std::string
& db_path
= "",
120 const std::string
& log_dir
= "");
122 // Return the name of the old info log file for "dbname".
123 extern std::string
OldInfoLogFileName(const std::string
& dbname
, uint64_t ts
,
124 const std::string
& db_path
= "",
125 const std::string
& log_dir
= "");
127 extern const std::string kOptionsFileNamePrefix
; // = "OPTIONS-"
128 extern const std::string kTempFileNameSuffix
; // = "dbtmp"
130 // Return a options file name given the "dbname" and file number.
131 // Format: OPTIONS-[number].dbtmp
132 extern std::string
OptionsFileName(const std::string
& dbname
,
134 extern std::string
OptionsFileName(uint64_t file_num
);
136 // Return a temp options file name given the "dbname" and file number.
137 // Format: OPTIONS-[number]
138 extern std::string
TempOptionsFileName(const std::string
& dbname
,
141 // Return the name to use for a metadatabase. The result will be prefixed with
143 extern std::string
MetaDatabaseName(const std::string
& dbname
, uint64_t number
);
145 // Return the name of the Identity file which stores a unique number for the db
146 // that will get regenerated if the db loses all its data and is recreated fresh
147 // either from a backup-image or empty
148 extern std::string
IdentityFileName(const std::string
& dbname
);
150 // If filename is a rocksdb file, store the type of the file in *type.
151 // The number encoded in the filename is stored in *number. If the
152 // filename was successfully parsed, returns true. Else return false.
153 // info_log_name_prefix is the path of info logs.
154 extern bool ParseFileName(const std::string
& filename
, uint64_t* number
,
155 const Slice
& info_log_name_prefix
, FileType
* type
,
156 WalFileType
* log_type
= nullptr);
157 // Same as previous function, but skip info log files.
158 extern bool ParseFileName(const std::string
& filename
, uint64_t* number
,
159 FileType
* type
, WalFileType
* log_type
= nullptr);
161 // Make the CURRENT file point to the descriptor file with the
162 // specified number. On its success and when dir_contains_current_file is not
163 // nullptr, the function will fsync the directory containing the CURRENT file
165 extern IOStatus
SetCurrentFile(FileSystem
* fs
, const std::string
& dbname
,
166 uint64_t descriptor_number
,
167 FSDirectory
* dir_contains_current_file
);
169 // Make the IDENTITY file for the db
170 extern Status
SetIdentityFile(Env
* env
, const std::string
& dbname
,
171 const std::string
& db_id
= {});
173 // Sync manifest file `file`.
174 extern IOStatus
SyncManifest(const ImmutableDBOptions
* db_options
,
175 WritableFileWriter
* file
);
177 // Return list of file names of info logs in `file_names`.
178 // The list only contains file name. The parent directory name is stored
180 // `db_log_dir` should be the one as in options.db_log_dir
181 extern Status
GetInfoLogFiles(const std::shared_ptr
<FileSystem
>& fs
,
182 const std::string
& db_log_dir
,
183 const std::string
& dbname
,
184 std::string
* parent_dir
,
185 std::vector
<std::string
>* file_names
);
187 extern std::string
NormalizePath(const std::string
& path
);
188 } // namespace ROCKSDB_NAMESPACE