javaprotocol-buffersproto3

Ordering of Protobuf maps


I am creating message objects in proto3 and using auto-generated java classes. I want to have unique key assigned to each of the message object.

message Obj {
    ...
    string unique_key = 1;
    ...
}

During construction of Obj, it receives from a microservice, a proto object called metaData, which is defined as follows:

message metData {
     map<string, string> keyFields = 1;
}

Based on entries in metaData object, unique_key is created by iterating over the map and hashing each of the entries.(There are upto 10 entries in keyFields)

The protobuf documentation says, the ordering of keys can't be defined. How should I guarantee that different metaData objects with same entries in keyFields generate same unique_key?


Solution

  • You have two options in general:

    1. Implement a commutative hash function. E.g. get the hashes of the keys and simply sum them ignoring overflow.
    2. Get the keys and sort them. Then hash them sorted. If there are "many" keys, consider caching the hash value.