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

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