Ĭ��
���� �������� 0
�뿪��IM������Ʒ�¿ӣ�����3���¹����ҿ�Դ����ߣ������������·��... ��վ�����㽨��
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�
�Ķ���2801�� | ���ۣ�0�� �ղ�1 ����1 ��1
΢��ɨһɨ��ע��

����������Ѷ�ƿ�������־�S��������ʱͨѶ���������Ű��Ż����޶���


1������


Protobuf �� Google ��Ʒ�����л����ܣ��ɿ�ƽ̨��������ʹ�ã���չ�����á��� XML��JSON �����л�������ͬ��Protobuf �㷺��Ӧ�������ݴ洢�����紫�䣬RPC ���õȻ����������������е�Э�顢���á����ݿ��ı��ﶼ���� Protobuf �����г��صģ��������������ܽ�һ�� Protobuf ����Э�飬�����ȿӡ�

IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_cover_opti.png

2��ϵ������


������ϵ�������еĵ� 11 ƪ����ϵ����Ŀ¼���£�


3��������ʶProtobuf  


�����򵥵Ľ�����һ�� Protobuf �������кܶ��ŵ㣬��Ҳ��һЩ��Ҫע����ȱ�㡣

�ŵ㣺

  • 1��Ч�ʸ���Protobuf �Զ����Ƹ�ʽ�洢���ݣ����� XML �� JSON ���ı���ʽ�����գ�Ҳ���졣���л��ͷ����л����ٶ�Ҳ�ܿ죻
  • 2��������֧����Protobuf ֧�ֶ��ֱ������ԣ����� C++��Java��Python �ȣ�
  • 3�������Ľṹ������ʹ�� Protobuf�����������ض������ݵĽṹ����������ά�������⣻
  • 4�����������������������ӻ���ɾ���ֶΣ��������ƻ��ϵ�Ӧ�ó����������ڳ��ڵ���Ŀ��˵�Ƿdz��м�ֵ�ġ�

ȱ�㣺

  • 1����ֱ�������� Protobuf �Ƕ����Ƹ�ʽ���˲���ֱ���Ķ����޸����������ڵ��ԺͲ�����˵���ܻ���Щ���ѣ�
  • 2��ȱ��һЩ��������������û���ڽ������ڡ�ʱ�����ͣ�������Щ���͵����ݣ���Ҫ�ֶ�ת���ɿ���֧�ֵ����ͣ��� string �� int��
  • 3����Ҫ�����ı��벽��������Ҫ�ȶ������ݽṹ��Ȼ��ʹ�� Protobuf �ı���������������Ŀ�����ԵĴ��룬����һ�������IJ��裬���ܻ�Ӱ�쿪�����̡�

�ܵ���˵��Protobuf ��һ��ǿ������Ч���������л����ߣ�����һ���濴�����������Լ������ԣ�����֮��������ǿ��Ҫ�������Ķ������������ļ�����ʽ���ֳ�����������ά������������������Ҫ�����ı���ԭ�����Լ���ʹ������ʲô��Ҫע���ĵط���

4������ԭ������


���� Protobuf ���ı����Ǻܽ��յģ������ȿ�һ�� message �Ľṹ��

��һ���򵥵����ӣ�
message Student {
string name = 1;
int32 age = 2; 
}

message ��һϵ�м�ֵ�ԣ�������֮��ʵ����ֻ�� tag ���кźͶ�Ӧ��ֵ����һ������������Ϥ�� json �ܲ�һ����

���Զ��� protobuf ��˵û�� .proto �ļ����޷��������ģ�
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_1.png

���� tag ��˵���������� message �ֶεı����Լ�������Ϣ�����ǿ�������ʵ�飬�� name ���� tag �������Ķ����ƴ�ӡ������

ʾ���������£�
funcmain() {
  student := student.Student{}
  student.Name = "t"
  marshal, _ := proto.Marshal(&student)
  fmt.Println(fmt.Sprintf("%08b", marshal)) // 00001010 00000001 01110100
}

