This is an automated email from the ASF dual-hosted git repository.
wangdan 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 c2d9affe8 refactor(configuration): use rocksdb API to read/write file
(#1626)
c2d9affe8 is described below
commit c2d9affe8f997d6d95c08cb449b7008005fc2cca
Author: Yingchun Lai <[email protected]>
AuthorDate: Wed Sep 27 08:10:06 2023 -0500
refactor(configuration): use rocksdb API to read/write file (#1626)
https://github.com/apache/incubator-pegasus/issues/887
There is no functional changes, but only refactor the files read/write
method
of configuration module.
---
src/utils/configuration.cpp | 34 ++++++++++------------------------
1 file changed, 10 insertions(+), 24 deletions(-)
diff --git a/src/utils/configuration.cpp b/src/utils/configuration.cpp
index 9aea53448..c69a556d2 100644
--- a/src/utils/configuration.cpp
+++ b/src/utils/configuration.cpp
@@ -33,14 +33,16 @@
* xxxx-xx-xx, author, fix bug about xxx
*/
-#include <errno.h>
+#include <fmt/core.h>
+#include <rocksdb/env.h>
+#include <rocksdb/status.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <utility>
#include "utils/configuration.h"
-#include "utils/filesystem.h"
+#include "utils/env.h"
#include "utils/strings.h"
namespace dsn {
@@ -67,33 +69,17 @@ bool configuration::load(const char *file_name, const char
*arguments)
{
_file_name = std::string(file_name);
- FILE *fd = ::fopen(file_name, "rb");
- if (fd == nullptr) {
- std::string cdir;
- dsn::utils::filesystem::get_current_directory(cdir);
- printf("ERROR: cannot open file %s in %s, err = %s\n",
- file_name,
- cdir.c_str(),
- strerror(errno));
- return false;
- }
- ::fseek(fd, 0, SEEK_END);
- int len = ftell(fd);
- if (len == -1 || len == 0) {
- printf("ERROR: cannot get length of %s, err = %s\n", file_name,
strerror(errno));
- ::fclose(fd);
+ auto s = rocksdb::ReadFileToString(
+ dsn::utils::PegasusEnv(dsn::utils::FileDataType::kNonSensitive),
_file_name, &_file_data);
+ if (!s.ok()) {
+ fmt::print(stderr, "ERROR: read file '{}' failed, err = {}\n",
_file_name, s.ToString());
return false;
}
- _file_data.resize(len + 1);
- ::fseek(fd, 0, SEEK_SET);
- auto sz = ::fread((char *)_file_data.c_str(), len, 1, fd);
- ::fclose(fd);
- if (sz != 1) {
- printf("ERROR: cannot read correct data of %s, err = %s\n", file_name,
strerror(errno));
+ if (_file_data.empty()) {
+ fmt::print(stderr, "ERROR: file '{}' is empty\n", _file_name);
return false;
}
- _file_data[len] = '\n';
// replace data with arguments
if (arguments != nullptr) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]