Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
liulx20 committed Nov 30, 2023
1 parent c164ba4 commit cb8cea7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 55 deletions.
56 changes: 55 additions & 1 deletion flex/storages/rt_mutable_graph/file_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
#ifndef GRAPHSCOPE_STORAGES_RT_MUTABLE_GRAPH_FILE_NAMES_H_
#define GRAPHSCOPE_STORAGES_RT_MUTABLE_GRAPH_FILE_NAMES_H_

#include <string>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <filesystem>
#include "glog/logging.h"

namespace gs {
// clang-format off
Expand Down Expand Up @@ -67,6 +74,43 @@ namespace gs {
*/
// clang-format on

inline void copy_file(const std::string& src, const std::string& dst) {
if (!std::filesystem::exists(src)) {
LOG(ERROR) << "file not exists: " << src;
return;
}
size_t len = std::filesystem::file_size(src);
int src_fd = open(src.c_str(), O_RDONLY);
bool creat = false;
if (!std::filesystem::exists(dst)) {
creat = true;
}
int dst_fd = open(dst.c_str(), O_WRONLY | O_CREAT);
if (creat) {
std::filesystem::perms readWritePermission =
std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write;
std::error_code errorCode;
std::filesystem::permissions(dst, readWritePermission,
std::filesystem::perm_options::add, errorCode);
if (errorCode) {
LOG(INFO) << "Failed to set read/write permission for file: " << dst
<< " " << errorCode.message() << std::endl;
}
}
ssize_t ret;
do {
ret = copy_file_range(src_fd, NULL, dst_fd, NULL, len, 0);
if (ret == -1) {
perror("copy_file_range");
return;
}
len -= ret;
} while (len > 0 && ret > 0);
close(src_fd);
close(dst_fd);
}

inline std::string schema_path(const std::string& work_dir) {
return work_dir + "/schema";
}
Expand All @@ -79,6 +123,16 @@ inline std::string snapshot_version_path(const std::string& work_dir) {
return snapshots_dir(work_dir) + "/VERSION";
}

inline std::string get_latest_snapshot(const std::string& work_dir) {
std::string snapshots_dir = work_dir + "/snapshots";
uint32_t version;
{
FILE* fin = fopen((snapshots_dir + "/VERSION").c_str(), "r");
fread(&version, sizeof(uint32_t), 1, fin);
}
return snapshots_dir + "/" + std::to_string(version);
}

inline uint32_t get_snapshot_version(const std::string& work_dir) {
std::string version_path = snapshot_version_path(work_dir);
FILE* version_file = fopen(version_path.c_str(), "rb");
Expand Down
10 changes: 0 additions & 10 deletions flex/storages/rt_mutable_graph/mutable_property_fragment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,6 @@ inline MutableCsrBase* create_csr(EdgeStrategy es,
return nullptr;
}

std::string get_latest_snapshot(const std::string& work_dir) {
std::string snapshots_dir = work_dir + "/snapshots";
uint32_t version;
{
FILE* fin = fopen((snapshots_dir + "/VERSION").c_str(), "r");
fread(&version, sizeof(uint32_t), 1, fin);
}
return snapshots_dir + "/" + std::to_string(version);
}

void MutablePropertyFragment::Open(const std::string& work_dir) {
loadSchema(schema_path(work_dir));
vertex_label_num_ = schema_.vertex_label_num();
Expand Down
45 changes: 1 addition & 44 deletions flex/utils/mmap_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,17 @@
#define GRAPHSCOPE_UTILS_MMAP_ARRAY_H_

#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <atomic>
#include <filesystem>
#include <string>
#include <string_view>

#include "flex/storages/rt_mutable_graph/file_names.h"
#include "glog/logging.h"
#include "grape/util.h"

namespace gs {

inline void copy_file(const std::string& src, const std::string& dst) {
if (!std::filesystem::exists(src)) {
LOG(ERROR) << "file not exists: " << src;
return;
}
size_t len = std::filesystem::file_size(src);
int src_fd = open(src.c_str(), O_RDONLY);
bool creat = false;
if (!std::filesystem::exists(dst)) {
creat = true;
}
int dst_fd = open(dst.c_str(), O_WRONLY | O_CREAT);
if (creat) {
std::filesystem::perms readWritePermission =
std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write;
std::error_code errorCode;
std::filesystem::permissions(dst, readWritePermission,
std::filesystem::perm_options::add, errorCode);
if (errorCode) {
LOG(INFO) << "Failed to set read/write permission for file: " << dst
<< " " << errorCode.message() << std::endl;
}
}
ssize_t ret;
do {
ret = copy_file_range(src_fd, NULL, dst_fd, NULL, len, 0);
if (ret == -1) {
perror("copy_file_range");
return;
}
len -= ret;
} while (len > 0 && ret > 0);
close(src_fd);
close(dst_fd);
}

template <typename T>
class mmap_array {
public:
Expand Down

0 comments on commit cb8cea7

Please sign in to comment.