小组成员:谢瑞阳、徐翔宇
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

293 righe
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 "include/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. // Return the length of this file in bytes.
  143. virtual uint64_t Size() const = 0;
  144. // Read up to "n" bytes from the file starting at "offset".
  145. // "scratch[0..n-1]" may be written by this routine. Sets "*result"
  146. // to the data that was read (including if fewer than "n" bytes were
  147. // successfully read). If an error was encountered, returns a
  148. // non-OK status.
  149. //
  150. // Safe for concurrent use by multiple threads.
  151. virtual Status Read(uint64_t offset, size_t n, Slice* result,
  152. char* scratch) const = 0;
  153. };
  154. // A file abstraction for sequential writing. The implementation
  155. // must provide buffering since callers may append small fragments
  156. // at a time to the file.
  157. class WritableFile {
  158. public:
  159. WritableFile() { }
  160. virtual ~WritableFile();
  161. virtual Status Append(const Slice& data) = 0;
  162. virtual Status Close() = 0;
  163. virtual Status Flush() = 0;
  164. virtual Status Sync() = 0;
  165. private:
  166. // No copying allowed
  167. WritableFile(const WritableFile&);
  168. void operator=(const WritableFile&);
  169. };
  170. // Identifies a locked file.
  171. class FileLock {
  172. public:
  173. FileLock() { }
  174. virtual ~FileLock();
  175. private:
  176. // No copying allowed
  177. FileLock(const FileLock&);
  178. void operator=(const FileLock&);
  179. };
  180. // Log the specified data to *info_log if info_log is non-NULL.
  181. extern void Log(Env* env, WritableFile* info_log, const char* format, ...)
  182. # if defined(__GNUC__) || defined(__clang__)
  183. __attribute__((__format__ (__printf__, 3, 4)))
  184. # endif
  185. ;
  186. // A utility routine: write "data" to the named file.
  187. extern Status WriteStringToFile(Env* env, const Slice& data,
  188. const std::string& fname);
  189. // A utility routine: read contents of named file into *data
  190. extern Status ReadFileToString(Env* env, const std::string& fname,
  191. std::string* data);
  192. // An implementation of Env that forwards all calls to another Env.
  193. // May be useful to clients who wish to override just part of the
  194. // functionality of another Env.
  195. class EnvWrapper : public Env {
  196. public:
  197. // Initialize an EnvWrapper that delegates all calls to *target
  198. explicit EnvWrapper(Env* target) : target_(target) { }
  199. virtual ~EnvWrapper();
  200. // Return the target to which this Env forwards all calls
  201. Env* target() const { return target_; }
  202. // The following text is boilerplate that forwards all methods to target()
  203. Status NewSequentialFile(const std::string& f, SequentialFile** r) {
  204. return target_->NewSequentialFile(f, r);
  205. }
  206. Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
  207. return target_->NewRandomAccessFile(f, r);
  208. }
  209. Status NewWritableFile(const std::string& f, WritableFile** r) {
  210. return target_->NewWritableFile(f, r);
  211. }
  212. bool FileExists(const std::string& f) { return target_->FileExists(f); }
  213. Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
  214. return target_->GetChildren(dir, r);
  215. }
  216. Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
  217. Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
  218. Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
  219. Status GetFileSize(const std::string& f, uint64_t* s) {
  220. return target_->GetFileSize(f, s);
  221. }
  222. Status RenameFile(const std::string& s, const std::string& t) {
  223. return target_->RenameFile(s, t);
  224. }
  225. Status LockFile(const std::string& f, FileLock** l) {
  226. return target_->LockFile(f, l);
  227. }
  228. Status UnlockFile(FileLock* l) { return target_->UnlockFile(l); }
  229. void Schedule(void (*f)(void*), void* a) {
  230. return target_->Schedule(f, a);
  231. }
  232. void StartThread(void (*f)(void*), void* a) {
  233. return target_->StartThread(f, a);
  234. }
  235. virtual Status GetTestDirectory(std::string* path) {
  236. return target_->GetTestDirectory(path);
  237. }
  238. virtual void Logv(WritableFile* log, const char* format, va_list ap) {
  239. return target_->Logv(log, format, ap);
  240. }
  241. uint64_t NowMicros() {
  242. return target_->NowMicros();
  243. }
  244. void SleepForMicroseconds(int micros) {
  245. target_->SleepForMicroseconds(micros);
  246. }
  247. private:
  248. Env* target_;
  249. };
  250. }
  251. #endif // STORAGE_LEVELDB_INCLUDE_ENV_H_