![](/img/trans.png)
[英]Implicitly-declared boost::iostreams::mapped_file_source is deprecated
[英]how to use boost::iostreams::mapped_file_source with a gzipped input file
我使用boost :: iostreams :: mapped_file_source從特定位置讀取文本文件到特定位置並操縱每一行(使用g ++ -Wall -O3 -lboost_iostreams -o test main.cpp編譯):
#include <iostream>
#include <string>
#include <boost/iostreams/device/mapped_file.hpp>
int main() {
boost::iostreams::mapped_file_source f_read;
f_read.open("in.txt");
long long int alignment_offset(0);
// set the start point
const char* pt_current(f_read.data() + alignment_offset);
// set the end point
const char* pt_last(f_read.data() + f_read.size());
const char* pt_current_line_start(pt_current);
std::string buffer;
while (pt_current && (pt_current != pt_last)) {
if ((pt_current = static_cast<const char*>(memchr(pt_current, '\n', pt_last - pt_current)))) {
buffer.assign(pt_current_line_start, pt_current - pt_current_line_start + 1);
// do something with buffer
pt_current++;
pt_current_line_start = pt_current;
}
}
return 0;
}
目前,我想讓這段代碼處理gzip文件並修改代碼如下:
#include<iostream>
#include<boost/iostreams/device/mapped_file.hpp>
#include<boost/iostreams/filter/gzip.hpp>
#include<boost/iostreams/filtering_streambuf.hpp>
#include<boost/iostreams/filtering_stream.hpp>
#include<boost/iostreams/stream.hpp>
int main() {
boost::iostreams::stream<boost::iostreams::mapped_file_source> file;
file.open(boost::iostreams::mapped_file_source("in.txt.gz"));
boost::iostreams::filtering_streambuf< boost::iostreams::input > in;
in.push(boost::iostreams::gzip_decompressor());
in.push(file);
std::istream std_str(&in);
std::string buffer;
while(1) {
std::getline(std_str, buffer);
if (std_str.eof()) break;
// do something with buffer
}
}
這段代碼也運行良好,但我不知道如何設置起點(pt_current)和終點(pt_last)就像第一個代碼一樣。 你能告訴我如何在第二個代碼中設置這兩個值嗎?
答案是否定的,這是不可能的。 壓縮流需要有索引。
真正的問題是為什么? 。 您正在使用內存映射文件。 進行即時壓縮/解壓縮只會降低性能並增加內存消耗。
如果你不是缺乏實際的文件存儲空間,那么你應該考慮使用二進制表示法,或保持原文不變。
當使用具有隨機訪問的文本文件時,二進制表示可以回避所涉及的大部分復雜性。
一些鼓舞人心的樣本:
您基本上發現的是文本文件不是隨機訪問,壓縮使索引基本上模糊(沒有從壓縮流偏移到未壓縮流偏移的精確映射)。
查看zlib常見問題解答中提到的zlib發行版中的zran.c
示例:
28. 我可以在壓縮流中隨機訪問數據嗎?
不,不是沒有一些准備。 如果在定期壓縮時使用
Z_FULL_FLUSH
,請在這些點上仔細寫入所有待處理數據,並保留這些位置的索引,然后您可以在這些點開始解壓縮。 您必須小心不要經常使用Z_FULL_FLUSH
,因為它會顯着降低壓縮性能。 或者,您可以掃描一次deflate流以生成索引,然后使用該索引進行隨機訪問。 參見examples/zran.c
¹您可以專門查看並行實現,例如pbzip2或pigz; 這些必然會使用這些“塊”或“幀”來安排跨核心的負載
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.