OmniSciDB  8a228a1076
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 628 of file Importer.h.

Constructor & Destructor Documentation

◆ DataStreamSink() [1/2]

import_export::DataStreamSink::DataStreamSink ( )
inline

Definition at line 630 of file Importer.h.

630 {}

◆ DataStreamSink() [2/2]

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

Definition at line 631 of file Importer.h.

◆ ~DataStreamSink()

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

Definition at line 633 of file Importer.h.

633 {}

Member Function Documentation

◆ archivePlumber()

ImportStatus import_export::DataStreamSink::archivePlumber ( )
protected

Definition at line 3300 of file Importer.cpp.

References arrow_throw_if(), import_export::Importer::buffer, copy_params, cpu_threads(), import_export::TypedImportBuffer::del_values(), import_export::CopyParams::delimiter, logger::ERROR, import_export::CopyParams::escape, measure< TimeT >::execution(), import_export::BadRowsTracker::file_name, file_offsets, file_offsets_mutex, file_path, import_export::CopyParams::file_type, ThreadController_NS::SimpleThreadController< FutureReturnType >::finish(), import_export::Importer::get_column_descs(), get_filesize(), import_export::Importer::getLoader(), import_export::HAS_HEADER, import_export::CopyParams::has_header, import_export::Importer::import_buffers_vec, import_compressed(), import_status, import_export::BadRowsTracker::importer, logger::INFO, kARRAY, kBIGINT, kBOOLEAN, kCHAR, kDATE, kDECIMAL, kDOUBLE, kFLOAT, kINT, kLINESTRING, kMULTIPOLYGON, kNUMERIC, kPOINT, kPOLYGON, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, import_export::CopyParams::line_delim, import_export::Importer::load(), load_failed, import_export::ImportStatus::load_truncated, import_export::Importer::loader, LOG, mapd_glob(), import_export::CopyParams::max_reject, import_export::Importer::max_threads, num_rows, foreign_storage::anonymous_namespace{LazyParquetImporter.cpp}::open_parquet_table(), Archive::parse_url(), import_export::CopyParams::plain_text, import_export::CopyParams::quote, import_export::CopyParams::quoted, import_export::BadRowsTracker::row_group, import_export::BadRowsTracker::rows, import_export::ImportStatus::rows_completed, import_export::ImportStatus::rows_estimated, import_export::ImportStatus::rows_rejected, import_export::CopyParams::s3_access_key, import_export::CopyParams::s3_endpoint, import_export::CopyParams::s3_region, import_export::CopyParams::s3_secret_key, ThreadController_NS::SimpleThreadController< FutureReturnType >::startThread(), import_export::CopyParams::threads, to_string(), total_file_size, run_benchmark_import::type, anonymous_namespace{ArrowImporter.h}::value_getter(), and VLOG.

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

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

◆ get_copy_params()

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

Definition at line 640 of file Importer.h.

Referenced by DBHandler::detect_column_types().

640 { return copy_params; }
+ Here is the caller graph for this function:

◆ import_compressed()

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

Definition at line 3698 of file Importer.cpp.

References File_Namespace::close(), copy_params, file_offsets, file_offsets_mutex, import_export::CopyParams::has_header, import_status, import_export::Importer::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, total_file_size, logger::WARNING, and File_Namespace::write().

Referenced by archivePlumber().

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

◆ importDelimited()

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

Member Data Documentation

◆ copy_params

◆ file_offsets

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

◆ file_offsets_mutex

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

◆ file_path

◆ import_status

◆ load_failed

◆ p_file

◆ total_file_size

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

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