OmniSciDB  c07336695a
PosixFileArchive.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ARCHIVE_POSIXFILEARCHIVE_H_
18 #define ARCHIVE_POSIXFILEARCHIVE_H_
19 
20 #include <cstdio>
21 
22 #include "Archive.h"
23 
24 // this is the archive class for files hosted locally or remotely with
25 // POSIX compliant file name. !! 7z files work only with this class !!
26 class PosixFileArchive : public Archive {
27  const size_t buf_size = (1 << 20);
28 
29  public:
30  PosixFileArchive(const std::string url, const bool plain_text)
31  : Archive(url, plain_text) {
32  // some well-known file.exts imply plain text
33  if (!this->plain_text) {
34  this->plain_text = boost::filesystem::extension(url_part(5)) == ".csv" ||
35  boost::filesystem::extension(url_part(5)) == ".tsv" ||
36  boost::filesystem::extension(url_part(5)) == ".txt" ||
37  boost::filesystem::extension(url_part(5)) == "";
38  }
39 
40  if (this->plain_text) {
41  buf = new char[buf_size];
42  }
43 
44  init_for_read();
45  }
46 
47  ~PosixFileArchive() override {
48  if (fp) {
49  fclose(fp);
50  }
51  if (buf) {
52  delete[] buf;
53  }
54  }
55 
56  void init_for_read() override {
57  auto file_path = url_part(5);
58  if (plain_text) {
59  if (nullptr == (fp = fopen(file_path.c_str(), "r"))) {
60  throw std::runtime_error(std::string("fopen(") + file_path +
61  "): " + strerror(errno));
62  }
63  } else {
64  if (ARCHIVE_OK != archive_read_open_filename(ar, file_path.c_str(), 1 << 16)) {
65  throw std::runtime_error(std::string("fopen(") + file_path +
66  "): " + strerror(errno));
67  }
68  }
69  }
70 
71  bool read_next_header() override {
72  if (plain_text) {
73  return !feof(fp);
74  } else {
76  }
77  }
78 
79  bool read_data_block(const void** buff, size_t* size, int64_t* offset) override {
80  if (plain_text) {
81  size_t nread;
82  if (0 >= (nread = fread(buf, 1, buf_size, fp))) {
83  return false;
84  }
85  *buff = buf;
86  *size = nread;
87  *offset = ftell(fp);
88  return true;
89  } else {
90  // need original (compressed) offset for row estimation of compressed files
91  auto ret = Archive::read_data_block(buff, size, offset);
93  return ret;
94  }
95  }
96 
97  private:
98  char* buf = nullptr;
99  FILE* fp = nullptr;
100 };
101 
102 #endif /* ARCHIVE_POSIXFILEARCHIVE_H_ */
virtual bool read_data_block(const void **buff, size_t *size, int64_t *offset)
Definition: Archive.h:108
const size_t buf_size
~PosixFileArchive() override
PosixFileArchive(const std::string url, const bool plain_text)
bool read_next_header() override
virtual int64_t get_position_compressed() const
Definition: Archive.h:119
archive * ar
Definition: Archive.h:188
bool read_data_block(const void **buff, size_t *size, int64_t *offset) override
const std::string url_part(const int i)
Definition: Archive.h:183
void init_for_read() override
std::string url
Definition: Archive.h:186
virtual bool read_next_header()
Definition: Archive.h:96
bool plain_text
Definition: Archive.h:189