From 5adb3486295b3580403055442a0a1da7427ef0f8 Mon Sep 17 00:00:00 2001 From: Timur Yusupov Date: Sun, 30 Jun 2019 05:42:53 +0300 Subject: [PATCH] #1661: Replaced `gscoped_ptr` with `std::unique_ptr` for `yb::Env`. 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 --- src/yb/consensus/log-test.cc | 4 +- src/yb/consensus/log.cc | 6 +- src/yb/fs/fs_manager-test.cc | 6 +- src/yb/master/mini_master.h | 1 + src/yb/rpc/rpc-test.cc | 2 +- src/yb/tools/pbc-dump.cc | 4 +- src/yb/tserver/remote_bootstrap_client.cc | 4 +- src/yb/tserver/remote_bootstrap_session.cc | 2 +- src/yb/util/env-test.cc | 32 +++---- src/yb/util/env.cc | 6 +- src/yb/util/env.h | 99 ++++++++++------------ src/yb/util/env_posix.cc | 47 +++++----- src/yb/util/env_util.cc | 12 +-- src/yb/util/memenv/memenv-test.cc | 26 +++--- src/yb/util/memenv/memenv.cc | 26 +++--- src/yb/util/pb_util-internal.h | 7 +- src/yb/util/pb_util-test.cc | 32 +++---- src/yb/util/pb_util.cc | 35 ++++---- src/yb/util/pb_util.h | 13 +-- src/yb/util/rolling_log.cc | 4 +- src/yb/util/rolling_log.h | 2 +- src/yb/yql/pgwrapper/pg_wrapper-test.cc | 4 +- 22 files changed, 182 insertions(+), 192 deletions(-) diff --git a/src/yb/consensus/log-test.cc b/src/yb/consensus/log-test.cc index c93819f42dd7..5d2a80152144 100644 --- a/src/yb/consensus/log-test.cc +++ b/src/yb/consensus/log-test.cc @@ -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 w_log_seg; + std::unique_ptr w_log_seg; RETURN_NOT_OK(fs_manager_->env()->NewWritableFile(fqp, &w_log_seg)); - gscoped_ptr r_log_seg; + std::unique_ptr r_log_seg; RETURN_NOT_OK(fs_manager_->env()->NewRandomAccessFile(fqp, &r_log_seg)); scoped_refptr readable_segment( diff --git a/src/yb/consensus/log.cc b/src/yb/consensus/log.cc index a85c1799b560..38d9d7b6d5f9 100644 --- a/src/yb/consensus/log.cc +++ b/src/yb/consensus/log.cc @@ -1038,7 +1038,7 @@ Status Log::SwitchToAllocatedSegment() { } // Create a new segment. - gscoped_ptr new_segment( + std::unique_ptr new_segment( new WritableLogSegment(new_segment_path, next_segment_file_)); // Set up the new header and footer. @@ -1070,7 +1070,7 @@ Status Log::SwitchToAllocatedSegment() { } // Open the segment we just created in readable form and add it to the reader. - gscoped_ptr readable_file; + std::unique_ptr readable_file; RETURN_NOT_OK(get_env()->NewRandomAccessFile( RandomAccessFileOptions(), new_segment_path, &readable_file)); @@ -1111,7 +1111,7 @@ Status Log::CreatePlaceholderSegment(const WritableFileOptions& opts, shared_ptr* out) { string path_tmpl = JoinPathSegments(log_dir_, kSegmentPlaceholderFileTemplate); VLOG_WITH_PREFIX(2) << "Creating temp. file for place holder segment, template: " << path_tmpl; - gscoped_ptr segment_file; + std::unique_ptr segment_file; RETURN_NOT_OK(get_env()->NewTempWritableFile(opts, path_tmpl, result_path, diff --git a/src/yb/fs/fs_manager-test.cc b/src/yb/fs/fs_manager-test.cc index b66c1fb77408..decfea51ffab 100644 --- a/src/yb/fs/fs_manager-test.cc +++ b/src/yb/fs/fs_manager-test.cc @@ -141,7 +141,7 @@ class FsManagerTestBase : public YBTest { const char* kServerType = "tserver_test"; private: - gscoped_ptr fs_manager_; + std::unique_ptr fs_manager_; string log_dir_ = ""; }; @@ -191,7 +191,7 @@ TEST_F(FsManagerTestBase, TestListTablets) { ASSERT_EQ(0, tablet_ids.size()); string path = fs_manager()->GetRaftGroupMetadataDir(); - gscoped_ptr writer; + std::unique_ptr writer; ASSERT_OK(env_->NewWritableFile( JoinPathSegments(path, "foo.tmp"), &writer)); ASSERT_OK(env_->NewWritableFile( @@ -210,7 +210,7 @@ TEST_F(FsManagerTestBase, TestCannotUseNonEmptyFsRoot) { ReinitFsManager({ path }, { path }); ASSERT_OK(fs_manager()->CreateInitialFileSystemLayout()); { - gscoped_ptr writer; + std::unique_ptr writer; ASSERT_OK(env_->NewWritableFile( JoinPathSegments(GetServerTypeDataPath(path, kServerType), "some_file"), &writer)); } diff --git a/src/yb/master/mini_master.h b/src/yb/master/mini_master.h index d34e9902a184..e9bc384bd30b 100644 --- a/src/yb/master/mini_master.h +++ b/src/yb/master/mini_master.h @@ -35,6 +35,7 @@ #include #include +#include "yb/gutil/gscoped_ptr.h" #include "yb/gutil/macros.h" #include "yb/gutil/port.h" #include "yb/util/env.h" diff --git a/src/yb/rpc/rpc-test.cc b/src/yb/rpc/rpc-test.cc index 30b6d9419aed..b201f7d6e97a 100644 --- a/src/yb/rpc/rpc-test.cc +++ b/src/yb/rpc/rpc-test.cc @@ -250,7 +250,7 @@ TEST_F(TestRpc, TestHighFDs) { // Open a bunch of fds just to increase our fd count. std::vector> fake_files; for (uint64_t i = 0; i < kNumFakeFiles; i++) { - gscoped_ptr f; + std::unique_ptr f; CHECK_OK(Env::Default()->NewRandomAccessFile("/dev/zero", &f)); fake_files.emplace_back(f.release()); } diff --git a/src/yb/tools/pbc-dump.cc b/src/yb/tools/pbc-dump.cc index c8e672d584e0..f60d5c145d63 100644 --- a/src/yb/tools/pbc-dump.cc +++ b/src/yb/tools/pbc-dump.cc @@ -56,9 +56,9 @@ namespace pb_util { Status DumpPBContainerFile(const string& filename) { Env* env = Env::Default(); - gscoped_ptr reader; + std::unique_ptr 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)); diff --git a/src/yb/tserver/remote_bootstrap_client.cc b/src/yb/tserver/remote_bootstrap_client.cc index 67d7d6a1a093..4940fa8d9f89 100644 --- a/src/yb/tserver/remote_bootstrap_client.cc +++ b/src/yb/tserver/remote_bootstrap_client.cc @@ -700,7 +700,7 @@ Status RemoteBootstrapClient::DownloadFile( WritableFileOptions opts; opts.sync_on_close = true; - gscoped_ptr file; + std::unique_ptr file; RETURN_NOT_OK(fs_manager_->env()->NewWritableFile(opts, file_path, &file)); data_id->set_file_name(file_pb.name()); @@ -781,7 +781,7 @@ Status RemoteBootstrapClient::DownloadWAL(uint64_t wal_segment_seqno) { WritableFileOptions opts; opts.sync_on_close = true; - gscoped_ptr writer; + std::unique_ptr writer; RETURN_NOT_OK_PREPEND(fs_manager_->env()->NewWritableFile(opts, temp_dest_path, &writer), "Unable to open file for writing"); diff --git a/src/yb/tserver/remote_bootstrap_session.cc b/src/yb/tserver/remote_bootstrap_session.cc index e33f404b3066..2e649a35783d 100644 --- a/src/yb/tserver/remote_bootstrap_session.cc +++ b/src/yb/tserver/remote_bootstrap_session.cc @@ -438,7 +438,7 @@ Status RemoteBootstrapSession::GetFilePiece(const std::string& path, file_name, path)); } - gscoped_ptr readable_file; + std::unique_ptr readable_file; RETURN_NOT_OK(fs_manager_->env()->NewRandomAccessFile(file_path, &readable_file)); diff --git a/src/yb/util/env-test.cc b/src/yb/util/env-test.cc index 013d82acf638..446b0c96f40c 100644 --- a/src/yb/util/env-test.cc +++ b/src/yb/util/env-test.cc @@ -137,7 +137,7 @@ class TestEnv : public YBTest, public ::testing::WithParamInterface { shared_ptr raf; ASSERT_OK(env_util::OpenFileForRandom(env_.get(), file_path, &raf)); Slice slice; - gscoped_ptr scratch(new uint8_t[file_size]); + std::unique_ptr 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; @@ -146,7 +146,7 @@ class TestEnv : public YBTest, public ::testing::WithParamInterface { } void MakeVectors(int num_slices, int slice_size, int num_iterations, - gscoped_ptr* data, vector >* vec) { + std::unique_ptr* data, vector >* vec) { data->reset(new faststring[num_iterations * num_slices]); vec->resize(num_iterations); @@ -168,7 +168,7 @@ class TestEnv : public YBTest, public ::testing::WithParamInterface { } void ReadAndVerifyTestData(RandomAccessFile* raf, size_t offset, size_t n) { - gscoped_ptr scratch(new uint8_t[n]); + std::unique_ptr scratch(new uint8_t[n]); Slice s; ASSERT_OK(env_util::ReadFully(raf, offset, n, &s, scratch.get())); @@ -187,7 +187,7 @@ class TestEnv : public YBTest, public ::testing::WithParamInterface { ASSERT_OK(file->Sync()); } - gscoped_ptr data; + std::unique_ptr data; vector > input; MakeVectors(num_slices, slice_size, iterations, &data, &input); @@ -413,7 +413,7 @@ TEST_F(TestEnv, TestHolePunch) { return; } string test_path = GetTestPath("test_env_wf"); - gscoped_ptr file; + std::unique_ptr file; ASSERT_OK(env_->NewRWFile(test_path, &file)); // Write 1 MB. The size and size-on-disk both agree. @@ -496,7 +496,7 @@ TEST_F(TestEnv, TestReadFully) { SeedRandom(); const string kTestPath = "test"; const int kFileSize = 64 * 1024; - gscoped_ptr mem(NewMemEnv(Env::Default())); + std::unique_ptr mem(NewMemEnv(Env::Default())); WriteTestFile(mem.get(), kTestPath, kFileSize); ASSERT_NO_FATALS(); @@ -509,7 +509,7 @@ TEST_F(TestEnv, TestReadFully) { const int kReadLength = 10000; Slice s; - gscoped_ptr scratch(new uint8_t[kReadLength]); + std::unique_ptr 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())); @@ -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 readable_file; + std::unique_ptr readable_file; ASSERT_OK(env->NewRandomAccessFile(test_file, &readable_file)); uint64_t size = ASSERT_RESULT(readable_file->Size()); ASSERT_EQ(0, size); @@ -626,7 +626,7 @@ TEST_F(TestEnv, TestIsDirectory) { ASSERT_TRUE(is_dir); string not_dir = GetTestPath("not_a_directory"); - gscoped_ptr writer; + std::unique_ptr writer; ASSERT_OK(env_->NewWritableFile(not_dir, &writer)); ASSERT_OK(env_->IsDirectory(not_dir, &is_dir)); ASSERT_FALSE(is_dir); @@ -647,7 +647,7 @@ static Status CreateDir(Env* env, const string& name, vector* created) { } static Status CreateFile(Env* env, const string& name, vector* created) { - gscoped_ptr writer; + std::unique_ptr writer; RETURN_NOT_OK(env->NewWritableFile(name, &writer)); created->push_back(writer->filename()); return Status::OK(); @@ -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 writer; + std::unique_ptr writer; ASSERT_OK(env_->NewWritableFile(JoinPathSegments(new_dir, new_file), &writer)); int num_calls = 0; ASSERT_TRUE(env_->Walk(new_dir, Env::PRE_ORDER, @@ -728,7 +728,7 @@ TEST_F(TestEnv, TestGetBlockSize) { // Try with a file. string path = GetTestPath("foo"); - gscoped_ptr writer; + std::unique_ptr writer; ASSERT_OK(env_->NewWritableFile(path, &writer)); block_size = ASSERT_RESULT(env_->GetBlockSize(path)); ASSERT_GT(block_size, 0); @@ -736,7 +736,7 @@ TEST_F(TestEnv, TestGetBlockSize) { TEST_F(TestEnv, TestRWFile) { // Create the file. - gscoped_ptr file; + std::unique_ptr file; ASSERT_OK(env_->NewRWFile(GetTestPath("foo"), &file)); // Append to it. @@ -745,7 +745,7 @@ TEST_F(TestEnv, TestRWFile) { // Read from it. Slice result; - gscoped_ptr scratch(new uint8_t[kTestData.length()]); + std::unique_ptr scratch(new uint8_t[kTestData.length()]); ASSERT_OK(file->Read(0, kTestData.length(), &result, scratch.get())); ASSERT_EQ(result, kTestData); uint64_t sz; @@ -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 scratch2(new uint8_t[kNewTestData.length()]); + std::unique_ptr scratch2(new uint8_t[kNewTestData.length()]); ASSERT_OK(file->Read(0, kNewTestData.length(), &result, scratch2.get())); // Retest. @@ -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 copy; + std::unique_ptr copy; ASSERT_OK(env->NewRandomAccessFile(copy_path, ©)); ASSERT_NO_FATALS(ReadAndVerifyTestData(copy.get(), 0, kFileSize)); } diff --git a/src/yb/util/env.cc b/src/yb/util/env.cc index d536c99a18b3..39bcb995e1d7 100644 --- a/src/yb/util/env.cc +++ b/src/yb/util/env.cc @@ -53,7 +53,7 @@ FileLock::~FileLock() { static Status DoWriteStringToFile(Env* env, const Slice& data, const std::string& fname, bool should_sync) { - gscoped_ptr file; + std::unique_ptr file; Status s = env->NewWritableFile(fname, &file); if (!s.ok()) { return s; @@ -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 file; + std::unique_ptr file; Status s = env->NewSequentialFile(fname, &file); if (!s.ok()) { return s; } static const int kBufferSize = 8192; - gscoped_ptr scratch(new uint8_t[kBufferSize]); + std::unique_ptr scratch(new uint8_t[kBufferSize]); while (true) { Slice fragment; s = file->Read(kBufferSize, &fragment, scratch.get()); diff --git a/src/yb/util/env.h b/src/yb/util/env.h index f0fef0df6628..5aa33b0e3247 100644 --- a/src/yb/util/env.h +++ b/src/yb/util/env.h @@ -33,7 +33,6 @@ #include #include "yb/gutil/callback_forward.h" -#include "yb/gutil/gscoped_ptr.h" #include "yb/util/result.h" #include "yb/util/status.h" #include "yb/util/strongly_typed_bool.h" @@ -67,7 +66,7 @@ class FileFactory { // // The returned file will only be accessed by one thread at a time. virtual CHECKED_STATUS NewSequentialFile(const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Create a brand new random access read-only file with the // specified name. On success, stores a pointer to the new file in @@ -77,12 +76,12 @@ class FileFactory { // // The returned file may be concurrently accessed by multiple threads. virtual CHECKED_STATUS NewRandomAccessFile(const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Like the previous NewRandomAccessFile, but allows options to be specified. virtual CHECKED_STATUS NewRandomAccessFile(const RandomAccessFileOptions& opts, const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Create an object that writes to a new file with the specified // name. Deletes any existing file with the same name and creates a @@ -92,14 +91,14 @@ class FileFactory { // // The returned file will only be accessed by one thread at a time. virtual CHECKED_STATUS NewWritableFile(const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Like the previous NewWritableFile, but allows options to be // specified. virtual CHECKED_STATUS NewWritableFile(const WritableFileOptions& opts, const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Creates a new WritableFile provided the name_template parameter. // The last six characters of name_template must be "XXXXXX" and these are @@ -112,7 +111,7 @@ class FileFactory { virtual CHECKED_STATUS NewTempWritableFile(const WritableFileOptions& opts, const std::string& name_template, std::string* created_filename, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Creates a new readable and writable file. If a file with the same name // already exists on disk, it is deleted. @@ -120,12 +119,12 @@ class FileFactory { // Some of the methods of the new file may be accessed concurrently, // while others are only safe for access by one thread at a time. virtual CHECKED_STATUS NewRWFile(const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Like the previous NewRWFile, but allows options to be specified. virtual CHECKED_STATUS NewRWFile(const RWFileOptions& opts, const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; virtual Result GetFileSize(const std::string& fname) = 0; }; @@ -136,48 +135,48 @@ class FileFactoryWrapper : public FileFactory { virtual ~FileFactoryWrapper() {} CHECKED_STATUS NewSequentialFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return target_->NewSequentialFile(fname, result); } CHECKED_STATUS NewRandomAccessFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return target_->NewRandomAccessFile(fname, result); } // Like the previous NewRandomAccessFile, but allows options to be specified. CHECKED_STATUS NewRandomAccessFile(const RandomAccessFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return target_->NewRandomAccessFile(opts, fname, result); } CHECKED_STATUS NewWritableFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return target_->NewWritableFile(fname, result); } CHECKED_STATUS NewWritableFile(const WritableFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return target_->NewWritableFile(opts, fname, result); } CHECKED_STATUS NewTempWritableFile(const WritableFileOptions& opts, const std::string& name_template, std::string* created_filename, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return target_->NewTempWritableFile(opts, name_template, created_filename, result); } CHECKED_STATUS NewRWFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return target_->NewRWFile(fname, result); } // Like the previous NewRWFile, but allows options to be specified. CHECKED_STATUS NewRWFile(const RWFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return target_->NewRWFile(opts, fname, result); } @@ -225,7 +224,7 @@ class Env { // // The returned file will only be accessed by one thread at a time. virtual CHECKED_STATUS NewSequentialFile(const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Create a brand new random access read-only file with the // specified name. On success, stores a pointer to the new file in @@ -235,16 +234,13 @@ class Env { // // The returned file may be concurrently accessed by multiple threads. virtual CHECKED_STATUS NewRandomAccessFile(const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Like the previous NewRandomAccessFile, but allows options to be specified. virtual CHECKED_STATUS NewRandomAccessFile(const RandomAccessFileOptions& opts, - const std::string& fname, - gscoped_ptr* result) = 0; - - // Wrapper function to instantiate RandomAccessFile into a unique_ptr. - virtual CHECKED_STATUS NewRandomAccessFile(const std::string& fname, + const std::string& fname, std::unique_ptr* result) = 0; + // Create an object that writes to a new file with the specified // name. Deletes any existing file with the same name and creates a // new file. On success, stores a pointer to the new file in @@ -253,14 +249,14 @@ class Env { // // The returned file will only be accessed by one thread at a time. virtual CHECKED_STATUS NewWritableFile(const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Like the previous NewWritableFile, but allows options to be // specified. virtual CHECKED_STATUS NewWritableFile(const WritableFileOptions& opts, - const std::string& fname, - gscoped_ptr* result) = 0; + const std::string& fname, + std::unique_ptr* result) = 0; // Creates a new WritableFile provided the name_template parameter. // The last six characters of name_template must be "XXXXXX" and these are @@ -271,9 +267,9 @@ class Env { // owner only. The implementation will create the file in a secure manner, // and will return an error Status if it is unable to open the file. virtual CHECKED_STATUS NewTempWritableFile(const WritableFileOptions& opts, - const std::string& name_template, - std::string* created_filename, - gscoped_ptr* result) = 0; + const std::string& name_template, + std::string* created_filename, + std::unique_ptr* result) = 0; // Creates a new readable and writable file. If a file with the same name // already exists on disk, it is deleted. @@ -281,12 +277,12 @@ class Env { // Some of the methods of the new file may be accessed concurrently, // while others are only safe for access by one thread at a time. virtual CHECKED_STATUS NewRWFile(const std::string& fname, - gscoped_ptr* result) = 0; + std::unique_ptr* result) = 0; // Like the previous NewRWFile, but allows options to be specified. virtual CHECKED_STATUS NewRWFile(const RWFileOptions& opts, - const std::string& fname, - gscoped_ptr* result) = 0; + const std::string& fname, + std::unique_ptr* result) = 0; // Returns true iff the named file exists. virtual bool FileExists(const std::string& fname) = 0; @@ -619,7 +615,7 @@ class WritableFile { class SequentialFileWrapper : public SequentialFile { public: - explicit SequentialFileWrapper(gscoped_ptr t) : target_(std::move(t)) { } + explicit SequentialFileWrapper(std::unique_ptr t) : target_(std::move(t)) { } virtual ~SequentialFileWrapper() { } // Return the target to which this SequentialFile forwards all calls. @@ -631,7 +627,7 @@ class SequentialFileWrapper : public SequentialFile { CHECKED_STATUS Skip(uint64_t n) override { return target_->Skip(n); } const std::string& filename() const override { return target_->filename(); } private: - gscoped_ptr target_; + std::unique_ptr target_; }; // An implementation of WritableFile that forwards all calls to another @@ -641,7 +637,7 @@ class SequentialFileWrapper : public SequentialFile { // protected virtual methods. class WritableFileWrapper : public WritableFile { public: - explicit WritableFileWrapper(gscoped_ptr t) : target_(std::move(t)) { } + explicit WritableFileWrapper(std::unique_ptr t) : target_(std::move(t)) { } virtual ~WritableFileWrapper() { } // Return the target to which this WritableFile forwards all calls. @@ -659,12 +655,12 @@ class WritableFileWrapper : public WritableFile { const std::string& filename() const override { return target_->filename(); } private: - gscoped_ptr target_; + std::unique_ptr target_; }; class RandomAccessFileWrapper : public RandomAccessFile { public: - explicit RandomAccessFileWrapper(gscoped_ptr t) : target_(std::move(t)) { } + explicit RandomAccessFileWrapper(std::unique_ptr t) : target_(std::move(t)) { } virtual ~RandomAccessFileWrapper() { } // Return the target to which this RandomAccessFile forwards all calls. @@ -678,7 +674,7 @@ class RandomAccessFileWrapper : public RandomAccessFile { const std::string& filename() const override { return target_->filename(); } size_t memory_footprint() const override { return target_->memory_footprint(); } private: - gscoped_ptr target_; + std::unique_ptr target_; }; // Creation-time options for RWFile @@ -804,40 +800,37 @@ class EnvWrapper : public Env { Env* target() const { return target_; } // The following text is boilerplate that forwards all methods to target() - CHECKED_STATUS NewSequentialFile(const std::string& f, gscoped_ptr* r) override { + CHECKED_STATUS NewSequentialFile(const std::string& f, + std::unique_ptr* r) override { return target_->NewSequentialFile(f, r); } CHECKED_STATUS NewRandomAccessFile(const std::string& f, - gscoped_ptr* r) override { + std::unique_ptr* r) override { return target_->NewRandomAccessFile(f, r); } CHECKED_STATUS NewRandomAccessFile(const RandomAccessFileOptions& opts, - const std::string& f, - gscoped_ptr* r) override { + const std::string& f, + std::unique_ptr* r) override { return target_->NewRandomAccessFile(opts, f, r); } - CHECKED_STATUS NewRandomAccessFile(const std::string& fname, - std::unique_ptr* result) override { - return target_->NewRandomAccessFile(fname, result); - } - CHECKED_STATUS NewWritableFile(const std::string& f, gscoped_ptr* r) override { + CHECKED_STATUS NewWritableFile(const std::string& f, std::unique_ptr* r) override { return target_->NewWritableFile(f, r); } CHECKED_STATUS NewWritableFile(const WritableFileOptions& o, - const std::string& f, - gscoped_ptr* r) override { + const std::string& f, + std::unique_ptr* r) override { return target_->NewWritableFile(o, f, r); } CHECKED_STATUS NewTempWritableFile(const WritableFileOptions& o, const std::string& t, - std::string* f, gscoped_ptr* r) override { + std::string* f, std::unique_ptr* r) override { return target_->NewTempWritableFile(o, t, f, r); } - CHECKED_STATUS NewRWFile(const std::string& f, gscoped_ptr* r) override { + CHECKED_STATUS NewRWFile(const std::string& f, std::unique_ptr* r) override { return target_->NewRWFile(f, r); } CHECKED_STATUS NewRWFile(const RWFileOptions& o, const std::string& f, - gscoped_ptr* r) override { + std::unique_ptr* r) override { return target_->NewRWFile(o, f, r); } bool FileExists(const std::string& f) override { return target_->FileExists(f); } diff --git a/src/yb/util/env_posix.cc b/src/yb/util/env_posix.cc index 16a3f1266bae..9ad61deec104 100644 --- a/src/yb/util/env_posix.cc +++ b/src/yb/util/env_posix.cc @@ -1006,55 +1006,47 @@ class PosixEnv : public Env { } virtual Status NewSequentialFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return file_factory_->NewSequentialFile(fname, result); } virtual Status NewRandomAccessFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return file_factory_->NewRandomAccessFile(fname, result); } virtual Status NewRandomAccessFile(const RandomAccessFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { - return file_factory_->NewRandomAccessFile(opts, fname, result); - } - - virtual Status NewRandomAccessFile(const std::string& fname, std::unique_ptr* result) override { - gscoped_ptr temp; - RETURN_NOT_OK(NewRandomAccessFile(fname, &temp)); - result->reset(temp.release()); - return Status::OK(); + return file_factory_->NewRandomAccessFile(opts, fname, result); } virtual Status NewWritableFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return file_factory_->NewWritableFile(fname, result); } virtual Status NewWritableFile(const WritableFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return file_factory_->NewWritableFile(opts, fname, result); } virtual Status NewTempWritableFile(const WritableFileOptions& opts, const std::string& name_template, std::string* created_filename, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return file_factory_->NewTempWritableFile(opts, name_template, created_filename, result); } virtual Status NewRWFile(const string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return file_factory_->NewRWFile(fname, result); } virtual Status NewRWFile(const RWFileOptions& opts, const string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return file_factory_->NewRWFile(opts, fname, result); } @@ -1271,7 +1263,7 @@ class PosixEnv : public Env { uint32_t size = 64; uint32_t len = 0; while (true) { - gscoped_ptr buf(new char[size]); + std::unique_ptr buf(new char[size]); #if defined(__linux__) int rc = readlink("/proc/self/exe", buf.get(), size); if (rc == -1) { @@ -1470,7 +1462,8 @@ class PosixFileFactory : public FileFactory { PosixFileFactory() {} ~PosixFileFactory() {} - Status NewSequentialFile(const std::string& fname, gscoped_ptr* result) override { + Status NewSequentialFile( + const std::string& fname, std::unique_ptr* result) override { TRACE_EVENT1("io", "PosixEnv::NewSequentialFile", "path", fname); ThreadRestrictions::AssertIOAllowed(); FILE* f = fopen(fname.c_str(), "r"); @@ -1483,13 +1476,13 @@ class PosixFileFactory : public FileFactory { } Status NewRandomAccessFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return NewRandomAccessFile(RandomAccessFileOptions(), fname, result); } Status NewRandomAccessFile(const RandomAccessFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { TRACE_EVENT1("io", "PosixEnv::NewRandomAccessFile", "path", fname); ThreadRestrictions::AssertIOAllowed(); int fd = open(fname.c_str(), O_RDONLY); @@ -1502,13 +1495,13 @@ class PosixFileFactory : public FileFactory { } Status NewWritableFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return NewWritableFile(WritableFileOptions(), fname, result); } Status NewWritableFile(const WritableFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { TRACE_EVENT1("io", "PosixEnv::NewWritableFile", "path", fname); int fd = -1; int extra_flags = 0; @@ -1522,10 +1515,10 @@ class PosixFileFactory : public FileFactory { Status NewTempWritableFile(const WritableFileOptions& opts, const std::string& name_template, std::string* created_filename, - gscoped_ptr* result) override { + std::unique_ptr* result) override { TRACE_EVENT1("io", "PosixEnv::NewTempWritableFile", "template", name_template); ThreadRestrictions::AssertIOAllowed(); - gscoped_ptr fname(new char[name_template.size() + 1]); + std::unique_ptr fname(new char[name_template.size() + 1]); ::snprintf(fname.get(), name_template.size() + 1, "%s", name_template.c_str()); int fd = -1; if (UseODirect(opts.o_direct)) { @@ -1541,12 +1534,12 @@ class PosixFileFactory : public FileFactory { return InstantiateNewWritableFile(*created_filename, fd, opts, result); } - Status NewRWFile(const string& fname, gscoped_ptr* result) override { + Status NewRWFile(const string& fname, std::unique_ptr* result) override { return NewRWFile(RWFileOptions(), fname, result); } Status NewRWFile(const RWFileOptions& opts, const string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { TRACE_EVENT1("io", "PosixEnv::NewRWFile", "path", fname); int fd = -1; RETURN_NOT_OK(DoOpen(fname, opts.mode, &fd)); @@ -1571,7 +1564,7 @@ class PosixFileFactory : public FileFactory { Status InstantiateNewWritableFile(const std::string& fname, int fd, const WritableFileOptions& opts, - gscoped_ptr* result) { + std::unique_ptr* result) { uint64_t file_size = 0; if (opts.mode == PosixEnv::OPEN_EXISTING) { file_size = VERIFY_RESULT(GetFileSize(fname)); diff --git a/src/yb/util/env_util.cc b/src/yb/util/env_util.cc index 4a9f15e628dc..84d16024d5c8 100644 --- a/src/yb/util/env_util.cc +++ b/src/yb/util/env_util.cc @@ -117,7 +117,7 @@ Status OpenFileForWrite(Env* env, const string& path, Status OpenFileForWrite(const WritableFileOptions& opts, Env *env, const string &path, shared_ptr *file) { - gscoped_ptr w; + std::unique_ptr w; RETURN_NOT_OK(env->NewWritableFile(opts, path, &w)); file->reset(w.release()); return Status::OK(); @@ -125,7 +125,7 @@ Status OpenFileForWrite(const WritableFileOptions& opts, Status OpenFileForRandom(Env *env, const string &path, shared_ptr *file) { - gscoped_ptr r; + std::unique_ptr r; RETURN_NOT_OK(env->NewRandomAccessFile(path, &r)); file->reset(r.release()); return Status::OK(); @@ -133,7 +133,7 @@ Status OpenFileForRandom(Env *env, const string &path, Status OpenFileForSequential(Env *env, const string &path, shared_ptr *file) { - gscoped_ptr r; + std::unique_ptr r; RETURN_NOT_OK(env->NewSequentialFile(path, &r)); file->reset(r.release()); return Status::OK(); @@ -185,16 +185,16 @@ Status CreateDirIfMissing(Env* env, const string& path, bool* created) { Status CopyFile(Env* env, const string& source_path, const string& dest_path, WritableFileOptions opts) { - gscoped_ptr source; + std::unique_ptr source; RETURN_NOT_OK(env->NewSequentialFile(source_path, &source)); uint64_t size = VERIFY_RESULT(env->GetFileSize(source_path)); - gscoped_ptr dest; + std::unique_ptr dest; RETURN_NOT_OK(env->NewWritableFile(opts, dest_path, &dest)); RETURN_NOT_OK(dest->PreAllocate(size)); const int32_t kBufferSize = 1024 * 1024; - gscoped_ptr scratch(new uint8_t[kBufferSize]); + std::unique_ptr scratch(new uint8_t[kBufferSize]); uint64_t bytes_read = 0; while (bytes_read < size) { diff --git a/src/yb/util/memenv/memenv-test.cc b/src/yb/util/memenv/memenv-test.cc index 1f4066afb119..d7b330d0f5c2 100644 --- a/src/yb/util/memenv/memenv-test.cc +++ b/src/yb/util/memenv/memenv-test.cc @@ -53,7 +53,7 @@ class MemEnvTest : public ::testing::Test { TEST_F(MemEnvTest, Basics) { uint64_t file_size; - gscoped_ptr writable_file; + std::unique_ptr writable_file; vector children; // Create the directory. @@ -97,8 +97,8 @@ TEST_F(MemEnvTest, Basics) { ASSERT_EQ(3, file_size); // Check that opening non-existent file fails. - gscoped_ptr seq_file; - gscoped_ptr rand_file; + std::unique_ptr seq_file; + std::unique_ptr rand_file; ASSERT_TRUE(!env_->NewSequentialFile("/dir/non_existent", &seq_file).ok()); ASSERT_TRUE(!seq_file); ASSERT_TRUE(!env_->NewRandomAccessFile("/dir/non_existent", &rand_file).ok()); @@ -121,7 +121,7 @@ TEST_F(MemEnvTest, ReadWrite) { ASSERT_OK(env_->CreateDir("/dir")); { - gscoped_ptr writable_file; + std::unique_ptr writable_file; ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file)); ASSERT_OK(writable_file->Append("hello ")); ASSERT_OK(writable_file->Append("world")); @@ -129,7 +129,7 @@ TEST_F(MemEnvTest, ReadWrite) { { // Read sequentially. - gscoped_ptr seq_file; + std::unique_ptr seq_file; ASSERT_OK(env_->NewSequentialFile("/dir/f", &seq_file)); ASSERT_OK(seq_file->Read(5, &result, scratch)); // Read "hello". ASSERT_EQ(0, result.compare("hello")); @@ -145,7 +145,7 @@ TEST_F(MemEnvTest, ReadWrite) { { // Random reads. - gscoped_ptr rand_file; + std::unique_ptr rand_file; ASSERT_OK(env_->NewRandomAccessFile("/dir/f", &rand_file)); ASSERT_OK(rand_file->Read(6, 5, &result, scratch)); // Read "world". ASSERT_EQ(0, result.compare("world")); @@ -172,7 +172,7 @@ TEST_F(MemEnvTest, Misc) { ASSERT_OK(env_->GetTestDirectory(&test_dir)); ASSERT_TRUE(!test_dir.empty()); - gscoped_ptr writable_file; + std::unique_ptr writable_file; ASSERT_OK(env_->NewWritableFile("/a/b", &writable_file)); // These are no-ops, but we test they return success. @@ -184,20 +184,20 @@ TEST_F(MemEnvTest, Misc) { TEST_F(MemEnvTest, LargeWrite) { const size_t kWriteSize = 300 * 1024; - gscoped_ptr scratch(new uint8_t[kWriteSize * 2]); + std::unique_ptr scratch(new uint8_t[kWriteSize * 2]); string write_data; for (size_t i = 0; i < kWriteSize; ++i) { write_data.append(1, static_cast(i)); } - gscoped_ptr writable_file; + std::unique_ptr writable_file; ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file)); ASSERT_OK(writable_file->Append("foo")); ASSERT_OK(writable_file->Append(write_data)); writable_file.reset(); - gscoped_ptr seq_file; + std::unique_ptr seq_file; Slice result; ASSERT_OK(env_->NewSequentialFile("/dir/f", &seq_file)); ASSERT_OK(seq_file->Read(3, &result, scratch.get())); // Read "foo". @@ -267,7 +267,7 @@ TEST_F(MemEnvTest, TempFile) { string bad_tmpl = "foo.YYY"; string path; - gscoped_ptr file; + std::unique_ptr file; // Ensure we don't accept a bad template. Status s = env_->NewTempWritableFile(WritableFileOptions(), bad_tmpl, &path, &file); @@ -295,7 +295,7 @@ TEST_F(MemEnvTest, TempFile) { TEST_F(MemEnvTest, TestRWFile) { // Create the file. - gscoped_ptr file; + std::unique_ptr file; ASSERT_OK(env_->NewRWFile("foo", &file)); // Append to it. @@ -304,7 +304,7 @@ TEST_F(MemEnvTest, TestRWFile) { // Read from it. Slice result; - gscoped_ptr scratch(new uint8_t[kTestData.length()]); + std::unique_ptr scratch(new uint8_t[kTestData.length()]); ASSERT_OK(file->Read(0, kTestData.length(), &result, scratch.get())); ASSERT_EQ(result, kTestData); diff --git a/src/yb/util/memenv/memenv.cc b/src/yb/util/memenv/memenv.cc index 171fbce79171..090836283469 100644 --- a/src/yb/util/memenv/memenv.cc +++ b/src/yb/util/memenv/memenv.cc @@ -364,7 +364,7 @@ class InMemoryEnv : public EnvWrapper { // Partial implementation of the Env interface. virtual Status NewSequentialFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { MutexLock lock(mutex_); if (file_map_.find(fname) == file_map_.end()) { return STATUS(IOError, fname, "File not found"); @@ -375,13 +375,13 @@ class InMemoryEnv : public EnvWrapper { } virtual Status NewRandomAccessFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return NewRandomAccessFile(RandomAccessFileOptions(), fname, result); } virtual Status NewRandomAccessFile(const RandomAccessFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { MutexLock lock(mutex_); if (file_map_.find(fname) == file_map_.end()) { return STATUS(IOError, fname, "File not found"); @@ -393,36 +393,36 @@ class InMemoryEnv : public EnvWrapper { virtual Status NewWritableFile(const WritableFileOptions& opts, const std::string& fname, - gscoped_ptr* result) override { - gscoped_ptr wf; + std::unique_ptr* result) override { + std::unique_ptr wf; RETURN_NOT_OK(CreateAndRegisterNewFile(fname, opts.mode, &wf)); result->reset(wf.release()); return Status::OK(); } virtual Status NewWritableFile(const std::string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return NewWritableFile(WritableFileOptions(), fname, result); } virtual Status NewRWFile(const RWFileOptions& opts, const string& fname, - gscoped_ptr* result) override { - gscoped_ptr rwf; + std::unique_ptr* result) override { + std::unique_ptr rwf; RETURN_NOT_OK(CreateAndRegisterNewFile(fname, opts.mode, &rwf)); result->reset(rwf.release()); return Status::OK(); } virtual Status NewRWFile(const string& fname, - gscoped_ptr* result) override { + std::unique_ptr* result) override { return NewRWFile(RWFileOptions(), fname, result); } virtual Status NewTempWritableFile(const WritableFileOptions& opts, const std::string& name_template, std::string* created_filename, - gscoped_ptr* result) override { + std::unique_ptr* result) override { // Not very random, but InMemoryEnv is basically a test env. Random random(GetCurrentTimeMicros()); while (true) { @@ -478,7 +478,7 @@ class InMemoryEnv : public EnvWrapper { } Status CreateDir(const std::string& dirname) override { - gscoped_ptr file; + std::unique_ptr file; return NewWritableFile(dirname, &file); } @@ -590,7 +590,7 @@ class InMemoryEnv : public EnvWrapper { // Create new internal representation of a writable file. template void CreateAndRegisterNewWritableFileUnlocked(const string& path, - gscoped_ptr* result) { + std::unique_ptr* result) { file_map_[path] = make_scoped_refptr(new FileState(path)); result->reset(new ImplType(file_map_[path])); } @@ -599,7 +599,7 @@ class InMemoryEnv : public EnvWrapper { template Status CreateAndRegisterNewFile(const string& fname, CreateMode mode, - gscoped_ptr* result) { + std::unique_ptr* result) { MutexLock lock(mutex_); if (ContainsKey(file_map_, fname)) { switch (mode) { diff --git a/src/yb/util/pb_util-internal.h b/src/yb/util/pb_util-internal.h index 07f86690ecb3..4a24b0ab7e89 100644 --- a/src/yb/util/pb_util-internal.h +++ b/src/yb/util/pb_util-internal.h @@ -78,7 +78,7 @@ class SequentialFileFileInputStream : public google::protobuf::io::ZeroCopyInput size_t buffer_used_; size_t buffer_offset_; const size_t buffer_size_; - gscoped_ptr buffer_; + std::unique_ptr buffer_; size_t total_read_; SequentialFile *rfile_; @@ -125,7 +125,7 @@ class WritableFileOutputStream : public google::protobuf::io::ZeroCopyOutputStre size_t buffer_offset_; const size_t buffer_size_; - gscoped_ptr buffer_; + std::unique_ptr buffer_; size_t flushed_; WritableFile *wfile_; @@ -134,4 +134,5 @@ class WritableFileOutputStream : public google::protobuf::io::ZeroCopyOutputStre } // namespace internal } // namespace pb_util } // namespace yb -#endif + +#endif // YB_UTIL_PB_UTIL_INTERNAL_H diff --git a/src/yb/util/pb_util-test.cc b/src/yb/util/pb_util-test.cc index 7d215ed863bc..28ed7c3fdbaa 100644 --- a/src/yb/util/pb_util-test.cc +++ b/src/yb/util/pb_util-test.cc @@ -103,7 +103,7 @@ Status TestPBUtil::BitFlipFileByteRange(const string& path, uint64_t offset, uin faststring buf; // Read the data from disk. { - gscoped_ptr file; + std::unique_ptr file; RETURN_NOT_OK(env_->NewRandomAccessFile(path, &file)); uint64_t size = VERIFY_RESULT(file->Size()); Slice slice; @@ -120,7 +120,7 @@ Status TestPBUtil::BitFlipFileByteRange(const string& path, uint64_t offset, uin } // Write the data back to disk. - gscoped_ptr file; + std::unique_ptr file; RETURN_NOT_OK(env_->NewWritableFile(path, &file)); RETURN_NOT_OK(file->Append(buf)); RETURN_NOT_OK(file->Close()); @@ -129,7 +129,7 @@ Status TestPBUtil::BitFlipFileByteRange(const string& path, uint64_t offset, uin } TEST_F(TestPBUtil, TestWritableFileOutputStream) { - gscoped_ptr env(NewMemEnv(Env::Default())); + std::unique_ptr env(NewMemEnv(Env::Default())); shared_ptr file; ASSERT_OK(env_util::OpenFileForWrite(env.get(), "/test", &file)); @@ -204,7 +204,7 @@ TEST_F(TestPBUtil, TestPBContainerCorruption) { // Test that an empty file looks like corruption. { // Create the empty file. - gscoped_ptr file; + std::unique_ptr file; ASSERT_OK(env_->NewWritableFile(path_, &file)); ASSERT_OK(file->Close()); } @@ -268,9 +268,9 @@ TEST_F(TestPBUtil, TestMultipleMessages) { pb.set_name("foo"); pb.set_note("bar"); - gscoped_ptr writer; + std::unique_ptr writer; ASSERT_OK(env_->NewWritableFile(path_, &writer)); - WritablePBContainerFile pb_writer(writer.Pass()); + WritablePBContainerFile pb_writer(std::move(writer)); ASSERT_OK(pb_writer.Init(pb)); for (int i = 0; i < 10; i++) { @@ -280,9 +280,9 @@ TEST_F(TestPBUtil, TestMultipleMessages) { ASSERT_OK(pb_writer.Close()); int pbs_read = 0; - gscoped_ptr reader; + std::unique_ptr reader; ASSERT_OK(env_->NewRandomAccessFile(path_, &reader)); - ReadablePBContainerFile pb_reader(reader.Pass()); + ReadablePBContainerFile pb_reader(std::move(reader)); ASSERT_OK(pb_reader.Init()); for (int i = 0;; i++) { ProtoContainerTestPB read_pb; @@ -306,12 +306,12 @@ TEST_F(TestPBUtil, TestInterleavedReadWrite) { pb.set_note("bar"); // Open the file for writing and reading. - gscoped_ptr writer; + std::unique_ptr writer; ASSERT_OK(env_->NewWritableFile(path_, &writer)); - WritablePBContainerFile pb_writer(writer.Pass()); - gscoped_ptr reader; + WritablePBContainerFile pb_writer(std::move(writer)); + std::unique_ptr reader; ASSERT_OK(env_->NewRandomAccessFile(path_, &reader)); - ReadablePBContainerFile pb_reader(reader.Pass()); + ReadablePBContainerFile pb_reader(std::move(reader)); // Write the header (writer) and validate it (reader). ASSERT_OK(pb_writer.Init(pb)); @@ -363,9 +363,9 @@ TEST_F(TestPBUtil, TestPopulateDescriptorSet) { void TestPBUtil::DumpPBCToString(const string& path, bool oneline_output, string* ret) { - gscoped_ptr reader; + std::unique_ptr reader; ASSERT_OK(env_->NewRandomAccessFile(path, &reader)); - ReadablePBContainerFile pb_reader(reader.Pass()); + ReadablePBContainerFile pb_reader(std::move(reader)); ASSERT_OK(pb_reader.Init()); ostringstream oss; ASSERT_OK(pb_reader.Dump(&oss, oneline_output)); @@ -409,9 +409,9 @@ TEST_F(TestPBUtil, TestDumpPBContainer) { pb.mutable_record_one()->set_name("foo"); pb.mutable_record_two()->mutable_record()->set_name("foo"); - gscoped_ptr writer; + std::unique_ptr writer; ASSERT_OK(env_->NewWritableFile(path_, &writer)); - WritablePBContainerFile pb_writer(writer.Pass()); + WritablePBContainerFile pb_writer(std::move(writer)); ASSERT_OK(pb_writer.Init(pb)); for (int i = 0; i < 2; i++) { diff --git a/src/yb/util/pb_util.cc b/src/yb/util/pb_util.cc index fdef6ffab8b9..de904e383dec 100644 --- a/src/yb/util/pb_util.cc +++ b/src/yb/util/pb_util.cc @@ -202,7 +202,7 @@ Status WritePBToPath(Env* env, const std::string& path, const string tmp_template = path + kTmpTemplateSuffix; string tmp_path; - gscoped_ptr file; + std::unique_ptr file; RETURN_NOT_OK(env->NewTempWritableFile(WritableFileOptions(), tmp_template, &tmp_path, &file)); env_util::ScopedFileDeleter tmp_deleter(env, tmp_path); @@ -280,9 +280,9 @@ void TruncateFields(Message* message, int max_len) { } } -WritablePBContainerFile::WritablePBContainerFile(gscoped_ptr writer) +WritablePBContainerFile::WritablePBContainerFile(std::unique_ptr writer) : closed_(false), - writer_(writer.Pass()) { + writer_(std::move(writer)) { } WritablePBContainerFile::~WritablePBContainerFile() { @@ -427,9 +427,9 @@ void WritablePBContainerFile::PopulateDescriptorSet( all_descs.Swap(output); } -ReadablePBContainerFile::ReadablePBContainerFile(gscoped_ptr reader) +ReadablePBContainerFile::ReadablePBContainerFile(std::unique_ptr reader) : offset_(0), - reader_(reader.Pass()) { + reader_(std::move(reader)) { } ReadablePBContainerFile::~ReadablePBContainerFile() { @@ -439,7 +439,7 @@ ReadablePBContainerFile::~ReadablePBContainerFile() { Status ReadablePBContainerFile::Init() { // Read header data. Slice header; - gscoped_ptr scratch; + std::unique_ptr scratch; RETURN_NOT_OK_PREPEND(ValidateAndRead(kPBContainerHeaderLen, EOF_NOT_OK, &header, &scratch), Substitute("Could not read header for proto container file $0", reader_->filename())); @@ -479,7 +479,7 @@ Status ReadablePBContainerFile::ReadNextPB(Message* msg) { // Read the size from the file. EOF here is acceptable: it means we're // out of PB entries. Slice size; - gscoped_ptr size_scratch; + std::unique_ptr size_scratch; RETURN_NOT_OK_PREPEND(ValidateAndRead(sizeof(uint32_t), EOF_OK, &size, &size_scratch), Substitute("Could not read data size from proto container file $0", reader_->filename())); @@ -487,7 +487,7 @@ Status ReadablePBContainerFile::ReadNextPB(Message* msg) { // Read body into buffer for checksum & parsing. Slice body; - gscoped_ptr body_scratch; + std::unique_ptr body_scratch; RETURN_NOT_OK_PREPEND(ValidateAndRead(data_size, EOF_NOT_OK, &body, &body_scratch), Substitute("Could not read body from proto container file $0", reader_->filename())); @@ -496,7 +496,7 @@ Status ReadablePBContainerFile::ReadNextPB(Message* msg) { uint32_t expected_checksum = 0; { Slice encoded_checksum; - gscoped_ptr encoded_checksum_scratch; + std::unique_ptr encoded_checksum_scratch; RETURN_NOT_OK_PREPEND(ValidateAndRead(kPBContainerChecksumLen, EOF_NOT_OK, &encoded_checksum, &encoded_checksum_scratch), Substitute("Could not read checksum from proto container file $0", @@ -565,7 +565,7 @@ Status ReadablePBContainerFile::Dump(ostream* os, bool oneline) { "Descriptor $0 referenced in container file not supported", pb_type())); } - gscoped_ptr msg(prototype->New()); + std::unique_ptr msg(prototype->New()); // Dump each message in the container file. int count = 0; @@ -586,13 +586,14 @@ Status ReadablePBContainerFile::Dump(ostream* os, bool oneline) { } Status ReadablePBContainerFile::Close() { - gscoped_ptr deleter; + std::unique_ptr deleter; deleter.swap(reader_); return Status::OK(); } Status ReadablePBContainerFile::ValidateAndRead(size_t length, EofOK eofOK, - Slice* result, gscoped_ptr* scratch) { + Slice* result, + std::unique_ptr* scratch) { // Validate the read length using the file size. uint64_t file_size = VERIFY_RESULT(reader_->Size()); if (offset_ + length > file_size) { @@ -613,7 +614,7 @@ Status ReadablePBContainerFile::ValidateAndRead(size_t length, EofOK eofOK, // Perform the read. Slice s; - gscoped_ptr local_scratch(new uint8_t[length]); + std::unique_ptr local_scratch(new uint8_t[length]); RETURN_NOT_OK(reader_->Read(offset_, length, &s, local_scratch.get())); // Sanity check the result. @@ -631,10 +632,10 @@ Status ReadablePBContainerFile::ValidateAndRead(size_t length, EofOK eofOK, Status ReadPBContainerFromPath(Env* env, const std::string& path, Message* msg) { - gscoped_ptr file; + std::unique_ptr file; RETURN_NOT_OK(env->NewRandomAccessFile(path, &file)); - ReadablePBContainerFile pb_file(file.Pass()); + ReadablePBContainerFile pb_file(std::move(file)); RETURN_NOT_OK(pb_file.Init()); RETURN_NOT_OK(pb_file.ReadNextPB(msg)); return pb_file.Close(); @@ -655,11 +656,11 @@ Status WritePBContainerToPath(Env* env, const std::string& path, const string tmp_template = path + kTmpTemplateSuffix; string tmp_path; - gscoped_ptr file; + std::unique_ptr file; RETURN_NOT_OK(env->NewTempWritableFile(WritableFileOptions(), tmp_template, &tmp_path, &file)); env_util::ScopedFileDeleter tmp_deleter(env, tmp_path); - WritablePBContainerFile pb_file(file.Pass()); + WritablePBContainerFile pb_file(std::move(file)); RETURN_NOT_OK(pb_file.Init(msg)); RETURN_NOT_OK(pb_file.Append(msg)); if (sync == pb_util::SYNC) { diff --git a/src/yb/util/pb_util.h b/src/yb/util/pb_util.h index 5560f776f7a9..ec09f3b681bf 100644 --- a/src/yb/util/pb_util.h +++ b/src/yb/util/pb_util.h @@ -1,4 +1,5 @@ // Licensed to the Apache Software Foundation (ASF) under one +// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file @@ -197,7 +198,7 @@ class WritablePBContainerFile { public: // Initializes the class instance; writer must be open. - explicit WritablePBContainerFile(gscoped_ptr writer); + explicit WritablePBContainerFile(std::unique_ptr writer); // Closes the container if not already closed. ~WritablePBContainerFile(); @@ -243,7 +244,7 @@ class WritablePBContainerFile { bool closed_; - gscoped_ptr writer_; + std::unique_ptr writer_; }; // Protobuf container file opened for reading. @@ -254,7 +255,7 @@ class ReadablePBContainerFile { public: // Initializes the class instance; reader must be open. - explicit ReadablePBContainerFile(gscoped_ptr reader); + explicit ReadablePBContainerFile(std::unique_ptr reader); // Closes the file if not already closed. ~ReadablePBContainerFile(); @@ -296,7 +297,7 @@ class ReadablePBContainerFile { // If 'eofOK' is EOF_OK, an EOF is returned as-is. Otherwise, it is // considered to be an invalid short read and returned as an error. CHECKED_STATUS ValidateAndRead(size_t length, EofOK eofOK, - Slice* result, gscoped_ptr* scratch); + Slice* result, std::unique_ptr* scratch); size_t offset_; @@ -304,9 +305,9 @@ class ReadablePBContainerFile { std::string pb_type_; // Wrapped in a gscoped_ptr so that clients need not include PB headers. - gscoped_ptr protos_; + std::unique_ptr protos_; - gscoped_ptr reader_; + std::unique_ptr reader_; }; // Convenience functions for protobuf containers holding just one record. diff --git a/src/yb/util/rolling_log.cc b/src/yb/util/rolling_log.cc index 437bed4dfeb4..31f15d0462c8 100644 --- a/src/yb/util/rolling_log.cc +++ b/src/yb/util/rolling_log.cc @@ -34,11 +34,11 @@ #include #include +#include #include #include #include -#include #include "yb/gutil/strings/numbers.h" #include "yb/gutil/strings/substitute.h" @@ -230,7 +230,7 @@ class ScopedGzipCloser { // blocked. Implementing it using the zlib stream APIs isn't too much code // and is less likely to be problematic. Status RollingLog::CompressFile(const std::string& path) const { - gscoped_ptr in_file; + std::unique_ptr in_file; RETURN_NOT_OK_PREPEND(env_->NewSequentialFile(path, &in_file), "Unable to open input file to compress"); diff --git a/src/yb/util/rolling_log.h b/src/yb/util/rolling_log.h index 3889dcbae317..b13666328c45 100644 --- a/src/yb/util/rolling_log.h +++ b/src/yb/util/rolling_log.h @@ -112,7 +112,7 @@ class RollingLog { int64_t size_limit_bytes_; - gscoped_ptr file_; + std::unique_ptr file_; bool compress_after_close_; DISALLOW_COPY_AND_ASSIGN(RollingLog); diff --git a/src/yb/yql/pgwrapper/pg_wrapper-test.cc b/src/yb/yql/pgwrapper/pg_wrapper-test.cc index 7f6b3ca0e4f4..aadba9a5050e 100644 --- a/src/yb/yql/pgwrapper/pg_wrapper-test.cc +++ b/src/yb/yql/pgwrapper/pg_wrapper-test.cc @@ -63,7 +63,7 @@ class PgWrapperTest : public PgWrapperTestBase { std::string tmp_dir; ASSERT_OK(Env::Default()->GetTestDirectory(&tmp_dir)); - gscoped_ptr tmp_file; + std::unique_ptr tmp_file; std::string tmp_file_name; ASSERT_OK( Env::Default()->NewTempWritableFile( @@ -316,7 +316,7 @@ TEST_F(PgWrapperOneNodeClusterTest, YB_DISABLE_TEST_IN_TSAN(TestPostgresPid)) { // Create empty postmaster.pid file and ensure that tserver can start up // Use sync_on_close flag to ensure that the file is flushed to disk when tserver tries to read it - gscoped_ptr file; + std::unique_ptr file; RWFileOptions opts; opts.sync_on_close = true; opts.mode = Env::CREATE_IF_NON_EXISTING_TRUNCATE;