feat(backend): reader maintains multibyte type
This commit is contained in:
parent
d0563d3fcb
commit
fc499aabd1
2 changed files with 14 additions and 2 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -50,6 +51,15 @@ func WriteUint(v uint64, buf []byte, p int) int {
|
||||||
return p + 1
|
return p + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ByteSizeUint(v uint64) int {
|
||||||
|
if v == 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
nBits := math.Floor(math.Log2(float64(v))) + 1
|
||||||
|
nBytes := math.Ceil(nBits / 7)
|
||||||
|
return int(nBytes)
|
||||||
|
}
|
||||||
|
|
||||||
func ReadInt(reader io.Reader) (int64, error) {
|
func ReadInt(reader io.Reader) (int64, error) {
|
||||||
ux, err := ReadUint(reader)
|
ux, err := ReadUint(reader)
|
||||||
x := int64(ux >> 1)
|
x := int64(ux >> 1)
|
||||||
|
|
|
||||||
|
|
@ -70,13 +70,15 @@ func (m *messageReaderImpl) Parse() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dirty hack to avoid extra memory allocation
|
// Dirty hack to avoid extra memory allocation
|
||||||
m.data[curr-1] = uint8(m.msgType)
|
mTypeByteSize := ByteSizeUint(m.msgType)
|
||||||
|
from := int(curr) - mTypeByteSize
|
||||||
|
WriteUint(m.msgType, m.data, from)
|
||||||
|
|
||||||
// Add message meta to list
|
// Add message meta to list
|
||||||
m.list = append(m.list, &MessageMeta{
|
m.list = append(m.list, &MessageMeta{
|
||||||
msgType: m.msgType,
|
msgType: m.msgType,
|
||||||
msgSize: m.msgSize + 1,
|
msgSize: m.msgSize + 1,
|
||||||
msgFrom: uint64(curr - 1),
|
msgFrom: uint64(from),
|
||||||
})
|
})
|
||||||
|
|
||||||
// Update data pointer
|
// Update data pointer
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue