This is an automated email from the ASF dual-hosted git repository.

laiyingchun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new 70d246739 refactor(file_system): remove useless read/write_file() 
(#1628)
70d246739 is described below

commit 70d2467393d9f4152059d7428859e9491776d49b
Author: Yingchun Lai <[email protected]>
AuthorDate: Tue Oct 10 12:30:00 2023 +0800

    refactor(file_system): remove useless read/write_file() (#1628)
    
    https://github.com/apache/incubator-pegasus/issues/887
    
    Remove `read_file()` and `write_file()` from `dsn::utils::filesystem` 
namespace.
---
 src/replica/bulk_load/replica_bulk_loader.cpp | 11 ++++---
 src/utils/filesystem.cpp                      | 45 ---------------------------
 src/utils/filesystem.h                        |  6 ----
 3 files changed, 7 insertions(+), 55 deletions(-)

diff --git a/src/replica/bulk_load/replica_bulk_loader.cpp 
b/src/replica/bulk_load/replica_bulk_loader.cpp
index 5261e4183..643816d42 100644
--- a/src/replica/bulk_load/replica_bulk_loader.cpp
+++ b/src/replica/bulk_load/replica_bulk_loader.cpp
@@ -16,6 +16,8 @@
 // under the License.
 
 #include <fmt/core.h>
+#include <rocksdb/env.h>
+#include <rocksdb/status.h>
 #include <functional>
 #include <memory>
 #include <unordered_map>
@@ -48,6 +50,7 @@
 #include "utils/fail_point.h"
 #include "utils/filesystem.h"
 #include "utils/fmt_logging.h"
+#include "utils/ports.h"
 #include "utils/string_view.h"
 #include "utils/thread_access_checker.h"
 
@@ -562,10 +565,10 @@ void replica_bulk_loader::download_sst_file(const 
std::string &remote_dir,
 error_code replica_bulk_loader::parse_bulk_load_metadata(const std::string 
&fname)
 {
     std::string buf;
-    error_code ec = utils::filesystem::read_file(fname, buf);
-    if (ec != ERR_OK) {
-        LOG_ERROR_PREFIX("read file {} failed, error = {}", fname, ec);
-        return ec;
+    auto s = rocksdb::ReadFileToString(rocksdb::Env::Default(), fname, &buf);
+    if (dsn_unlikely(!s.ok())) {
+        LOG_ERROR_PREFIX("read file {} failed, error = {}", fname, 
s.ToString());
+        return ERR_FILE_OPERATION_FAILED;
     }
 
     blob bb = blob::create_from_bytes(std::move(buf));
diff --git a/src/utils/filesystem.cpp b/src/utils/filesystem.cpp
index 2f80a51d5..a252d823b 100644
--- a/src/utils/filesystem.cpp
+++ b/src/utils/filesystem.cpp
@@ -48,7 +48,6 @@
 #include <sys/stat.h>
 // IWYU pragma: no_include <bits/struct_stat.h>
 #include <unistd.h>
-#include <istream>
 #include <memory>
 
 #include "utils/defer.h"
@@ -830,36 +829,6 @@ std::pair<error_code, bool> is_directory_empty(const 
std::string &dirname)
     return res;
 }
 
-error_code read_file(const std::string &fname, std::string &buf)
-{
-    if (!file_exists(fname)) {
-        LOG_ERROR("file({}) doesn't exist", fname);
-        return ERR_FILE_OPERATION_FAILED;
-    }
-
-    int64_t file_sz = 0;
-    if (!file_size(fname, file_sz)) {
-        LOG_ERROR("get file({}) size failed", fname);
-        return ERR_FILE_OPERATION_FAILED;
-    }
-
-    buf.resize(file_sz);
-    std::ifstream fin(fname, std::ifstream::in);
-    if (!fin.is_open()) {
-        LOG_ERROR("open file({}) failed", fname);
-        return ERR_FILE_OPERATION_FAILED;
-    }
-    fin.read(&buf[0], file_sz);
-    CHECK_EQ_MSG(file_sz,
-                 fin.gcount(),
-                 "read file({}) failed, file_size = {} but read size = {}",
-                 fname,
-                 file_sz,
-                 fin.gcount());
-    fin.close();
-    return ERR_OK;
-}
-
 bool verify_file(const std::string &fname,
                  FileDataType type,
                  const std::string &expected_md5,
@@ -933,20 +902,6 @@ bool create_directory(const std::string &path, std::string 
&absolute_path, std::
     return true;
 }
 
-bool write_file(const std::string &fname, std::string &buf)
-{
-    if (!file_exists(fname)) {
-        LOG_ERROR("file({}) doesn't exist", fname);
-        return false;
-    }
-
-    std::ofstream fstream;
-    fstream.open(fname.c_str());
-    fstream << buf;
-    fstream.close();
-    return true;
-}
-
 bool check_dir_rw(const std::string &path, std::string &err_msg)
 {
     FAIL_POINT_INJECT_F("filesystem_check_dir_rw", [path](string_view str) {
diff --git a/src/utils/filesystem.h b/src/utils/filesystem.h
index 440128a97..be62e7683 100644
--- a/src/utils/filesystem.h
+++ b/src/utils/filesystem.h
@@ -146,9 +146,6 @@ error_code deprecated_md5sum(const std::string &file_path, 
/*out*/ std::string &
 //          B is represent wheter the directory is empty, true means empty, 
otherwise false
 std::pair<error_code, bool> is_directory_empty(const std::string &dirname);
 
-// TODO(yingchun): remove it!
-error_code read_file(const std::string &fname, /*out*/ std::string &buf);
-
 // compare file metadata calculated by fname with expected md5 and file_size
 bool verify_file(const std::string &fname,
                  FileDataType type,
@@ -162,9 +159,6 @@ bool create_directory(const std::string &path,
                       /*out*/ std::string &absolute_path,
                       /*out*/ std::string &err_msg);
 
-// TODO(yingchun): remove it!
-bool write_file(const std::string &fname, std::string &buf);
-
 // check if directory is readable and writable
 // call `create_directory` before to make `path` exist
 bool check_dir_rw(const std::string &path, /*out*/ std::string &err_msg);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to