0% found this document useful (0 votes)
5 views3 pages

New 2

Anime names

Uploaded by

Umair Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

New 2

Anime names

Uploaded by

Umair Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include "reassembler.

hh"
#include <algorithm>
#include <iostream>

void Reassembler::insert(uint64_t first_index, std::string data, bool


is_last_substring) {
std::cout << "\n[Insert] first_index: " << first_index << ", data: \"" << data
<< "\", is_last_substring: " << std::boolalpha << is_last_substring
<< "\n";

// Get available capacity of the output ByteStream


size_t capacity = output_.writer().available_capacity();
std::cout << "[Insert] Available capacity: " << capacity << "\n";

if (first_index >= next_unassembled_ + capacity) {


std::cout << "[Insert] Data is beyond writable capacity. Ignoring insert.\
n";
return; // Ignore if data is beyond the writable capacity
}

// Trim data to fit within the stream's capacity


if (first_index + data.size() > next_unassembled_ + capacity) {
size_t new_size = next_unassembled_ + capacity - first_index;
std::cout << "[Insert] Trimming data from size " << data.size() << " to "
<< new_size << "\n";
data.resize(new_size);
}

size_t written = 0;
if (first_index <= next_unassembled_) {
size_t overlap = next_unassembled_ - first_index;
if (overlap < data.size()) {
std::string segment = data.substr(overlap);
output_.writer().push(segment);
written = segment.size();
next_unassembled_ += written;
std::cout << "[Insert] Pushed data to ByteStream: \"" << segment
<< "\", written: " << written << ", next_unassembled_: " <<
next_unassembled_ << "\n";
}
}

first_index += written;
data = data.substr(written);
std::cout << "[Insert] Remaining data after push: \"" << data << "\"\n";

// Handle unassembled data (data that could not be written yet)


if (!data.empty()) {
auto it = unassembled_.lower_bound(first_index);
std::cout << "[Insert] Merging unassembled data. Current unassembled map:\
n";
for (const auto &entry : unassembled_) {
std::cout << " Unassembled index: " << entry.first << ", data: \"" <<
entry.second << "\"\n";
}

// Merge new data with overlapping unassembled data


while (it != unassembled_.end() && it->first <= first_index + data.size())
{
size_t overlap_start = it->first;
size_t overlap_len = std::min(overlap_start + it->second.size(),
first_index + data.size()) - overlap_start;

std::cout << "[Insert] Overlapping range detected. overlap_start: " <<


overlap_start
<< ", overlap_len: " << overlap_len << "\n";

if (overlap_start >= first_index) {


data.replace(overlap_start - first_index, overlap_len, it-
>second.substr(overlap_len));
}

std::cout << "[Insert] Removing overlapping data. Decrementing


bytes_pending_ by "
<< it->second.size() << "\n";
bytes_pending_ -= it->second.size();
it = unassembled_.erase(it);
}

// Only insert the remaining data into unassembled_ if it doesn't already


exist
if (!data.empty() && first_index >= next_unassembled_) {
std::cout << "[Insert] Inserting remaining data into unassembled_: \""
<< data
<< "\", at index: " << first_index << "\n";
unassembled_[first_index] = data;
bytes_pending_ += data.size();
std::cout << "[Insert] Incrementing bytes_pending_ by " << data.size()
<< ", new bytes_pending_: " << bytes_pending_ << "\n";
} else {
std::cout << "[Insert] Skipping insertion of fully assembled data.\n";
}
}

// Push unassembled data that can now be assembled


auto it = unassembled_.find(next_unassembled_);
while (it != unassembled_.end() && it->first == next_unassembled_) {
std::string segment = it->second;
output_.writer().push(segment);
next_unassembled_ += segment.size();
bytes_pending_ -= segment.size();
std::cout << "[Insert] Pushing unassembled data: \"" << segment
<< "\", new next_unassembled_: " << next_unassembled_
<< ", new bytes_pending_: " << bytes_pending_ << "\n";
it = unassembled_.erase(it); // Remove after pushing
}

if (is_last_substring) {
eof_ = true;
std::cout << "[Insert] Received last substring. Marking EOF.\n";
}

// Close the ByteStream if all data has been assembled and we've received the
EOF marker
if (eof_ && bytes_pending_ == 0 && next_unassembled_ == first_index +
data.size()) {
output_.writer().close();
std::cout << "[Insert] All data assembled. Closing ByteStream.\n";
}

// Final Debugging Information


std::cout << "[Insert] Final state:\n";
std::cout << " first_index: " << first_index << "\n";
std::cout << " next_unassembled_: " << next_unassembled_ << "\n";
std::cout << " bytes_pending_: " << bytes_pending_ << "\n";
std::cout << " Unassembled Map:\n";
for (const auto &entry : unassembled_) {
std::cout << " Unassembled index: " << entry.first << ", data: \"" <<
entry.second << "\"\n";
}
}

You might also like