- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 following implementation 在 C++ 中解析带分隔符的 protobuf 消息(来自文件) readDelimitedFrom()
- 也复制如下:
bool readDelimitedFrom(
google::protobuf::io::ZeroCopyInputStream* rawInput,
google::protobuf::MessageLite* message) {
// We create a new coded stream for each message. Don't worry, this is fast,
// and it makes sure the 64MB total size limit is imposed per-message rather
// than on the whole stream. (See the CodedInputStream interface for more
// info on this limit.)
google::protobuf::io::CodedInputStream input(rawInput);
// Read the size.
uint32_t size;
if (!input.ReadVarint32(&size)) return false;
// Tell the stream not to read beyond that size.
google::protobuf::io::CodedInputStream::Limit limit =
input.PushLimit(size);
// Parse the message.
if (!message->MergeFromCodedStream(&input)) return false;
if (!input.ConsumedEntireMessage()) return false;
// Release the limit.
input.PopLimit(limit);
return true;
}
我的问题是我需要根据消息中包含的 uint32_t
字段对消息进行分组并分批处理它们 - 我们称它为 id
。
目前,我的主循环中有以下代码:
...
int infd = -1;
_sopen_s(&infd, argv[1], _O_RDONLY | _O_BINARY, _SH_DENYWR, _S_IREAD);
google::protobuf::io::ZeroCopyInputStream *input =
new google::protobuf::io::FileInputStream(infd);
std::vector<ProtoMessage> msgList;
bool readMore = true;
do {
ProtoMessage msg;
readMore = readNextMessage(input, msg, msgList);
if (!msgList.empty()) {
std::cout << "Processing Message Batch - ID: " << msgList[0].id();
/* some processing done here */
}
} while (readMore);
readNextMessage()
的实现如下:
bool readNextMessage(
google::protobuf::io::ZeroCopyInputStream* rawInput,
ProtoMessage& nextMsg,
std::vector<ProtoMessage>& batchList) {
bool sameBatch = false;
uint32_t msgID = 0;
do {
if (readDelimitedFrom(rawInput, &scan) == -1)
return false;
if (nextMsg.id() == 0)
msgID = nextMsg.id(); // guaranteed to be non-zero
if (sameBatch = (msgID == nextMsg.id()))
batchList.push_back(nextMsg);
} while (sameBatch);
// need a way to roll-back here as nextMsg is now the first new
// ProtoMessage belonging to a new batch.
return true;
}
这个函数的逻辑相当简单:获取一个 ZeroCopyInputStream
并使用 readDelimitedFrom()
解析它以将 ProtoMessage
消息分组到一个 vector 中基于他们的 id
字段。如果遇到具有新 ID 的消息,则停止并将控制返回给 main
以处理消息批处理。
这导致了非期望的要求,即必须使用/读取第一条消息(包括其 Varint32 编码的大小)不属于前一批,而没有办法“备份”溪流。我希望能够将 ZeroCopyInputStream
指向最后一个 readDelimitedFrom()
之前的位置。
有什么方法可以修改readDelimitedFrom()
使其也返回调用时消耗的字节数,然后在ZeroCopyInputStream
上使用指针运算来实现所需的功能?
提供的函数 ZeroCopyInputStream::Backup()
有一个先决条件,即 ZeroCopyInputStream::Next()
是最后一个方法调用。显然,使用 CodedInputStream
包装器解析分隔消息时情况并非如此。
最佳答案
ZeroCopyInputStream::Backup()
只能备份接收到的最后一个缓冲区。一条消息可能跨越多个缓冲区,因此在给定 ZeroCopyInputStream
接口(interface)的情况下,没有通用的方法来执行您想要的操作。
一些选项:
rawInput->ByteCount()
,以便准确确定消息开始的字节位置。如果您需要回滚,请向后查找底层文件并在其之上重新创建 ZeroCopyInputStream
。当然,这只有在您从文件中读取时才有效。关于c++ - 是否可以使用 Varint32 大小前缀的 Protocol Buffer 消息实现类似 'FileInputStream::BackUp()' 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32747013/
为什么这段代码的输出是: package main import ( "fmt" "encoding/binary" ) func main() { var myByte by
我目前正在研究 protocol buffers 的文档.变体被描述为: Each byte in a varint, except the last byte, has the most signi
为什么 binary.Varint 返回的结果与 binary.Read 不同? package main import "fmt" import "encoding/binary" import "
有人有过将 cocoaasyncsocket 与 google protobuf 一起使用的经验吗?我想使用 varint 来分隔帧,使用基于 netty 的客户端/服务器组合非常简单,但在使用 co
我有一个 python 整数列表,我想知道当编码为 Protocol Buffers 可变长度整数序列或 varints 时它将占用多少空间。 .在不对整数进行实际编码的情况下解决这个问题的最佳方法是
我正在使用 levigo ,Go 的 leveldb 绑定(bind)。我的 key 是 int64 的,需要保持排序。默认情况下,leveldb 使用字节比较器,所以我尝试使用 varint 编码。
我需要阅读 VarInts来自 C/C++ 中的 linux 套接字。任何图书馆、想法或其他东西? 我尝试读取并将 char 转换为 bool[8] 以尝试读取 VarInt 但没有成功... 此外,
我的应用程序多次发送 varint。每次我必须为 2 个对象分配内存:CodedOutputStream 和 FileOutputStream,然后再释放它。 IMO 这是不必要的时间损失。如果没有所
我想问一下,因为我觉得很奇怪:varint 的编写方式取决于目标。 我的简单代码可以写入文件或套接字。当我写入文件时,hexdump 显示 0000000 02ac 0000002 当我写入套接字时,
每个人, 我正在使用 protobuf-net 库将文本数据序列化-反序列化为二进制文件。我过去有过类似的错误,但后来我犯了将二进制数据写入文本文件的错误。这次我确定文件是以二进制模式写入的。当我读取
我正在使用 google protocol buffer 库: $protoc --version libprotoc 2.5.0 我在网上搜索,它说一个整数的值编码由多字节组成,每个字节的第一个位,
目前,我正在使用DataOutput.writeLong(long)序列化一些长数据。这样做的问题很明显:在很多情况下,多头都非常小。我想知道性能最高的 varint 实现是什么?我已经从 Proto
下面的代码在数字 2^31-1 时产生错误(大)结果。这是出乎意料的,因为我从未使用过 uint32_t。 void encodeVarint(uint64_t value, uint8_t* ou
我注意到 Jeff 的幻灯片“构建大规模信息检索系统的挑战”,也可以在这里下载:http://research.google.com/people/jeff/WSDM09-keynote.pdf ,提
我是一名优秀的程序员,十分优秀!