| 37 | const int CharCodec::MAX_SYMBOL = 4; |
| 38 | |
| 39 | Status CharCodec::DecodeBuffer(uint8_t** buffer, int64_t* bytes_read, MemPool* pool, |
| 40 | bool eosr, bool decompress, ScannerContext* context) { |
| 41 | std::string result_prefix; |
| 42 | std::string result_core; |
| 43 | std::string result_suffix; |
| 44 | |
| 45 | // We're about to create a new decoding buffer (if we can't reuse). Attach the |
| 46 | // memory from previous decoding rounds to 'pool'. In case of streaming decompression |
| 47 | // this is already done in DecompressStreamToBuffer(). |
| 48 | if (!decompress && !reuse_buffer_) { |
| 49 | if (pool != nullptr) { |
| 50 | pool->AcquireData(memory_pool_, false); |
| 51 | } else { |
| 52 | memory_pool_->FreeAll(); |
| 53 | } |
| 54 | out_buffer_ = nullptr; |
| 55 | } |
| 56 | |
| 57 | uint8_t* buf_start = *buffer; |
| 58 | uint8_t* buf_end = buf_start + *bytes_read; |
| 59 | |
| 60 | // Allocate memory twice the size of the input buffer to handle the worst case |
| 61 | ScopedMemTracker scoped_mem_tracker(memory_pool_->mem_tracker()); |
| 62 | RETURN_IF_ERROR(scoped_mem_tracker.TryConsume((*bytes_read) * 2)); |
| 63 | |
| 64 | RETURN_IF_ERROR(HandlePrefix(&buf_start, buf_end, &result_prefix)); |
| 65 | RETURN_IF_ERROR(HandleCore(&buf_start, buf_end, &result_core)); |
| 66 | RETURN_IF_ERROR(HandleSuffix(&buf_start, buf_end, &result_suffix)); |
| 67 | |
| 68 | if (eosr && !partial_symbol_.empty()) { |
| 69 | return Status(TErrorCode::CHARSET_CONVERSION_ERROR, |
| 70 | "End of stream reached with partial symbol."); |
| 71 | } |
| 72 | |
| 73 | // In case of decompression, decompressed data can be freed up after decoding |
| 74 | if (decompress) { |
| 75 | memory_pool_->FreeAll(); |
| 76 | } else if (eosr) { |
| 77 | context->ReleaseCompletedResources(false); |
| 78 | } |
| 79 | |
| 80 | // Concat the results onto the output buffer |
| 81 | *bytes_read = result_prefix.size() + result_core.size() + result_suffix.size(); |
| 82 | if (out_buffer_ == nullptr || buffer_length_ < *bytes_read) { |
| 83 | buffer_length_ = *bytes_read; |
| 84 | out_buffer_ = memory_pool_->TryAllocate(buffer_length_); |
| 85 | if (UNLIKELY(out_buffer_ == nullptr)) { |
| 86 | string details = Substitute( |
| 87 | "HdfsTextScanner::DecodeBuffer() failed to allocate $1 bytes.", *bytes_read); |
| 88 | return memory_pool_->mem_tracker()->MemLimitExceeded(nullptr, details, *bytes_read); |
| 89 | } |
| 90 | } |
| 91 | *buffer = out_buffer_; |
| 92 | memcpy(*buffer, result_prefix.data(), result_prefix.size()); |
| 93 | memcpy(*buffer + result_prefix.size(), result_core.data(), result_core.size()); |
| 94 | memcpy(*buffer + result_prefix.size() + result_core.size(), |
| 95 | result_suffix.data(), result_suffix.size()); |
| 96 |
no test coverage detected