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.

410 lines
9.7 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 година
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 година
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 година
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. #include "helpers/memenv/memenv.h"
  5. #include <string.h>
  6. #include <limits>
  7. #include <map>
  8. #include <string>
  9. #include <vector>
  10. #include "leveldb/env.h"
  11. #include "leveldb/status.h"
  12. #include "port/port.h"
  13. #include "port/thread_annotations.h"
  14. #include "util/mutexlock.h"
  15. namespace leveldb {
  16. namespace {
  17. class FileState {
  18. public:
  19. // FileStates are reference counted. The initial reference count is zero
  20. // and the caller must call Ref() at least once.
  21. FileState() : refs_(0), size_(0) {}
  22. // Increase the reference count.
  23. void Ref() {
  24. MutexLock lock(&refs_mutex_);
  25. ++refs_;
  26. }
  27. // Decrease the reference count. Delete if this is the last reference.
  28. void Unref() {
  29. bool do_delete = false;
  30. {
  31. MutexLock lock(&refs_mutex_);
  32. --refs_;
  33. assert(refs_ >= 0);
  34. if (refs_ <= 0) {
  35. do_delete = true;
  36. }
  37. }
  38. if (do_delete) {
  39. delete this;
  40. }
  41. }
  42. uint64_t Size() const {
  43. MutexLock lock(&blocks_mutex_);
  44. return size_;
  45. }
  46. void Truncate() {
  47. MutexLock lock(&blocks_mutex_);
  48. for (char*& block : blocks_) {
  49. delete[] block;
  50. }
  51. blocks_.clear();
  52. size_ = 0;
  53. }
  54. Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const {
  55. MutexLock lock(&blocks_mutex_);
  56. if (offset > size_) {
  57. return Status::IOError("Offset greater than file size.");
  58. }
  59. const uint64_t available = size_ - offset;
  60. if (n > available) {
  61. n = static_cast<size_t>(available);
  62. }
  63. if (n == 0) {
  64. *result = Slice();
  65. return Status::OK();
  66. }
  67. assert(offset / kBlockSize <= std::numeric_limits<size_t>::max());
  68. size_t block = static_cast<size_t>(offset / kBlockSize);
  69. size_t block_offset = offset % kBlockSize;
  70. size_t bytes_to_copy = n;
  71. char* dst = scratch;
  72. while (bytes_to_copy > 0) {
  73. size_t avail = kBlockSize - block_offset;
  74. if (avail > bytes_to_copy) {
  75. avail = bytes_to_copy;
  76. }
  77. memcpy(dst, blocks_[block] + block_offset, avail);
  78. bytes_to_copy -= avail;
  79. dst += avail;
  80. block++;
  81. block_offset = 0;
  82. }
  83. *result = Slice(scratch, n);
  84. return Status::OK();
  85. }
  86. Status Append(const Slice& data) {
  87. const char* src = data.data();
  88. size_t src_len = data.size();
  89. MutexLock lock(&blocks_mutex_);
  90. while (src_len > 0) {
  91. size_t avail;
  92. size_t offset = size_ % kBlockSize;
  93. if (offset != 0) {
  94. // There is some room in the last block.
  95. avail = kBlockSize - offset;
  96. } else {
  97. // No room in the last block; push new one.
  98. blocks_.push_back(new char[kBlockSize]);
  99. avail = kBlockSize;
  100. }
  101. if (avail > src_len) {
  102. avail = src_len;
  103. }
  104. memcpy(blocks_.back() + offset, src, avail);
  105. src_len -= avail;
  106. src += avail;
  107. size_ += avail;
  108. }
  109. return Status::OK();
  110. }
  111. private:
  112. // Private since only Unref() should be used to delete it.
  113. ~FileState() {
  114. Truncate();
  115. }
  116. // No copying allowed.
  117. FileState(const FileState&);
  118. void operator=(const FileState&);
  119. port::Mutex refs_mutex_;
  120. int refs_ GUARDED_BY(refs_mutex_);
  121. mutable port::Mutex blocks_mutex_;
  122. std::vector<char*> blocks_ GUARDED_BY(blocks_mutex_);
  123. uint64_t size_ GUARDED_BY(blocks_mutex_);
  124. enum { kBlockSize = 8 * 1024 };
  125. };
  126. class SequentialFileImpl : public SequentialFile {
  127. public:
  128. explicit SequentialFileImpl(FileState* file) : file_(file), pos_(0) {
  129. file_->Ref();
  130. }
  131. ~SequentialFileImpl() {
  132. file_->Unref();
  133. }
  134. virtual Status Read(size_t n, Slice* result, char* scratch) {
  135. Status s = file_->Read(pos_, n, result, scratch);
  136. if (s.ok()) {
  137. pos_ += result->size();
  138. }
  139. return s;
  140. }
  141. virtual Status Skip(uint64_t n) {
  142. if (pos_ > file_->Size()) {
  143. return Status::IOError("pos_ > file_->Size()");
  144. }
  145. const uint64_t available = file_->Size() - pos_;
  146. if (n > available) {
  147. n = available;
  148. }
  149. pos_ += n;
  150. return Status::OK();
  151. }
  152. private:
  153. FileState* file_;
  154. uint64_t pos_;
  155. };
  156. class RandomAccessFileImpl : public RandomAccessFile {
  157. public:
  158. explicit RandomAccessFileImpl(FileState* file) : file_(file) {
  159. file_->Ref();
  160. }
  161. ~RandomAccessFileImpl() {
  162. file_->Unref();
  163. }
  164. virtual Status Read(uint64_t offset, size_t n, Slice* result,
  165. char* scratch) const {
  166. return file_->Read(offset, n, result, scratch);
  167. }
  168. private:
  169. FileState* file_;
  170. };
  171. class WritableFileImpl : public WritableFile {
  172. public:
  173. WritableFileImpl(FileState* file) : file_(file) {
  174. file_->Ref();
  175. }
  176. ~WritableFileImpl() {
  177. file_->Unref();
  178. }
  179. virtual Status Append(const Slice& data) {
  180. return file_->Append(data);
  181. }
  182. virtual Status Close() { return Status::OK(); }
  183. virtual Status Flush() { return Status::OK(); }
  184. virtual Status Sync() { return Status::OK(); }
  185. private:
  186. FileState* file_;
  187. };
  188. class NoOpLogger : public Logger {
  189. public:
  190. virtual void Logv(const char* format, va_list ap) { }
  191. };
  192. class InMemoryEnv : public EnvWrapper {
  193. public:
  194. explicit InMemoryEnv(Env* base_env) : EnvWrapper(base_env) { }
  195. virtual ~InMemoryEnv() {
  196. for (FileSystem::iterator i = file_map_.begin(); i != file_map_.end(); ++i){
  197. i->second->Unref();
  198. }
  199. }
  200. // Partial implementation of the Env interface.
  201. virtual Status NewSequentialFile(const std::string& fname,
  202. SequentialFile** result) {
  203. MutexLock lock(&mutex_);
  204. if (file_map_.find(fname) == file_map_.end()) {
  205. *result = nullptr;
  206. return Status::IOError(fname, "File not found");
  207. }
  208. *result = new SequentialFileImpl(file_map_[fname]);
  209. return Status::OK();
  210. }
  211. virtual Status NewRandomAccessFile(const std::string& fname,
  212. RandomAccessFile** result) {
  213. MutexLock lock(&mutex_);
  214. if (file_map_.find(fname) == file_map_.end()) {
  215. *result = nullptr;
  216. return Status::IOError(fname, "File not found");
  217. }
  218. *result = new RandomAccessFileImpl(file_map_[fname]);
  219. return Status::OK();
  220. }
  221. virtual Status NewWritableFile(const std::string& fname,
  222. WritableFile** result) {
  223. MutexLock lock(&mutex_);
  224. FileSystem::iterator it = file_map_.find(fname);
  225. FileState* file;
  226. if (it == file_map_.end()) {
  227. // File is not currently open.
  228. file = new FileState();
  229. file->Ref();
  230. file_map_[fname] = file;
  231. } else {
  232. file = it->second;
  233. file->Truncate();
  234. }
  235. *result = new WritableFileImpl(file);
  236. return Status::OK();
  237. }
  238. virtual Status NewAppendableFile(const std::string& fname,
  239. WritableFile** result) {
  240. MutexLock lock(&mutex_);
  241. FileState** sptr = &file_map_[fname];
  242. FileState* file = *sptr;
  243. if (file == nullptr) {
  244. file = new FileState();
  245. file->Ref();
  246. }
  247. *result = new WritableFileImpl(file);
  248. return Status::OK();
  249. }
  250. virtual bool FileExists(const std::string& fname) {
  251. MutexLock lock(&mutex_);
  252. return file_map_.find(fname) != file_map_.end();
  253. }
  254. virtual Status GetChildren(const std::string& dir,
  255. std::vector<std::string>* result) {
  256. MutexLock lock(&mutex_);
  257. result->clear();
  258. for (FileSystem::iterator i = file_map_.begin(); i != file_map_.end(); ++i){
  259. const std::string& filename = i->first;
  260. if (filename.size() >= dir.size() + 1 && filename[dir.size()] == '/' &&
  261. Slice(filename).starts_with(Slice(dir))) {
  262. result->push_back(filename.substr(dir.size() + 1));
  263. }
  264. }
  265. return Status::OK();
  266. }
  267. void DeleteFileInternal(const std::string& fname)
  268. EXCLUSIVE_LOCKS_REQUIRED(mutex_) {
  269. if (file_map_.find(fname) == file_map_.end()) {
  270. return;
  271. }
  272. file_map_[fname]->Unref();
  273. file_map_.erase(fname);
  274. }
  275. virtual Status DeleteFile(const std::string& fname) {
  276. MutexLock lock(&mutex_);
  277. if (file_map_.find(fname) == file_map_.end()) {
  278. return Status::IOError(fname, "File not found");
  279. }
  280. DeleteFileInternal(fname);
  281. return Status::OK();
  282. }
  283. virtual Status CreateDir(const std::string& dirname) {
  284. return Status::OK();
  285. }
  286. virtual Status DeleteDir(const std::string& dirname) {
  287. return Status::OK();
  288. }
  289. virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) {
  290. MutexLock lock(&mutex_);
  291. if (file_map_.find(fname) == file_map_.end()) {
  292. return Status::IOError(fname, "File not found");
  293. }
  294. *file_size = file_map_[fname]->Size();
  295. return Status::OK();
  296. }
  297. virtual Status RenameFile(const std::string& src,
  298. const std::string& target) {
  299. MutexLock lock(&mutex_);
  300. if (file_map_.find(src) == file_map_.end()) {
  301. return Status::IOError(src, "File not found");
  302. }
  303. DeleteFileInternal(target);
  304. file_map_[target] = file_map_[src];
  305. file_map_.erase(src);
  306. return Status::OK();
  307. }
  308. virtual Status LockFile(const std::string& fname, FileLock** lock) {
  309. *lock = new FileLock;
  310. return Status::OK();
  311. }
  312. virtual Status UnlockFile(FileLock* lock) {
  313. delete lock;
  314. return Status::OK();
  315. }
  316. virtual Status GetTestDirectory(std::string* path) {
  317. *path = "/test";
  318. return Status::OK();
  319. }
  320. virtual Status NewLogger(const std::string& fname, Logger** result) {
  321. *result = new NoOpLogger;
  322. return Status::OK();
  323. }
  324. private:
  325. // Map from filenames to FileState objects, representing a simple file system.
  326. typedef std::map<std::string, FileState*> FileSystem;
  327. port::Mutex mutex_;
  328. FileSystem file_map_ GUARDED_BY(mutex_);
  329. };
  330. } // namespace
  331. Env* NewMemEnv(Env* base_env) {
  332. return new InMemoryEnv(base_env);
  333. }
  334. } // namespace leveldb