filename.h 7.3 KB

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