Protobuf使用重复一条消息与许多空字段
我正在设计一个代表一个事件的原始福夫,每个事件都可以容纳额外的字段。
有很多可能的额外字段(〜100),但是每个消息中只有一小部分(〜3)
每个额外的字段只会使用一次,但是其中多个可以存在,因此我想要要具有anyof
消息类型的概念,但不幸的是,Protobuf中没有这样的事情。
因此,尝试嘲笑这种行为,并如这个将我所有的额外字段都放在oneof
中,用消息包装,然后在我的事件中使用此消息作为重复
:
message ExtraField {
oneof extra_field_value {
string extraData1 = 1;
uint64 extraData2 = 2;
....
SomeOtherMessage extraData100 = 100;
}
}
message MyEvent {
uint64 timestamp = 1;
string event_name = 2;
string some_other_data = 3;
...
repeated ExtraField extra_fields = 8;
}
即使此解决方案更明确,以理解我,但它不是最有效的内存,并且具有Oneof
实现的重复消息允许添加相同的额外字段多一次(不必要的行为),
我也可以将所有额外的字段写入一条内部消息,但是如果我正确理解的话,其中大多数将一直是空的
message ExtraFields {
string extraData1 = 1;
uint64 extraData2 = 2;
....
SomeOtherMessage extraData100 = 100;
}
message MyEvent {
uint64 timestamp = 1;
string event_name = 2;
string some_other_data = 3;
...
extraFields extra_fields = 8;
}
,使用消息中的空字段不会使我的序列化数据更大,因此,第二个Protobuf设计是首选的实践,
我对吗? 我的需求还有另一种Protobuf设计吗?
I'm designing a protobuf to represent an event, where each event can hold extra fields.
There are a lot of possible extra fields (~100), but only a small portion of them will be used in each message (~3)
Each extra field will be used only once, but multiple of them can exist, therefore I would like to have a concept of an anyof
message type, but unfortunately, there is no such thing in protobuf.
So to try and mock this behavior, and as mentioned in this discussion I thought I can put all my extra fields in a oneof
, wrap it with a message, and use this message as repeated
in my event:
message ExtraField {
oneof extra_field_value {
string extraData1 = 1;
uint64 extraData2 = 2;
....
SomeOtherMessage extraData100 = 100;
}
}
message MyEvent {
uint64 timestamp = 1;
string event_name = 2;
string some_other_data = 3;
...
repeated ExtraField extra_fields = 8;
}
Even though this solution is more explicit for my understanding, it isn't the most memory effective, and the repeated message with oneof
implementation allows to add the same extra field more than once (unwanted behavior)
I can also just write all the extra fields as-is in an inner message, but most of them will be empty all the time
message ExtraFields {
string extraData1 = 1;
uint64 extraData2 = 2;
....
SomeOtherMessage extraData100 = 100;
}
message MyEvent {
uint64 timestamp = 1;
string event_name = 2;
string some_other_data = 3;
...
extraFields extra_fields = 8;
}
If I understand correctly, using empty fields in a message isn't going to make my serialized data larger, and therefore the second protobuf design is the preferred practice
Am I correct?
Is there another protobuf design for my needs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论