Protocol Buffers(1):序列化、編譯與使用


Protocol Buffers docs:https://developers.google.com/protocol-buffers/docs/overview
github:https://github.com/protocolbuffers/protobuf

序列化與反序列化

有些時候,我們希望給數據結構或對象拍個“快照”,或者保存成文件,或者傳輸給其他應用程序。比如,在神經網絡訓練過程中,我們會將不同階段的網絡權重以模型文件的形式保存下來,如果訓練意外終止,可以重新載入模型文件將模型復原,繼續訓練。

將數據結構或對象以某種格式轉化爲字節流的過程,稱之爲序列化(Serialization),目的是把當前的狀態保存下來,在需要時復原數據結構或對象(序列化時不包含與對象相關聯的函數,所以後面只提數據結構)。反序列化(Deserialization),是序列化的逆過程,讀取字節流,根據約定的格式協議,將數據結構復原。如下圖所示,圖片來自geeksforgeeks

Serialization and Deserialization

在介紹具體技術之前,我們先在腦海裏分析下序列化和反序列化的過程:

  • 代碼運行過程中,數據結構和對象位於內存,其中的各項數據成員可能彼此緊鄰,也可能分佈在並不連續的各個內存區域,比如指針指向的內存塊等;
  • 文件中字節是順序存儲的,要想將數據結構保存成文件,就需要把所有的數據成員平鋪開(flatten),然後串接在一起;
  • 直接串接可能是不行的,因爲字節流中沒有天然的分界,所以在序列化時需要按照某種約定的格式(協議),以便在反序列化時知道“從哪裏到哪裏是哪個數據成員”,因此格式可能需要約定:指代數據成員的標識、起始位置、終止位置、長度、分隔符等
  • 由上可見,格式協議是最重要的,它直接決定了序列化和反序列化的效率、字節流的大小和可讀性等

Protocol Buffers概覽

本文的主角Protocol Buffers,簡稱Protobuf,是谷歌開源的一項序列化技術,用官方語言介紹就是:

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.
You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.

跨語言,跨平臺,相比XML和JSON 更小、更快、更容易,因爲XML、JSON爲了可閱讀、自解釋被設計成字符文本形式,所以體積更大,在編碼解碼上也更麻煩,而Protobuf序列化爲binary stream,體積更小,**但是喪失了可讀性——後面我們將看到可讀性可以通過另一種方式得到保證。**至於上面的"You define how you want your data to be structured once"該怎麼理解?參看下圖,圖片素材來自 Protocol Buffers官網首頁

Protocol Buffers Example
首先是proto文件,在其中定義我們想要序列化的數據結構,如上圖中的message Person,通過Protobuf提供的protoc.exe生成編解碼代碼文件(C++語言是.cc和.h),其中定義了類Person,類的各個成員變量與proto文件中的定義保持一致。序列化時,定義Person對象,對其成員變量賦值,調用序列化成員函數,將對象保存到文件。反序列化時,讀入文件,將Person對象復原,讀取相應的數據成員。

proto文件僅定義了數據的結構(name、id、email),具體的數據內容(1234、“John Doe”、“[email protected]”)保存在序列化生成的文件中,通過簡單的思考可知,序列化後的文件裏應該會存在一些輔助信息用來將數據內容與數據結構對應起來,以便在反序列化時將數據內容賦值給對應的成員。

流程如下:
Protocol Buffers Pipeline

對Protobuf有了大致的瞭解後,我們來看看如何編譯和使用Protobuf。

Protocol Buffers C++ 編譯

github release 下載對應版本的源碼,參見 cmake/README.md查看如何通過源碼編譯,筆者使用的是VS2015,通過如下指令編譯:

# 源碼位於protobuf-3.7.1目錄,cd protobuf-3.7.1/cmake
mkdir build
cd build
mkdir solution
cd solution
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=../../../../install ../.. -Dprotobuf_BUILD_TESTS=OFF

運行上面指令,會在solution目錄下生成vs解決方案,編譯整個解決方案,其中的INSTALL工程會生成install文件夾(位於protobuf-3.7.1/…/install),內含3個子文件夾:

  • bin - that contains protobuf protoc.exe compiler;
  • include - that contains C++ headers and protobuf *.proto files;
  • lib - that contains linking libraries and CMake configuration files for protobuf package.

