10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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