OmniSciDB  2e3a973ef4
anonymous_namespace{TableArchiver.cpp} Namespace Reference

Functions

std::string abs_path (const File_Namespace::GlobalFileMgr *global_file_mgr)
 
std::string run (const std::string &cmd, const std::string &chdir="")
 
std::string simple_file_cat (const std::string &archive_path, const std::string &file_name, const std::string &compression)
 
std::string get_table_schema (const std::string &archive_path, const std::string &table, const std::string &compression)
 
void adjust_altered_table_files (const std::string &temp_data_dir, const std::unordered_map< int, int > &column_ids_map)
 
void rename_table_directories (const File_Namespace::GlobalFileMgr *global_file_mgr, const std::string &temp_data_dir, const std::vector< std::string > &target_paths, const std::string &name_prefix)
 

Variables

auto simple_file_closer = [](FILE* f) { std::fclose(f); }
 

Function Documentation

◆ abs_path()

std::string anonymous_namespace{TableArchiver.cpp}::abs_path ( const File_Namespace::GlobalFileMgr global_file_mgr)
inline

Definition at line 66 of file TableArchiver.cpp.

References File_Namespace::GlobalFileMgr::getBasePath().

Referenced by TableArchiver::dumpTable(), rename_table_directories(), and TableArchiver::restoreTable().

66  {
67  return boost::filesystem::canonical(global_file_mgr->getBasePath()).string();
68 }
std::string getBasePath() const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ adjust_altered_table_files()

void anonymous_namespace{TableArchiver.cpp}::adjust_altered_table_files ( const std::string &  temp_data_dir,
const std::unordered_map< int, int > &  column_ids_map 
)

Definition at line 157 of file TableArchiver.cpp.

References CHECK, ThreadController_NS::SimpleThreadController< FutureReturnType >::checkThreadsStatus(), cpu_threads(), omnisci::file_size(), ThreadController_NS::SimpleThreadController< FutureReturnType >::finish(), MAPD_FILE_EXT, simple_file_closer, split(), ThreadController_NS::SimpleThreadController< FutureReturnType >::startThread(), and to_string().

Referenced by TableArchiver::restoreTable().

158  {
159  boost::filesystem::path base_path(temp_data_dir);
160  boost::filesystem::recursive_directory_iterator end_it;
162  for (boost::filesystem::recursive_directory_iterator fit(base_path); fit != end_it;
163  ++fit) {
164  if (boost::filesystem::is_regular_file(fit->status())) {
165  const std::string file_path = fit->path().string();
166  const std::string file_name = fit->path().filename().string();
167  std::vector<std::string> tokens;
168  boost::split(tokens, file_name, boost::is_any_of("."));
169  // ref. FileMgr::init for hint of data file name layout
170  if (tokens.size() > 2 && MAPD_FILE_EXT == "." + tokens[2]) {
171  thread_controller.startThread([file_name, file_path, tokens, &column_ids_map] {
172  const auto page_size = boost::lexical_cast<int64_t>(tokens[1]);
173  const auto file_size = boost::filesystem::file_size(file_path);
174  std::unique_ptr<FILE, decltype(simple_file_closer)> fp(
175  std::fopen(file_path.c_str(), "r+"), simple_file_closer);
176  if (!fp) {
177  throw std::runtime_error("Failed to open " + file_path +
178  " for update: " + std::strerror(errno));
179  }
180  // ref. FileInfo::openExistingFile for hint of chunk header layout
181  for (size_t page = 0; page < file_size / page_size; ++page) {
182  int ints[8];
183  if (0 != std::fseek(fp.get(), page * page_size, SEEK_SET)) {
184  throw std::runtime_error("Failed to seek to page# " + std::to_string(page) +
185  file_path + " for read: " + std::strerror(errno));
186  }
187  if (1 != fread(ints, sizeof ints, 1, fp.get())) {
188  throw std::runtime_error("Failed to read " + file_path + ": " +
189  std::strerror(errno));
190  }
191  if (ints[0] > 0) { // header size
192  auto cit = column_ids_map.find(ints[3]);
193  CHECK(cit != column_ids_map.end());
194  if (ints[3] != cit->second) {
195  ints[3] = cit->second;
196  if (0 != std::fseek(fp.get(), page * page_size, SEEK_SET)) {
197  throw std::runtime_error("Failed to seek to page# " +
198  std::to_string(page) + file_path +
199  " for write: " + std::strerror(errno));
200  }
201  if (1 != fwrite(ints, sizeof ints, 1, fp.get())) {
202  throw std::runtime_error("Failed to write " + file_path + ": " +
203  std::strerror(errno));
204  }
205  }
206  }
207  }
208  });
209  thread_controller.checkThreadsStatus();
210  }
211  }
212  }
213  thread_controller.finish();
214 }
#define MAPD_FILE_EXT
Definition: File.h:25
std::string to_string(char const *&&v)
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
#define CHECK(condition)
Definition: Logger.h:197
int cpu_threads()
Definition: thread_count.h:24
size_t file_size(const int fd)
Definition: omnisci_fs.cpp:31
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_table_schema()

