Skip to content

Commit

Permalink
#1661: Replaced gscoped_ptr with std::unique_ptr for yb::Env.
Browse files Browse the repository at this point in the history
Summary: Replaced `gscoped_ptr` with `std::unique_ptr` for `yb::Env`. This is needed as a prerequisite step for unifying `yb::Env` and `rocksdb::Env`.

Test Plan: Jenkins

Reviewers: sergei, mikhail

Reviewed By: mikhail

Subscribers: ybase, bogdan

Differential Revision: https://phabricator.dev.yugabyte.com/D6835
  • Loading branch information
ttyusupov committed Jun 30, 2019
1 parent 1d52d22 commit 5adb348
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 192 deletions.
4 changes: 2 additions & 2 deletions src/yb/consensus/log-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ class LogTest : public LogTestBase {
int first_repl_index,
LogReader* reader) {
string fqp = GetTestPath(strings::Substitute("wal-00000000$0", sequence_number));
gscoped_ptr<WritableFile> w_log_seg;
std::unique_ptr<WritableFile> w_log_seg;
RETURN_NOT_OK(fs_manager_->env()->NewWritableFile(fqp, &w_log_seg));
gscoped_ptr<RandomAccessFile> r_log_seg;
std::unique_ptr<RandomAccessFile> r_log_seg;
RETURN_NOT_OK(fs_manager_->env()->NewRandomAccessFile(fqp, &r_log_seg));

scoped_refptr<ReadableLogSegment> readable_segment(
Expand Down
6 changes: 3 additions & 3 deletions src/yb/consensus/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ Status Log::SwitchToAllocatedSegment() {
}

// Create a new segment.
gscoped_ptr<WritableLogSegment> new_segment(
std::unique_ptr<WritableLogSegment> new_segment(
new WritableLogSegment(new_segment_path, next_segment_file_));

// Set up the new header and footer.
Expand Down Expand Up @@ -1070,7 +1070,7 @@ Status Log::SwitchToAllocatedSegment() {
}

// Open the segment we just created in readable form and add it to the reader.
gscoped_ptr<RandomAccessFile> readable_file;
std::unique_ptr<RandomAccessFile> readable_file;
RETURN_NOT_OK(get_env()->NewRandomAccessFile(
RandomAccessFileOptions(), new_segment_path, &readable_file));

Expand Down Expand Up @@ -1111,7 +1111,7 @@ Status Log::CreatePlaceholderSegment(const WritableFileOptions& opts,
shared_ptr<WritableFile>* out) {
string path_tmpl = JoinPathSegments(log_dir_, kSegmentPlaceholderFileTemplate);
VLOG_WITH_PREFIX(2) << "Creating temp. file for place holder segment, template: " << path_tmpl;
gscoped_ptr<WritableFile> segment_file;
std::unique_ptr<WritableFile> segment_file;
RETURN_NOT_OK(get_env()->NewTempWritableFile(opts,
path_tmpl,
result_path,
Expand Down
6 changes: 3 additions & 3 deletions src/yb/fs/fs_manager-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class FsManagerTestBase : public YBTest {
const char* kServerType = "tserver_test";

private:
gscoped_ptr<FsManager> fs_manager_;
std::unique_ptr<FsManager> fs_manager_;
string log_dir_ = "";
};

Expand Down Expand Up @@ -191,7 +191,7 @@ TEST_F(FsManagerTestBase, TestListTablets) {
ASSERT_EQ(0, tablet_ids.size());

string path = fs_manager()->GetRaftGroupMetadataDir();
gscoped_ptr<WritableFile> writer;
std::unique_ptr<WritableFile> writer;
ASSERT_OK(env_->NewWritableFile(
JoinPathSegments(path, "foo.tmp"), &writer));
ASSERT_OK(env_->NewWritableFile(
Expand All @@ -210,7 +210,7 @@ TEST_F(FsManagerTestBase, TestCannotUseNonEmptyFsRoot) {
ReinitFsManager({ path }, { path });
ASSERT_OK(fs_manager()->CreateInitialFileSystemLayout());
{
gscoped_ptr<WritableFile> writer;
std::unique_ptr<WritableFile> writer;
ASSERT_OK(env_->NewWritableFile(
JoinPathSegments(GetServerTypeDataPath(path, kServerType), "some_file"), &writer));
}
Expand Down
1 change: 1 addition & 0 deletions src/yb/master/mini_master.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <string>
#include <vector>

#include "yb/gutil/gscoped_ptr.h"
#include "yb/gutil/macros.h"
#include "yb/gutil/port.h"
#include "yb/util/env.h"
Expand Down
2 changes: 1 addition & 1 deletion src/yb/rpc/rpc-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ TEST_F(TestRpc, TestHighFDs) {
// Open a bunch of fds just to increase our fd count.
std::vector<std::unique_ptr<RandomAccessFile>> fake_files;
for (uint64_t i = 0; i < kNumFakeFiles; i++) {
gscoped_ptr<RandomAccessFile> f;
std::unique_ptr<RandomAccessFile> f;
CHECK_OK(Env::Default()->NewRandomAccessFile("/dev/zero", &f));
fake_files.emplace_back(f.release());
}
Expand Down
4 changes: 2 additions & 2 deletions src/yb/tools/pbc-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ namespace pb_util {

Status DumpPBContainerFile(const string& filename) {
Env* env = Env::Default();
gscoped_ptr<RandomAccessFile> reader;
std::unique_ptr<RandomAccessFile> reader;
RETURN_NOT_OK(env->NewRandomAccessFile(filename, &reader));
ReadablePBContainerFile pb_reader(reader.Pass());
ReadablePBContainerFile pb_reader(std::move(reader));
RETURN_NOT_OK(pb_reader.Init());
RETURN_NOT_OK(pb_reader.Dump(&std::cout, FLAGS_oneline));

Expand Down
4 changes: 2 additions & 2 deletions src/yb/tserver/remote_bootstrap_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ Status RemoteBootstrapClient::DownloadFile(

WritableFileOptions opts;
opts.sync_on_close = true;
gscoped_ptr<WritableFile> file;
std::unique_ptr<WritableFile> file;
RETURN_NOT_OK(fs_manager_->env()->NewWritableFile(opts, file_path, &file));

data_id->set_file_name(file_pb.name());
Expand Down Expand Up @@ -781,7 +781,7 @@ Status RemoteBootstrapClient::DownloadWAL(uint64_t wal_segment_seqno) {

WritableFileOptions opts;
opts.sync_on_close = true;
gscoped_ptr<WritableFile> writer;
std::unique_ptr<WritableFile> writer;
RETURN_NOT_OK_PREPEND(fs_manager_->env()->NewWritableFile(opts, temp_dest_path, &writer),
"Unable to open file for writing");

Expand Down
2 changes: 1 addition & 1 deletion src/yb/tserver/remote_bootstrap_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ Status RemoteBootstrapSession::GetFilePiece(const std::string& path,
file_name, path));
}

gscoped_ptr<RandomAccessFile> readable_file;
std::unique_ptr<RandomAccessFile> readable_file;

RETURN_NOT_OK(fs_manager_->env()->NewRandomAccessFile(file_path, &readable_file));

Expand Down
32 changes: 16 additions & 16 deletions src/yb/util/env-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class TestEnv : public YBTest, public ::testing::WithParamInterface<bool> {
shared_ptr<RandomAccessFile> raf;
ASSERT_OK(env_util::OpenFileForRandom(env_.get(), file_path, &raf));
Slice slice;
gscoped_ptr<uint8_t[]> scratch(new uint8_t[file_size]);
std::unique_ptr<uint8_t[]> scratch(new uint8_t[file_size]);
ASSERT_OK(env_util::ReadFully(raf.get(), 0, file_size, &slice, scratch.get()));
ASSERT_EQ(file_size, slice.size());
uint64_t file_checksum = 0;
Expand All @@ -146,7 +146,7 @@ class TestEnv : public YBTest, public ::testing::WithParamInterface<bool> {
}

void MakeVectors(int num_slices, int slice_size, int num_iterations,
gscoped_ptr<faststring[]>* data, vector<vector<Slice > >* vec) {
std::unique_ptr<faststring[]>* data, vector<vector<Slice > >* vec) {
data->reset(new faststring[num_iterations * num_slices]);
vec->resize(num_iterations);

Expand All @@ -168,7 +168,7 @@ class TestEnv : public YBTest, public ::testing::WithParamInterface<bool> {
}

void ReadAndVerifyTestData(RandomAccessFile* raf, size_t offset, size_t n) {
gscoped_ptr<uint8_t[]> scratch(new uint8_t[n]);
std::unique_ptr<uint8_t[]> scratch(new uint8_t[n]);
Slice s;
ASSERT_OK(env_util::ReadFully(raf, offset, n, &s,
scratch.get()));
Expand All @@ -187,7 +187,7 @@ class TestEnv : public YBTest, public ::testing::WithParamInterface<bool> {
ASSERT_OK(file->Sync());
}

gscoped_ptr<faststring[]> data;
std::unique_ptr<faststring[]> data;
vector<vector<Slice> > input;

MakeVectors(num_slices, slice_size, iterations, &data, &input);
Expand Down Expand Up @@ -413,7 +413,7 @@ TEST_F(TestEnv, TestHolePunch) {
return;
}
string test_path = GetTestPath("test_env_wf");
gscoped_ptr<RWFile> file;
std::unique_ptr<RWFile> file;
ASSERT_OK(env_->NewRWFile(test_path, &file));

// Write 1 MB. The size and size-on-disk both agree.
Expand Down Expand Up @@ -496,7 +496,7 @@ TEST_F(TestEnv, TestReadFully) {
SeedRandom();
const string kTestPath = "test";
const int kFileSize = 64 * 1024;
gscoped_ptr<Env> mem(NewMemEnv(Env::Default()));
std::unique_ptr<Env> mem(NewMemEnv(Env::Default()));

WriteTestFile(mem.get(), kTestPath, kFileSize);
ASSERT_NO_FATALS();
Expand All @@ -509,7 +509,7 @@ TEST_F(TestEnv, TestReadFully) {

const int kReadLength = 10000;
Slice s;
gscoped_ptr<uint8_t[]> scratch(new uint8_t[kReadLength]);
std::unique_ptr<uint8_t[]> scratch(new uint8_t[kReadLength]);

// Verify that ReadFully reads the whole requested data.
ASSERT_OK(env_util::ReadFully(&sr_raf, 0, kReadLength, &s, scratch.get()));
Expand Down Expand Up @@ -559,7 +559,7 @@ TEST_F(TestEnv, TestOpenEmptyRandomAccessFile) {
Env* env = Env::Default();
string test_file = JoinPathSegments(GetTestDataDirectory(), "test_file");
ASSERT_NO_FATALS(WriteTestFile(env, test_file, 0));
gscoped_ptr<RandomAccessFile> readable_file;
std::unique_ptr<RandomAccessFile> readable_file;
ASSERT_OK(env->NewRandomAccessFile(test_file, &readable_file));
uint64_t size = ASSERT_RESULT(readable_file->Size());
ASSERT_EQ(0, size);
Expand Down Expand Up @@ -626,7 +626,7 @@ TEST_F(TestEnv, TestIsDirectory) {
ASSERT_TRUE(is_dir);

string not_dir = GetTestPath("not_a_directory");
gscoped_ptr<WritableFile> writer;
std::unique_ptr<WritableFile> writer;
ASSERT_OK(env_->NewWritableFile(not_dir, &writer));
ASSERT_OK(env_->IsDirectory(not_dir, &is_dir));
ASSERT_FALSE(is_dir);
Expand All @@ -647,7 +647,7 @@ static Status CreateDir(Env* env, const string& name, vector<string>* created) {
}

static Status CreateFile(Env* env, const string& name, vector<string>* created) {
gscoped_ptr<WritableFile> writer;
std::unique_ptr<WritableFile> writer;
RETURN_NOT_OK(env->NewWritableFile(name, &writer));
created->push_back(writer->filename());
return Status::OK();
Expand Down Expand Up @@ -707,7 +707,7 @@ TEST_F(TestEnv, TestWalkCbReturnsError) {
string new_dir = GetTestPath("foo");
string new_file = "myfile";
ASSERT_OK(env_->CreateDir(new_dir));
gscoped_ptr<WritableFile> writer;
std::unique_ptr<WritableFile> writer;
ASSERT_OK(env_->NewWritableFile(JoinPathSegments(new_dir, new_file), &writer));
int num_calls = 0;
ASSERT_TRUE(env_->Walk(new_dir, Env::PRE_ORDER,
Expand All @@ -728,15 +728,15 @@ TEST_F(TestEnv, TestGetBlockSize) {

// Try with a file.
string path = GetTestPath("foo");
gscoped_ptr<WritableFile> writer;
std::unique_ptr<WritableFile> writer;
ASSERT_OK(env_->NewWritableFile(path, &writer));
block_size = ASSERT_RESULT(env_->GetBlockSize(path));
ASSERT_GT(block_size, 0);
}

TEST_F(TestEnv, TestRWFile) {
// Create the file.
gscoped_ptr<RWFile> file;
std::unique_ptr<RWFile> file;
ASSERT_OK(env_->NewRWFile(GetTestPath("foo"), &file));

// Append to it.
Expand All @@ -745,7 +745,7 @@ TEST_F(TestEnv, TestRWFile) {

// Read from it.
Slice result;
gscoped_ptr<uint8_t[]> scratch(new uint8_t[kTestData.length()]);
std::unique_ptr<uint8_t[]> scratch(new uint8_t[kTestData.length()]);
ASSERT_OK(file->Read(0, kTestData.length(), &result, scratch.get()));
ASSERT_EQ(result, kTestData);
uint64_t sz;
Expand All @@ -757,7 +757,7 @@ TEST_F(TestEnv, TestRWFile) {
ASSERT_OK(file->Write(kTestData.length(), kTestData));
ASSERT_OK(file->Write(1, kTestData));
string kNewTestData = "aabcdebcdeabcde";
gscoped_ptr<uint8_t[]> scratch2(new uint8_t[kNewTestData.length()]);
std::unique_ptr<uint8_t[]> scratch2(new uint8_t[kNewTestData.length()]);
ASSERT_OK(file->Read(0, kNewTestData.length(), &result, scratch2.get()));

// Retest.
Expand Down Expand Up @@ -811,7 +811,7 @@ TEST_F(TestEnv, TestCopyFile) {
Env* env = Env::Default();
ASSERT_NO_FATALS(WriteTestFile(env, orig_path, kFileSize));
ASSERT_OK(env_util::CopyFile(env, orig_path, copy_path, WritableFileOptions()));
gscoped_ptr<RandomAccessFile> copy;
std::unique_ptr<RandomAccessFile> copy;
ASSERT_OK(env->NewRandomAccessFile(copy_path, &copy));
ASSERT_NO_FATALS(ReadAndVerifyTestData(copy.get(), 0, kFileSize));
}
Expand Down
6 changes: 3 additions & 3 deletions src/yb/util/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ FileLock::~FileLock() {
static Status DoWriteStringToFile(Env* env, const Slice& data,
const std::string& fname,
bool should_sync) {
gscoped_ptr<WritableFile> file;
std::unique_ptr<WritableFile> file;
Status s = env->NewWritableFile(fname, &file);
if (!s.ok()) {
return s;
Expand Down Expand Up @@ -86,13 +86,13 @@ Status WriteStringToFileSync(Env* env, const Slice& data,

Status ReadFileToString(Env* env, const std::string& fname, faststring* data) {
data->clear();
gscoped_ptr<SequentialFile> file;
std::unique_ptr<SequentialFile> file;
Status s = env->NewSequentialFile(fname, &file);
if (!s.ok()) {
return s;
}
static const int kBufferSize = 8192;
gscoped_ptr<uint8_t[]> scratch(new uint8_t[kBufferSize]);
std::unique_ptr<uint8_t[]> scratch(new uint8_t[kBufferSize]);
while (true) {
Slice fragment;
s = file->Read(kBufferSize, &fragment, scratch.get());
Expand Down
Loading

0 comments on commit 5adb348

Please sign in to comment.