OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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

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

Definition at line 69 of file TableArchiver.cpp.

References File_Namespace::GlobalFileMgr::getBasePath().

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

69  {
70  return boost::filesystem::canonical(global_file_mgr->getBasePath()).string();
71 }
std::string getBasePath() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 160 of file TableArchiver.cpp.

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

Referenced by TableArchiver::restoreTable().

161  {
162  boost::filesystem::path base_path(temp_data_dir);
163  boost::filesystem::recursive_directory_iterator end_it;
165  for (boost::filesystem::recursive_directory_iterator fit(base_path); fit != end_it;
166  ++fit) {
167  if (boost::filesystem::is_regular_file(fit->status())) {
168  const std::string file_path = fit->path().string();
169  const std::string file_name = fit->path().filename().string();
170  std::vector<std::string> tokens;
171  boost::split(tokens, file_name, boost::is_any_of("."));
172  // ref. FileMgr::init for hint of data file name layout
173  if (tokens.size() > 2 && MAPD_FILE_EXT == "." + tokens[2]) {
174  thread_controller.startThread([file_name, file_path, tokens, &column_ids_map] {
175  const auto page_size = boost::lexical_cast<int64_t>(tokens[1]);
176  const auto file_size = boost::filesystem::file_size(file_path);
177  std::unique_ptr<FILE, decltype(simple_file_closer)> fp(
178  std::fopen(file_path.c_str(), "r+"), simple_file_closer);
179  if (!fp) {
180  throw std::runtime_error("Failed to open " + file_path +
181  " for update: " + std::strerror(errno));
182  }
183  // ref. FileInfo::openExistingFile for hint of chunk header layout
184  for (size_t page = 0; page < file_size / page_size; ++page) {
185  int ints[8];
186  if (0 != std::fseek(fp.get(), page * page_size, SEEK_SET)) {
187  throw std::runtime_error("Failed to seek to page# " + std::to_string(page) +
188  file_path + " for read: " + std::strerror(errno));
189  }
190  if (1 != fread(ints, sizeof ints, 1, fp.get())) {
191  throw std::runtime_error("Failed to read " + file_path + ": " +
192  std::strerror(errno));
193  }
194  if (ints[0] > 0) { // header size
195  auto cit = column_ids_map.find(ints[3]);
196  CHECK(cit != column_ids_map.end());
197  if (ints[3] != cit->second) {
198  ints[3] = cit->second;
199  if (0 != std::fseek(fp.get(), page * page_size, SEEK_SET)) {
200  throw std::runtime_error("Failed to seek to page# " +
201  std::to_string(page) + file_path +
202  " for write: " + std::strerror(errno));
203  }
204  if (1 != fwrite(ints, sizeof ints, 1, fp.get())) {
205  throw std::runtime_error("Failed to write " + file_path + ": " +
206  std::strerror(errno));
207  }
208  }
209  }
210  }
211  });
212  thread_controller.checkThreadsStatus();
213  }
214  }
215  }
216  thread_controller.finish();
217 }
::FILE * fopen(const char *filename, const char *mode)
Definition: omnisci_fs.cpp:72
#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:203
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:

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 148 of file TableArchiver.cpp.

References simple_file_cat(), and table_schema_filename.

Referenced by TableArchiver::restoreTable().

150  {
151  const auto schema_str =
152  simple_file_cat(archive_path, table_schema_filename, compression);
153  std::regex regex("@T");
154  return std::regex_replace(schema_str, regex, table);
155 }
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:

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 219 of file TableArchiver.cpp.

References abs_path().

Referenced by TableArchiver::restoreTable().

222  {
223  boost::filesystem::path base_path(temp_data_dir);
224  boost::filesystem::directory_iterator end_it;
225  int target_path_index = 0;
226  for (boost::filesystem::directory_iterator fit(base_path); fit != end_it; ++fit) {
227  if (!boost::filesystem::is_regular_file(fit->status())) {
228  const std::string file_path = fit->path().string();
229  const std::string file_name = fit->path().filename().string();
230  if (boost::istarts_with(file_name, name_prefix)) {
231  const std::string target_path =
232  abs_path(global_file_mgr) + "/" + target_paths[target_path_index++];
233  if (std::rename(file_path.c_str(), target_path.c_str())) {
234  throw std::runtime_error("Failed to rename file " + file_path + " to " +
235  target_path + ": " + std::strerror(errno));
236  }
237  }
238  }
239  }
240 }
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:

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

Definition at line 73 of file TableArchiver.cpp.

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

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

+ Here is the call graph for this function:

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 127 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().

129  {
132 #if defined(__APPLE__)
133  constexpr static auto opt_occurrence = "--fast-read";
134 #else
135  constexpr static auto opt_occurrence = "--occurrence=1";
136 #endif
137  boost::filesystem::path temp_dir =
138  boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
139  boost::filesystem::create_directories(temp_dir);
140  run("tar " + compression + " -xvf " + get_quoted_string(archive_path) + " " +
141  opt_occurrence + " " + file_name,
142  temp_dir.string());
143  const auto output = run("cat " + (temp_dir / file_name).string());
144  boost::filesystem::remove_all(temp_dir);
145  return output;
146 }
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:634
static bool run

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

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

Definition at line 67 of file TableArchiver.cpp.

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