通過上面3個文件夾,我們就可以完成序列化和反序列化工作。

Protocol Buffers C++ 使用

下面通過一個例子說明怎麼使用Protobuf。

新建proto文件example.proto,添加內容如下:

package example;

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

每個filed的格式:
required/optional/repeated FieldType FieldName = FieldNumber(a unique number in current message)

  • Field Numbers are used to identify your fields in the message binary format.
  • required: a well-formed message must have exactly one of this field.
  • optional: a well-formed message can have zero or one of this field (but not more than one).
  • repeated: this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.

將example.proto文件複製到bin目錄,運行如下指令:

protoc.exe example.proto --cpp_out=./

--cpp_out指定了生成cpp代碼文件的目錄,也可通過--java_out--python_out等來指定其他語言代碼生成的目錄。上面指令會在當前目錄下生成example.pb.cc和example.pb.h兩個文件,其中命名空間example下定義了Person類,該類繼承自public ::google::protobuf::MessagePerson的數據成員含有name_id_email_,以及對應的sethas等成員函數。

接下來,在vs中新建一個測試工程,

  • 將include目錄添加到 附加包含目錄,
  • 將lib目錄添加到 附加庫目錄,將lib文件添加到 附加依賴項,
  • 將生成example.pb.cc 和 example.pb.h也添加到工程,
  • 新建main.cpp,#include "example.pb.h"

添加如下內容:

#include "example.pb.h"

int main()
{
    // Set data
    example::Person msg;
    msg.set_id(1234);
    msg.set_name("John Doe");
    msg.set_email("[email protected]");

    // Serialization
    fstream output("./Person.bin", ios::out | ios::binary);
    msg.SerializePartialToOstream(&output);
    output.close();

    // Deserialization
    example::Person msg1;
    fstream input("./Person.bin", ios::in | ios::binary);
    msg1.ParseFromIstream(&input);
    input.close();

    // Get data
    cout << msg1.id() << endl; // 1234
    cout << msg1.name() << endl; // John Doe
    cout << msg1.email() << endl; // [email protected]

    return 0;
}

上面代碼將對象保存到Person.bin文件,在反序列化恢復對象。Person.bin文件內容如下:
Person binary stream

還是能看出一些規律的,字符串前1個字節表示的整數與字符串的長度相同,這是偶然嗎?如果字符串很長,比如600個字符,超出1個字節能表示的範圍怎麼辦?其他字節又是什麼含義?

這些問題,比如關於Protobuf是如何編碼的,以及生成的cc和h文件代碼細節,留到後面的文章介紹。

Protocol Buffers的可讀性

二進制文件雖然體積更小,但其可讀性無疑是差的,XML和JSON的優勢之一就是可讀性,可讀意味着可編輯、可人工校驗,Protobuf是不是就不能做到了呢?

並不是的,讓我們繼續在main函數中添加如下代碼:

#include "google/protobuf/io/zero_copy_stream_impl.h"

int main()
{
    // ……
    
    // Serialization to text file
    fw.open("./Person.txt", ios::out | ios::binary);
    google::protobuf::io::OstreamOutputStream *output = new google::protobuf::io::OstreamOutputStream(&fw);
    google::protobuf::TextFormat::Print(msg, output);
    delete output;
    fw.close();

    // Deserialization from text file
    example::Person msg2;
    fr.open("./Person.txt", ios::in | ios::binary);
    google::protobuf::io::IstreamInputStream input(&fr);
    google::protobuf::TextFormat::Parse(&input, &msg2);
    fr.close();

    // Get data
    cout << msg2.id() << endl; // 1234
    cout << msg2.name() << endl; // John Doe
    cout << msg2.email() << endl; // [email protected]
}

這段代碼是將對象保存成文本文件,再復原。打開文件Person.txt,其內容如下:

name: "John Doe"
id: 1234
email: "[email protected]"

和JSON是不是很像,也是類似的key-value對。

有了文本文件我們就可以直接閱讀、校驗和修改序列化後的數據,並且自如地在二進制文件和文本文件間轉換,比如修改文本文件、恢復成對象、再導出二進制文件。

相信通過這篇文章,你已經對Protocol Buffer有了初步的瞭解,後續文章將深入介紹Protobuf的編解碼和源碼細節。

以上。

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章