Flex  0.17.9
file_names.h
Go to the documentation of this file.
1 
15 #ifndef STORAGES_RT_MUTABLE_GRAPH_FILE_NAMES_H_
16 #define STORAGES_RT_MUTABLE_GRAPH_FILE_NAMES_H_
17 
18 #include <assert.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <filesystem>
26 
27 #include "glog/logging.h"
28 
29 namespace gs {
30 // clang-format off
31 /*
32  ├── schema
33  ├── runtime
34  │   ├── allocator // allocator dir
35  │   ├── tails // tails (mutable parts) of tables
36  │   │   ├── vertex_table_PERSON.col_0
37  │   │   ├── vertex_table_PERSON.col_1.data
38  │   │   └── vertex_table_PERSON.col_1.items
39  │   └── tmp // tmp dir, used for touched vertex maps, vertex tables and adjlists of csrs
40  │   ├── ie_PERSON_KNOWS_PERSON.adj
41  │   ├── oe_PERSON_KNOWS_PERSON.adj
42  │   ├── vertex_map_PERSON.indices
43  │   ├── vertex_map_PERSON.keys
44  │   ├── vertex_table_PERSON.col_0
45  │   ├── vertex_table_PERSON.col_1.data
46  │   └── vertex_table_PERSON.col_1.items
47  └── bulk_load_progress.log // bulk load progress file
48  ├── snapshots // snapshots dir
49  │   ├── 0
50  │   │   ├── ie_PERSON_KNOWS_PERSON.deg
51  │   │   ├── ie_PERSON_KNOWS_PERSON.nbr
52  │   │   ├── oe_PERSON_KNOWS_PERSON.deg
53  │   │   ├── oe_PERSON_KNOWS_PERSON.nbr
54  │   │   ├── vertex_map_PERSON.indices
55  │   │   ├── vertex_map_PERSON.keys
56  │   │   ├── vertex_map_PERSON.meta
57  │   │   ├── vertex_table_PERSON.col_0
58  │   │   ├── vertex_table_PERSON.col_1.data
59  │   │   └── vertex_table_PERSON.col_1.items
60  │   ├── 1234567
61  │   │   ├── ie_PERSON_KNOWS_PERSON.deg
62  │   │   ├── ie_PERSON_KNOWS_PERSON.nbr
63  │   │   ├── oe_PERSON_KNOWS_PERSON.deg
64  │   │   ├── oe_PERSON_KNOWS_PERSON.nbr
65  │   │   ├── vertex_map_PERSON.indices
66  │   │   ├── vertex_map_PERSON.keys
67  │   │   ├── vertex_map_PERSON.meta
68  │   │   ├── vertex_table_PERSON.col_0
69  │   │   ├── vertex_table_PERSON.col_1.data
70  │   │   └── vertex_table_PERSON.col_1.items
71  │   ├── ...
72  │   └── VERSION
73  └── wal // wal dir
74  ├── log_0
75  ├── log_1
76  └── ...
77 */
78 // clang-format on
79 
80 inline void copy_file(const std::string& src, const std::string& dst) {
81  if (!std::filesystem::exists(src)) {
82  LOG(ERROR) << "file not exists: " << src;
83  return;
84  }
85 #if USE_COPY_FILE_RANGE
86  size_t len = std::filesystem::file_size(src);
87  int src_fd = open(src.c_str(), O_RDONLY, 0777);
88  bool creat = false;
89  if (!std::filesystem::exists(dst)) {
90  creat = true;
91  }
92  int dst_fd = open(dst.c_str(), O_WRONLY | O_CREAT, 0777);
93  if (creat) {
94  std::filesystem::perms readWritePermission =
95  std::filesystem::perms::owner_read |
96  std::filesystem::perms::owner_write;
97  std::error_code errorCode;
98  std::filesystem::permissions(dst, readWritePermission,
99  std::filesystem::perm_options::add, errorCode);
100  if (errorCode) {
101  LOG(ERROR) << "Failed to set read/write permission for file: " << dst
102  << " " << errorCode.message() << std::endl;
103  }
104 
105  // For a newly created file, you may need to close and then reopen it,
106  // otherwise you may encounter a copy_file_range "Invalid cross-device link"
107  // error, one possible cause of the error could be that the
108  // file's metadata has not yet been flushed to the file system.
109  close(dst_fd);
110  dst_fd = open(dst.c_str(), O_WRONLY, 0777);
111  }
112  ssize_t ret;
113  do {
114  ret = copy_file_range(src_fd, NULL, dst_fd, NULL, len, 0);
115  if (ret == -1) {
116  perror("copy_file_range");
117  return;
118  }
119  len -= ret;
120  } while (len > 0 && ret > 0);
121  close(src_fd);
122  close(dst_fd);
123 #else
124  bool creat = false;
125  if (!std::filesystem::exists(dst)) {
126  creat = true;
127  }
128  std::error_code errorCode;
130  src, dst, std::filesystem::copy_options::overwrite_existing, errorCode);
131  if (errorCode) {
132  LOG(ERROR) << "Failed to copy file from " << src << " to " << dst << " "
133  << errorCode.message() << std::endl;
134  }
135  if (creat) {
136  std::filesystem::perms readWritePermission =
137  std::filesystem::perms::owner_read |
138  std::filesystem::perms::owner_write;
139  std::error_code errorCode;
140  std::filesystem::permissions(dst, readWritePermission,
141  std::filesystem::perm_options::add, errorCode);
142  if (errorCode) {
143  LOG(INFO) << "Failed to set read/write permission for file: " << dst
144  << " " << errorCode.message() << std::endl;
145  }
146  }
147 
148 #endif
149 }
150 
151 inline std::string schema_path(const std::string& work_dir) {
152  return work_dir + "/schema";
153 }
154 
155 inline std::string snapshots_dir(const std::string& work_dir) {
156  return work_dir + "/snapshots/";
157 }
158 
159 inline std::string snapshot_version_path(const std::string& work_dir) {
160  return snapshots_dir(work_dir) + "/VERSION";
161 }
162 
163 inline std::string get_latest_snapshot(const std::string& work_dir) {
164  std::string snapshots_dir = work_dir + "/snapshots";
165  uint32_t version;
166  {
167  FILE* fin = fopen((snapshots_dir + "/VERSION").c_str(), "r");
168  CHECK_EQ(fread(&version, sizeof(uint32_t), 1, fin), 1);
169  fclose(fin);
170  }
171  return snapshots_dir + "/" + std::to_string(version);
172 }
173 
174 inline uint32_t get_snapshot_version(const std::string& work_dir) {
175  std::string version_path = snapshot_version_path(work_dir);
176  FILE* version_file = fopen(version_path.c_str(), "rb");
177  uint32_t version = 0;
178  CHECK_EQ(fread(&version, sizeof(uint32_t), 1, version_file), 1);
179  fclose(version_file);
180  return version;
181 }
182 
183 inline void set_snapshot_version(const std::string& work_dir,
184  uint32_t version) {
185  std::string version_path = snapshot_version_path(work_dir);
186  FILE* version_file = fopen(version_path.c_str(), "wb");
187  CHECK_EQ(fwrite(&version, sizeof(uint32_t), 1, version_file), 1);
188  fflush(version_file);
189  fclose(version_file);
190 }
191 
192 inline std::string snapshot_dir(const std::string& work_dir, uint32_t version) {
193  return snapshots_dir(work_dir) + std::to_string(version) + "/";
194 }
195 
196 inline std::string wal_dir(const std::string& work_dir) {
197  return work_dir + "/wal/";
198 }
199 
200 inline std::string runtime_dir(const std::string& work_dir) {
201  return work_dir + "/runtime/";
202 }
203 
204 inline std::string update_txn_dir(const std::string& work_dir,
205  uint32_t version) {
206  return runtime_dir(work_dir) + "update_txn_" + std::to_string(version) + "/";
207 }
208 
209 inline std::string allocator_dir(const std::string& work_dir) {
210  return runtime_dir(work_dir) + "allocator/";
211 }
212 
213 inline std::string tmp_dir(const std::string& work_dir) {
214  return runtime_dir(work_dir) + "tmp/";
215 }
216 
217 inline std::string bulk_load_progress_file(const std::string& work_dir) {
218  return tmp_dir(work_dir) + "bulk_load_progress.log";
219 }
220 
221 inline void clear_tmp(const std::string& work_dir) {
222  std::string tmp_dir_str = tmp_dir(work_dir);
223  if (std::filesystem::exists(tmp_dir_str)) {
224  assert(std::filesystem::is_directory(tmp_dir_str));
225  if (std::filesystem::directory_iterator(tmp_dir_str) !=
226  std::filesystem::directory_iterator()) {
227  for (const auto& entry :
228  std::filesystem::directory_iterator(tmp_dir_str)) {
229  std::filesystem::remove_all(entry.path());
230  }
231  }
232  }
233 }
234 
235 inline std::string vertex_map_prefix(const std::string& label) {
236  return "vertex_map_" + label;
237 }
238 
239 inline std::string ie_prefix(const std::string& src_label,
240  const std::string& dst_label,
241  const std::string edge_label) {
242  return "ie_" + src_label + "_" + edge_label + "_" + dst_label;
243 }
244 
245 inline std::string oe_prefix(const std::string& src_label,
246  const std::string& dst_label,
247  const std::string edge_label) {
248  return "oe_" + src_label + "_" + edge_label + "_" + dst_label;
249 }
250 
251 inline std::string edata_prefix(const std::string& src_label,
252  const std::string& dst_label,
253  const std::string& edge_label) {
254  return "e_" + src_label + "_" + edge_label + "_" + dst_label + "_data";
255 }
256 inline std::string vertex_table_prefix(const std::string& label) {
257  return "vertex_table_" + label;
258 }
259 
260 inline std::string thread_local_allocator_prefix(const std::string& work_dir,
261  int thread_id) {
262  return allocator_dir(work_dir) + "allocator_" + std::to_string(thread_id) +
263  "_";
264 }
265 
266 } // namespace gs
267 
268 #endif // STORAGES_RT_MUTABLE_GRAPH_FILE_NAMES_H_
gs::oe_prefix
std::string oe_prefix(const std::string &src_label, const std::string &dst_label, const std::string edge_label)
Definition: file_names.h:245
gs::update_txn_dir
std::string update_txn_dir(const std::string &work_dir, uint32_t version)
Definition: file_names.h:204
gs::get_latest_snapshot
std::string get_latest_snapshot(const std::string &work_dir)
Definition: file_names.h:163
gs::vertex_table_prefix
std::string vertex_table_prefix(const std::string &label)
Definition: file_names.h:256
gs::edata_prefix
std::string edata_prefix(const std::string &src_label, const std::string &dst_label, const std::string &edge_label)
Definition: file_names.h:251
std::to_string
std::string to_string(const gs::flex::interactive::Code &status)
Definition: result.h:166
gs::set_snapshot_version
void set_snapshot_version(const std::string &work_dir, uint32_t version)
Definition: file_names.h:183
gs
Definition: adj_list.h:23
gs::clear_tmp
void clear_tmp(const std::string &work_dir)
Definition: file_names.h:221
gs::vertex_map_prefix
std::string vertex_map_prefix(const std::string &label)
Definition: file_names.h:235
gs::thread_local_allocator_prefix
std::string thread_local_allocator_prefix(const std::string &work_dir, int thread_id)
Definition: file_names.h:260
gs::snapshot_version_path
std::string snapshot_version_path(const std::string &work_dir)
Definition: file_names.h:159
gs::runtime_dir
std::string runtime_dir(const std::string &work_dir)
Definition: file_names.h:200
gs::bulk_load_progress_file
std::string bulk_load_progress_file(const std::string &work_dir)
Definition: file_names.h:217
gs::get_snapshot_version
uint32_t get_snapshot_version(const std::string &work_dir)
Definition: file_names.h:174
gs::copy_file
void copy_file(const std::string &src, const std::string &dst)
Definition: file_names.h:80
gs::snapshot_dir
std::string snapshot_dir(const std::string &work_dir, uint32_t version)
Definition: file_names.h:192
gs::ie_prefix
std::string ie_prefix(const std::string &src_label, const std::string &dst_label, const std::string edge_label)
Definition: file_names.h:239
gs::snapshots_dir
std::string snapshots_dir(const std::string &work_dir)
Definition: file_names.h:155
gs::allocator_dir
std::string allocator_dir(const std::string &work_dir)
Definition: file_names.h:209
gs::wal_dir
std::string wal_dir(const std::string &work_dir)
Definition: file_names.h:196
gs::tmp_dir
std::string tmp_dir(const std::string &work_dir)
Definition: file_names.h:213
gs::schema_path
std::string schema_path(const std::string &work_dir)
Definition: file_names.h:151