Derecho  0.9
Distributed systems toolkit for RDMA
PersistNoLog_impl.hpp
Go to the documentation of this file.
1 #ifndef PERSIST_NO_LOG_IMPL_HPP
2 #define PERSIST_NO_LOG_IMPL_HPP
3 
4 #define _NOLOG_OBJECT_DIR_ ((storageType == ST_MEM) ? getPersRamdiskPath().c_str() : getPersFilePath().c_str())
5 #define _NOLOG_OBJECT_NAME_ ((object_name == nullptr) ? typeid(ObjectType).name() : object_name)
6 
7 namespace persistent {
8 
9 template <typename ObjectType, StorageType storageType>
11  ObjectType& obj,
12  const char* object_name) noexcept(false) {
13  char filepath[256];
14  char tmpfilepath[260];
15 
16  // 0 - create dir
18  // 1 - get object file name
19  sprintf(filepath, "%s/%d-%s-nolog", _NOLOG_OBJECT_DIR_, storageType, _NOLOG_OBJECT_NAME_);
20  sprintf(tmpfilepath, "%s.tmp", filepath);
21  // 2 - serialize
22  auto size = mutils::bytes_size(obj);
23  char* buf = new char[size];
24  bzero(buf, size);
25  mutils::to_bytes(obj, buf);
26  // 3 - write to tmp file
27  int fd = open(tmpfilepath, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP | S_IROTH);
28  if(fd == -1) {
29  throw PERSIST_EXP_OPEN_FILE(errno);
30  }
31  ssize_t nWrite = write(fd, buf, size);
32  delete[] buf;
33  if(nWrite != (ssize_t)size) {
34  throw PERSIST_EXP_WRITE_FILE(errno);
35  }
36  close(fd);
37  // 4 - atomically rename
38  if(rename(tmpfilepath, filepath) != 0) {
39  throw PERSIST_EXP_RENAME_FILE(errno);
40  }
41 }
42 
43 template <typename ObjectType, StorageType storageType>
44 std::unique_ptr<ObjectType> loadNoLogObjectFromFile(
45  const char* object_name,
46  mutils::DeserializationManager* dm) noexcept(false) {
47  char filepath[256];
48 
49  // 0 - get object file name
50  sprintf(filepath, "%s/%d-%s-nolog", _NOLOG_OBJECT_DIR_, storageType, _NOLOG_OBJECT_NAME_);
51 
52  // 0.5 - object file
54  if(fs::exists(filepath)) {
55  if(!fs::remove(filepath)) {
56  dbg_default_error("{} loadNoLogObjectFromFile failed to remove file {}.", _NOLOG_OBJECT_NAME_, filepath);
57  throw PERSIST_EXP_REMOVE_FILE(errno);
58  }
59  }
60  }
61 
62  // 1 - load file
64  if(!checkRegularFile(filepath)) {
65  return std::unique_ptr<ObjectType>{};
66  }
67  int fd = open(filepath, O_RDONLY);
68  struct stat stat_buf;
69  if(fd == -1 || (fstat(fd, &stat_buf) != 0)) {
70  throw PERSIST_EXP_READ_FILE(errno);
71  }
72 
73  char* buf = new char[stat_buf.st_size];
74  if(!buf) {
75  close(fd);
76  throw PERSIST_EXP_OOM(errno);
77  }
78  if(read(fd, buf, stat_buf.st_size) != stat_buf.st_size) {
79  close(fd);
80  throw PERSIST_EXP_READ_FILE(errno);
81  }
82  close(fd);
83 
84  // 2 - deserialize
85  std::unique_ptr<ObjectType> ret = mutils::from_bytes<ObjectType>(dm, buf);
86  delete[] buf;
87 
88  return ret;
89 }
90 }
91 
92 #endif //PERSIST_NO_LOG_IMPL_HPP
#define PERSIST_EXP_WRITE_FILE(x)
#define _NOLOG_OBJECT_NAME_
This file include all common types internal to derecho and not necessarily being known by a client pr...
bool checkRegularFile(const std::string &file) noexcept(false)
Definition: util.hpp:58
#define CONF_PERS_RESET
Definition: conf.hpp:51
#define PERSIST_EXP_OPEN_FILE(x)
#define dbg_default_error(...)
Definition: logger.hpp:48
auto bytes_size(const T &)
Just calls sizeof(T)
#define PERSIST_EXP_OOM(x)
The manager for any RemoteDeserializationContexts.
std::unique_ptr< ObjectType > loadNoLogObjectFromFile(const char *object_name, mutils::DeserializationManager *dm) noexcept(false)
load data from file
#define PERSIST_EXP_REMOVE_FILE(x)
#define _NOLOG_OBJECT_DIR_
#define PERSIST_EXP_READ_FILE(x)
const bool getConfBoolean(const std::string &key)
Definition: conf.cpp:146
void saveNoLogObjectInFile(ObjectType &obj, const char *object_name) noexcept(false)
save object in file
void checkOrCreateDir(const std::string &dirPath) noexcept(false)
Definition: util.hpp:43
std::size_t to_bytes(const ByteRepresentable &b, char *v)
calls b.to_bytes(v) when b is a ByteRepresentable; calls std::memcpy() when b is POD.
#define PERSIST_EXP_RENAME_FILE(x)