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.

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