-
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
186 additions
and
53 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
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,34 @@ | ||
#pragma once | ||
|
||
#include "common/copier_config/copier_config.h" | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
class CSVFileWriter { | ||
public: | ||
CSVFileWriter(const std::vector<std::string>& columnNames) : columnNames{columnNames} {}; | ||
void openForWriting(const std::string& filePath); | ||
|
||
template<typename T> | ||
void add(const T value); | ||
|
||
void add(const common::ku_string_t& value); | ||
void addEndl(); | ||
|
||
private: | ||
static constexpr int flushAfterLines = 10000; | ||
std::ofstream file; | ||
int64_t unflushedLines = 0; | ||
std::string line = ""; | ||
const std::vector<std::string>& columnNames; | ||
void add(std::string value); | ||
void addHeader(); | ||
void flush(); | ||
void addSeparator(char separator = ','); | ||
|
||
inline const std::vector<std::string>& getColumnNames() const { return columnNames; } | ||
}; | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
add_library(kuzu_processor_operator_copy_to | ||
OBJECT | ||
copy_to.cpp | ||
csv_file_writer.cpp | ||
) | ||
|
||
set(ALL_OBJECT_FILES | ||
|
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,68 @@ | ||
#include "processor/operator/copy_to/csv_file_writer.h" | ||
|
||
#include "common/string_utils.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); | ||
} | ||
addHeader(); | ||
} | ||
|
||
void CSVFileWriter::add(std::string value) { | ||
StringUtils::replaceAll(value, "\"", "\\\""); | ||
line += '"' + value + '"'; | ||
addSeparator(); | ||
} | ||
|
||
template<typename T> | ||
void CSVFileWriter::add(const T value) { | ||
line += std::to_string(value); | ||
addSeparator(); | ||
} | ||
|
||
void CSVFileWriter::add(const common::ku_string_t& value) { | ||
add(value.getAsString()); | ||
} | ||
|
||
void CSVFileWriter::addEndl() { | ||
line.back() = '\n'; | ||
unflushedLines++; | ||
flush(); | ||
} | ||
|
||
void CSVFileWriter::addSeparator(char separator) { | ||
line += separator; | ||
} | ||
|
||
void CSVFileWriter::addHeader() { | ||
for (auto columnName : getColumnNames()) { | ||
add(columnName); | ||
} | ||
addEndl(); | ||
} | ||
|
||
void CSVFileWriter::flush() { | ||
file << line; | ||
line.clear(); | ||
if (unflushedLines >= flushAfterLines) { | ||
file.flush(); | ||
unflushedLines = 0; | ||
return; | ||
} | ||
} | ||
|
||
template void CSVFileWriter::add(const int64_t value); | ||
template void CSVFileWriter::add(const int32_t value); | ||
template void CSVFileWriter::add(const int16_t value); | ||
template void CSVFileWriter::add(const float value); | ||
template void CSVFileWriter::add(const double value); | ||
|
||
} // namespace processor | ||
} // namespace kuzu |