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.
5 #include "helpers/memenv/memenv.h"
7 #include "leveldb/env.h"
8 #include "leveldb/status.h"
10 #include "util/mutexlock.h"
22 // FileStates are reference counted. The initial reference count is zero
23 // and the caller must call Ref() at least once.
24 FileState() : refs_(0), size_(0) {}
26 // Increase the reference count.
28 MutexLock lock(&refs_mutex_);
32 // Decrease the reference count. Delete if this is the last reference.
34 bool do_delete = false;
37 MutexLock lock(&refs_mutex_);
50 uint64_t Size() const { return size_; }
52 Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const {
54 return Status::IOError("Offset greater than file size.");
56 const uint64_t available = size_ - offset;
65 size_t block = offset / kBlockSize;
66 size_t block_offset = offset % kBlockSize;
68 if (n <= kBlockSize - block_offset) {
69 // The requested bytes are all in the first block.
70 *result = Slice(blocks_[block] + block_offset, n);
74 size_t bytes_to_copy = n;
77 while (bytes_to_copy > 0) {
78 size_t avail = kBlockSize - block_offset;
79 if (avail > bytes_to_copy) {
80 avail = bytes_to_copy;
82 memcpy(dst, blocks_[block] + block_offset, avail);
84 bytes_to_copy -= avail;
90 *result = Slice(scratch, n);
94 Status Append(const Slice& data) {
95 const char* src = data.data();
96 size_t src_len = data.size();
100 size_t offset = size_ % kBlockSize;
103 // There is some room in the last block.
104 avail = kBlockSize - offset;
106 // No room in the last block; push new one.
107 blocks_.push_back(new char[kBlockSize]);
111 if (avail > src_len) {
114 memcpy(blocks_.back() + offset, src, avail);
124 // Private since only Unref() should be used to delete it.
126 for (std::vector<char*>::iterator i = blocks_.begin(); i != blocks_.end();
132 // No copying allowed.
133 FileState(const FileState&);
134 void operator=(const FileState&);
136 port::Mutex refs_mutex_;
137 int refs_; // Protected by refs_mutex_;
139 // The following fields are not protected by any mutex. They are only mutable
140 // while the file is being written, and concurrent access is not allowed
141 // to writable files.
142 std::vector<char*> blocks_;
145 enum { kBlockSize = 8 * 1024 };
148 class SequentialFileImpl : public SequentialFile {
150 explicit SequentialFileImpl(FileState* file) : file_(file), pos_(0) {
154 ~SequentialFileImpl() {
158 virtual Status Read(size_t n, Slice* result, char* scratch) {
159 Status s = file_->Read(pos_, n, result, scratch);
161 pos_ += result->size();
166 virtual Status Skip(uint64_t n) {
167 if (pos_ > file_->Size()) {
168 return Status::IOError("pos_ > file_->Size()");
170 const size_t available = file_->Size() - pos_;
183 class RandomAccessFileImpl : public RandomAccessFile {
185 explicit RandomAccessFileImpl(FileState* file) : file_(file) {
189 ~RandomAccessFileImpl() {
193 virtual Status Read(uint64_t offset, size_t n, Slice* result,
194 char* scratch) const {
195 return file_->Read(offset, n, result, scratch);
202 class WritableFileImpl : public WritableFile {
204 WritableFileImpl(FileState* file) : file_(file) {
208 ~WritableFileImpl() {
212 virtual Status Append(const Slice& data) {
213 return file_->Append(data);
216 virtual Status Close() { return Status::OK(); }
217 virtual Status Flush() { return Status::OK(); }
218 virtual Status Sync() { return Status::OK(); }
224 class NoOpLogger : public Logger {
226 virtual void Logv(const char* format, va_list ap) { }
229 class InMemoryEnv : public EnvWrapper {
231 explicit InMemoryEnv(Env* base_env) : EnvWrapper(base_env) { }
233 virtual ~InMemoryEnv() {
234 for (FileSystem::iterator i = file_map_.begin(); i != file_map_.end(); ++i){
239 // Partial implementation of the Env interface.
240 virtual Status NewSequentialFile(const std::string& fname,
241 SequentialFile** result) {
242 MutexLock lock(&mutex_);
243 if (file_map_.find(fname) == file_map_.end()) {
245 return Status::IOError(fname, "File not found");
248 *result = new SequentialFileImpl(file_map_[fname]);
252 virtual Status NewRandomAccessFile(const std::string& fname,
253 RandomAccessFile** result) {
254 MutexLock lock(&mutex_);
255 if (file_map_.find(fname) == file_map_.end()) {
257 return Status::IOError(fname, "File not found");
260 *result = new RandomAccessFileImpl(file_map_[fname]);
264 virtual Status NewWritableFile(const std::string& fname,
265 WritableFile** result) {
266 MutexLock lock(&mutex_);
267 if (file_map_.find(fname) != file_map_.end()) {
268 DeleteFileInternal(fname);
271 FileState* file = new FileState();
273 file_map_[fname] = file;
275 *result = new WritableFileImpl(file);
279 virtual bool FileExists(const std::string& fname) {
280 MutexLock lock(&mutex_);
281 return file_map_.find(fname) != file_map_.end();
284 virtual Status GetChildren(const std::string& dir,
285 std::vector<std::string>* result) {
286 MutexLock lock(&mutex_);
289 for (FileSystem::iterator i = file_map_.begin(); i != file_map_.end(); ++i){
290 const std::string& filename = i->first;
292 if (filename.size() >= dir.size() + 1 && filename[dir.size()] == '/' &&
293 Slice(filename).starts_with(Slice(dir))) {
294 result->push_back(filename.substr(dir.size() + 1));
301 void DeleteFileInternal(const std::string& fname) {
302 if (file_map_.find(fname) == file_map_.end()) {
306 file_map_[fname]->Unref();
307 file_map_.erase(fname);
310 virtual Status DeleteFile(const std::string& fname) {
311 MutexLock lock(&mutex_);
312 if (file_map_.find(fname) == file_map_.end()) {
313 return Status::IOError(fname, "File not found");
316 DeleteFileInternal(fname);
320 virtual Status CreateDir(const std::string& dirname) {
324 virtual Status DeleteDir(const std::string& dirname) {
328 virtual Status GetFileSize(const std::string& fname, uint64_t* file_size) {
329 MutexLock lock(&mutex_);
330 if (file_map_.find(fname) == file_map_.end()) {
331 return Status::IOError(fname, "File not found");
334 *file_size = file_map_[fname]->Size();
338 virtual Status RenameFile(const std::string& src,
339 const std::string& target) {
340 MutexLock lock(&mutex_);
341 if (file_map_.find(src) == file_map_.end()) {
342 return Status::IOError(src, "File not found");
345 DeleteFileInternal(target);
346 file_map_[target] = file_map_[src];
347 file_map_.erase(src);
351 virtual Status LockFile(const std::string& fname, FileLock** lock) {
352 *lock = new FileLock;
356 virtual Status UnlockFile(FileLock* lock) {
361 virtual Status GetTestDirectory(std::string* path) {
366 virtual Status NewLogger(const std::string& fname, Logger** result) {
367 *result = new NoOpLogger;
372 // Map from filenames to FileState objects, representing a simple file system.
373 typedef std::map<std::string, FileState*> FileSystem;
375 FileSystem file_map_; // Protected by mutex_.
380 Env* NewMemEnv(Env* base_env) {
381 return new InMemoryEnv(base_env);
384 } // namespace leveldb