��ӡ�����Ľ�����������
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_2.png

��ͼ�У����� name �� string ���ͣ����Ե�һ�� byte ��tag���ڶ� byte �� string �ij��ȣ������� byte ��ֵ��Ҳ���������������õ� ��t����

���������ȿ��� tag��
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_3.png

tag ������������������Ϣ���ֶ����ţ��ֶ����ͣ����㷽ʽ������ͼ�Ĺ�ʽ����ͼ�н� name �����ֶ����л��ɶ��������ǿ��Կ�������һ�� bit �DZ���λ����ʾ�Ƿ��ֶν�β��������0��ʾ tag �ѽ�β��tag ռ��1byte�������� 4 �� bit ��ʾ�����ֶ����ţ����Է�Χ 1 �� 15 �е��ֶα���ֻ��Ҫ 1 bit���б��룬���ǿ�������ʵ�鿴������ tag �ij�16������ͼ��ʾ����

IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_4.png

����ͼ��ʾ��ÿ�� byte ��һ��bit��ʾ�Ƿ�������0��ʾ�������������� tag ������ byte ��ʾ������ protobuf ��С�˱����ģ���Ҫת�ɴ��˷����Ķ����������ǿ���֪�� tag ȥ��ÿ�� byte ��һ�� bit ֮�󣬺���λ��ʾ���ͣ���3������λ�DZ��ű�ʾ 16��

���Դ�����������������Ҳ����֪�����ֶξ����ܾ���һЩ���ֶξ�����Ҫ���� 16 ���������Ϳ�����һ�� byte ��ʾ�ˡ�

ͬʱ����Ҳ����֪����protobuf ���л��Dz����ֶ����ģ����������ͻ��˵� proto �ļ�ֻ�޸����ֶ����������������ǰ�ȫ�ģ������˼����ø������б��Ż��ǽ�����ԭ�����ֶΡ�������Ҫע�����Dz�Ҫ�޸��ֶ����͡�

���������ǿ������ͣ�protobuf �������� 6 �����ͣ����������Ƿ����ģ�
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_5.png

�����������У�Name �� string ������������ tag ���ͽ������� 010 ��Ҳ���� 2��

5��Varints���뼼��ԭ��


���� protobuf ��˵��������������ѹ���ģ���ͨ������һ�� int32 ������Ҫ 4 byte���� protobuf ��ʾ127���ڵ�����ֻ��Ҫ 2 byte����Ϊ����һ����ͨ int32 �������֣��������ֺ�С����ôʵ������Чλ���٣�����Ҫ��ʾ 1 �������֣������ƿ�����������
0000000000000000 00000000 00000001

ǰ 3 ���ֽڶ��� 0 û�б�ʾ�κ���Ϣ��protobuf ���ǽ���Щ 0 ��ȥ���ˣ��� 1 byte ��ʾ 1 �������֣����� 1 byte ��ʾ tag �ı��ź����ͣ�����ռ���� 2byte��

�������Ƕ����� student ���� age ���� 150��
funcmain() {
  student := student.Student{}
  student.Age = 150
  marshal, _ := proto.Marshal(&student)
  fmt.Println(fmt.Sprintf("%08b", marshal)) //00010000 10010110 00000001
  fmt.Println(fmt.Sprintf("%08b", "a"))
}

������ӡ�����Ķ��������£���Ϊ 150 ���� 127��������Ҫ������ byte ��ʾ��
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_6.png

��һ�� byte �� tag �����Ͳ����ظ������ˡ��������� byte ����ʵ��ֵ��ÿ�� byte ������λ bit �DZ���λ����ʾ�Ƿ�������Ȼ������ת���ɴ��˱�ʾ�����������Ϳ��Եõ�����ֵ�� 150��

6��ZigZag ����


Varints ����֮���Կ�����������ռ�Ĵ洢�ֽ�������Ϊȥ���� 0 �����Ƕ��ڸ�����˵�Ͳ����ˣ���Ϊ�����ķ���λΪ 1�����Ҷ���32 λ���з���������ת���� 64 λ�޷�����������

