小组成员:谢瑞阳、徐翔宇
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.

374 lines
8.9 KiB

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