Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incremental write of Protocol Buffer object

I have Protocol Buffer for logging data.

message Message {
    required double val1 = 1;
    optional int val2 = 2;
}

message BigObject {
    repeated Message message = 1;
}

I receive messages one per second. They stored in memory with my BigObject and they used for some tasks. But at the same time i want to store that messages in file for backup in case application crash. Simple writing BigObject every time will be waste of time. And I trying to find way to write only added messages since last write to file. Is there a way for that?

like image 714
Evgen Bodunov Avatar asked Jul 26 '11 09:07

Evgen Bodunov


People also ask

What are protocol buffers used for?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

What is protocol buffer in gRPC?

Protocol Buffer, a.k.a. Protobuf Protobuf is the most commonly used IDL (Interface Definition Language) for gRPC. It's where you basically store your data and function contracts in the form of a proto file.

Why protobuf is faster than JSON?

JSON is usually easier to debug (the serialized format is human-readable) and easier to work with (no need to define message types, compile them, install additional libraries, etc.). Protobuf, on the other hand, usually compresses data better and has built-in protocol documentation via the schema.

What is protocol buffer in Tensorflow?

What are protocol buffers? Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.


1 Answers

Protobuf is an appendable format, and your layout is ideal for this. Just open your file positioned at the end, and start with a new (empty) BigObject. Add/serialize just the new Message instance, and write to the file (from the end onwards).

Now, if you parse your file from the beginning you will get a single BigObject with all the Message instances (old and new).

You could actually do this by logging each individual Message as it arrives, as long as you wrap it in a BigObject each time, i.e. in pseudo-code

loop {
    msg = await NextMessage();
    wrapper = new BigObject();
    wrapper.Messages.Add(msg);

    file = OpenFileAtEnd();
    wrapper.WriteTo(file);
    file.Close();
}
like image 190
Marc Gravell Avatar answered Oct 02 '22 03:10

Marc Gravell