filename.h 7.9 KB

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