std::string anonymous_namespace{TableArchiver.cpp}::get_table_schema ( const std::string &  archive_path,
const std::string &  table,
const std::string &  compression 
)
inline

Definition at line 145 of file TableArchiver.cpp.

References simple_file_cat(), and table_schema_filename.

Referenced by TableArchiver::restoreTable().

147  {
148  const auto schema_str =
149  simple_file_cat(archive_path, table_schema_filename, compression);
150  std::regex regex("@T");
151  return std::regex_replace(schema_str, regex, table);
152 }
static constexpr char const * table_schema_filename
std::string simple_file_cat(const std::string &archive_path, const std::string &file_name, const std::string &compression)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rename_table_directories()

void anonymous_namespace{TableArchiver.cpp}::rename_table_directories ( const File_Namespace::GlobalFileMgr global_file_mgr,
const std::string &  temp_data_dir,
const std::vector< std::string > &  target_paths,
const std::string &  name_prefix 
)

Definition at line 216 of file TableArchiver.cpp.

References abs_path().

Referenced by TableArchiver::restoreTable().

219  {
220  boost::filesystem::path base_path(temp_data_dir);
221  boost::filesystem::directory_iterator end_it;
222  int target_path_index = 0;
223  for (boost::filesystem::directory_iterator fit(base_path); fit != end_it; ++fit) {
224  if (!boost::filesystem::is_regular_file(fit->status())) {
225  const std::string file_path = fit->path().string();
226  const std::string file_name = fit->path().filename().string();
227  if (boost::istarts_with(file_name, name_prefix)) {
228  const std::string target_path =
229  abs_path(global_file_mgr) + "/" + target_paths[target_path_index++];
230  if (std::rename(file_path.c_str(), target_path.c_str())) {
231  throw std::runtime_error("Failed to rename file " + file_path + " to " +
232  target_path + ": " + std::strerror(errno));
233  }
234  }
235  }
236  }
237 }
std::string abs_path(const File_Namespace::GlobalFileMgr *global_file_mgr)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ run()

std::string anonymous_namespace{TableArchiver.cpp}::run ( const std::string &  cmd,
const std::string &  chdir = "" 
)
inline

Definition at line 70 of file TableArchiver.cpp.

References logger::ERROR, error_code, measure< TimeT >::execution(), LOG, to_string(), and VLOG.

