Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. virtual std::string GetName() const { return "[memenv]"; }
  148. private:
  149. FileState* file_;
  150. uint64_t pos_;
  151. };
  152. class RandomAccessFileImpl : public RandomAccessFile {
  153. public:
  154. explicit RandomAccessFileImpl(FileState* file) : file_(file) {
  155. file_->Ref();
  156. }
  157. ~RandomAccessFileImpl() {
  158. file_->Unref();
  159. }
  160. virtual Status Read(uint64_t offset, size_t n, Slice* result,
  161. char* scratch) const {
  162. return file_->Read(offset, n, result, scratch);
  163. }
  164. virtual std::string GetName() const { return "[memenv]"; }
  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. virtual std::string GetName() const { return "[memenv]"; }
  183. private:
  184. FileState* file_;
  185. };
  186. class NoOpLogger : public Logger {
  187. public:
  188. virtual void Logv(const char* format, va_list ap) { }
  189. };
  190. class InMemoryEnv : public EnvWrapper {
  191. public:
  192. explicit InMemoryEnv(Env* base_env) : EnvWrapper(base_env) { }
  193. virtual ~InMemoryEnv() {
  194. for (FileSystem::iterator i = file_map_.begin(); i != file_map_.end(); ++i){
  195. i->second->Unref();
  196. }
  197. }
  198. // Partial implementation of the Env interface.
  199. virtual Status NewSequentialFile(const std::string& fname,
  200. SequentialFile** result) {
  201. MutexLock lock(&mutex_);
  202. if (file_map_.find(fname) == file_map_.end()) {
  203. *result = NULL;
  204. return Status::IOError(fname, "File not found");
  205. }
  206. *result = new SequentialFileImpl(file_map_[fname]);
  207. return Status::OK();
  208. }
  209. virtual Status NewRandomAccessFile(const std::string& fname,
  210. RandomAccessFile** result) {
  211. MutexLock lock(&mutex_);
  212. if (file_map_.find(fname) == file_map_.end()) {
  213. *result = NULL;
  214. return Status::IOError(fname, "File not found");
  215. }
  216. *result = new RandomAccessFileImpl(file_map_[fname]);
  217. return Status::OK();
  218. }
  219. virtual Status NewWritableFile(const std::string& fname,
  220. WritableFile** result) {
  221. MutexLock lock(&mutex_);
  222. if (file_map_.find(fname) != file_map_.end()) {
  223. DeleteFileInternal(fname);
  224. }
  225. FileState* file = new FileState();
  226. file->Ref();
  227. file_map_[fname] = file;
  228. *result = new WritableFileImpl(file);
  229. return Status::OK();
  230. }
  231. virtual Status NewAppendableFile(const std::string& fname,
  232. WritableFile** result) {
  233. MutexLock lock(&mutex_);
  234. FileState** sptr = &file_map_[fname];
  235. FileState* file = *sptr;
  236. if (file == NULL) {
  237. file = new FileState();
  238. file->Ref();
  239. }
  240. *result = new WritableFileImpl(file);
  241. return Status::OK();
  242. }
  243. virtual bool FileExists(const std::string& fname) {
  244. MutexLock lock(&mutex_);
  245. return file_map_.find(fname) != file_map_.end();
  246. }
  247. virtual Status GetChildren(const std::string& dir,
  248. std::vector<std::string>* result) {
  249. MutexLock lock(&mutex_);
  250. result->clear();
  251. for (FileSystem::iterator i = file_map_.begin(); i != file_map_.end(); ++i){
  252. const std::string& filename = i->first;
  253. if (filename.size() >= dir.size() + 1 && filename[dir.size()] == '/' &&
  254. Slice(filename).starts_with(Slice(dir))) {
  255. result->push_back(filename.substr(dir.size() + 1));
  256. }
  257. }
  258. return Status::OK();
  259. }
  260. void DeleteFileInternal(const std::string& fname) {
  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_; // Protected by mutex_.
  321. };
  322. } // namespace
  323. Env* NewMemEnv(Env* base_env) {
  324. return new InMemoryEnv(base_env);
  325. }
  326. } // namespace leveldb