OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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)=0
 
const CopyParamsget_copy_params () const
 
void import_compressed (std::vector< std::string > &file_paths)
 

Protected Member Functions

ImportStatus archivePlumber ()
 

Protected Attributes

CopyParams copy_params
 
const std::string file_path
 
FILE * p_file = nullptr
 
ImportStatus import_status
 
bool load_failed = false
 
size_t total_file_size {0}
 
std::vector< size_t > file_offsets
 
std::mutex file_offsets_mutex
 

Detailed Description

Definition at line 629 of file Importer.h.

Constructor & Destructor Documentation

import_export::DataStreamSink::DataStreamSink ( )
inline

Definition at line 631 of file Importer.h.

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

Definition at line 632 of file Importer.h.

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

Definition at line 634 of file Importer.h.

634 {}

Member Function Documentation

ImportStatus import_export::DataStreamSink::archivePlumber ( )
protected

Definition at line 3299 of file Importer.cpp.

References copy_params, file_path, import_export::CopyParams::file_type, get_filesize(), omnisci::glob(), import_compressed(), import_status, and total_file_size.

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

3299  {
3300  // in generalized importing scheme, reaching here file_path may
3301  // contain a file path, a url or a wildcard of file paths.
3302  // see CopyTableStmt::execute.
3303  auto file_paths = omnisci::glob(file_path);
3304  if (file_paths.size() == 0) {
3305  file_paths.push_back(file_path);
3306  }
3307 
3308  // sum up sizes of all local files -- only for local files. if
3309  // file_path is a s3 url, sizes will be obtained via S3Archive.
3310  for (const auto& file_path : file_paths) {
3312  }
3313 
3314 #ifdef ENABLE_IMPORT_PARQUET
3315  // s3 parquet goes different route because the files do not use libarchive
3316  // but parquet api, and they need to landed like .7z files.
3317  //
3318  // note: parquet must be explicitly specified by a WITH parameter "parquet='true'",
3319  // because for example spark sql users may specify a output url w/o file
3320  // extension like this:
3321  // df.write
3322  // .mode("overwrite")
3323  // .parquet("s3://bucket/folder/parquet/mydata")
3324  // without the parameter, it means plain or compressed csv files.
3325  // note: .ORC and AVRO files should follow a similar path to Parquet?
3326  if (copy_params.file_type == FileType::PARQUET) {
3327  import_parquet(file_paths);
3328  } else
3329 #endif
3330  {
3331  import_compressed(file_paths);
3332  }
3333  return import_status;
3334 }
auto get_filesize(const std::string &file_path)
Definition: Importer.cpp:76
std::vector< std::string > glob(const std::string &pattern)
void import_compressed(std::vector< std::string > &file_paths)
Definition: Importer.cpp:3698
const std::string file_path
Definition: Importer.h:648

+ 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 641 of file Importer.h.

References copy_params.

Referenced by DBHandler::detect_column_types().

641 { return copy_params; }

+ Here is the caller graph for this function:

void import_export::DataStreamSink::import_compressed ( std::vector< std::string > &  file_paths)

Definition at line 3698 of file Importer.cpp.

References omnisci::close(), copy_params, file_offsets, file_offsets_mutex, import_export::CopyParams::has_header, import_status, importDelimited(), import_export::CopyParams::line_delim, import_export::ImportStatus::load_truncated, LOG, import_export::NO_HEADER, p_file, Archive::parse_url(), import_export::CopyParams::plain_text, 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::status_mutex, total_file_size, logger::WARNING, and File_Namespace::write().

Referenced by archivePlumber().

