-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
187 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include "processor/operator/copy_to/file_writer.h" | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
class CSVFileWriter : public FileWriter { | ||
public: | ||
void openForWriting(const std::string& filePath); | ||
void add(const int64_t& value); | ||
void add(const common::ku_string_t& value); | ||
void addEndl(); | ||
|
||
private: | ||
std::ofstream file; | ||
int64_t lineCount = 0; | ||
std::string line = ""; | ||
|
||
void flush(); | ||
void addSeparator(char separator = ','); | ||
}; | ||
|
||
} // namespace processor | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include "common/copier_config/copier_config.h" | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
class FileWriter { | ||
public: | ||
virtual ~FileWriter(){}; | ||
virtual void openForWriting(const std::string& filePath) = 0; | ||
|
||
virtual void add(const int64_t& value) = 0; | ||
virtual void add(const common::ku_string_t& value) = 0; | ||
|
||
virtual void addEndl() = 0; | ||
}; | ||
|
||
} // namespace processor | ||
} // namespace kuzu |
16 changes: 16 additions & 0 deletions
16
src/include/processor/operator/copy_to/file_writer_creator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include "processor/operator/copy_to/file_writer.h" | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
// Factory to return the instance of the file writer according to the required type | ||
class FileWriterCreator { | ||
public: | ||
virtual ~FileWriterCreator(){}; | ||
static std::unique_ptr<FileWriter> create(common::CopyDescription::FileType fileType); | ||
}; | ||
|
||
} // namespace processor | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "processor/operator/copy_to/csv_file_writer.h" | ||
|
||
using namespace kuzu::common; | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
void CSVFileWriter::openForWriting(const std::string& filePath) { | ||
file.open(filePath); | ||
if (!file.is_open()) { | ||
throw CopyException("Could not open file " + filePath); | ||
} | ||
} | ||
|
||
void CSVFileWriter::add(const int64_t& value) { | ||
line += std::to_string(value); | ||
addSeparator(); | ||
} | ||
|
||
void CSVFileWriter::add(const common::ku_string_t& value) { | ||
// TODO: escape | ||
line += '"' + value.getAsString() + '"'; | ||
addSeparator(); | ||
} | ||
|
||
void CSVFileWriter::addEndl() { | ||
line.back() = '\n'; | ||
lineCount++; | ||
flush(); | ||
} | ||
|
||
void CSVFileWriter::addSeparator(char separator) { | ||
line += separator; | ||
} | ||
|
||
// TODO: To improve performance, use custom flush to persist after X lines | ||
void CSVFileWriter::flush() { | ||
file << line; | ||
file.flush(); | ||
line.clear(); | ||
} | ||
|
||
} // namespace processor | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "processor/operator/copy_to/file_writer.h" | ||
|
||
using namespace kuzu::common; | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
// TODO: check if this would be necessary | ||
|
||
} // namespace processor | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "processor/operator/copy_to/file_writer_creator.h" | ||
|
||
#include "processor/operator/copy_to/csv_file_writer.h" | ||
|
||
using namespace kuzu::common; | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
std::unique_ptr<FileWriter> FileWriterCreator::create(common::CopyDescription::FileType fileType) { | ||
switch (fileType) { | ||
case CopyDescription::FileType::CSV: | ||
return std::make_unique<CSVFileWriter>(); | ||
case CopyDescription::FileType::PARQUET: | ||
// return std::make_unique<ParquetFileWriter>(); | ||
default: | ||
throw std::runtime_error("File type not supported"); | ||
} | ||
} | ||
|
||
} // namespace processor | ||
} // namespace kuzu |