0

我想在一个文件中保留一堆简单的结构(目前每个结构只有 3 个整数),并且能够在任何给定时间读回其中一个结构。

作为第一步,我试图将它们输出到一个文件中,然后使用 boost::serialization 将它们读回。目前我正在这样做,它崩溃了:

std::array<Patch, 3> outPatches;

outPatches[0].ZOrigin = 0;
outPatches[0].XOrigin = 0;
outPatches[0].Resolution = 64;

outPatches[1].ZOrigin = 1;
outPatches[1].XOrigin = 5;
outPatches[1].Resolution = 3;

outPatches[2].ZOrigin = 123;
outPatches[2].XOrigin = 546;
outPatches[2].Resolution = 6;

std::ofstream ofs("testing.sss", std::ios::binary);

for (auto const& patch : outPatches)
{
    std::cout << "start archive: " << ofs.tellp() << std::endl;
    {
    boost::archive::binary_oarchive oa(ofs);
    std::cout << "start patch: " << ofs.tellp() << std::endl;

    oa << patch;
    }
}

ofs.close();


std::array<Patch, 3> inPatches;

std::ifstream ifs("testing.sss", std::ios::binary);

for (auto& patch : inPatches)
{
    std::cout << "start archive: " << ifs.tellg() << std::endl;
    {
    boost::archive::binary_iarchive ia(ifs); // <-- crash here on second patch

    std::cout << "start patch: " << ifs.tellg() << std::endl;

    ia >> patch;
    }
}

ifs.close();

for (int i = 0; i != 3; ++i)
    std::cout << "check: " << (inPatches[i] == outPatches[i]) << std::endl;

我正计划使用 tell 对每个结构的位置进行索引,并在加载时寻求跳到该结构。这是一个合理的方法吗?除了基础知识之外,我对流的了解不多。

我尝试将所有补丁放在一个 o/iarchive 中,这样可以很好地按顺序读取所有内容。但是,在流中搜索不起作用。

我找到了这个,这可能是我想要的,但我不知道它在做什么或如何使用它,或者它是否适用于 boost::serialization: read part of a file with iostreams

如有必要,我可能愿意切换到另一种序列化方法,因为我对此还没有走得太远。

编辑 3:将编辑 1 和 2 移至答案。

4

2 回答 2

1

我曾经有过类似的情况(使用提升/序列化)。我当时所做的(如果我记得的话,它非常有效)是将文件映射到虚拟地址,编写一个在内存缓冲区而不是文件上操作的流媒体,并为我想要读取的每个部分分配适当的偏移量流媒体作为缓冲区开始/长度并使用流媒体初始化 iarchive,因此序列化库将其视为每个对象都在单独的文件中。

当然,添加到文件需要重新映射。现在回想起来,这似乎有点奇怪,但它是有效的,公平的。

于 2013-04-15T11:50:38.950 回答
1

提升序列化

似乎不可能在 boost 序列化存档中跳过。到目前为止,我最好的方法是在一个流上使用多个档案:

static const int numPatches = 5000;

std::vector<int> indices(numPatches, 0);
std::iota(indices.begin(), indices.end(), 0);

std::vector<Patch> outPatches(numPatches, Patch());

std::for_each(outPatches.begin(), outPatches.end(), 
    [] (Patch& p)
    {
        p.ZOrigin = rand();
        p.XOrigin = rand();
        p.Resolution = rand();
    });


std::vector<int64_t> offsets(numPatches, 0);

std::ofstream ofs("testing.sss", std::ios::binary);

for (auto i : indices)
{
    offsets[i] = ofs.tellp();

    boost::archive::binary_oarchive oa(ofs, 
        boost::archive::no_header | boost::archive::no_tracking);
    oa << outPatches[i];
}

ofs.close();


std::random_shuffle(indices.begin(), indices.end());


std::vector<Patch> inPatches(numPatches, Patch());

std::ifstream ifs("testing.sss", std::ios::binary);

for (auto i : indices)
{
    ifs.seekg(offsets[i]);

    boost::archive::binary_iarchive ia(ifs,
        boost::archive::no_header | boost::archive::no_tracking);
    ia >> inPatches[i];

    ifs.clear();
}

std::cout << std::all_of(indices.begin(), indices.end(), 
    [&] (int i) { return inPatches[i] == outPatches[i]; }) << std::endl;

不幸的是,这很慢,所以我认为我不能使用它。接下来是测试 protobuf。


谷歌::protobuf

我有一些使用 protobuf 的东西。它需要一些摆弄(显然我必须使用 LimitingInputStream 类型,并存储每个对象的大小),但它比 boost::serialization 版本快得多:

static const int numPatches = 500;

std::vector<int> indices(numPatches, 0);
std::iota(indices.begin(), indices.end(), 0);

std::vector<Patch> outPatches(numPatches, Patch());

std::for_each(outPatches.begin(), outPatches.end(), 
    [] (Patch& p)
    {
        p.ZOrigin = rand();
        p.XOrigin = rand();
        p.Resolution = 64;
    });


std::vector<int64_t> streamOffset(numPatches, 0);
std::vector<int64_t> streamSize(numPatches, 0);

std::ofstream ofs("testing.sss", std::ios::binary);

PatchBuffer buffer;

for (auto i : indices)
{
    buffer.Clear();

    WriteToPatchBuffer(buffer, outPatches[i]);

    streamOffset[i] = ofs.tellp();
    streamSize[i] = buffer.ByteSize();

    buffer.SerializeToOstream(&ofs);
}

ofs.close();

std::random_shuffle(indices.begin(), indices.end());

std::vector<Patch> inPatches(numPatches, Patch());

std::ifstream ifs("testing.sss", std::ios::binary);

for (auto i : indices)
{
    ifs.seekg(streamOffset[i]);

    buffer.Clear();

    google::protobuf::io::IstreamInputStream iis(&ifs);
    google::protobuf::io::LimitingInputStream lis(&iis, streamSize[i]);
    buffer.ParseFromZeroCopyStream(&lis);

    ReadFromPatchBuffer(inPatches[i], buffer);

    ifs.clear();
}

std::cout << std::all_of(indices.begin(), indices.end(), 
    [&] (int i) { return inPatches[i] == outPatches[i]; }) << std::endl;
于 2013-04-15T15:32:21.973 回答