ã¨ã³ã³ã¼ãã£ã³ã° â Protocol Buffers
Ruby版ä½ãããã«é¨åçã«è¨³ãã¦ãã®ã§ããã£ããã ããå®æããã¾ãããProtocol Buffersã®ãã¤ããªã¨ã³ã³ã¼ãã£ã³ã°è©³ç´°ã§ãããã®æ å ±ãå¿ è¦ãªäººã¯ããã¾ãããªãã¨ãããã¾ããããããããã°ã©ããã
http://code.google.com/apis/protocolbuffers/docs/encoding.html
ãã®ããã¥ã¡ã³ãã¯ãããã³ã«ãããã¡ã¡ãã»ã¼ã¸ã®ãã¤ããªã»ã¯ã¤ã¤å½¢å¼ã«ã¤ãã¦èª¬æãã¦ãã¾ããã¢ããªã±ã¼ã·ã§ã³ã§ãããã³ã«ãããã¡ã使ç¨ããã ãã§ããã°æ°ã«ããå¿ è¦ã¯ããã¾ãããããããã³ã«ãããã¡ã®æ§ã ãªãã©ã¼ããããã¨ã³ã³ã¼ããããã¡ãã»ã¼ã¸ã®ãµã¤ãºã«ã©ãå½±é¿ããããç解ãããã¨ã¯é常ã«å½¹ã«ç«ã¤ã§ãããã
ç°¡åãªã¡ãã»ã¼ã¸
次ã®ã¨ã¦ãç°¡åãªã¡ãã»ã¼ã¸å®ç¾©ãããã¨ãã¾ããã:
message Test1 { required int32 a = 1; }
ã¢ããªã±ã¼ã·ã§ã³ã§Test1ã¡ãã»ã¼ã¸ãä½æããaã«150ãè¨å®ãã¾ããããããã¡ãã»ã¼ã¸ãã·ãªã¢ã©ã¤ãºãã¦åºåã¹ããªã¼ã ã«æµãã¾ããã¨ã³ã³ã¼ããããã¡ãã»ã¼ã¸ãè¦ãã¨ããã¨ã次ã®3ãã¤ããç®ã«å ¥ãã§ããã:
08 96 01
ã¨ãããããã¨ã¦ãå°ããã¦æ°åã ããªã®ã¯åããã¾ãã â ã§ããã®æå³ã¯ï¼ããã¯ãã®å ãã¿ã¦ãã ãã...
Base 128 å¯å¤é·æ´æ°
åç´ãªãããã³ã«ãããã¡ã¨ã³ã³ã¼ãã£ã³ã°ãç解ããã«ã¯ãã¾ãæåã«å¯å¤é·æ´æ°ãç解ããå¿ è¦ãããã¾ããå¯å¤é·æ´æ°ã¯1以ä¸ã®ãã¤ãã使ã£ã¦æ´æ°ãã·ãªã¢ã©ã¤ãºããæ¹æ³ã§ãå°ããªæ°ã»ã©å°ãªããã¤ãæ°ã§æ¸ã¿ã¾ãã
æçµãã¤ããé¤ããå¯å¤é·æ´æ°ã«å«ã¾ãããã¤ãããããã«ã¯æä¸ä½ããã(most significant bit: msb)ãã»ãããããã¾ã ç¶ãã®ãã¤ãããããã¨ãåããã¾ããããããã®ãã¤ãã®ä¸ä½7ãããã¯are used to store the two's complement representation of the number in groups of 7 bits, least significant group first.
ã§ã¯ãããã§ã¯ä¾ã¨ãã¦æ°å¤ã®1ãèãã¾ããããã¯ä¸ãã¤ããªã®ã§æä¸ä½ãããã¯ã»ããããã¾ããã
0000 0001
ããã«130ãè¦ã¦ã¿ã¾ããããã¯ããå°ãè¤éã§ãã
1000 0010 0000 0001
ããã130ã§ããã¨ã©ããã¦åããã®ã§ããããï¼ã¾ãããããã®ãã¤ãã®æä¸ä½ãããã¯åã«æ°å¤ãæå¾ã¾ã§å°éãããã©ããã示ãã ããªã®ã§é¤ãã¾ã(è¦ã¦åããããã«ãå¯å¤é·å¤æ°ãããä¸ãã¤ãç¶ãã¦ããã®ã§æåã®ãã¤ããã»ããããã¦ãã¾ã):
1000 0010 0000 0001 â 000 0010 000 0001
次ã«ãå ã«æ¸ããã¨ãããå¯å¤é·æ´æ°ã¯æ°å¤ãæä¸ä½ããããå ã«ä¿åããã®ã§7ããããä¸ã°ã«ã¼ãã¨ãã¦é çªãå ¥ãæ¿ãã¾ãããã®å¾ãããããçµåãã¦æçµçãªå¤ãå¾ã¾ã:
000 0010 000 0001 â 000 0001 ++ 000 0010 â 10000010 â 128 + 2 = 130
ã¡ãã»ã¼ã¸æ§é
ãåç¥ã®ã¨ããããããã³ã«ãããã¡ã¡ãã»ã¼ã¸ã¯ä¸é£ã®ãã¼-å¤ãã¢ã§ãããã¤ããªãã¼ã¸ã§ã³ã®ã¡ãã»ã¼ã¸ã¯ãã¼ã¨ãã¦åã«ãã£ã¼ã«ãã®çªå·ã使ãã¾ããããããã®ååã¨å®£è¨ãããåã¯ã¡ãã»ã¼ã¸åå®ç¾©(ã¤ã¾ã.protoãã¡ã¤ã«)ãåç §ãã¦ã§ã³ã¼ãã®æå¾ã«æ±ºå®ããã¾ãã
ã¡ãã»ã¼ã¸ãã¨ã³ã³ã¼ããããã¨ãã¼ã¨å¤ã¯ä¸ã¤ã®ãã¤ãåã«ã¾ã¨ãããã¾ããã¡ãã»ã¼ã¸ããã³ã¼ããããã¨ãã¯ãã¼ãµã¯ç解ã§ããªããã£ã¼ã«ããã¹ãããããªããã°ããã¾ãããããããã°ãã¡ãã»ã¼ã¸ã«æ°ãããã£ã¼ã«ããä»ãå ããããã¨ãã¦ãããã®ãã¨ãç¥ããªãå¤ãããã°ã©ã ã妨ããã«æ¸ã¿ã¾ãããã®ããã«ãç©çãã©ã¼ãããã§ã®ã¡ãã»ã¼ã¸ããããã®ãã¢ã«ããã"ãã¼"ã¯æ¬¡ã®äºã¤ã®å¤ãããªãã¾ã: .protoãã¡ã¤ã«ã«æ¸ããã¦ãããã£ã¼ã«ãçªå·ã¨ãç¶ãå¤ã®é·ããå¾ãã®ã«å¿ è¦ãªæ å ±ã ããå«ãã¯ã¤ã¤ã¿ã¤ã
使ç¨å¯è½ãªã¯ã¤ã¤ã¿ã¤ãã¯ä»¥ä¸ã®éãã§ã:
ã¿ã¤ã | æå³ | 対象 |
---|---|---|
0 | å¯å¤é·æ´æ° | int32, int64, int32, int64, uint32, uint64, sint32, sint64, bool, enum |
1 | 64ããã | fixed64, sfixed64, double |
2 | length-delimited | string, bytes, embedded messages |
3 | ã°ã«ã¼ãéå§ | groups (deprecated) |
4 | ã°ã«ã¼ãçµäº | groups (deprecated) |
5 | 32ããã | fixed32, sfixed32, float |
ã¹ããªã¼ã ãããã¡ãã»ã¼ã¸ã®ãã¼ã¯ (field_number << 3) | wire_type ã¨ããå¤ãæã¤å¯å¤é·æ´æ°ã§ããã¤ã¾ãããã®æ°ã®æå¾ã®3ãããã«ã¯ã¤ã¤ã¿ã¤ããåãããã¦ãã¾ãã
ããä¸åº¦ç°¡åãªä¾ãè¦ã¦ã¿ã¾ããããã¹ããªã¼ã ã®æåã®æ°ã常ã«å¯å¤é·æ´æ°ã®ãã¼ã§ãããã¨ã¯ãåããã ã¨æãã¾ããããã§ã¯ãã®æ°ã¯08ãã¤ã¾ã(æä¸ä½ãé¤ãã¨)次ã®ããã«ãªãã¾ã:
000 1000
æå¾ã®3ãããããã¯ã¤ã¤ã¿ã¤ã(0)ãåãåºãã¦ãå³ã«3ãããã·ããããã¨ãã£ã¼ã«ãçªå·(1)ãå¾ããã¾ããããã§ã¿ã°ã1ã§ç¶ãå¤ã¯å¯å¤é·æ´æ°ã§ãããã¨ãåããã¾ãããå ã®ã»ã¯ã·ã§ã³ã§ç解ããå¯å¤é·å¼æ°ã®ãã³ã¼ãæ¹æ³ã使ãã°ã次ã®ãã¤ãã«æ ¼ç´ããã¦ããå¤ã150ã ã¨åããã¾ãã
96 01 = 1001 0110 0000 0001 â 000 0001 ++ 001 0110 (æä¸ä½ããããé¤ãããã¨7ããããã¨ã«ä¸¦ã³ãéã«ãã) â 10010110 â 2 + 4 + 16 + 128 = 150
å¤ã®åã«ã¤ãã¦ããã«
符å·ä»æ´æ°
ããã¾ã§ã®ã»ã¯ã·ã§ã³ããåããããã«ãã¯ã¤ã¤ã¿ã¤ã0ã«é¢é£ä»ãããã¦ãããããã³ã«ãããã¡ã¼ã¿ã¤ãã¯å ¨ã¦å¯å¤é·å¼æ°ã«ã¨ã³ã³ã¼ãããã¾ããããã符å·ä»æ´æ°å(sint32ã¨sint64)ã¨"æ¨æºçãª"æ´æ°å(int32ã¨int64)ã«ã¯è² ã®å¤ãã¨ã³ã³ã¼ãããéãéè¦ãªéããããã¾ããããè² æ°ã«int32ãint64ã使ã£ããããããã¯å·¨å¤§ãªç¬¦å·ç¡ãæ´æ°ãå¹ççã«æ±ãããã®ãã®ãªã®ã§ãçµæã®å¯å¤é·æ´æ°ã¯å¸¸ã«10ãã¤ãé·ããªãã¾ãã符å·ä»ã®åã使ãã°ãçµæã®å¯å¤é·æ´æ°ã«ã¯ãã£ã¨ãã£ã¨å¹ççãªZigZagã¨ã³ã³ã¼ãã£ã³ã°ã使ç¨ããã¾ãã
ZigZagã¨ã³ã³ã¼ãã£ã³ã°ã¯ç¬¦å·ä»æ´æ°ã符å·ç¡ãã®æ´æ°ã«ããããã¦ã絶対å¤ãå°ããªæ°(ä¾ãã°-1)ã¯å°ããªå¯å¤é·å¼æ°ã«ã¨ã³ã³ã¼ãããã¾ããæ£æ°ã¨è² æ°ã"zig-zag"ã«è¡ã£ããæ¥ãããã¦ã次ã®è¡¨ã®ããã«-1ã¯1ã«ã¨ã³ã³ã¼ãããã1ã¯2ã«ã-2ã¯3ã«ãã¨ç¶ãã¾ãã
符å·ä»ãªãªã¸ãã« | ã¨ã³ã³ã¼ããããå¤ |
---|---|
0 | 0 |
-1 | 1 |
1 | 2 |
-2 | 3 |
2147483647 | 4294967294 |
-2147483648 | 4294967295 |
ã¤ã¾ãsint32ã§ã¯nãã¨ã³ã³ã¼ãããã®ã«
(n << 1) ^ (n >> 31)
ã使ç¨ããã64ãããçã§ã¯
(n << 1) ^ (n >> 63)
ã使ç¨ããã¾ãã
å¾åã®ã·ããé¨å â(n >> 31)ã®é¨å â ã¯ç®è¡ã·ããã§ãããã¨ã«æ³¨æãã¦ãã ãããã¤ã¾ããã·ããã®çµæã¯(nãæ£ã®ã¨ãã¯)å ¨ã¦0ã¾ãã¯(nãè² ã®ã¨ãã¯)å ¨ã¦1ã«ãªãã¾ãã
sint32ãsint64ããã¼ã¹ãããã¨ãå¤ã¯å ã®ç¬¦å·ä»ã®å¤ã«ãã³ã¼ãããã¾ãã
å¯å¤é·æ´æ°ä»¥å¤ã®æ°
å¯å¤é·æ´æ°ä»¥å¤ã®æ°å¤åã¯ç°¡åã§ããã¯ã¤ã¤ã¿ã¤ã1ã®doubleã¨floatã§ããã°ãã¼ãµã¯ãã¼ã¿é·ã64ãããåºå®ã ã¨æå¾ ã§ããã¯ã¤ã¤ã¿ã¤ã5ã®floatã¨fixed32ã®ã¨ããåæ§ã«32ãããã ã¨æå¾ ã§ãã¾ãããããã®å ´åãå¤ã¯ãªãã«ã¨ã³ãã£ã¢ã³ãã¤ããªã¼ãã¼ã§æ ¼ç´ããã¾ãã
æåå
ã¯ã¤ã¤ã¿ã¤ã2(length-delimited)ã¯ãå¤é¨ãå¯å¤é·æ´æ°ã®ãã¼ã¿é·ã®å¾ã«ãã®é·ãã®ãã¼ã¿ãã¤ããç¶ãã¨è¨ãæå³ã§ãã
message Test2 { required string b = 2; }
bã®å¤ã"testing"ã«ããã¨æ¬¡ã®ããã«ãªãã¾ã:
12 07 <span style="color:#ff0000">74 65 73 74 69 6e 67</span>
赤ãé¨åãUTF8ã®"testing"ã§ããããã§ã¯ãã¼ã¯0x12âtag=2, type=2ã«ãªãã¾ããå¤ã®ä¸ã«ããé·ãã示ãå¯å¤é·æ´æ°ã¯7ã§ãlo and beholdã7ãã¤ãã®æååãå¾ã«ç¶ããã¨ãåããã¾ãã
çµã¿è¾¼ã¿ã¡ãã»ã¼ã¸
ããã¯ä¾ã«åºã¦ããåTest1ãçµã¿è¾¼ã¾ããã¡ãã»ã¼ã¸ã®å®ç¾©ã§ã:
message Test3 { required Test1 c = 3; }
ã¨ã³ã³ã¼ããããå½¢ã¯ä»¥ä¸ã§ã:
1a 03 <span style="color:#FF0000;">08 96 01</span>
å¾åã®3ãã¤ãã¯æåã®ä¾ã¨å ¨ãåã(08 96 01)ã§ãããã¨ãè¦ã¦ã¨ããã¨æãã¾ããããã«ãã®åã«ã¯æ°å¤ã®3ãããã¾ã â çµã¿è¾¼ã¿ã¡ãã»ã¼ã¸ã¯æåå(wire type=2)ã¨å ¨ãåãããã«å¦çããã¾ãã
ãªãã·ã§ãã«è¦ç´ ã¨ç¹°è¿ãè¦ç´
ã¡ãã»ã¼ã¸å®ç¾©ã«ç¹°è¿ãè¦ç´ ããããªããã¨ã³ã³ã¼ããããã¡ãã»ã¼ã¸ã«ã¯åãã¿ã°çªå·ãããªãä¸ã¤ä»¥ä¸ã®ãã¼:å¤ãã¢ãå«ã¾ãã¾ããç¹°è¿ãã®å¤ã¯é£ç¶ãã¦ããå¿ è¦ã¯ããã¾ãããä»ã®ãã£ã¼ã«ããéã«å ¥ããã¨ããããã¾ããç¹°ãè¿ãããè¦ç´ ã®é çªã¯ãã¼ã¹æã¨åãã«ãªãã¾ãããä»ã®ãã£ã¼ã«ãã¨ã®é çªã¯å¤±ããã¾ãã
è¦ç´ ããªãã·ã§ãã«ãªããã¨ã³ã³ã¼ããããã¡ãã»ã¼ã¸ã«ã¯ãã®ã¿ã°çªå·ãæã¤ãã¼:å¤ãã¢ããããããããªããããªãããããã¾ããã
é常ã§ããã°ãã¨ã³ã³ã¼ããããã¡ãã»ã¼ã¸ã«ã¯ãªãã·ã§ãã«ã ã£ããç¹°ãè¿ãããããããã£ã¼ã«ãã®ã¤ã³ã¹ã¿ã³ã¹ãä¸ã¤ä»¥ä¸å«ã¾ãããã¨ã¯ããã¾ãããããã©ãããã¼ãµã¯ãããã£ãå ´åã§ãæ£ããå¦çã§ãããã¨ãæ±ãããã¾ããæ°å¤åã¨æåååã¯ãåãå¤ããªãã©ãç¾ããå ´åããã¼ãµã¯æå¾ã®å¤ã ããæå¹ã§ããã¨è¦ãªãã¾ããçµã¿è¾¼ã¿ã¡ãã»ã¼ã¸ãã£ã¼ã«ãã®å ´åããã¼ãµã¯Message::MergeFromã¡ã½ãããå¼ã°ããã®ã¨åãããã«ãè¤æ°ã®ã¤ã³ã¹ã¿ã³ã¹ãåããã£ã¼ã«ãã«ãã¼ã¸ãã¾ããã¤ã¾ããå¾è ã®ã¤ã³ã¹ã¿ã³ã¹ã«ããåä½ã®ã¹ã«ã©ãã£ã¼ã«ãã¯åè ã®ãã®ã§ç½®ãæããããçµã¿è¾¼ã¿ã¡ãã»ã¼ã¸ã¯ãã¼ã¸ãããç¹°è¿ããã£ã¼ã«ãã¯çµåããã¾ãããã®ã«ã¼ã«ã®çµæãã¨ã³ã³ã¼ããããäºã¤ã®ã¡ãã»ã¼ã¸ã®çµåã¯ãäºã¤ã®ã¡ãã»ã¼ã¸ãå¥ã ã«ãã¼ã¹ãã¦çµæã®ãªãã¸ã§ã¯ãããã¼ã¸ããã®ã¨å ¨ãåãã«ãªãã¾ããã¤ã¾ãã以ä¸ã¯:
MyMessage message; message.ParseFromString(str1 + str2);
次ã¨å ¨ãåãã§ã:
MyMessage message, message2; message.ParseFromString(str1); message2.ParseFromString(str2); message.MergeFrom(message2);
ãã®æ§è³ªã¯æã«ããæçã§ãã¿ã¤ããç¥ããªãäºã¤ã®ã¡ãã»ã¼ã¸ããã¼ã¸ãããã¨ãã§ãã¾ãã
ãã£ã¼ã«ãã®é åº
.protoå ã§ãã£ã¼ã«ãçªå·ãã©ããªé åºã«ã¤ãããã¨ãã¡ãã»ã¼ã¸ãã·ãªã¢ã©ã¤ãºãããã¨ãæ¢ç¥ã®ãã£ã¼ã«ãã¯çæãããC++, Java, Pythonã³ã¼ãã®ä¸ã§ãã£ã¼ã«ãçªå·ã«æ²¿ã£ãé åºã«ãªã£ã¦ããªããã°ããã¾ããããããããã¨ã§ã³ã¼ãããã¼ã¹ããæã«ãã£ã¼ã«ãçªå·ãé ã«ä¸¦ãã§ãããã¨ã«ä¾åããæé©åãè¡ããã¨ãã§ãã¾ãããã ãããããã³ã«ãããã¡ã¼ã®ãã¼ãµã¼ã¯ãã£ã¼ã«ããã©ããªé åºã§ãã£ã¦ããã¼ã¹ã§ããªããã°ããã¾ãããå ¨ã¦ã®ã¡ãã»ã¼ã¸ãåç´ã«ä¸ã¤ã®ãªãã¸ã§ã¯ããã·ãªã¢ã©ã¤ãºãããã®ãããªãããã§ã¯ãªããä¾ãã°ãäºã¤ã®ã¡ãã»ã¼ã¸ãåç´ã«çµåãã¦ãã¼ã¸ãããã¨ãæã¨ãã¦æç¨ã ããã§ãã
ã¡ãã»ã¼ã¸ã«ä¸æãªãã£ã¼ã«ããåå¨ããã¨ãã¯ãç¾å¨ã®ã¨ããJavaã¨C++ã®å®è£ ã§ã¯ããããé åºè¯ã並ãã æ¢ç¥ã®ãã£ã¼ã«ãã®å¾ã«ä»»æã®é çªã§æ¸ãè¾¼ã¿ã¾ããä»ã®Pythonã®å®è£ ã§ã¯ä¸æãªãã£ã¼ã«ãã«ã¤ãã¦ã¯ä½ãè¡ãã¾ããã