小组成员:姚凯文(kevinyao0901),姜嘉琪
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.

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