Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::CSVEncoder Class Reference
Collaboration diagram for duckdb::CSVEncoder:

Public Member Functions

 CSVEncoder (ClientContext &context, const string &encoding_name, idx_t buffer_size)
 Constructor, basically takes an encoding and the output buffer size.
idx_t Encode (FileHandle &file_handle_input, char *output_buffer, const idx_t decoded_buffer_size)
 Main encode function, it reads the file into an encoded buffer and converts it to the output buffer.

Public Attributes

string encoding_name

Private Attributes

QueryContext context
CSVEncoderBuffer encoded_buffer
 The actual encoded buffer.
CSVEncoderBuffer remaining_bytes_buffer
 Potential remaining bytes.
optional_ptr< EncodingFunction > encoding_function
 Actual Encoding Function.
char pass_on_byte
 Pass-on Byte, used to check if we are done with the file, but must be appended to next buffer.
bool has_pass_on_byte = false

Constructor & Destructor Documentation

◆ CSVEncoder()

duckdb::CSVEncoder::CSVEncoder ( ClientContext & context,
const string & encoding_name,
idx_t buffer_size )

Constructor, basically takes an encoding and the output buffer size.

167027 : context(context_p), pass_on_byte(0) {
167028 auto &config = DBConfig::GetConfig(context_p);
167029 encoding_name = StringUtil::Lower(encoding_name_to_find);
167030 auto function = config.GetEncodeFunction(encoding_name_to_find);
167031 if (!function) {
167032 // Maybe we can try to auto-load from our encodings extension, if this somehow fails, we just error.
167033 if (Catalog::TryAutoLoad(context_p, "encodings")) {
167034 // If it successfully loaded, we can try to get our function again
167035 function = config.GetEncodeFunction(encoding_name_to_find);
167036 }
167037 }
167038 if (!function) {
167039 // If at this point we still do not have a function we throw an error.
167040 auto loaded_encodings = config.GetLoadedEncodedFunctions();
167041 std::ostringstream error;
167042 error << "The CSV Reader does not support the encoding: \"" << encoding_name_to_find << "\"\n";
167043 if (!context_p.db->ExtensionIsLoaded("encodings")) {
167044 error << "It is possible that the encoding exists in the encodings extension. You can try \"INSTALL "
167045 "encodings; LOAD encodings\""
167046 << "\n";
167047 }
167048 error << "The currently supported encodings are: " << '\n';
167049 for (auto &encoding_function : loaded_encodings) {
167050 error << "* " << encoding_function.get().GetName() << '\n';
167051 }
167052 throw InvalidInputException(error.str());
167053 }
167054
167055 // We ensure that the encoded buffer size is an even number to make the two byte lookup on utf-16 work
167056 idx_t encoded_buffer_size = buffer_size % 2 != 0 ? buffer_size - 1 : buffer_size;
167057 if (encoded_buffer_size == 0) {
167058 // This might happen if buffer size = 1
167059 encoded_buffer_size = 2;
167060 }
167061 D_ASSERT(encoded_buffer_size > 0);
167062 encoded_buffer.Initialize(encoded_buffer_size);
167063 remaining_bytes_buffer.Initialize(function->GetBytesPerIteration());
167064 encoding_function = function;
167065}
CSVEncoderBuffer encoded_buffer
The actual encoded buffer.
Definition duckdb.hpp:45056
char pass_on_byte
Pass-on Byte, used to check if we are done with the file, but must be appended to next buffer.
Definition duckdb.hpp:45062
CSVEncoderBuffer remaining_bytes_buffer
Potential remaining bytes.
Definition duckdb.hpp:45058
optional_ptr< EncodingFunction > encoding_function
Actual Encoding Function.
Definition duckdb.hpp:45060
static DUCKDB_API string Lower(const string &str)
Convert a string to lowercase.
Definition duckdb.cpp:109630
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

Member Function Documentation

◆ Encode()

