From f195d8d5231339242118937797d1b38e080cbcf7 Mon Sep 17 00:00:00 2001 From: sdong Date: Mon, 3 Feb 2020 17:38:50 -0800 Subject: [PATCH] Use ReadFileToString() to get content from IDENTITY file (#6365) Summary: Right now when reading IDENTITY file, we use a very similar logic as ReadFileToString() while it does an extra file size check, which may be expensive in some file systems. There is no reason to duplicate the logic. Use ReadFileToString() instead. Pull Request resolved: https://github.com/facebook/rocksdb/pull/6365 Test Plan: RUn all existing tests. Differential Revision: D19709399 fbshipit-source-id: 3bac31f3b2471f98a0d2694278b41e9cd34040fe --- db/db_impl/db_impl.cc | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/db/db_impl/db_impl.cc b/db/db_impl/db_impl.cc index 423af5dcfd..890ea09947 100644 --- a/db/db_impl/db_impl.cc +++ b/db/db_impl/db_impl.cc @@ -3354,31 +3354,12 @@ Status DBImpl::GetDbIdentity(std::string& identity) const { Status DBImpl::GetDbIdentityFromIdentityFile(std::string* identity) const { std::string idfilename = IdentityFileName(dbname_); const FileOptions soptions; - std::unique_ptr id_file_reader; - Status s; - { - std::unique_ptr idfile; - s = fs_->NewSequentialFile(idfilename, soptions, &idfile, nullptr); - if (!s.ok()) { - return s; - } - id_file_reader.reset( - new SequentialFileReader(std::move(idfile), idfilename)); + + Status s = ReadFileToString(fs_.get(), idfilename, identity); + if (!s.ok()) { + return s; } - uint64_t file_size; - s = fs_->GetFileSize(idfilename, IOOptions(), &file_size, nullptr); - if (!s.ok()) { - return s; - } - char* buffer = - reinterpret_cast(alloca(static_cast(file_size))); - Slice id; - s = id_file_reader->Read(static_cast(file_size), &id, buffer); - if (!s.ok()) { - return s; - } - identity->assign(id.ToString()); // If last character is '\n' remove it from identity if (identity->size() > 0 && identity->back() == '\n') { identity->pop_back();