Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::trace_api::compressed_file_impl Struct Reference
Collaboration diagram for sysio::trace_api::compressed_file_impl:

Public Member Functions

 ~compressed_file_impl ()
 
void read (char *d, size_t n, fc::cfile &file)
 
void seek (long loc, fc::cfile &file)
 

Public Attributes

z_stream strm
 
std::vector< uint8_tcompressed_buffer = std::vector<uint8_t>(compressed_buffer_size)
 
std::vector< uint8_tread_buffer = std::vector<uint8_t>(read_buffer_size)
 
size_t remaining_read_buffer = 0
 
bool initialized = false
 
size_t file_size = 0
 

Static Public Attributes

static constexpr size_t read_buffer_size = 4*1024
 
static constexpr size_t compressed_buffer_size = 4*1024
 

Detailed Description

Definition at line 22 of file compressed_file.cpp.

Constructor & Destructor Documentation

◆ ~compressed_file_impl()

sysio::trace_api::compressed_file_impl::~compressed_file_impl ( )
inline

Definition at line 26 of file compressed_file.cpp.

Member Function Documentation

◆ read()

void sysio::trace_api::compressed_file_impl::read ( char * d,
size_t n,
fc::cfile & file )
inline

Definition at line 34 of file compressed_file.cpp.

35 {
36 if (!initialized) {
37 if (Z_OK != inflateInit2(&strm, raw_zlib_window_bits)) {
38 throw std::runtime_error("failed to initialize decompression");
39 }
40
42 strm.avail_in = 0;
43 initialized = true;
44 }
45
46 size_t written = 0;
47
48 // consume the left over from the last read if there is any
49 if (remaining_read_buffer > 0) {
50 auto to_read = std::min(remaining_read_buffer, n);
51 std::memcpy(d, read_buffer.data(), to_read );
52 remaining_read_buffer -= to_read;
53 written += to_read;
54
55 if ( remaining_read_buffer > 0 ) {
56 std::memmove(read_buffer.data(), read_buffer.data() + to_read, remaining_read_buffer);
57 return;
58 }
59 }
60
61
62 // decompress more chunks
63 while (written < n) {
64 if ( strm.avail_in == 0 ) {
65 size_t remaining = file_size - file.tellp();
66 size_t to_read = std::min((size_t)compressed_buffer.size(), remaining);
67 file.read(reinterpret_cast<char*>(compressed_buffer.data()), to_read);
68 strm.avail_in = to_read;
69 strm.next_in = compressed_buffer.data();
70 }
71
72 do {
73 strm.avail_out = read_buffer.size();
74 strm.next_out = read_buffer.data();
75 auto ret = inflate(&strm, Z_NO_FLUSH);
76
77 if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
78 throw compressed_file_error("Error decompressing: " + std::string(strm.msg));
79 }
80
81
82
83 auto bytes_decompressed = read_buffer.size() - strm.avail_out;
84 if (bytes_decompressed > 0) {
85 auto to_copy = std::min(bytes_decompressed, n - written);
86 std::memcpy(d + written, read_buffer.data(), to_copy);
87 written += to_copy;
88
89 if (bytes_decompressed > to_copy) {
90 // move remaining to the front of the buffer
91 std::memmove(read_buffer.data(), read_buffer.data() + to_copy, bytes_decompressed - to_copy);
92 remaining_read_buffer = bytes_decompressed - to_copy;
93 }
94 }
95
96 if (written < n && ret == Z_STREAM_END) {
97 throw std::ios_base::failure("Attempting to read past the end of a compressed file");
98 }
99
100 if (ret == Z_BUF_ERROR) {
101 // need more input
102 if (strm.avail_in != 0) {
103 throw compressed_file_error("Error decompressing cannot continue processing input");
104 }
105 break;
106 }
107
108 } while (strm.avail_out == 0 && written < n);
109 }
110 }
CK_RV ret
Here is the caller graph for this function:

◆ seek()

void sysio::trace_api::compressed_file_impl::seek ( long loc,
fc::cfile & file )
inline

Definition at line 112 of file compressed_file.cpp.

112 {
113 if (initialized) {
114 inflateEnd(&strm);
115 initialized = false;
116 }
117
118 long remaining = loc;
119
120 // read in the seek point map
121 file.seek_end(-expected_seek_point_count_size);
122 seek_point_count_type seek_point_count = 0;
123 file.read(reinterpret_cast<char*>(&seek_point_count), sizeof(seek_point_count));
124
125 if (seek_point_count > 0) {
126 int seek_map_size = sizeof(seek_point_entry) * seek_point_count;
127 file.seek_end(-expected_seek_point_count_size - seek_map_size);
128
129 std::vector<seek_point_entry> seek_point_map(seek_point_count);
130 file.read(reinterpret_cast<char*>(seek_point_map.data()), seek_point_map.size() * sizeof(seek_point_entry));
131
132 // seek to the neareast seek point
133 auto iter = std::lower_bound(seek_point_map.begin(), seek_point_map.end(), (uint64_t)loc, []( const auto& lhs, const auto& rhs ){
134 return std::get<0>(lhs) < rhs;
135 });
136
137 // special case when there is a seek point that is exact
138 if ( iter != seek_point_map.end() && std::get<0>(*iter) == loc ) {
139 file.seek(std::get<1>(*iter));
140 return;
141 }
142
143 // special case when this is before the first seek point
144 if ( iter == seek_point_map.begin() ) {
145 file.seek(0);
146 } else {
147 // if lower bound wasn't exact iter will be one past the seek point we need
148 const auto& seek_pt = *(iter - 1);
149 file.seek(std::get<1>(seek_pt));
150 remaining -= std::get<0>(seek_pt);
151 }
152 } else {
153 file.seek(0);
154 }
155
156 // read up to the expected offset
157 if (remaining > 0) {
158 auto pre_read_buffer = std::vector<char>(remaining);
159 read(pre_read_buffer.data(), pre_read_buffer.size(), file);
160 }
161 }
unsigned __int64 uint64_t
Definition stdint.h:136
void read(char *d, size_t n, fc::cfile &file)
Here is the call graph for this function:

Member Data Documentation

◆ compressed_buffer

std::vector<uint8_t> sysio::trace_api::compressed_file_impl::compressed_buffer = std::vector<uint8_t>(compressed_buffer_size)

Definition at line 164 of file compressed_file.cpp.

◆ compressed_buffer_size

size_t sysio::trace_api::compressed_file_impl::compressed_buffer_size = 4*1024
staticconstexpr

Definition at line 24 of file compressed_file.cpp.

◆ file_size

size_t sysio::trace_api::compressed_file_impl::file_size = 0

Definition at line 168 of file compressed_file.cpp.

◆ initialized

bool sysio::trace_api::compressed_file_impl::initialized = false

Definition at line 167 of file compressed_file.cpp.

◆ read_buffer

std::vector<uint8_t> sysio::trace_api::compressed_file_impl::read_buffer = std::vector<uint8_t>(read_buffer_size)

Definition at line 165 of file compressed_file.cpp.

◆ read_buffer_size

size_t sysio::trace_api::compressed_file_impl::read_buffer_size = 4*1024
staticconstexpr

Definition at line 23 of file compressed_file.cpp.

◆ remaining_read_buffer

size_t sysio::trace_api::compressed_file_impl::remaining_read_buffer = 0

Definition at line 166 of file compressed_file.cpp.

◆ strm

z_stream sysio::trace_api::compressed_file_impl::strm

Definition at line 163 of file compressed_file.cpp.


The documentation for this struct was generated from the following file: