作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

389 rindas
14 KiB

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