作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

290 řádky
10 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. //
  5. // An Env is an interface used by the leveldb implementation to access
  6. // operating system functionality like the filesystem etc. Callers
  7. // may wish to provide a custom Env object when opening a database to
  8. // get fine gain control; e.g., to rate limit file system operations.
  9. #ifndef STORAGE_LEVELDB_INCLUDE_ENV_H_
  10. #define STORAGE_LEVELDB_INCLUDE_ENV_H_
  11. #include <cstdarg>
  12. #include <string>
  13. #include <vector>
  14. #include <stdint.h>
  15. #include "leveldb/status.h"
  16. namespace leveldb {
  17. class FileLock;
  18. class RandomAccessFile;
  19. class SequentialFile;
  20. class Slice;
  21. class WritableFile;
  22. class Env {
  23. public:
  24. Env() { }
  25. virtual ~Env();
  26. // Return a default environment suitable for the current operating
  27. // system. Sophisticated users may wish to provide their own Env
  28. // implementation instead of relying on this default environment.
  29. //
  30. // The result of Default() belongs to leveldb and must never be deleted.
  31. static Env* Default();
  32. // Create a brand new sequentially-readable file with the specified name.
  33. // On success, stores a pointer to the new file in *result and returns OK.
  34. // On failure stores NULL in *result and returns non-OK. If the file does
  35. // not exist, returns a non-OK status.
  36. //
  37. // The returned file will only be accessed by one thread at a time.
  38. virtual Status NewSequentialFile(const std::string& fname,
  39. SequentialFile** result) = 0;
  40. // Create a brand new random access read-only file with the
  41. // specified name. On success, stores a pointer to the new file in
  42. // *result and returns OK. On failure stores NULL in *result and
  43. // returns non-OK. If the file does not exist, returns a non-OK
  44. // status.
  45. //
  46. // The returned file may be concurrently accessed by multiple threads.
  47. virtual Status NewRandomAccessFile(const std::string& fname,
  48. RandomAccessFile** result) = 0;
  49. // Create an object that writes to a new file with the specified
  50. // name. Deletes any existing file with the same name and creates a
  51. // new file. On success, stores a pointer to the new file in
  52. // *result and returns OK. On failure stores NULL in *result and
  53. // returns non-OK.
  54. //
  55. // The returned file will only be accessed by one thread at a time.
  56. virtual Status NewWritableFile(const std::string& fname,
  57. WritableFile** result) = 0;
  58. // Returns true iff the named file exists.
  59. virtual bool FileExists(const std::string& fname) = 0;
  60. // Store in *result the names of the children of the specified directory.
  61. // The names are relative to "dir".
  62. // Original contents of *results are dropped.
  63. virtual Status GetChildren(const std::string& dir,
  64. std::vector<std::string>* result) = 0;
  65. // Delete the named file.
  66. virtual Status DeleteFile(const std::string& fname) = 0;
  67. // Create the specified directory.
  68. virtual Status CreateDir(const std::string& dirname) = 0;
  69. // Delete the specified directory.
  70. virtual Status DeleteDir(const std::string& dirname) = 0;
  71. // Store the size of fname in *file_size.
  72. virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) = 0;
  73. // Rename file src to target.
  74. virtual Status RenameFile(const std::string& src,
  75. const std::string& target) = 0;
  76. // Lock the specified file. Used to prevent concurrent access to
  77. // the same db by multiple processes. On failure, stores NULL in
  78. // *lock and returns non-OK.
  79. //
  80. // On success, stores a pointer to the object that represents the
  81. // acquired lock in *lock and returns OK. The caller should call
  82. // UnlockFile(*lock) to release the lock. If the process exits,
  83. // the lock will be automatically released.
  84. //
  85. // If somebody else already holds the lock, finishes immediately
  86. // with a failure. I.e., this call does not wait for existing locks
  87. // to go away.
  88. //
  89. // May create the named file if it does not already exist.
  90. virtual Status LockFile(const std::string& fname, FileLock** lock) = 0;
  91. // Release the lock acquired by a previous successful call to LockFile.
  92. // REQUIRES: lock was returned by a successful LockFile() call
  93. // REQUIRES: lock has not already been unlocked.
  94. virtual Status UnlockFile(FileLock* lock) = 0;
  95. // Arrange to run "(*function)(arg)" once in a background thread.
  96. //
  97. // "function" may run in an unspecified thread. Multiple functions
  98. // added to the same Env may run concurrently in different threads.
  99. // I.e., the caller may not assume that background work items are
  100. // serialized.
  101. virtual void Schedule(
  102. void (*function)(void* arg),
  103. void* arg) = 0;
  104. // Start a new thread, invoking "function(arg)" within the new thread.
  105. // When "function(arg)" returns, the thread will be destroyed.
  106. virtual void StartThread(void (*function)(void* arg), void* arg) = 0;
  107. // *path is set to a temporary directory that can be used for testing. It may
  108. // or many not have just been created. The directory may or may not differ
  109. // between runs of the same process, but subsequent calls will return the
  110. // same directory.
  111. virtual Status GetTestDirectory(std::string* path) = 0;
  112. // Write an entry to the log file with the specified format.
  113. virtual void Logv(WritableFile* log, const char* format, va_list ap) = 0;
  114. // Returns the number of micro-seconds since some fixed point in time. Only
  115. // useful for computing deltas of time.
  116. virtual uint64_t NowMicros() = 0;
  117. // Sleep/delay the thread for the perscribed number of micro-seconds.
  118. virtual void SleepForMicroseconds(int micros) = 0;
  119. private:
  120. // No copying allowed
  121. Env(const Env&);
  122. void operator=(const Env&);
  123. };
  124. // A file abstraction for reading sequentially through a file
  125. class SequentialFile {
  126. public:
  127. SequentialFile() { }
  128. virtual ~SequentialFile();
  129. // Read up to "n" bytes from the file. "scratch[0..n-1]" may be
  130. // written by this routine. Sets "*result" to the data that was
  131. // read (including if fewer than "n" bytes were successfully read).
  132. // If an error was encountered, returns a non-OK status.
  133. //
  134. // REQUIRES: External synchronization
  135. virtual Status Read(size_t n, Slice* result, char* scratch) = 0;
  136. };
  137. // A file abstraction for randomly reading the contents of a file.
  138. class RandomAccessFile {
  139. public:
  140. RandomAccessFile() { }
  141. virtual ~RandomAccessFile();
  142. // Read up to "n" bytes from the file starting at "offset".
  143. // "scratch[0..n-1]" may be written by this routine. Sets "*result"
  144. // to the data that was read (including if fewer than "n" bytes were
  145. // successfully read). If an error was encountered, returns a
  146. // non-OK status.
  147. //
  148. // Safe for concurrent use by multiple threads.
  149. virtual Status Read(uint64_t offset, size_t n, Slice* result,
  150. char* scratch) const = 0;
  151. };
  152. // A file abstraction for sequential writing. The implementation
  153. // must provide buffering since callers may append small fragments
  154. // at a time to the file.
  155. class WritableFile {
  156. public:
  157. WritableFile() { }
  158. virtual ~WritableFile();
  159. virtual Status Append(const Slice& data) = 0;
  160. virtual Status Close() = 0;
  161. virtual Status Flush() = 0;
  162. virtual Status Sync() = 0;
  163. private:
  164. // No copying allowed
  165. WritableFile(const WritableFile&);
  166. void operator=(const WritableFile&);
  167. };
  168. // Identifies a locked file.
  169. class FileLock {
  170. public:
  171. FileLock() { }
  172. virtual ~FileLock();
  173. private:
  174. // No copying allowed
  175. FileLock(const FileLock&);
  176. void operator=(const FileLock&);
  177. };
  178. // Log the specified data to *info_log if info_log is non-NULL.
  179. extern void Log(Env* env, WritableFile* info_log, const char* format, ...)
  180. # if defined(__GNUC__) || defined(__clang__)
  181. __attribute__((__format__ (__printf__, 3, 4)))
  182. # endif
  183. ;
  184. // A utility routine: write "data" to the named file.
  185. extern Status WriteStringToFile(Env* env, const Slice& data,
  186. const std::string& fname);
  187. // A utility routine: read contents of named file into *data
  188. extern Status ReadFileToString(Env* env, const std::string& fname,
  189. std::string* data);
  190. // An implementation of Env that forwards all calls to another Env.
  191. // May be useful to clients who wish to override just part of the
  192. // functionality of another Env.
  193. class EnvWrapper : public Env {
  194. public:
  195. // Initialize an EnvWrapper that delegates all calls to *target
  196. explicit EnvWrapper(Env* target) : target_(target) { }
  197. virtual ~EnvWrapper();
  198. // Return the target to which this Env forwards all calls
  199. Env* target() const { return target_; }
  200. // The following text is boilerplate that forwards all methods to target()
  201. Status NewSequentialFile(const std::string& f, SequentialFile** r) {
  202. return target_->NewSequentialFile(f, r);
  203. }
  204. Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
  205. return target_->NewRandomAccessFile(f, r);
  206. }
  207. Status NewWritableFile(const std::string& f, WritableFile** r) {
  208. return target_->NewWritableFile(f, r);
  209. }
  210. bool FileExists(const std::string& f) { return target_->FileExists(f); }
  211. Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
  212. return target_->GetChildren(dir, r);
  213. }
  214. Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
  215. Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
  216. Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
  217. Status GetFileSize(const std::string& f, uint64_t* s) {
  218. return target_->GetFileSize(f, s);
  219. }
  220. Status RenameFile(const std::string& s, const std::string& t) {
  221. return target_->RenameFile(s, t);
  222. }
  223. Status LockFile(const std::string& f, FileLock** l) {
  224. return target_->LockFile(f, l);
  225. }
  226. Status UnlockFile(FileLock* l) { return target_->UnlockFile(l); }
  227. void Schedule(void (*f)(void*), void* a) {
  228. return target_->Schedule(f, a);
  229. }
  230. void StartThread(void (*f)(void*), void* a) {
  231. return target_->StartThread(f, a);
  232. }
  233. virtual Status GetTestDirectory(std::string* path) {
  234. return target_->GetTestDirectory(path);
  235. }
  236. virtual void Logv(WritableFile* log, const char* format, va_list ap) {
  237. return target_->Logv(log, format, ap);
  238. }
  239. uint64_t NowMicros() {
  240. return target_->NowMicros();
  241. }
  242. void SleepForMicroseconds(int micros) {
  243. target_->SleepForMicroseconds(micros);
  244. }
  245. private:
  246. Env* target_;
  247. };
  248. }
  249. #endif // STORAGE_LEVELDB_INCLUDE_ENV_H_