作者: 韩晨旭 10225101440 李畅 10225102463
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.

333 regels
12 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. // May set "*result" to point at data in "scratch[0..n-1]", so
  137. // "scratch[0..n-1]" must be live when "*result" is used.
  138. // If an error was encountered, returns a non-OK status.
  139. //
  140. // REQUIRES: External synchronization
  141. virtual Status Read(size_t n, Slice* result, char* scratch) = 0;
  142. // Skip "n" bytes from the file. This is guaranteed to be no
  143. // slower that reading the same data, but may be faster.
  144. //
  145. // If end of file is reached, skipping will stop at the end of the
  146. // file, and Skip will return OK.
  147. //
  148. // REQUIRES: External synchronization
  149. virtual Status Skip(uint64_t n) = 0;
  150. private:
  151. // No copying allowed
  152. SequentialFile(const SequentialFile&);
  153. void operator=(const SequentialFile&);
  154. };
  155. // A file abstraction for randomly reading the contents of a file.
  156. class RandomAccessFile {
  157. public:
  158. RandomAccessFile() { }
  159. virtual ~RandomAccessFile();
  160. // Read up to "n" bytes from the file starting at "offset".
  161. // "scratch[0..n-1]" may be written by this routine. Sets "*result"
  162. // to the data that was read (including if fewer than "n" bytes were
  163. // successfully read). May set "*result" to point at data in
  164. // "scratch[0..n-1]", so "scratch[0..n-1]" must be live when
  165. // "*result" is used. If an error was encountered, returns a non-OK
  166. // status.
  167. //
  168. // Safe for concurrent use by multiple threads.
  169. virtual Status Read(uint64_t offset, size_t n, Slice* result,
  170. char* scratch) const = 0;
  171. private:
  172. // No copying allowed
  173. RandomAccessFile(const RandomAccessFile&);
  174. void operator=(const RandomAccessFile&);
  175. };
  176. // A file abstraction for sequential writing. The implementation
  177. // must provide buffering since callers may append small fragments
  178. // at a time to the file.
  179. class WritableFile {
  180. public:
  181. WritableFile() { }
  182. virtual ~WritableFile();
  183. virtual Status Append(const Slice& data) = 0;
  184. virtual Status Close() = 0;
  185. virtual Status Flush() = 0;
  186. virtual Status Sync() = 0;
  187. private:
  188. // No copying allowed
  189. WritableFile(const WritableFile&);
  190. void operator=(const WritableFile&);
  191. };
  192. // An interface for writing log messages.
  193. class Logger {
  194. public:
  195. Logger() { }
  196. virtual ~Logger();
  197. // Write an entry to the log file with the specified format.
  198. virtual void Logv(const char* format, va_list ap) = 0;
  199. private:
  200. // No copying allowed
  201. Logger(const Logger&);
  202. void operator=(const Logger&);
  203. };
  204. // Identifies a locked file.
  205. class FileLock {
  206. public:
  207. FileLock() { }
  208. virtual ~FileLock();
  209. private:
  210. // No copying allowed
  211. FileLock(const FileLock&);
  212. void operator=(const FileLock&);
  213. };
  214. // Log the specified data to *info_log if info_log is non-NULL.
  215. extern void Log(Logger* info_log, const char* format, ...)
  216. # if defined(__GNUC__) || defined(__clang__)
  217. __attribute__((__format__ (__printf__, 2, 3)))
  218. # endif
  219. ;
  220. // A utility routine: write "data" to the named file.
  221. extern Status WriteStringToFile(Env* env, const Slice& data,
  222. const std::string& fname);
  223. // A utility routine: read contents of named file into *data
  224. extern Status ReadFileToString(Env* env, const std::string& fname,
  225. std::string* data);
  226. // An implementation of Env that forwards all calls to another Env.
  227. // May be useful to clients who wish to override just part of the
  228. // functionality of another Env.
  229. class EnvWrapper : public Env {
  230. public:
  231. // Initialize an EnvWrapper that delegates all calls to *t
  232. explicit EnvWrapper(Env* t) : target_(t) { }
  233. virtual ~EnvWrapper();
  234. // Return the target to which this Env forwards all calls
  235. Env* target() const { return target_; }
  236. // The following text is boilerplate that forwards all methods to target()
  237. Status NewSequentialFile(const std::string& f, SequentialFile** r) {
  238. return target_->NewSequentialFile(f, r);
  239. }
  240. Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
  241. return target_->NewRandomAccessFile(f, r);
  242. }
  243. Status NewWritableFile(const std::string& f, WritableFile** r) {
  244. return target_->NewWritableFile(f, r);
  245. }
  246. bool FileExists(const std::string& f) { return target_->FileExists(f); }
  247. Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
  248. return target_->GetChildren(dir, r);
  249. }
  250. Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
  251. Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
  252. Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
  253. Status GetFileSize(const std::string& f, uint64_t* s) {
  254. return target_->GetFileSize(f, s);
  255. }
  256. Status RenameFile(const std::string& s, const std::string& t) {
  257. return target_->RenameFile(s, t);
  258. }
  259. Status LockFile(const std::string& f, FileLock** l) {
  260. return target_->LockFile(f, l);
  261. }
  262. Status UnlockFile(FileLock* l) { return target_->UnlockFile(l); }
  263. void Schedule(void (*f)(void*), void* a) {
  264. return target_->Schedule(f, a);
  265. }
  266. void StartThread(void (*f)(void*), void* a) {
  267. return target_->StartThread(f, a);
  268. }
  269. virtual Status GetTestDirectory(std::string* path) {
  270. return target_->GetTestDirectory(path);
  271. }
  272. virtual Status NewLogger(const std::string& fname, Logger** result) {
  273. return target_->NewLogger(fname, result);
  274. }
  275. uint64_t NowMicros() {
  276. return target_->NowMicros();
  277. }
  278. void SleepForMicroseconds(int micros) {
  279. target_->SleepForMicroseconds(micros);
  280. }
  281. private:
  282. Env* target_;
  283. };
  284. } // namespace leveldb
  285. #endif // STORAGE_LEVELDB_INCLUDE_ENV_H_