idx_t duckdb::CSVEncoder::Encode ( FileHandle & file_handle_input,
char * output_buffer,
const idx_t decoded_buffer_size )

Main encode function, it reads the file into an encoded buffer and converts it to the output buffer.

167067 {
167068 idx_t output_buffer_pos = 0;
167069 // Check if we have some left-overs. These can either be
167070 // 1. missing decoded bytes
167071 if (remaining_bytes_buffer.HasDataToRead()) {
167072 D_ASSERT(remaining_bytes_buffer.cur_pos == 0);
167073 const auto remaining_bytes_buffer_ptr = remaining_bytes_buffer.Ptr();
167074 for (; remaining_bytes_buffer.cur_pos < remaining_bytes_buffer.GetSize(); remaining_bytes_buffer.cur_pos++) {
167075 output_buffer[output_buffer_pos++] = remaining_bytes_buffer_ptr[remaining_bytes_buffer.cur_pos];
167076 }
167077 remaining_bytes_buffer.Reset();
167078 }
167079 // 2. remaining encoded buffer
167080 if (encoded_buffer.HasDataToRead()) {
167081 encoding_function->GetFunction()(encoded_buffer, output_buffer, output_buffer_pos, decoded_buffer_size,
167083 remaining_bytes_buffer.actual_encoded_buffer_size, encoding_function.get());
167084 }
167085 // 3. a new encoded buffer from the file
167086 while (output_buffer_pos < decoded_buffer_size) {
167087 idx_t current_decoded_buffer_start = output_buffer_pos;
167088 vector<char> pass_on_buffer;
167089 if (encoded_buffer.cur_pos != encoded_buffer.GetSize() &&
167090 encoding_function->GetLookupBytes() > encoded_buffer.GetSize() - encoded_buffer.cur_pos) {
167091 // We don't have enough lookup bytes for it, if that's the case, we need to set off these bytes to the next
167092 // buffer
167093 for (idx_t i = encoded_buffer.GetSize() - encoded_buffer.cur_pos; i < encoding_function->GetLookupBytes();
167094 i++) {
167095 pass_on_buffer.push_back(encoded_buffer.Ptr()[i]);
167096 }
167097 }
167098 encoded_buffer.Reset();
167099 for (idx_t i = 0; i < pass_on_buffer.size(); i++) {
167100 encoded_buffer.Ptr()[i] = pass_on_buffer[i];
167101 }
167102 if (has_pass_on_byte) {
167103 encoded_buffer.Ptr()[pass_on_buffer.size()] = pass_on_byte;
167104 }
167105 auto actual_encoded_bytes = static_cast<idx_t>(
167106 file_handle_input.Read(context, encoded_buffer.Ptr() + pass_on_buffer.size() + has_pass_on_byte,
167107 encoded_buffer.GetCapacity() - pass_on_buffer.size() - has_pass_on_byte));
167108 encoded_buffer.SetSize(actual_encoded_bytes + pass_on_buffer.size() + has_pass_on_byte);
167109 if (actual_encoded_bytes < encoded_buffer.GetCapacity() - pass_on_buffer.size()) {
167110 encoded_buffer.last_buffer = true;
167111 has_pass_on_byte = false;
167112 } else {
167113 auto bytes_read = static_cast<idx_t>(file_handle_input.Read(context, &pass_on_byte, 1));
167114 if (bytes_read == 0) {
167115 encoded_buffer.last_buffer = true;
167116 has_pass_on_byte = false;
167117 } else {
167118 has_pass_on_byte = true;
167119 }
167120 }
167121 encoding_function->GetFunction()(encoded_buffer, output_buffer, output_buffer_pos, decoded_buffer_size,
167123 remaining_bytes_buffer.actual_encoded_buffer_size, encoding_function.get());
167124 if (output_buffer_pos == current_decoded_buffer_start) {
167125 return output_buffer_pos;
167126 }
167127 }
167128 return output_buffer_pos;
167129}

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