3698  {
3699 #ifdef _MSC_VER
3700  throw std::runtime_error("CSV Import not yet supported on Windows.");
3701 #else
3702  // a new requirement is to have one single input stream into
3703  // Importer::importDelimited, so need to move pipe related
3704  // stuff to the outmost block.
3705  int fd[2];
3706  if (pipe(fd) < 0) {
3707  throw std::runtime_error(std::string("failed to create a pipe: ") + strerror(errno));
3708  }
3709  signal(SIGPIPE, SIG_IGN);
3710 
3711  std::exception_ptr teptr;
3712  // create a thread to read uncompressed byte stream out of pipe and
3713  // feed into importDelimited()
3714  ImportStatus ret;
3715  auto th_pipe_reader = std::thread([&]() {
3716  try {
3717  // importDelimited will read from FILE* p_file
3718  if (0 == (p_file = fdopen(fd[0], "r"))) {
3719  throw std::runtime_error(std::string("failed to open a pipe: ") +
3720  strerror(errno));
3721  }
3722 
3723  // in future, depending on data types of this uncompressed stream
3724  // it can be feed into other function such like importParquet, etc
3725  ret = importDelimited(file_path, true);
3726  } catch (...) {
3727  if (!teptr) { // no replace
3728  teptr = std::current_exception();
3729  }
3730  }
3731 
3732  if (p_file) {
3733  fclose(p_file);
3734  }
3735  p_file = 0;
3736  });
3737 
3738  // create a thread to iterate all files (in all archives) and
3739  // forward the uncompressed byte stream to fd[1] which is
3740  // then feed into importDelimited, importParquet, and etc.
3741  auto th_pipe_writer = std::thread([&]() {
3742  std::unique_ptr<S3Archive> us3arch;
3743  bool stop = false;
3744  for (size_t fi = 0; !stop && fi < file_paths.size(); fi++) {
3745  try {
3746  auto file_path = file_paths[fi];
3747  std::unique_ptr<Archive> uarch;
3748  std::map<int, std::string> url_parts;
3749  Archive::parse_url(file_path, url_parts);
3750  const std::string S3_objkey_url_scheme = "s3ok";
3751  if ("file" == url_parts[2] || "" == url_parts[2]) {
3752  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
3753  } else if ("s3" == url_parts[2]) {
3754 #ifdef HAVE_AWS_S3
3755  // new a S3Archive with a shared s3client.
3756  // should be safe b/c no wildcard with s3 url
3757  us3arch.reset(new S3Archive(file_path,
3763  us3arch->init_for_read();
3764  total_file_size += us3arch->get_total_file_size();
3765  // not land all files here but one by one in following iterations
3766  for (const auto& objkey : us3arch->get_objkeys()) {
3767  file_paths.emplace_back(std::string(S3_objkey_url_scheme) + "://" + objkey);
3768  }
3769  continue;
3770 #else
3771  throw std::runtime_error("AWS S3 support not available");
3772 #endif // HAVE_AWS_S3
3773  } else if (S3_objkey_url_scheme == url_parts[2]) {
3774 #ifdef HAVE_AWS_S3
3775  auto objkey = file_path.substr(3 + S3_objkey_url_scheme.size());
3776  auto file_path =
3777  us3arch->land(objkey, teptr, nullptr != dynamic_cast<Detector*>(this));
3778  if (0 == file_path.size()) {
3779  throw std::runtime_error(std::string("failed to land s3 object: ") + objkey);
3780  }
3781  uarch.reset(new PosixFileArchive(file_path, copy_params.plain_text));
3782  // file not removed until file closed
3783  us3arch->vacuum(objkey);
3784 #else
3785  throw std::runtime_error("AWS S3 support not available");
3786 #endif // HAVE_AWS_S3
3787  }
3788 #if 0 // TODO(ppan): implement and enable any other archive class
3789  else
3790  if ("hdfs" == url_parts[2])
3791  uarch.reset(new HdfsArchive(file_path));
3792 #endif
3793  else {
3794  throw std::runtime_error(std::string("unsupported archive url: ") + file_path);
3795  }
3796 
3797  // init the archive for read
3798  auto& arch = *uarch;
3799 
3800  // coming here, the archive of url should be ready to be read, unarchived
3801  // and uncompressed by libarchive into a byte stream (in csv) for the pipe
3802  const void* buf;
3803  size_t size;
3804  bool just_saw_archive_header;
3805  bool is_detecting = nullptr != dynamic_cast<Detector*>(this);
3806  bool first_text_header_skipped = false;
3807  // start reading uncompressed bytes of this archive from libarchive
3808  // note! this archive may contain more than one files!
3809  file_offsets.push_back(0);
3810  while (!stop && !!(just_saw_archive_header = arch.read_next_header())) {
3811  bool insert_line_delim_after_this_file = false;
3812  while (!stop) {
3813  int64_t offset{-1};
3814  auto ok = arch.read_data_block(&buf, &size, &offset);
3815  // can't use (uncompressed) size, so track (max) file offset.
3816  // also we want to capture offset even on e.o.f.
3817  if (offset > 0) {
3818  std::unique_lock<std::mutex> lock(file_offsets_mutex);
3819  file_offsets.back() = offset;
3820  }
3821  if (!ok) {
3822  break;
3823  }
3824  // one subtle point here is now we concatenate all files
3825  // to a single FILE stream with which we call importDelimited
3826  // only once. this would make it misunderstand that only one
3827  // header line is with this 'single' stream, while actually
3828  // we may have one header line for each of the files.
3829  // so we need to skip header lines here instead in importDelimited.
3830  const char* buf2 = (const char*)buf;
3831  int size2 = size;
3833  just_saw_archive_header && (first_text_header_skipped || !is_detecting)) {
3834  while (size2-- > 0) {
3835  if (*buf2++ == copy_params.line_delim) {
3836  break;
3837  }
3838  }
3839  if (size2 <= 0) {
3840  LOG(WARNING) << "No line delimiter in block." << std::endl;
3841  } else {
3842  just_saw_archive_header = false;
3843  first_text_header_skipped = true;
3844  }
3845  }
3846  // In very rare occasions the write pipe somehow operates in a mode similar to
3847  // non-blocking while pipe(fds) should behave like pipe2(fds, 0) which means
3848  // blocking mode. On such a unreliable blocking mode, a possible fix is to
3849  // loop reading till no bytes left, otherwise the annoying `failed to write
3850  // pipe: Success`...
3851  if (size2 > 0) {
3852  int nremaining = size2;
3853  while (nremaining > 0) {
3854  // try to write the entire remainder of the buffer to the pipe
3855  int nwritten = write(fd[1], buf2, nremaining);
3856  // how did we do?
3857  if (nwritten < 0) {
3858  // something bad happened
3859  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
3860  // ignore these, assume nothing written, try again
3861  nwritten = 0;
3862  } else {
3863  // a real error
3864  throw std::runtime_error(
3865  std::string("failed or interrupted write to pipe: ") +
3866  strerror(errno));
3867  }
3868  } else if (nwritten == nremaining) {
3869  // we wrote everything; we're done
3870  break;
3871  }
3872  // only wrote some (or nothing), try again
3873  nremaining -= nwritten;
3874  buf2 += nwritten;
3875  // no exception when too many rejected
3876  // @simon.eves how would this get set? from the other thread? mutex
3877  // needed?
3879  stop = true;
3880  break;
3881  }
3882  }
3883  // check that this file (buf for size) ended with a line delim
3884  if (size > 0) {
3885  const char* plast = static_cast<const char*>(buf) + (size - 1);
3886  insert_line_delim_after_this_file = (*plast != copy_params.line_delim);
3887  }
3888  }
3889  }
3890  // if that file didn't end with a line delim, we insert one here to terminate
3891  // that file's stream use a loop for the same reason as above
3892  if (insert_line_delim_after_this_file) {
3893  while (true) {
3894  // write the delim char to the pipe
3895  int nwritten = write(fd[1], &copy_params.line_delim, 1);
3896  // how did we do?
3897  if (nwritten < 0) {
3898  // something bad happened
3899  if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
3900  // ignore these, assume nothing written, try again
3901  nwritten = 0;
3902  } else {
3903  // a real error
3904  throw std::runtime_error(
3905  std::string("failed or interrupted write to pipe: ") +
3906  strerror(errno));
3907  }
3908  } else if (nwritten == 1) {
3909  // we wrote it; we're done
3910  break;
3911  }
3912  }
3913  }
3914  }
3915  } catch (...) {
3916  // when import is aborted because too many data errors or because end of a
3917  // detection, any exception thrown by s3 sdk or libarchive is okay and should be
3918  // suppressed.
3919  mapd_shared_lock<mapd_shared_mutex> read_lock(status_mutex);
3921  break;
3922  }
3923  if (import_status.rows_completed > 0) {
3924  if (nullptr != dynamic_cast<Detector*>(this)) {
3925  break;
3926  }
3927  }
3928  if (!teptr) { // no replace
3929  teptr = std::current_exception();
3930  }
3931  break;
3932  }
3933  }
3934  // close writer end
3935  close(fd[1]);
3936  });
3937 
3938  th_pipe_reader.join();
3939  th_pipe_writer.join();
3940 
3941  // rethrow any exception happened herebefore
3942  if (teptr) {
3943  std::rethrow_exception(teptr);
3944  }
3945 #endif
3946 }
std::string s3_secret_key
Definition: CopyParams.h:63
#define LOG(tag)
Definition: Logger.h:188
virtual ImportStatus importDelimited(const std::string &file_path, const bool decompressed)=0
ImportHeaderRow has_header
Definition: CopyParams.h:48
void close(const int fd)
Definition: omnisci_fs.cpp:68
mapd_shared_lock< mapd_shared_mutex > read_lock
size_t write(FILE *f, const size_t offset, const size_t size, int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:126
static mapd_shared_mutex status_mutex
Definition: Importer.cpp:145
std::string s3_access_key
Definition: CopyParams.h:62
std::vector< size_t > file_offsets
Definition: Importer.h:653
static void parse_url(const std::string url, std::map< int, std::string > &url_parts)
Definition: Archive.h:153
const std::string file_path
Definition: Importer.h:648

+ 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 
)
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 653 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 654 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 648 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: