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).
5 //
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.
9 //
10 // File names used by DB code
11 
12 #pragma once
13 #include <stdint.h>
14 #include <unordered_map>
15 #include <string>
16 #include <vector>
17 
18 #include "options/db_options.h"
19 #include "port/port.h"
20 #include "rocksdb/file_system.h"
21 #include "rocksdb/options.h"
22 #include "rocksdb/slice.h"
23 #include "rocksdb/status.h"
24 #include "rocksdb/transaction_log.h"
25 
26 namespace ROCKSDB_NAMESPACE {
27 
28 class Env;
29 class Directory;
30 class WritableFileWriter;
31 
32 #ifdef OS_WIN
33 const char kFilePathSeparator = '\\';
34 #else
35 const char kFilePathSeparator = '/';
36 #endif
37 
38 enum FileType {
39   kLogFile,
40   kDBLockFile,
41   kTableFile,
42   kDescriptorFile,
43   kCurrentFile,
44   kTempFile,
45   kInfoLogFile,  // Either the current one, or an old one
46   kMetaDatabase,
47   kIdentityFile,
48   kOptionsFile,
49   kBlobFile
50 };
51 
52 // Return the name of the log file with the specified number
53 // in the db named by "dbname".  The result will be prefixed with
54 // "dbname".
55 extern std::string LogFileName(const std::string& dbname, uint64_t number);
56 
57 extern std::string LogFileName(uint64_t number);
58 
59 extern std::string BlobFileName(const std::string& bdirname, uint64_t number);
60 
61 extern std::string BlobFileName(const std::string& dbname,
62                                 const std::string& blob_dir, uint64_t number);
63 
64 static const std::string ARCHIVAL_DIR = "archive";
65 
66 extern std::string ArchivalDirectory(const std::string& dbname);
67 
68 //  Return the name of the archived log file with the specified number
69 //  in the db named by "dbname". The result will be prefixed with "dbname".
70 extern std::string ArchivedLogFileName(const std::string& dbname,
71                                        uint64_t num);
72 
73 extern std::string MakeTableFileName(const std::string& name, uint64_t number);
74 
75 extern std::string MakeTableFileName(uint64_t number);
76 
77 // Return the name of sstable with LevelDB suffix
78 // created from RocksDB sstable suffixed name
79 extern std::string Rocks2LevelTableFileName(const std::string& fullname);
80 
81 // the reverse function of MakeTableFileName
82 // TODO(yhchiang): could merge this function with ParseFileName()
83 extern uint64_t TableFileNameToNumber(const std::string& name);
84 
85 // Return the name of the sstable with the specified number
86 // in the db named by "dbname".  The result will be prefixed with
87 // "dbname".
88 extern std::string TableFileName(const std::vector<DbPath>& db_paths,
89                                  uint64_t number, uint32_t path_id);
90 
91 // Sufficient buffer size for FormatFileNumber.
92 const size_t kFormatFileNumberBufSize = 38;
93 
94 extern void FormatFileNumber(uint64_t number, uint32_t path_id, char* out_buf,
95                              size_t out_buf_size);
96 
97 // Return the name of the descriptor file for the db named by
98 // "dbname" and the specified incarnation number.  The result will be
99 // prefixed with "dbname".
100 extern std::string DescriptorFileName(const std::string& dbname,
101                                       uint64_t number);
102 
103 // Return the name of the current file.  This file contains the name
104 // of the current manifest file.  The result will be prefixed with
105 // "dbname".
106 extern std::string CurrentFileName(const std::string& dbname);
107 
108 // Return the name of the lock file for the db named by
109 // "dbname".  The result will be prefixed with "dbname".
110 extern std::string LockFileName(const std::string& dbname);
111 
112 // Return the name of a temporary file owned by the db named "dbname".
113 // The result will be prefixed with "dbname".
114 extern std::string TempFileName(const std::string& dbname, uint64_t number);
115 
116 // A helper structure for prefix of info log names.
117 struct InfoLogPrefix {
118   char buf[260];
119   Slice prefix;
120   // Prefix with DB absolute path encoded
121   explicit InfoLogPrefix(bool has_log_dir, const std::string& db_absolute_path);
122   // Default Prefix
123   explicit InfoLogPrefix();
124 };
125 
126 // Return the name of the info log file for "dbname".
127 extern std::string InfoLogFileName(const std::string& dbname,
128                                    const std::string& db_path = "",
129                                    const std::string& log_dir = "");
130 
131 // Return the name of the old info log file for "dbname".
132 extern std::string OldInfoLogFileName(const std::string& dbname, uint64_t ts,
133                                       const std::string& db_path = "",
134                                       const std::string& log_dir = "");
135 
136 static const std::string kOptionsFileNamePrefix = "OPTIONS-";
137 static const std::string kTempFileNameSuffix = "dbtmp";
138 
139 // Return a options file name given the "dbname" and file number.
140 // Format:  OPTIONS-[number].dbtmp
141 extern std::string OptionsFileName(const std::string& dbname,
142                                    uint64_t file_num);
143 
144 // Return a temp options file name given the "dbname" and file number.
145 // Format:  OPTIONS-[number]
146 extern std::string TempOptionsFileName(const std::string& dbname,
147                                        uint64_t file_num);
148 
149 // Return the name to use for a metadatabase. The result will be prefixed with
150 // "dbname".
151 extern std::string MetaDatabaseName(const std::string& dbname,
152                                     uint64_t number);
153 
154 // Return the name of the Identity file which stores a unique number for the db
155 // that will get regenerated if the db loses all its data and is recreated fresh
156 // either from a backup-image or empty
157 extern std::string IdentityFileName(const std::string& dbname);
158 
159 // If filename is a rocksdb file, store the type of the file in *type.
160 // The number encoded in the filename is stored in *number.  If the
161 // filename was successfully parsed, returns true.  Else return false.
162 // info_log_name_prefix is the path of info logs.
163 extern bool ParseFileName(const std::string& filename, uint64_t* number,
164                           const Slice& info_log_name_prefix, FileType* type,
165                           WalFileType* log_type = nullptr);
166 // Same as previous function, but skip info log files.
167 extern bool ParseFileName(const std::string& filename, uint64_t* number,
168                           FileType* type, WalFileType* log_type = nullptr);
169 
170 // Make the CURRENT file point to the descriptor file with the
171 // specified number.
172 extern IOStatus SetCurrentFile(FileSystem* fs, const std::string& dbname,
173                                uint64_t descriptor_number,
174                                FSDirectory* directory_to_fsync);
175 
176 // Make the IDENTITY file for the db
177 extern Status SetIdentityFile(Env* env, const std::string& dbname,
178                               const std::string& db_id = {});
179 
180 // Sync manifest file `file`.
181 extern IOStatus SyncManifest(Env* env, const ImmutableDBOptions* db_options,
182                              WritableFileWriter* file);
183 
184 // Return list of file names of info logs in `file_names`.
185 // The list only contains file name. The parent directory name is stored
186 // in `parent_dir`.
187 // `db_log_dir` should be the one as in options.db_log_dir
188 extern Status GetInfoLogFiles(Env* env, const std::string& db_log_dir,
189                               const std::string& dbname,
190                               std::string* parent_dir,
191                               std::vector<std::string>* file_names);
192 
193 extern std::string NormalizePath(const std::string& path);
194 }  // namespace ROCKSDB_NAMESPACE
195