aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorgabor@google.com <gabor@google.com@62dab493-f737-651d-591e-8d6aee1b9529>2011-07-15 00:20:57 +0000
committergabor@google.com <gabor@google.com@62dab493-f737-651d-591e-8d6aee1b9529>2011-07-15 00:20:57 +0000
commit917b88dd720b6e658c1fd7812bc61c605f315124 (patch)
treeacb156c19aadd00e7a14dcd26b76671d36808dae /util
parente67cd63d8a779971247078b2715b6ae7b37dbc2e (diff)
downloadsrc-917b88dd720b6e658c1fd7812bc61c605f315124.tar.gz
Small tweaks and bugfixes for Issue 18 and 19.
Slight tweak to the no-overlap optimization: only push to level 2 to reduce the amount of wasted space when the same small key range is being repeatedly overwritten. Fix for Issue 18: Avoid failure on Windows by avoiding deletion of lock file until the end of DestroyDB(). Fix for Issue 19: Disregard sequence numbers when checking for overlap in sstable ranges. This fixes issue 19: when writing the same key over and over again, we would generate a sequence of sstables that were never merged together since their sequence numbers were disjoint. Don't ignore map/unmap error checks. Miscellaneous fixes for small problems Sanjay found while diagnosing issue/9 and issue/16 (corruption_testr failures). - log::Reader reports the record type when it finds an unexpected type. - log::Reader no longer reports an error when it encounters an expected zero record regardless of the setting of the "checksum" flag. - Added a missing forward declaration. - Documented a side-effects of larger write buffer sizes (longer recovery time). git-svn-id: http://leveldb.googlecode.com/svn/trunk@37 62dab493-f737-651d-591e-8d6aee1b9529
Diffstat (limited to 'util')
-rw-r--r--util/env_posix.cc61
1 files changed, 36 insertions, 25 deletions
diff --git a/util/env_posix.cc b/util/env_posix.cc
index fec1599..46723e2 100644
--- a/util/env_posix.cc
+++ b/util/env_posix.cc
@@ -28,6 +28,10 @@ namespace leveldb {
namespace {
+static Status IOError(const std::string& context, int err_number) {
+ return Status::IOError(context, strerror(err_number));
+}
+
class PosixSequentialFile: public SequentialFile {
private:
std::string filename_;
@@ -47,7 +51,7 @@ class PosixSequentialFile: public SequentialFile {
// We leave status as ok if we hit the end of the file
} else {
// A partial read with an error: return a non-ok status
- s = Status::IOError(filename_, strerror(errno));
+ s = IOError(filename_, errno);
}
}
return s;
@@ -55,7 +59,7 @@ class PosixSequentialFile: public SequentialFile {
virtual Status Skip(uint64_t n) {
if (fseek(file_, n, SEEK_CUR)) {
- return Status::IOError(filename_, strerror(errno));
+ return IOError(filename_, errno);
}
return Status::OK();
}
@@ -78,7 +82,7 @@ class PosixRandomAccessFile: public RandomAccessFile {
*result = Slice(scratch, (r < 0) ? 0 : r);
if (r < 0) {
// An error: return a non-ok status
- s = Status::IOError(filename_, strerror(errno));
+ s = IOError(filename_, errno);
}
return s;
}
@@ -114,13 +118,16 @@ class PosixMmapFile : public WritableFile {
return s;
}
- void UnmapCurrentRegion() {
+ bool UnmapCurrentRegion() {
+ bool result = true;
if (base_ != NULL) {
if (last_sync_ < limit_) {
// Defer syncing this data until next Sync() call, if any
pending_sync_ = true;
}
- munmap(base_, limit_ - base_);
+ if (munmap(base_, limit_ - base_) != 0) {
+ result = false;
+ }
file_offset_ += limit_ - base_;
base_ = NULL;
limit_ = NULL;
@@ -132,6 +139,7 @@ class PosixMmapFile : public WritableFile {
map_size_ *= 2;
}
}
+ return result;
}
bool MapNewRegion() {
@@ -181,8 +189,10 @@ class PosixMmapFile : public WritableFile {
assert(dst_ <= limit_);
size_t avail = limit_ - dst_;
if (avail == 0) {
- UnmapCurrentRegion();
- MapNewRegion();
+ if (!UnmapCurrentRegion() ||
+ !MapNewRegion()) {
+ return IOError(filename_, errno);
+ }
}
size_t n = (left <= avail) ? left : avail;
@@ -197,17 +207,18 @@ class PosixMmapFile : public WritableFile {
virtual Status Close() {
Status s;
size_t unused = limit_ - dst_;
- UnmapCurrentRegion();
- if (unused > 0) {
+ if (!UnmapCurrentRegion()) {
+ s = IOError(filename_, errno);
+ } else if (unused > 0) {
// Trim the extra space at the end of the file
if (ftruncate(fd_, file_offset_ - unused) < 0) {
- s = Status::IOError(filename_, strerror(errno));
+ s = IOError(filename_, errno);
}
}
if (close(fd_) < 0) {
if (s.ok()) {
- s = Status::IOError(filename_, strerror(errno));
+ s = IOError(filename_, errno);
}
}
@@ -228,7 +239,7 @@ class PosixMmapFile : public WritableFile {
// Some unmapped data was not synced
pending_sync_ = false;
if (fdatasync(fd_) < 0) {
- s = Status::IOError(filename_, strerror(errno));
+ s = IOError(filename_, errno);
}
}
@@ -239,7 +250,7 @@ class PosixMmapFile : public WritableFile {
size_t p2 = TruncateToPageBoundary(dst_ - base_ - 1);
last_sync_ = dst_;
if (msync(base_ + p1, p2 - p1 + page_size_, MS_SYNC) < 0) {
- s = Status::IOError(filename_, strerror(errno));
+ s = IOError(filename_, errno);
}
}
@@ -276,7 +287,7 @@ class PosixEnv : public Env {
FILE* f = fopen(fname.c_str(), "r");
if (f == NULL) {
*result = NULL;
- return Status::IOError(fname, strerror(errno));
+ return IOError(fname, errno);
} else {
*result = new PosixSequentialFile(fname, f);
return Status::OK();
@@ -288,7 +299,7 @@ class PosixEnv : public Env {
int fd = open(fname.c_str(), O_RDONLY);
if (fd < 0) {
*result = NULL;
- return Status::IOError(fname, strerror(errno));
+ return IOError(fname, errno);
}
*result = new PosixRandomAccessFile(fname, fd);
return Status::OK();
@@ -300,7 +311,7 @@ class PosixEnv : public Env {
const int fd = open(fname.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
if (fd < 0) {
*result = NULL;
- s = Status::IOError(fname, strerror(errno));
+ s = IOError(fname, errno);
} else {
*result = new PosixMmapFile(fname, fd, page_size_);
}
@@ -316,7 +327,7 @@ class PosixEnv : public Env {
result->clear();
DIR* d = opendir(dir.c_str());
if (d == NULL) {
- return Status::IOError(dir, strerror(errno));
+ return IOError(dir, errno);
}
struct dirent* entry;
while ((entry = readdir(d)) != NULL) {
@@ -329,7 +340,7 @@ class PosixEnv : public Env {
virtual Status DeleteFile(const std::string& fname) {
Status result;
if (unlink(fname.c_str()) != 0) {
- result = Status::IOError(fname, strerror(errno));
+ result = IOError(fname, errno);
}
return result;
};
@@ -337,7 +348,7 @@ class PosixEnv : public Env {
virtual Status CreateDir(const std::string& name) {
Status result;
if (mkdir(name.c_str(), 0755) != 0) {
- result = Status::IOError(name, strerror(errno));
+ result = IOError(name, errno);
}
return result;
};
@@ -345,7 +356,7 @@ class PosixEnv : public Env {
virtual Status DeleteDir(const std::string& name) {
Status result;
if (rmdir(name.c_str()) != 0) {
- result = Status::IOError(name, strerror(errno));
+ result = IOError(name, errno);
}
return result;
};
@@ -355,7 +366,7 @@ class PosixEnv : public Env {
struct stat sbuf;
if (stat(fname.c_str(), &sbuf) != 0) {
*size = 0;
- s = Status::IOError(fname, strerror(errno));
+ s = IOError(fname, errno);
} else {
*size = sbuf.st_size;
}
@@ -365,7 +376,7 @@ class PosixEnv : public Env {
virtual Status RenameFile(const std::string& src, const std::string& target) {
Status result;
if (rename(src.c_str(), target.c_str()) != 0) {
- result = Status::IOError(src, strerror(errno));
+ result = IOError(src, errno);
}
return result;
}
@@ -375,9 +386,9 @@ class PosixEnv : public Env {
Status result;
int fd = open(fname.c_str(), O_RDWR | O_CREAT, 0644);
if (fd < 0) {
- result = Status::IOError(fname, strerror(errno));
+ result = IOError(fname, errno);
} else if (LockOrUnlock(fd, true) == -1) {
- result = Status::IOError("lock " + fname, strerror(errno));
+ result = IOError("lock " + fname, errno);
close(fd);
} else {
PosixFileLock* my_lock = new PosixFileLock;
@@ -391,7 +402,7 @@ class PosixEnv : public Env {
PosixFileLock* my_lock = reinterpret_cast<PosixFileLock*>(lock);
Status result;
if (LockOrUnlock(my_lock->fd_, false) == -1) {
- result = Status::IOError(strerror(errno));
+ result = IOError("unlock", errno);
}
close(my_lock->fd_);
delete my_lock;