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

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