70  {
71  VLOG(3) << "running cmd: " << cmd;
72  int rcode;
73  std::error_code ec;
74  std::string output, errors;
75  const auto time_ms = measure<>::execution([&]() {
76  using namespace boost::process;
77  ipstream stdout, stderr;
78  if (!chdir.empty()) {
79  rcode = system(cmd, std_out > stdout, std_err > stderr, ec, start_dir = chdir);
80  } else {
81  rcode = system(cmd, std_out > stdout, std_err > stderr, ec);
82  }
83  std::ostringstream ss_output, ss_errors;
84  stdout >> ss_output.rdbuf();
85  stderr >> ss_errors.rdbuf();
86  output = ss_output.str();
87  errors = ss_errors.str();
88  });
89  if (rcode || ec) {
90  LOG(ERROR) << "failed cmd: " << cmd;
91  LOG(ERROR) << "exit code: " << rcode;
92  LOG(ERROR) << "error code: " << ec.value() << " - " << ec.message();
93  LOG(ERROR) << "stdout: " << output;
94  LOG(ERROR) << "stderr: " << errors;
95 #if defined(__APPLE__)
96  // osx bsdtar options "--use-compress-program" and "--fast-read" together
97  // run into pipe write error after tar extracts the first occurrence of a
98  // file and closes the read end while the decompression program still writes
99  // to the pipe. bsdtar doesn't handle this situation well like gnu tar does.
100  if (1 == rcode && cmd.find("--fast-read") &&
101  (errors.find("cannot write decoded block") != std::string::npos ||
102  errors.find("Broken pipe") != std::string::npos)) {
103  // ignore this error, or lose speed advantage of "--fast-read" on osx.
104  LOG(ERROR) << "tar error ignored on osx for --fast-read";
105  } else
106 #endif
107  // circumvent tar warning on reading file that is "changed as we read it".
108  // this warning results from reading a table file under concurrent inserts
109  if (1 == rcode && errors.find("changed as we read") != std::string::npos) {
110  LOG(ERROR) << "tar error ignored under concurrent inserts";
111  } else {
112  throw std::runtime_error("Failed to run command: " + cmd +
113  "\nexit code: " + std::to_string(rcode) + "\nerrors:\n" +
114  (rcode ? errors : ec.message()));
115  }
116  } else {
117  VLOG(3) << "finished cmd: " << cmd;
118  VLOG(3) << "time: " << time_ms << " ms";
119  VLOG(3) << "stdout: " << output;
120  }
121  return output;
122 }
#define LOG(tag)
Definition: Logger.h:188
std::string to_string(char const *&&v)
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t uint32_t const int64_t int32_t * error_code
static TimeT::rep execution(F func, Args &&... args)
Definition: sample.cpp:29
#define VLOG(n)
Definition: Logger.h:291
+ Here is the call graph for this function:

◆ simple_file_cat()

std::string anonymous_namespace{TableArchiver.cpp}::simple_file_cat ( const std::string &  archive_path,
const std::string &  file_name,
const std::string &  compression 
)
inline

Definition at line 124 of file TableArchiver.cpp.

References get_quoted_string(), ddl_utils::IMPORT, run, and ddl_utils::validate_allowed_file_path().

Referenced by get_table_schema(), and TableArchiver::restoreTable().

126  {
129 #if defined(__APPLE__)
130  constexpr static auto opt_occurrence = "--fast-read";
131 #else
132  constexpr static auto opt_occurrence = "--occurrence=1";
133 #endif
134  boost::filesystem::path temp_dir =
135  boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
136  boost::filesystem::create_directories(temp_dir);
137  run("tar " + compression + " -xvf " + get_quoted_string(archive_path) + " " +
138  opt_occurrence + " " + file_name,
139  temp_dir.string());
140  const auto output = run("cat " + (temp_dir / file_name).string());
141  boost::filesystem::remove_all(temp_dir);
142  return output;
143 }
std::string get_quoted_string(const std::string &filename, char quote, char escape)
Quote a string while escaping any existing quotes in the string.
void validate_allowed_file_path(const std::string &file_path, const DataTransferType data_transfer_type, const bool allow_wildcards)
Definition: DdlUtils.cpp:611
static bool run
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ simple_file_closer

auto anonymous_namespace{TableArchiver.cpp}::simple_file_closer = [](FILE* f) { std::fclose(f); }
inline

Definition at line 64 of file TableArchiver.cpp.

Referenced by adjust_altered_table_files(), and TableArchiver::dumpTable().