@@ -2,19 +2,26 @@ package client
22
33import (
44 "fmt"
5+ "math"
56)
67
7- type collationbody []byte
8-
98var (
10- collationsizelimit = int64 (2 ^ 20 )
9+ collationsizelimit = int64 (math . Pow ( float64 ( 2 ), float64 ( 20 )) )
1110 chunkSize = int64 (32 )
1211 indicatorSize = int64 (1 )
1312 numberOfChunks = collationsizelimit / chunkSize
1413 chunkDataSize = chunkSize - indicatorSize
1514 totalDatasize = numberOfChunks * chunkDataSize
1615)
1716
17+ type collationbody []byte
18+
19+ type body interface {
20+ length () int64
21+ validateblob () error
22+ ParseBlob ()
23+ }
24+
1825func (cb collationbody ) length () int64 {
1926
2027 return int64 (len (cb ))
@@ -39,31 +46,48 @@ func (cb collationbody) validateBody() error {
3946
4047// Parse Collation body and modify it accordingly
4148
42- func (cb collationbody ) ParseBlob () {
49+ func (cb collationbody ) serializeBlob () [] byte {
4350
4451 terminalLength := cb .length () % chunkDataSize
4552 chunksNumber := cb .length () / chunkDataSize
4653 indicatorByte := make ([]byte , 1 )
4754 indicatorByte [0 ] = 0
48- var tempbody collationbody
55+ var tempbody []byte
56+
57+ // if blob is less than 31 bytes, it adds the indicator chunk and pads the remaining empty bytes to the right
58+
59+ if chunksNumber == 0 {
60+ paddedbytes := make ([]byte , cb .length ()- terminalLength )
61+ indicatorByte [0 ] = byte (terminalLength )
62+ tempbody = append (indicatorByte , append (cb , paddedbytes ... )... )
63+ return tempbody
64+ }
65+
66+ //if there is no need to pad empty bytes, then the indicator byte is added as 00011111
67+
68+ if terminalLength == 0 {
69+
70+ for i := int64 (1 ); i < chunksNumber ; i ++ {
71+ tempbody = append (tempbody , append (indicatorByte , cb [(i - 1 )* chunkDataSize :i * chunkDataSize ]... )... )
72+
73+ }
74+ indicatorByte [0 ] = byte (chunkDataSize )
75+ tempbody = append (tempbody , append (indicatorByte , cb [(chunksNumber - 1 )* chunkDataSize :chunksNumber * chunkDataSize ]... )... )
76+ return tempbody
77+
78+ }
4979
5080 // Appends empty indicator bytes to non terminal-chunks
5181 for i := int64 (1 ); i <= chunksNumber ; i ++ {
5282 tempbody = append (tempbody , append (indicatorByte , cb [(i - 1 )* chunkDataSize :i * chunkDataSize ]... )... )
5383
5484 }
5585 // Appends indicator bytes to terminal-chunks , and if the index of the chunk delimiter is non-zero adds it to the chunk
56- if terminalLength != 0 {
57- indicatorByte [0 ] = byte (terminalLength )
58- tempbody = append (tempbody , append (indicatorByte , cb [chunksNumber * chunkDataSize :(chunksNumber * chunkDataSize )+ (terminalLength + 1 )]... )... )
86+ indicatorByte [0 ] = byte (terminalLength )
87+ tempbody = append (tempbody , append (indicatorByte , cb [chunksNumber * chunkDataSize :(chunksNumber * chunkDataSize )+ (terminalLength + 1 )]... )... )
88+ emptyBytes := make ([]byte , (chunkDataSize - cb .length ()))
89+ cb = append (cb , emptyBytes ... )
5990
60- }
61- cb = tempbody
91+ return tempbody
6292
63- // Pad the collation body with empty bytes until it is equal to 1 Mib
64- if cb .length () < collationsizelimit {
65- emptyBytes := make ([]byte , (collationsizelimit - cb .length ()))
66- cb = append (cb , emptyBytes ... )
67-
68- }
6993}
0 commit comments