小组成员:10215300402-朱维清 & 10222140408 谷杰
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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