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.

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