OmniSciDB  94e8789169
 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 3306 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().

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

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