OmniSciDB  471d68cefb
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
import_export::DataStreamSink Class Referenceabstract

#include <Importer.h>

+ Inheritance diagram for import_export::DataStreamSink:
+ Collaboration diagram for import_export::DataStreamSink:

Public Member Functions

 DataStreamSink ()
 
 DataStreamSink (const CopyParams &copy_params, const std::string file_path)
 
virtual ~DataStreamSink ()
 
virtual ImportStatus importDelimited (const std::string &file_path, const bool decompressed, const Catalog_Namespace::SessionInfo *session_info)=0
 
const CopyParamsget_copy_params () const
 
void import_compressed (std::vector< std::string > &file_paths, const Catalog_Namespace::SessionInfo *session_info)
 

Protected Member Functions

ImportStatus archivePlumber (const Catalog_Namespace::SessionInfo *session_info)
 

Protected Attributes

CopyParams copy_params
 
const std::string file_path
 
FILE * p_file = nullptr
 
ImportStatus import_status_
 
mapd_shared_mutex import_mutex_
 
size_t total_file_size {0}
 
std::vector< size_t > file_offsets
 
std::mutex file_offsets_mutex
 

Detailed Description

Definition at line 678 of file Importer.h.

Constructor & Destructor Documentation

import_export::DataStreamSink::DataStreamSink ( )
inline

Definition at line 680 of file Importer.h.

680 {}
import_export::DataStreamSink::DataStreamSink ( const CopyParams copy_params,
const std::string  file_path 
)
inline

Definition at line 681 of file Importer.h.

virtual import_export::DataStreamSink::~DataStreamSink ( )
inlinevirtual

Definition at line 683 of file Importer.h.

683 {}

Member Function Documentation

ImportStatus import_export::DataStreamSink::archivePlumber ( const Catalog_Namespace::SessionInfo session_info)
protected

Definition at line 3616 of file Importer.cpp.

References copy_params, file_path, import_export::CopyParams::file_sort_order_by, import_export::CopyParams::file_sort_regex, import_export::CopyParams::file_type, get_filesize(), import_compressed(), import_status_, shared::local_glob_filter_sort_files(), import_export::CopyParams::regex_path_filter, total_file_size, and shared::validate_sort_options().

Referenced by import_export::Importer::import(), and import_export::Detector::read_file().

3617  {
3618  // in generalized importing scheme, reaching here file_path may
3619  // contain a file path, a url or a wildcard of file paths.
3620  // see CopyTableStmt::execute.
3621 
3622  std::vector<std::string> file_paths;
3623  try {
3630  } catch (const shared::FileNotFoundException& e) {
3631  // After finding no matching files locally, file_path may still be an s3 url
3632  file_paths.push_back(file_path);
3633  }
3634 
3635  // sum up sizes of all local files -- only for local files. if
3636  // file_path is a s3 url, sizes will be obtained via S3Archive.
3637  for (const auto& file_path : file_paths) {
3639  }
3640 
3641 #ifdef ENABLE_IMPORT_PARQUET
3642  // s3 parquet goes different route because the files do not use libarchive
3643  // but parquet api, and they need to landed like .7z files.
3644  //
3645  // note: parquet must be explicitly specified by a WITH parameter "parquet='true'",
3646  // because for example spark sql users may specify a output url w/o file
3647  // extension like this:
3648  // df.write
3649  // .mode("overwrite")
3650  // .parquet("s3://bucket/folder/parquet/mydata")
3651  // without the parameter, it means plain or compressed csv files.
3652  // note: .ORC and AVRO files should follow a similar path to Parquet?
3653  if (copy_params.file_type == FileType::PARQUET) {
3654  import_parquet(file_paths, session_info);
3655  } else
3656 #endif
3657  {
3658  import_compressed(file_paths, session_info);
3659  }
3660 
3661  return import_status_;
3662 }
std::vector< std::string > local_glob_filter_sort_files(const std::string &file_path, const std::optional< std::string > &filter_regex, const std::optional< std::string > &sort_by, const std::optional< std::string > &sort_regex)
void validate_sort_options(const std::optional< std::string > &sort_by, const std::optional< std::string > &sort_regex)
void import_compressed(std::vector< std::string > &file_paths, const Catalog_Namespace::SessionInfo *session_info)
Definition: Importer.cpp:4066
auto get_filesize(const std::string &file_path)
Definition: Importer.cpp:91
std::optional< std::string > regex_path_filter
Definition: CopyParams.h:86
std::optional< std::string > file_sort_order_by
Definition: CopyParams.h:87
std::optional< std::string > file_sort_regex
Definition: CopyParams.h:88
const std::string file_path
Definition: Importer.h:703

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const CopyParams& import_export::DataStreamSink::get_copy_params ( ) const
inline

Definition at line 695 of file Importer.h.

References copy_params.

Referenced by DBHandler::detect_column_types().

695 { return copy_params; }

+ Here is the caller graph for this function:

void import_export::DataStreamSink::import_compressed ( std::vector< std::string > &  file_paths,
const Catalog_Namespace::SessionInfo session_info 
)

Definition at line 4066 of file Importer.cpp.

References import_export::CopyParams::buffer_size, omnisci::close(), copy_params, file_offsets, file_offsets_mutex, import_export::CopyParams::file_sort_order_by, import_export::CopyParams::file_sort_regex, import_export::CopyParams::has_header, import_mutex_, import_status_, importDelimited(), import_export::CopyParams::line_delim, import_export::ImportStatus::load_failed, LOG, import_export::NO_HEADER, p_file, Archive::parse_url(), import_export::CopyParams::plain_text, import_export::CopyParams::regex_path_filter, import_export::ImportStatus::rows_completed, import_export::CopyParams::s3_access_key, import_export::CopyParams::s3_endpoint, import_export::CopyParams::s3_region, import_export::CopyParams::s3_secret_key, import_export::CopyParams::s3_session_token, total_file_size, logger::WARNING, and File_Namespace::write().

Referenced by archivePlumber().

4068  {
4069  // a new requirement is to have one single input stream into
4070  // Importer::importDelimited, so need to move pipe related
4071  // stuff to the outmost block.
4072  int fd[2];
4073 #ifdef _WIN32
4074  // For some reason when folly is used to create the pipe, reader can
4075  // read nothing.
4076  auto pipe_res =
4077  _pipe(fd, static_cast<unsigned int>(copy_params.buffer_size), _O_BINARY);
4078 #else
4079  auto pipe_res = pipe(fd);
4080 #endif
4081  if (pipe_res < 0) {
4082  throw std::runtime_error(std::string("failed to create a pipe: ") + strerror(errno));
4083  }
4084 #ifndef _WIN32
4085  signal(SIGPIPE, SIG_IGN);
4086 #endif
4087 
4088  std::exception_ptr teptr;
4089  // create a thread to read uncompressed byte stream out of pipe and
4090  // feed into importDelimited()
4091  ImportStatus ret1;
4092  auto th_pipe_reader = std::thread([&]() {
4093  try {
4094  // importDelimited will read from FILE* p_file
4095  if (0 == (p_file = fdopen(fd[0], "r"))) {
4096  throw std::runtime_error(std::string("failed to open a pipe: ") +
4097  strerror(errno));
4098  }
4099 
4100  // in future, depending on data types of this uncompressed stream
4101  // it can be feed into other function such like importParquet, etc
4102  ret1 = importDelimited(file_path, true, session_info);
4103 
4104  } catch (...) {
4105  if (!teptr) { // no replace
4106  teptr = std::current_exception();
4107  }
4108  }
4109 
4110  if (p_file) {
4111  fclose(p_file);
4112  }
4113  p_file = 0;
4114  });
4115 
4116  // create a thread to iterate all files (in all archives) and
4117  // forward the uncompressed byte stream to fd[1] which is
4118  // then feed into importDelimited, importParquet, and etc.
4119  auto th_pipe_writer = std::thread([&]() {
4120  std::unique_ptr<S3Archive> us3arch;
4121  bool stop = false;
4122  for (size_t fi = 0; !stop && fi < file_paths.size(); fi++) {
4123  try {
4124  auto file_path = file_paths[fi];
4125  std::unique_ptr<Archive> uarch;
4126  std::map<int, std::string> url_parts;
4127  Archive::parse_url(file_path, url_parts);
4128  const std::string S3_objkey_url_scheme = "s3ok";
4129  if ("file" == url_parts[2] || "" == url_parts[2]) {
4130  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
4131  } else if ("s3" == url_parts[2]) {
4132 #ifdef HAVE_AWS_S3
4133  // new a S3Archive with a shared s3client.
4134  // should be safe b/c no wildcard with s3 url
4135  us3arch.reset(new S3Archive(file_path,
4145  us3arch->init_for_read();
4146  total_file_size += us3arch->get_total_file_size();
4147  // not land all files here but one by one in following iterations
4148  for (const auto& objkey : us3arch->get_objkeys()) {
4149  file_paths.emplace_back(std::string(S3_objkey_url_scheme) + "://" + objkey);
4150  }
4151  continue;
4152 #else
4153  throw std::runtime_error("AWS S3 support not available");
4154 #endif // HAVE_AWS_S3
4155  } else if (S3_objkey_url_scheme == url_parts[2]) {
4156 #ifdef HAVE_AWS_S3
4157  auto objkey = file_path.substr(3 + S3_objkey_url_scheme.size());
4158  auto file_path =
4159  us3arch->land(objkey, teptr, nullptr != dynamic_cast<Detector*>(this));
4160  if (0 == file_path.size()) {
4161  throw std::runtime_error(std::string("failed to land s3 object: ") + objkey);
4162  }
4163  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
4164  // file not removed until file closed
4165  us3arch->vacuum(objkey);
4166 #else
4167  throw std::runtime_error("AWS S3 support not available");
4168 #endif // HAVE_AWS_S3
4169  }
4170 #if 0 // TODO(ppan): implement and enable any other archive class
4171  else
4172  if ("hdfs" == url_parts[2])
4173  uarch.reset(new HdfsArchive(file_path));
4174 #endif
4175  else {
4176  throw std::runtime_error(std::string("unsupported archive url: ") + file_path);
4177  }
4178 
4179  // init the archive for read
4180  auto& arch = *uarch;
4181 
4182  // coming here, the archive of url should be ready to be read, unarchived
4183  // and uncompressed by libarchive into a byte stream (in csv) for the pipe
4184  const void* buf;
4185  size_t size;
4186  bool just_saw_archive_header;
4187  bool is_detecting = nullptr != dynamic_cast<Detector*>(this);
4188  bool first_text_header_skipped = false;
4189  // start reading uncompressed bytes of this archive from libarchive
4190  // note! this archive may contain more than one files!
4191  file_offsets.push_back(0);
4192  size_t num_block_read = 0;
4193  while (!stop && !!(just_saw_archive_header = arch.read_next_header())) {
4194  bool insert_line_delim_after_this_file = false;
4195  while (!stop) {
4196  int64_t offset{-1};
4197  auto ok = arch.read_data_block(&buf, &size, &offset);
4198  // can't use (uncompressed) size, so track (max) file offset.
4199  // also we want to capture offset even on e.o.f.
4200  if (offset > 0) {
4201  std::unique_lock<std::mutex> lock(file_offsets_mutex);
4202  file_offsets.back() = offset;
4203  }
4204  if (!ok) {
4205  break;
4206  }
4207  // one subtle point here is now we concatenate all files
4208  // to a single FILE stream with which we call importDelimited
4209  // only once. this would make it misunderstand that only one
4210  // header line is with this 'single' stream, while actually
4211  // we may have one header line for each of the files.
4212  // so we need to skip header lines here instead in importDelimited.
4213  const char* buf2 = (const char*)buf;
4214  int size2 = size;
4216  just_saw_archive_header && (first_text_header_skipped || !is_detecting)) {
4217  while (size2-- > 0) {
4218  if (*buf2++ == copy_params.line_delim) {
4219  break;
4220  }
4221  }
4222  if (size2 <= 0) {
4223  LOG(WARNING) << "No line delimiter in block." << std::endl;
4224  } else {
4225  just_saw_archive_header = false;
4226  first_text_header_skipped = true;
4227  }
4228  }
4229  // In very rare occasions the write pipe somehow operates in a mode similar
4230  // to non-blocking while pipe(fds) should behave like pipe2(fds, 0) which
4231  // means blocking mode. On such a unreliable blocking mode, a possible fix
4232  // is to loop reading till no bytes left, otherwise the annoying `failed to
4233  // write pipe: Success`...
4234  if (size2 > 0) {
4235  int nremaining = size2;
4236  while (nremaining > 0) {
4237  // try to write the entire remainder of the buffer to the pipe
4238  int nwritten = write(fd[1], buf2, nremaining);
4239  // how did we do?
4240  if (nwritten < 0) {
4241  // something bad happened
4242  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
4243  // ignore these, assume nothing written, try again
4244  nwritten = 0;
4245  } else {
4246  // a real error
4247  throw std::runtime_error(
4248  std::string("failed or interrupted write to pipe: ") +
4249  strerror(errno));
4250  }
4251  } else if (nwritten == nremaining) {
4252  // we wrote everything; we're done
4253  break;
4254  }
4255  // only wrote some (or nothing), try again
4256  nremaining -= nwritten;
4257  buf2 += nwritten;
4258  // no exception when too many rejected
4259  mapd_shared_lock<mapd_shared_mutex> read_lock(import_mutex_);
4261  stop = true;
4262  break;
4263  }
4264  }
4265  // check that this file (buf for size) ended with a line delim
4266  if (size > 0) {
4267  const char* plast = static_cast<const char*>(buf) + (size - 1);
4268  insert_line_delim_after_this_file = (*plast != copy_params.line_delim);
4269  }
4270  }
4271  ++num_block_read;
4272  }
4273 
4274  // if that file didn't end with a line delim, we insert one here to terminate
4275  // that file's stream use a loop for the same reason as above
4276  if (insert_line_delim_after_this_file) {
4277  while (true) {
4278  // write the delim char to the pipe
4279  int nwritten = write(fd[1], &copy_params.line_delim, 1);
4280  // how did we do?
4281  if (nwritten < 0) {
4282  // something bad happened
4283  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
4284  // ignore these, assume nothing written, try again
4285  nwritten = 0;
4286  } else {
4287  // a real error
4288  throw std::runtime_error(
4289  std::string("failed or interrupted write to pipe: ") +
4290  strerror(errno));
4291  }
4292  } else if (nwritten == 1) {
4293  // we wrote it; we're done
4294  break;
4295  }
4296  }
4297  }
4298  }
4299  } catch (...) {
4300  // when import is aborted because too many data errors or because end of a
4301  // detection, any exception thrown by s3 sdk or libarchive is okay and should be
4302  // suppressed.
4303  mapd_shared_lock<mapd_shared_mutex> read_lock(import_mutex_);
4305  break;
4306  }
4307  if (import_status_.rows_completed > 0) {
4308  if (nullptr != dynamic_cast<Detector*>(this)) {
4309  break;
4310  }
4311  }
4312  if (!teptr) { // no replace
4313  teptr = std::current_exception();
4314  }
4315  break;
4316  }
4317  }
4318  // close writer end
4319  close(fd[1]);
4320  });
4321 
4322  th_pipe_reader.join();
4323  th_pipe_writer.join();
4324 
4325  // rethrow any exception happened herebefore
4326  if (teptr) {
4327  std::rethrow_exception(teptr);
4328  }
4329 }
std::string s3_secret_key
Definition: CopyParams.h:66
#define LOG(tag)
Definition: Logger.h:203
mapd_shared_mutex import_mutex_
Definition: Importer.h:706
size_t write(FILE *f, const size_t offset, const size_t size, const int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:141
ImportHeaderRow has_header
Definition: CopyParams.h:51
virtual ImportStatus importDelimited(const std::string &file_path, const bool decompressed, const Catalog_Namespace::SessionInfo *session_info)=0
void close(const int fd)
Definition: omnisci_fs.cpp:68
std::optional< std::string > regex_path_filter
Definition: CopyParams.h:86
std::string s3_session_token
Definition: CopyParams.h:67
mapd_shared_lock< mapd_shared_mutex > read_lock
std::string s3_access_key
Definition: CopyParams.h:65
std::vector< size_t > file_offsets
Definition: Importer.h:708
std::optional< std::string > file_sort_order_by
Definition: CopyParams.h:87
std::optional< std::string > file_sort_regex
Definition: CopyParams.h:88
static void parse_url(const std::string url, std::map< int, std::string > &url_parts)
Definition: Archive.h:167
const std::string file_path
Definition: Importer.h:703

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

virtual ImportStatus import_export::DataStreamSink::importDelimited ( const std::string &  file_path,
const bool  decompressed,
const Catalog_Namespace::SessionInfo session_info 
)
pure virtual

Implemented in import_export::Importer, and import_export::Detector.

Referenced by import_compressed().

+ Here is the caller graph for this function:

Member Data Documentation

std::vector<size_t> import_export::DataStreamSink::file_offsets
protected

Definition at line 708 of file Importer.h.

Referenced by import_compressed(), and import_export::Importer::importDelimited().

std::mutex import_export::DataStreamSink::file_offsets_mutex
protected

Definition at line 709 of file Importer.h.

Referenced by import_compressed(), and import_export::Importer::importDelimited().

const std::string import_export::DataStreamSink::file_path
protected

Definition at line 703 of file Importer.h.

Referenced by archivePlumber(), and import_export::Importer::Importer().

size_t import_export::DataStreamSink::total_file_size {0}
protected

The documentation for this class was generated from the following files: