@@ -2,19 +2,26 @@ package client
2
2
3
3
import (
4
4
"fmt"
5
+ "math"
5
6
)
6
7
7
- type collationbody []byte
8
-
9
8
var (
10
- collationsizelimit = int64 (2 ^ 20 )
9
+ collationsizelimit = int64 (math . Pow ( float64 ( 2 ), float64 ( 20 )) )
11
10
chunkSize = int64 (32 )
12
11
indicatorSize = int64 (1 )
13
12
numberOfChunks = collationsizelimit / chunkSize
14
13
chunkDataSize = chunkSize - indicatorSize
15
14
totalDatasize = numberOfChunks * chunkDataSize
16
15
)
17
16
17
+ type collationbody []byte
18
+
19
+ type body interface {
20
+ length () int64
21
+ validateblob () error
22
+ ParseBlob ()
23
+ }
24
+
18
25
func (cb collationbody ) length () int64 {
19
26
20
27
return int64 (len (cb ))
@@ -39,31 +46,48 @@ func (cb collationbody) validateBody() error {
39
46
40
47
// Parse Collation body and modify it accordingly
41
48
42
- func (cb collationbody ) ParseBlob () {
49
+ func (cb collationbody ) serializeBlob () [] byte {
43
50
44
51
terminalLength := cb .length () % chunkDataSize
45
52
chunksNumber := cb .length () / chunkDataSize
46
53
indicatorByte := make ([]byte , 1 )
47
54
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
+ }
49
79
50
80
// Appends empty indicator bytes to non terminal-chunks
51
81
for i := int64 (1 ); i <= chunksNumber ; i ++ {
52
82
tempbody = append (tempbody , append (indicatorByte , cb [(i - 1 )* chunkDataSize :i * chunkDataSize ]... )... )
53
83
54
84
}
55
85
// 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 ... )
59
90
60
- }
61
- cb = tempbody
91
+ return tempbody
62
92
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
- }
69
93
}
0 commit comments