From c01612f83c03c8cf4ca6a67ba72251428fa1628a Mon Sep 17 00:00:00 2001 From: nisdas Date: Wed, 2 May 2018 00:36:16 +0800 Subject: [PATCH] sharding/client: Change to serialize blobs(#92) --- sharding/client/utils.go | 56 ++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/sharding/client/utils.go b/sharding/client/utils.go index c7972f619703..0950dbdb4a22 100644 --- a/sharding/client/utils.go +++ b/sharding/client/utils.go @@ -2,12 +2,11 @@ package client import ( "fmt" + "math" ) -type collationbody []byte - var ( - collationsizelimit = int64(2 ^ 20) + collationsizelimit = int64(math.Pow(float64(2), float64(20))) chunkSize = int64(32) indicatorSize = int64(1) numberOfChunks = collationsizelimit / chunkSize @@ -15,6 +14,14 @@ var ( totalDatasize = numberOfChunks * chunkDataSize ) +type collationbody []byte + +type body interface { + length() int64 + validateblob() error + ParseBlob() +} + func (cb collationbody) length() int64 { return int64(len(cb)) @@ -39,13 +46,36 @@ func (cb collationbody) validateBody() error { // Parse Collation body and modify it accordingly -func (cb collationbody) ParseBlob() { +func (cb collationbody) serializeBlob() []byte { terminalLength := cb.length() % chunkDataSize chunksNumber := cb.length() / chunkDataSize indicatorByte := make([]byte, 1) indicatorByte[0] = 0 - var tempbody collationbody + var tempbody []byte + + // if blob is less than 31 bytes, it adds the indicator chunk and pads the remaining empty bytes to the right + + if chunksNumber == 0 { + paddedbytes := make([]byte, cb.length()-terminalLength) + indicatorByte[0] = byte(terminalLength) + tempbody = append(indicatorByte, append(cb, paddedbytes...)...) + return tempbody + } + + //if there is no need to pad empty bytes, then the indicator byte is added as 00011111 + + if terminalLength == 0 { + + for i := int64(1); i < chunksNumber; i++ { + tempbody = append(tempbody, append(indicatorByte, cb[(i-1)*chunkDataSize:i*chunkDataSize]...)...) + + } + indicatorByte[0] = byte(chunkDataSize) + tempbody = append(tempbody, append(indicatorByte, cb[(chunksNumber-1)*chunkDataSize:chunksNumber*chunkDataSize]...)...) + return tempbody + + } // Appends empty indicator bytes to non terminal-chunks for i := int64(1); i <= chunksNumber; i++ { @@ -53,17 +83,11 @@ func (cb collationbody) ParseBlob() { } // Appends indicator bytes to terminal-chunks , and if the index of the chunk delimiter is non-zero adds it to the chunk - if terminalLength != 0 { - indicatorByte[0] = byte(terminalLength) - tempbody = append(tempbody, append(indicatorByte, cb[chunksNumber*chunkDataSize:(chunksNumber*chunkDataSize)+(terminalLength+1)]...)...) + indicatorByte[0] = byte(terminalLength) + tempbody = append(tempbody, append(indicatorByte, cb[chunksNumber*chunkDataSize:(chunksNumber*chunkDataSize)+(terminalLength+1)]...)...) + emptyBytes := make([]byte, (chunkDataSize - cb.length())) + cb = append(cb, emptyBytes...) - } - cb = tempbody + return tempbody - // Pad the collation body with empty bytes until it is equal to 1 Mib - if cb.length() < collationsizelimit { - emptyBytes := make([]byte, (collationsizelimit - cb.length())) - cb = append(cb, emptyBytes...) - - } }