���� -1���� Varints ����֮���Ķ����ƣ�
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_7.png

���� Varints ���븺���ܹ����㶨ռ�� 11 byte��tag һ��byte��ֵռ�� 10 byte��

Ϊ�� Google Protocol Buffer ������ sint32 �������ͣ����� zigzag ���롣����������ӳ�����޷���������Ȼ���ٲ��� varint ���뷽ʽ���롣

���磺
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_8.png

�������ı���Ҳ���ǽ� -1 ������ 1���� 1 ������ 2��ȫ������ӳ�䡣

ʵ�ʵ� Zigzag ӳ�亯��Ϊ��
(n << 1) ^ (n >> 31)  //for 32 bit 
(n << 1) ^ (n >> 63)  //for 64 bit

����ʹ����˵��ֻ�DZ��뷽ʽ���ˣ�ʹ���Dz���Ӱ�죬���Զ��������кܸ߱������������ݣ����Գ���ʹ�� sint ���ͣ���ʡһЩ�ռ䡣

7�� embedded messages & repeated


�������ڶ��������� proto��
message Lecture {
int32 price =1 ; 
}
message Student {
repeated int32 scores = 1;
Lecture lecture = 2;
}

�� scores ȡֵΪ [1,2,3]������֮��������ʵ�����潲�� string ���ͺ��񡣵�һ�� byte �� tag���ڶ� byte �� len������Ϊ 3���������� byte ����ֵ�������趨�� 1��2��3��

������ֻ������ scores �ı���ͼ��
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_9.png

�������� embedded messages ���ͣ��� Lecture �� price ����Ϊ150 ���ˡ�

������ֻ������ Lecture �ı���ͼ��
IMͨѶЭ��ר��ѧϰ(ʮһ)��IM�������������ܡ�����������Protobuf�Ͳȿ��ܽ�_10.png

��ʵ�ṹҲ�ܼ򵥣����ߵ��� Student ���ͣ��ұ��� Lecture ���͡��е㲻ͬ���Ƕ��� embedded messages �Ὣ��С����������

8�� �ȿ��ܽ�������ʵ��


8.1�ֶα���


��Ҫע�����Ƿ�Χ 1 �� 15 �е��ֶα�����Ҫһ���ֽڽ��б��룬�����ֶα��ź��ֶ����ͣ���Χ 16 �� 2047 �е��ֶα�����Ҫ�����ֽڡ�������Ӧ�ñ������� 1 �� 15 ��Ϊ�dz�Ƶ�����ֵ���ϢԪ�ء�

��Ϊ Protobuf �ĵ�һ�� byte �������ж��Ƿ���β�����Ե��ֽڱ�ʾ���кŵ�ʱ������λ���㣬��������λ��ʾ���ͣ�����ֻʣ�� 4 λ�����ˡ�Ҳ����˵���������ֶ��������� 16 ʱ������Ҫ���������ϵ��ֽڱ�ʾ�ˡ�

8.2�����ֶ�


һ���������£������Dz������׵�ɾ���ֶεģ���ֹ�ͻ��˺ͷ����˳���Э�鲻һ�µ�������������ͨ����ȫɾ��ij���ֶλ�����ע�͵���������Ϣ���ͣ���ôδ���������˲�֪������ tag ���ֶα�ɾ�����ˣ����ǿ���ʹ�� reserved �����DZ�ɾ�����ֶΡ�

�磺
message Foo {
reserved2, 15, 9 to 11;
reserved"foo", "bar";
}

��Ȼ���� message ��reservedҲ������ö��������ʹ�á�

8.3��Ҫ�޸��ֶ� tag �����Լ��ֶ�����


Protobuf ���л��Dz����ֶ����ģ����������ͻ��˵� proto �ļ�ֻ�޸����ֶ����������������ǰ�ȫ�ģ������˼����ø������б��Ż��ǽ�����ԭ�����ֶΡ�

