作者: 韩晨旭 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.

353 lines
13 KiB

Release 1.18 Changes are: * Update version number to 1.18 * Replace the basic fprintf call with a call to fwrite in order to work around the apparent compiler optimization/rewrite failure that we are seeing with the new toolchain/iOS SDKs provided with Xcode6 and iOS8. * Fix ALL the header guards. * Createed a README.md with the LevelDB project description. * A new CONTRIBUTING file. * Don't implicitly convert uint64_t to size_t or int. Either preserve it as uint64_t, or explicitly cast. This fixes MSVC warnings about possible value truncation when compiling this code in Chromium. * Added a DumpFile() library function that encapsulates the guts of the "leveldbutil dump" command. This will allow clients to dump data to their log files instead of stdout. It will also allow clients to supply their own environment. * leveldb: Remove unused function 'ConsumeChar'. * leveldbutil: Remove unused member variables from WriteBatchItemPrinter. * OpenBSD, NetBSD and DragonflyBSD have _LITTLE_ENDIAN, so define PLATFORM_IS_LITTLE_ENDIAN like on FreeBSD. This fixes: * issue #143 * issue #198 * issue #249 * Switch from <cstdatomic> to <atomic>. The former never made it into the standard and doesn't exist in modern gcc versions at all. The later contains everything that leveldb was using from the former. This problem was noticed when porting to Portable Native Client where no memory barrier is defined. The fact that <cstdatomic> is missing normally goes unnoticed since memory barriers are defined for most architectures. * Make Hash() treat its input as unsigned. Before this change LevelDB files from platforms with different signedness of char were not compatible. This change fixes: issue #243 * Verify checksums of index/meta/filter blocks when paranoid_checks set. * Invoke all tools for iOS with xcrun. (This was causing problems with the new XCode 5.1.1 image on pulse.) * include <sys/stat.h> only once, and fix the following linter warning: "Found C system header after C++ system header" * When encountering a corrupted table file, return Status::Corruption instead of Status::InvalidArgument. * Support cygwin as build platform, patch is from https://code.google.com/p/leveldb/issues/detail?id=188 * Fix typo, merge patch from https://code.google.com/p/leveldb/issues/detail?id=159 * Fix typos and comments, and address the following two issues: * issue #166 * issue #241 * Add missing db synchronize after "fillseq" in the benchmark. * Removed unused variable in SeekRandom: value (issue #201)
10 years ago
  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 <string>
  15. #include <vector>
  16. #include <stdarg.h>
  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. Implementations should return a
  40. // NotFound status when the file does not exist.
  41. //
  42. // The returned file will only be accessed by one thread at a time.
  43. virtual Status NewSequentialFile(const std::string& fname,
  44. SequentialFile** result) = 0;
  45. // Create a brand new random access read-only file with the
  46. // specified name. On success, stores a pointer to the new file in
  47. // *result and returns OK. On failure stores NULL in *result and
  48. // returns non-OK. If the file does not exist, returns a non-OK
  49. // status. Implementations should return a NotFound status when the file does
  50. // not exist.
  51. //
  52. // The returned file may be concurrently accessed by multiple threads.
  53. virtual Status NewRandomAccessFile(const std::string& fname,
  54. RandomAccessFile** result) = 0;
  55. // Create an object that writes to a new file with the specified
  56. // name. Deletes any existing file with the same name and creates a
  57. // new file. On success, stores a pointer to the new file in
  58. // *result and returns OK. On failure stores NULL in *result and
  59. // returns non-OK.
  60. //
  61. // The returned file will only be accessed by one thread at a time.
  62. virtual Status NewWritableFile(const std::string& fname,
  63. WritableFile** result) = 0;
  64. // Create an object that either appends to an existing file, or
  65. // writes to a new file (if the file does not exist to begin with).
  66. // On success, stores a pointer to the new file in *result and
  67. // returns OK. On failure stores NULL in *result and returns
  68. // non-OK.
  69. //
  70. // The returned file will only be accessed by one thread at a time.
  71. //
  72. // May return an IsNotSupportedError error if this Env does
  73. // not allow appending to an existing file. Users of Env (including
  74. // the leveldb implementation) must be prepared to deal with
  75. // an Env that does not support appending.
  76. virtual Status NewAppendableFile(const std::string& fname,
  77. WritableFile** result);
  78. // Returns true iff the named file exists.
  79. virtual bool FileExists(const std::string& fname) = 0;
  80. // Store in *result the names of the children of the specified directory.
  81. // The names are relative to "dir".
  82. // Original contents of *results are dropped.
  83. virtual Status GetChildren(const std::string& dir,
  84. std::vector<std::string>* result) = 0;
  85. // Delete the named file.
  86. virtual Status DeleteFile(const std::string& fname) = 0;
  87. // Create the specified directory.
  88. virtual Status CreateDir(const std::string& dirname) = 0;
  89. // Delete the specified directory.
  90. virtual Status DeleteDir(const std::string& dirname) = 0;
  91. // Store the size of fname in *file_size.
  92. virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) = 0;
  93. // Rename file src to target.
  94. virtual Status RenameFile(const std::string& src,
  95. const std::string& target) = 0;
  96. // Lock the specified file. Used to prevent concurrent access to
  97. // the same db by multiple processes. On failure, stores NULL in
  98. // *lock and returns non-OK.
  99. //
  100. // On success, stores a pointer to the object that represents the
  101. // acquired lock in *lock and returns OK. The caller should call
  102. // UnlockFile(*lock) to release the lock. If the process exits,
  103. // the lock will be automatically released.
  104. //
  105. // If somebody else already holds the lock, finishes immediately
  106. // with a failure. I.e., this call does not wait for existing locks
  107. // to go away.
  108. //
  109. // May create the named file if it does not already exist.
  110. virtual Status LockFile(const std::string& fname, FileLock** lock) = 0;
  111. // Release the lock acquired by a previous successful call to LockFile.
  112. // REQUIRES: lock was returned by a successful LockFile() call
  113. // REQUIRES: lock has not already been unlocked.
  114. virtual Status UnlockFile(FileLock* lock) = 0;
  115. // Arrange to run "(*function)(arg)" once in a background thread.
  116. //
  117. // "function" may run in an unspecified thread. Multiple functions
  118. // added to the same Env may run concurrently in different threads.
  119. // I.e., the caller may not assume that background work items are
  120. // serialized.
  121. virtual void Schedule(
  122. void (*function)(void* arg),
  123. void* arg) = 0;
  124. // Start a new thread, invoking "function(arg)" within the new thread.
  125. // When "function(arg)" returns, the thread will be destroyed.
  126. virtual void StartThread(void (*function)(void* arg), void* arg) = 0;
  127. // *path is set to a temporary directory that can be used for testing. It may
  128. // or many not have just been created. The directory may or may not differ
  129. // between runs of the same process, but subsequent calls will return the
  130. // same directory.
  131. virtual Status GetTestDirectory(std::string* path) = 0;
  132. // Create and return a log file for storing informational messages.
  133. virtual Status NewLogger(const std::string& fname, Logger** result) = 0;
  134. // Returns the number of micro-seconds since some fixed point in time. Only
  135. // useful for computing deltas of time.
  136. virtual uint64_t NowMicros() = 0;
  137. // Sleep/delay the thread for the prescribed number of micro-seconds.
  138. virtual void SleepForMicroseconds(int micros) = 0;
  139. private:
  140. // No copying allowed
  141. Env(const Env&);
  142. void operator=(const Env&);
  143. };
  144. // A file abstraction for reading sequentially through a file
  145. class SequentialFile {
  146. public:
  147. SequentialFile() { }
  148. virtual ~SequentialFile();
  149. // Read up to "n" bytes from the file. "scratch[0..n-1]" may be
  150. // written by this routine. Sets "*result" to the data that was
  151. // read (including if fewer than "n" bytes were successfully read).
  152. // May set "*result" to point at data in "scratch[0..n-1]", so
  153. // "scratch[0..n-1]" must be live when "*result" is used.
  154. // If an error was encountered, returns a non-OK status.
  155. //
  156. // REQUIRES: External synchronization
  157. virtual Status Read(size_t n, Slice* result, char* scratch) = 0;
  158. // Skip "n" bytes from the file. This is guaranteed to be no
  159. // slower that reading the same data, but may be faster.
  160. //
  161. // If end of file is reached, skipping will stop at the end of the
  162. // file, and Skip will return OK.
  163. //
  164. // REQUIRES: External synchronization
  165. virtual Status Skip(uint64_t n) = 0;
  166. private:
  167. // No copying allowed
  168. SequentialFile(const SequentialFile&);
  169. void operator=(const SequentialFile&);
  170. };
  171. // A file abstraction for randomly reading the contents of a file.
  172. class RandomAccessFile {
  173. public:
  174. RandomAccessFile() { }
  175. virtual ~RandomAccessFile();
  176. // Read up to "n" bytes from the file starting at "offset".
  177. // "scratch[0..n-1]" may be written by this routine. Sets "*result"
  178. // to the data that was read (including if fewer than "n" bytes were
  179. // successfully read). May set "*result" to point at data in
  180. // "scratch[0..n-1]", so "scratch[0..n-1]" must be live when
  181. // "*result" is used. If an error was encountered, returns a non-OK
  182. // status.
  183. //
  184. // Safe for concurrent use by multiple threads.
  185. virtual Status Read(uint64_t offset, size_t n, Slice* result,
  186. char* scratch) const = 0;
  187. private:
  188. // No copying allowed
  189. RandomAccessFile(const RandomAccessFile&);
  190. void operator=(const RandomAccessFile&);
  191. };
  192. // A file abstraction for sequential writing. The implementation
  193. // must provide buffering since callers may append small fragments
  194. // at a time to the file.
  195. class WritableFile {
  196. public:
  197. WritableFile() { }
  198. virtual ~WritableFile();
  199. virtual Status Append(const Slice& data) = 0;
  200. virtual Status Close() = 0;
  201. virtual Status Flush() = 0;
  202. virtual Status Sync() = 0;
  203. private:
  204. // No copying allowed
  205. WritableFile(const WritableFile&);
  206. void operator=(const WritableFile&);
  207. };
  208. // An interface for writing log messages.
  209. class Logger {
  210. public:
  211. Logger() { }
  212. virtual ~Logger();
  213. // Write an entry to the log file with the specified format.
  214. virtual void Logv(const char* format, va_list ap) = 0;
  215. private:
  216. // No copying allowed
  217. Logger(const Logger&);
  218. void operator=(const Logger&);
  219. };
  220. // Identifies a locked file.
  221. class FileLock {
  222. public:
  223. FileLock() { }
  224. virtual ~FileLock();
  225. private:
  226. // No copying allowed
  227. FileLock(const FileLock&);
  228. void operator=(const FileLock&);
  229. };
  230. // Log the specified data to *info_log if info_log is non-NULL.
  231. extern void Log(Logger* info_log, const char* format, ...)
  232. # if defined(__GNUC__) || defined(__clang__)
  233. __attribute__((__format__ (__printf__, 2, 3)))
  234. # endif
  235. ;
  236. // A utility routine: write "data" to the named file.
  237. extern Status WriteStringToFile(Env* env, const Slice& data,
  238. const std::string& fname);
  239. // A utility routine: read contents of named file into *data
  240. extern Status ReadFileToString(Env* env, const std::string& fname,
  241. std::string* data);
  242. // An implementation of Env that forwards all calls to another Env.
  243. // May be useful to clients who wish to override just part of the
  244. // functionality of another Env.
  245. class EnvWrapper : public Env {
  246. public:
  247. // Initialize an EnvWrapper that delegates all calls to *t
  248. explicit EnvWrapper(Env* t) : target_(t) { }
  249. virtual ~EnvWrapper();
  250. // Return the target to which this Env forwards all calls
  251. Env* target() const { return target_; }
  252. // The following text is boilerplate that forwards all methods to target()
  253. Status NewSequentialFile(const std::string& f, SequentialFile** r) {
  254. return target_->NewSequentialFile(f, r);
  255. }
  256. Status NewRandomAccessFile(const std::string& f, RandomAccessFile** r) {
  257. return target_->NewRandomAccessFile(f, r);
  258. }
  259. Status NewWritableFile(const std::string& f, WritableFile** r) {
  260. return target_->NewWritableFile(f, r);
  261. }
  262. Status NewAppendableFile(const std::string& f, WritableFile** r) {
  263. return target_->NewAppendableFile(f, r);
  264. }
  265. bool FileExists(const std::string& f) { return target_->FileExists(f); }
  266. Status GetChildren(const std::string& dir, std::vector<std::string>* r) {
  267. return target_->GetChildren(dir, r);
  268. }
  269. Status DeleteFile(const std::string& f) { return target_->DeleteFile(f); }
  270. Status CreateDir(const std::string& d) { return target_->CreateDir(d); }
  271. Status DeleteDir(const std::string& d) { return target_->DeleteDir(d); }
  272. Status GetFileSize(const std::string& f, uint64_t* s) {
  273. return target_->GetFileSize(f, s);
  274. }
  275. Status RenameFile(const std::string& s, const std::string& t) {
  276. return target_->RenameFile(s, t);
  277. }
  278. Status LockFile(const std::string& f, FileLock** l) {
  279. return target_->LockFile(f, l);
  280. }
  281. Status UnlockFile(FileLock* l) { return target_->UnlockFile(l); }
  282. void Schedule(void (*f)(void*), void* a) {
  283. return target_->Schedule(f, a);
  284. }
  285. void StartThread(void (*f)(void*), void* a) {
  286. return target_->StartThread(f, a);
  287. }
  288. virtual Status GetTestDirectory(std::string* path) {
  289. return target_->GetTestDirectory(path);
  290. }
  291. virtual Status NewLogger(const std::string& fname, Logger** result) {
  292. return target_->NewLogger(fname, result);
  293. }
  294. uint64_t NowMicros() {
  295. return target_->NowMicros();
  296. }
  297. void SleepForMicroseconds(int micros) {
  298. target_->SleepForMicroseconds(micros);
  299. }
  300. private:
  301. Env* target_;
  302. };
  303. } // namespace leveldb
  304. #endif // STORAGE_LEVELDB_INCLUDE_ENV_H_