Skip to content

Commit

Permalink
In-memory environment read beyond EOF
Browse files Browse the repository at this point in the history
Summary:
Made it consistent with posix Env, which uses pread() that returns 0
(success) when an offset is given beyond EOF. The purpose of making these Envs
behave consistently is I am repurposing the in-memory Envs' tests for the basic
Env tests in D58635.

Test Plan: ran mock_env_test and memenv_test

Reviewers: IslamAbdelRahman, lightmark, sdong

Reviewed By: sdong

Subscribers: andrewkr, dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D58845
  • Loading branch information
ajkr committed May 27, 2016
1 parent 0e20000 commit 57461fb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
16 changes: 8 additions & 8 deletions util/memenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include <string.h>

#include <algorithm>
#include <map>
#include <string>
#include <vector>

#include "rocksdb/env.h"
#include "rocksdb/status.h"
#include "port/port.h"
#include "util/mutexlock.h"
#include <map>
#include <string.h>
#include <string>
#include <vector>

namespace rocksdb {

Expand Down Expand Up @@ -70,10 +73,7 @@ class FileState {
uint64_t Size() const { return size_; }

Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const {
if (offset > size_) {
return Status::IOError("Offset greater than file size.");
}
const uint64_t available = size_ - offset;
const uint64_t available = size_ - std::min(size_, offset);
if (n > available) {
n = available;
}
Expand Down
2 changes: 1 addition & 1 deletion util/memenv_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ TEST_F(MemEnvTest, ReadWrite) {
ASSERT_EQ(0, result.compare("d"));

// Too high offset.
ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok());
ASSERT_TRUE(rand_file->Read(1000, 5, &result, scratch).ok());
}

TEST_F(MemEnvTest, Locks) {
Expand Down
5 changes: 1 addition & 4 deletions util/mock_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ class MemFile {

Status Read(uint64_t offset, size_t n, Slice* result, char* scratch) const {
MutexLock lock(&mutex_);
if (offset > Size()) {
return Status::IOError("Offset greater than file size.");
}
const uint64_t available = Size() - offset;
const uint64_t available = Size() - std::min(Size(), offset);
if (n > available) {
n = available;
}
Expand Down
2 changes: 1 addition & 1 deletion util/mock_env_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TEST_F(MockEnvTest, ReadWrite) {
ASSERT_EQ(0, result.compare("d"));

// Too high offset.
ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok());
ASSERT_TRUE(rand_file->Read(1000, 5, &result, scratch).ok());
}

TEST_F(MockEnvTest, Locks) {
Expand Down

0 comments on commit 57461fb

Please sign in to comment.