������Ҫע�����Dz�Ҫ�޸��ֶ����ͣ��Լ����б��ţ��޸���֮���Ϳ��ܰ��ձ����Ҵ����͡�

8.4��Ҫʹ�� required �ؼ���


required ��ζ����Ϣ�б������������ֶΣ������ֶε�ֵ���뱻���á����������л����߷����л��Ĺ����У����ֶ�û�б����ã���ô protobuf ���ͻ��׳�һ��������

�������ڳ��ڶ�����һ�� required �ֶΣ������ں����İ汾������Ҫɾ��������ô���ͻ��������⣬��Ϊ�ɵĴ������ڴ����ֶ�ʼ�մ��ڡ�Ϊ��ȷ�������ԣ�Google �����°汾�� Protobuf��protobuf 3�����Ѿ�����֧�� required ���η���

8.5����ʹ��С����


Varints ������ʾ127���ڵ�����ֻ��Ҫ 2 byte��1 byte �� tag��1 byte ��ֵ��ѹ��Ч���ܺá�����������ʾһ���ܴ������� ��1<<31 - 1����ȥ tag ����Ҫռ�� 5 byte������ͨ�� int 32 ��1 byte����Ϊ protobuf ÿ��byte����λ��һ����ʶ��ռ�� 1 bit��

8.6��Ҫ���为�������� sint32 �� sint64


��Ϊ�����ķ���λΪ 1������ Varints �������ڸ���������32λ���з���������ת����64λ�޷��������������� Varints ���븺���ܹ����㶨ռ��11 byte��tag һ�� byte��ֵռ�� 10 byte��

�� sint32 �� sint64 ����������ӳ�����޷���������Ȼ���ٲ��� varint ���뷽ʽ���룬�������ֱȽϻ��ǿ��Խ�ʡһ���Ŀռ��ġ��������¶����а�������ӭת������~

9�� �����


[1] һ������Protocol Buffer��Java������ʾ
[2] ����ѡ����ʱͨѶӦ�õ����ݴ�����ʽ
[3] ǿ�н��齫Protobuf��Ϊ���ļ�ʱͨѶӦ�����ݴ�����ʽ
[4] �ƶ���IM������Ҫ���Եļ������⣨��ͨ��Э��ѡ����
[5] �����ƶ���IM��������Щ�ӣ��ܹ����ơ�ͨ��Э���Ϳͻ���
[6] ������ϵʵ�ʣ�һ�׵��͵�IMͨ��Э����������
[7] 58����ʵʱ��Ϣϵͳ��Э�����Ƶȼ���ʵ������
[8] ����ɨä����һ������UDP�ĵ���ʱ���紫����Э�顪��QUIC����
[9] APP����̨ͨ�����ݸ�ʽ���ݽ������ı�Э�鵽������Э��
[10] һ���ڼ��û���IM�ܹ������ɻ�(��ƪ)�������ܹ����������ֵ�
[11] һ���ڼ��û���IM�ܹ������ɻ�(��ƪ)���ɿ��ԡ������ԡ������Ż���
[12] �����ֵ�ר�ң���������һ���ڼ���Ϣ���ķֲ�ʽIMϵͳ
[13] ΢���Ŷӷ�����������΢��ʮ��ǰ��IM��Ϣ�շ��ܹ�������������
[14] ֧�ְ����˳���Ⱥ�ĵ�Web��IM�ܹ�������ʵ��
[15] һ��ߣ�������д��룬��ҵ΢�ŵ�ȫ�º���NEXT�ͻ��˼ܹ��ݽ�֮·

��ʱͨѶ�� - ��ʱͨѶ������������ ��Դ�� - ��ʱͨѶ������������

��һƪ��Web��ʵʱͨ�ż���SSE��Я�̻�Ʊҵ���е�ʵ��Ӧ����һƪ������������Ƶ���ţ�������֪����Webǰ������Ƶ֪ʶ

��������¼�����¼���ר��

�Ƽ�����
����¥�� ×
ʹ��΢�Ŵ��ͣ� ʹ��֧�������ͣ�

���ض���