Search code examples
c++protocol-buffers

Handle image and delta updates using protobuf 3


Having this proto message :

message Price
{
Double bid = 1;
Double ask = 2;
Double last = 3;
Double tick = 4;
Int64 bidQty = 5;
Int64 askQty = 6;
String tradingPhase = 7;
// ... other fields
}

Is there an efficient way to handle delta updates.

After sending the full image (whole message) the first time, I need to send/receive only modified fields. Using proto 2, there is has_field() method for each field to check the existence of a field in the message.

Is there a solution using the proto 3 without defining all fields as optional ?


Solution

  • Here is the relevant documentation

    Message fields can be one of the following:

    • singular: a well-formed message can have zero or one of this field (but not more than one). When using proto3 syntax, this is the default field rule when no other field rules are specified for a given field. You cannot determine whether it was parsed from the wire. It will be serialized to the wire unless it is the default value. For more on this subject, see Field Presence.

    • optional: the same as singular, except that you can check to see if the value was explicitly set. An optional field is in one of two possible states: the field is set, and contains a value that was explicitly set or parsed from the wire. It will be serialized to the wire. the field is unset, and will return the default value. It will not be serialized to the wire.

    • repeated: this field type can be repeated zero or more times in a well-formed message. The order of the repeated values will be preserved. map: this is a paired key/value field type. See Maps for more on this field type.

    It sounds like you need to declare the fields optional in order to test whether they are specified.