我想将我的地形数据保存到一个文件中并只加载其中的一部分,因为它太大而无法将其作为一个整体存储在内存中。实际上我什至不知道 protobuf 是否适合这个目的。
例如我会有这样的结构(在语法上可能无效,我只知道简单的基础知识):
message Quad {
required int32 x = 1;
required int32 z = 2;
repeated int32 y = 3;
}
x 和 z 值在我的程序中可用,通过使用它们我想找到具有相同 x 的正确 Quad 对象> 和 z(在文件中)以获得 y 值。但是,我不能只使用 ParseFromIstream() 解析文件,因为(我认为是这样)它将整个文件加载到内存中,但在我的例子中文件太大了。
那么,protobuf 是否能够加载一个对象,让我检查它,如果对象有误,给我第二个?
实际上...我只想问:ParseFromIstream() 是否将整个文件加载到内存中?
最佳答案
虽然有些库允许您部分读取文件,但 Google 推荐的技术是简单地让文件包含多条消息:
https://developers.google.com/protocol-buffers/docs/techniques
Protocol Buffers are not designed to handle large messages. As a general rule of thumb, if you are dealing in messages larger than a megabyte each, it may be time to consider an alternate strategy.
That said, Protocol Buffers are great for handling individual messages within a large data set. Usually, large data sets are really just a collection of small pieces, where each small piece may be a structured piece of data.
因此,您可以将一长串 Quad
消息写入文件,由消息的长度分隔。如果您需要随机查找特定的 Quad
,您可能需要添加某种索引。
关于c++ - protobuf 可以部分读取吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14899556/