wp
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package enc
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
|
||||
@@ -18,32 +19,33 @@ type InstantDeltaCompressor struct {
|
||||
state *InstantDeltaCapturedState
|
||||
}
|
||||
|
||||
func NewInstantDeltaCompressor(buf []byte, payloadSize int, fracDigits byte) *InstantDeltaCompressor {
|
||||
func NewInstantDeltaCompressor(buf []byte, fracDigits byte) *InstantDeltaCompressor {
|
||||
var coef float64 = 1
|
||||
if fracDigits > 0 {
|
||||
coef = math.Pow(10, float64(fracDigits))
|
||||
}
|
||||
s := &InstantDeltaCompressor{
|
||||
buf: buf,
|
||||
pos: payloadSize,
|
||||
coef: coef,
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *InstantDeltaCompressor) RestoreState(payloadSize int) {
|
||||
if payloadSize > 0 {
|
||||
u64, _, err := bin.GetVarInt64(buf)
|
||||
s.pos = payloadSize - 1 // завжди показує на h
|
||||
// base value на початку
|
||||
u64, _, err := bin.GetVarUint64(s.buf)
|
||||
if err != nil {
|
||||
log.Fatalf("bug: get base value: %s", err)
|
||||
}
|
||||
s.baseValue = float64(u64) / s.coef
|
||||
s.pos--
|
||||
s.h = s.buf[s.pos]
|
||||
var n int
|
||||
s.lastDelta, n, err = bin.ReverseGetVarInt64(s.buf[:s.pos])
|
||||
s.lastDelta, _, err = bin.ReverseGetVarInt64(s.buf[:s.pos-1])
|
||||
if err != nil {
|
||||
log.Fatalf("bug: get last delta: %s", err)
|
||||
}
|
||||
s.pos -= n
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// func (s *InstantDeltaCompressor) Size() int {
|
||||
@@ -184,31 +186,60 @@ func (s *InstantDeltaCompressor) Offset() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Snapshot - для створення снапшота.
|
||||
func (s *InstantDeltaCompressor) Snapshot() (left []byte, right []byte) {
|
||||
func (s *InstantDeltaCompressor) Size() int {
|
||||
if s.state == nil {
|
||||
left = s.buf[:s.pos]
|
||||
right = s.encodeTail(s.lastDelta, s.h)
|
||||
return s.pos
|
||||
} else {
|
||||
left = s.state.Payload
|
||||
right = s.encodeTail(s.state.LastDelta, s.state.H)
|
||||
return len(s.state.Payload)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *InstantDeltaCompressor) encodeTail(lastDelta int64, h byte) []byte {
|
||||
tail := make([]byte, 10) // max var uint64 + h
|
||||
n, _ := bin.PutVarInt64(tail, lastDelta)
|
||||
tail[n] = h
|
||||
return tail[:n+1]
|
||||
// Snapshot - для створення снапшота.
|
||||
func (s *InstantDeltaCompressor) Payload() []byte {
|
||||
if s.state == nil {
|
||||
return s.buf[:s.pos]
|
||||
} else {
|
||||
return s.state.Payload
|
||||
}
|
||||
}
|
||||
|
||||
func (s *InstantDeltaCompressor) Rotate(newbuf []byte) (payloadSize int) {
|
||||
n, _ := bin.PutVarInt64(s.buf[s.pos:], s.lastDelta)
|
||||
s.pos += n
|
||||
s.buf[s.pos] = s.h
|
||||
s.pos++
|
||||
payloadSize = s.pos
|
||||
func (s *InstantDeltaCompressor) WritePayloadTo(w io.Writer) (err error) {
|
||||
if s.state == nil {
|
||||
err = bin.WriteUint16(w, uint16(s.pos))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = w.Write(s.buf[:s.pos])
|
||||
return
|
||||
} else {
|
||||
size := bin.CountVarInt64(s.state.LastDelta) + hSize + len(s.state.Payload)
|
||||
err = bin.WriteUint16(w, uint16(size))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = w.Write(s.state.Payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = bin.WriteVarInt64(w, s.state.LastDelta)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.Write([]byte{
|
||||
s.state.H,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// func (s *InstantDeltaCompressor) encodeTail(lastDelta int64, h byte) []byte {
|
||||
// tail := make([]byte, 10) // max var uint64 + h
|
||||
// n, _ := bin.PutVarInt64(tail, lastDelta)
|
||||
// tail[n] = h
|
||||
// return tail[:n+1]
|
||||
// }
|
||||
|
||||
func (s *InstantDeltaCompressor) Rotate(newbuf []byte) {
|
||||
// УВАГА!
|
||||
// state не чіпаємо
|
||||
s.buf = newbuf
|
||||
@@ -216,7 +247,14 @@ func (s *InstantDeltaCompressor) Rotate(newbuf []byte) (payloadSize int) {
|
||||
s.baseValue = 0
|
||||
s.lastDelta = 0
|
||||
s.h = 0
|
||||
return
|
||||
}
|
||||
|
||||
func (s *InstantDeltaCompressor) LastValue() float64 {
|
||||
if s.state == nil {
|
||||
return s.baseValue + float64(s.lastDelta)*s.coef
|
||||
} else {
|
||||
return s.baseValue + float64(s.state.LastDelta)*s.coef
|
||||
}
|
||||
}
|
||||
|
||||
func (s *InstantDeltaCompressor) CreateDecompressor() qb.ValueDecompressor {
|
||||
|
||||
Reference in New Issue
Block a user