wp
This commit is contained in:
@@ -25,19 +25,19 @@ type ValueDeltaCompressor struct {
|
||||
toUint64 func(float64, float64) uint64
|
||||
// (value, coef) => uint64
|
||||
toFloat64 func(uint64, float64) float64
|
||||
state *qb.ValueDeltaCapturedState
|
||||
}
|
||||
|
||||
// Після відновлення із снапшота
|
||||
func NewValueDeltaCompressor(metricType qb.MetricType, fracDigits byte, buf []byte, payloadSize int) *ValueDeltaCompressor {
|
||||
var coef float64 = 1
|
||||
if fracDigits > 0 {
|
||||
coef = math.Pow(10, float64(fracDigits))
|
||||
}
|
||||
s := &ValueDeltaCompressor{
|
||||
buf: buf,
|
||||
coef: coef,
|
||||
coef: 1,
|
||||
pos: payloadSize,
|
||||
}
|
||||
if fracDigits > 0 {
|
||||
s.coef = math.Pow(10, float64(fracDigits))
|
||||
}
|
||||
if metricType == qb.Cumulative {
|
||||
s.calcDelta = calcCumulativeDelta
|
||||
s.toUint64 = toCumulativeUint64
|
||||
@@ -48,16 +48,19 @@ func NewValueDeltaCompressor(metricType qb.MetricType, fracDigits byte, buf []by
|
||||
s.toFloat64 = toInstantFloat64
|
||||
}
|
||||
if payloadSize > 0 {
|
||||
// base value на початку
|
||||
// fmt.Printf("% x\n", buf)
|
||||
// fmt.Printf("% x\n", buf[:s.pos])
|
||||
// fmt.Printf("pos: %d\n", s.pos)
|
||||
u64, _, err := bin.GetVarUint64(s.buf)
|
||||
if err != nil {
|
||||
log.Fatalf("bug: get base value: %s", err)
|
||||
}
|
||||
s.baseValue = float64(u64) / s.coef
|
||||
s.lastDelta, _, err = bin.ReverseGetVarUint64(s.buf[:s.pos-2]) // skip h byte
|
||||
s.baseValue = s.toFloat64(u64, s.coef)
|
||||
s.lastDelta, _, err = bin.ReverseGetVarUint64(s.buf[:s.pos-1]) // skip h byte
|
||||
if err != nil {
|
||||
log.Fatalf("bug: get last delta: %s", err)
|
||||
}
|
||||
//fmt.Printf("lastDelta: %d\n", s.lastDelta)
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -68,9 +71,9 @@ func NewValueDeltaCompressor(metricType qb.MetricType, fracDigits byte, buf []by
|
||||
// arr -
|
||||
func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvaluationReport {
|
||||
var (
|
||||
delta uint64
|
||||
offset int
|
||||
i int
|
||||
delta uint64
|
||||
rewindOffset int
|
||||
i int
|
||||
)
|
||||
if s.pos > 0 {
|
||||
delta = s.calcDelta(s.baseValue, s.coef, value)
|
||||
@@ -81,7 +84,7 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
|
||||
// incrementRun
|
||||
tmp[i] = h + 1
|
||||
i++
|
||||
offset = 1 // перезапис h
|
||||
rewindOffset = 1 // перезапис h
|
||||
} else {
|
||||
// endSeries
|
||||
n, _ := bin.ReversePutVarUint64(tmp, delta)
|
||||
@@ -98,7 +101,7 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
|
||||
i += n
|
||||
tmp[i] = h + 1
|
||||
i++
|
||||
offset = 1 // перезапис h
|
||||
rewindOffset = 1 // перезапис h
|
||||
} else {
|
||||
// endSeries
|
||||
n, _ := bin.ReversePutVarUint64(tmp, delta)
|
||||
@@ -115,11 +118,11 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
|
||||
i += n
|
||||
tmp[i] = 0 // start new run (length=2)
|
||||
i++
|
||||
offset = 1 + n // перезапис пари delta/h
|
||||
rewindOffset = 1 + n // перезапис пари delta/h
|
||||
} else {
|
||||
tmp[i] = 0 // change literal (length=1) to run (length=2)
|
||||
i++
|
||||
offset = 1
|
||||
rewindOffset = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,51 +135,61 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
|
||||
i++
|
||||
}
|
||||
return qb.ValueEvaluationReport{
|
||||
Offset: offset,
|
||||
ChangeSize: i,
|
||||
TotalSpace: s.pos + i - offset,
|
||||
Delta: delta,
|
||||
RewindOffset: rewindOffset,
|
||||
Offset: s.pos - rewindOffset,
|
||||
ChangeSize: i,
|
||||
TotalSpace: s.pos + i - rewindOffset,
|
||||
Delta: delta,
|
||||
}
|
||||
}
|
||||
|
||||
// pos завжди вказує на h
|
||||
func (s *ValueDeltaCompressor) Append(offset int, change []byte, value float64, delta uint64) {
|
||||
func (s *ValueDeltaCompressor) Append(rewindOffset int, change []byte, value float64, delta uint64) {
|
||||
if s.pos > 0 {
|
||||
s.lastDelta = delta
|
||||
} else {
|
||||
s.baseValue = value
|
||||
}
|
||||
copy(s.buf[s.pos-offset:], change)
|
||||
s.pos += len(change) - offset
|
||||
copy(s.buf[s.pos-rewindOffset:], change)
|
||||
s.pos += len(change) - rewindOffset
|
||||
}
|
||||
|
||||
func (s *ValueDeltaCompressor) DeleteLast() {
|
||||
|
||||
}
|
||||
|
||||
func (s *ValueDeltaCompressor) CaptureState() qb.ValueDeltaCapturedState {
|
||||
// if s.state != nil {
|
||||
// qb.Abort(qb.RepeatableLock, nil)
|
||||
// }
|
||||
// позиція посувається вліво, отже може перескочити на попередній chunk
|
||||
pos := s.pos - 1 - bin.CountVarUint64(s.lastDelta)
|
||||
func (s *ValueDeltaCompressor) getState() qb.ValueDeltaCapturedState {
|
||||
bound := s.pos - 1 - bin.CountVarUint64(s.lastDelta)
|
||||
return qb.ValueDeltaCapturedState{
|
||||
H: s.buf[s.pos-1],
|
||||
LastDelta: s.lastDelta,
|
||||
Payload: s.buf[:pos],
|
||||
Payload: s.buf[:bound],
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ValueDeltaCompressor) CaptureState() {
|
||||
state := s.getState()
|
||||
s.state = &state
|
||||
}
|
||||
|
||||
func (s *ValueDeltaCompressor) Tail(offset int) []byte {
|
||||
return s.buf[offset:s.pos]
|
||||
}
|
||||
|
||||
// для зростання буфера під час вставки даних. State не цікавить
|
||||
func (s *ValueDeltaCompressor) Size() int {
|
||||
//if s.state == nil {
|
||||
return s.pos
|
||||
// } else {
|
||||
// return bin.CountVarUint64(s.state.LastDelta) + hSize + len(s.state.Payload)
|
||||
// }
|
||||
}
|
||||
|
||||
// // Snapshot - для створення снапшота.
|
||||
func (s *ValueDeltaCompressor) StoredSize() int {
|
||||
if s.state == nil {
|
||||
return len(s.buf) - s.pos
|
||||
} else {
|
||||
return bin.CountVarUint64(uint64(s.state.LastDelta)) + 1 + len(s.state.Payload)
|
||||
}
|
||||
}
|
||||
|
||||
// Snapshot - для створення снапшота.
|
||||
// func (s *ValueDeltaCompressor) Payload() []byte {
|
||||
// if s.state == nil {
|
||||
// return s.buf[:s.pos]
|
||||
@@ -205,9 +218,7 @@ func (s *ValueDeltaCompressor) WritePayloadTo(w io.Writer, state *qb.ValueDeltaC
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ValueDeltaCompressor) Rotate(newbuf []byte) {
|
||||
// УВАГА!
|
||||
// state не чіпаємо
|
||||
func (s *ValueDeltaCompressor) ReplaceBuffer(newbuf []byte) {
|
||||
s.buf = newbuf
|
||||
s.pos = 0
|
||||
s.baseValue = 0
|
||||
@@ -226,17 +237,18 @@ func (s *ValueDeltaCompressor) LastValue() float64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s *ValueDeltaCompressor) CreateDecompressor() qb.ValueDecompressor {
|
||||
// Decompressor читає виключно збережені на диск дані
|
||||
func (s *ValueDeltaCompressor) CreateDecompressor(metricType qb.MetricType, fracDigits byte) qb.ValueDecompressor {
|
||||
if s.pos == 0 {
|
||||
return nil
|
||||
}
|
||||
return NewValueDeltaDecompressorFromState(ValueDeltaDecompressorFromStateOptions{
|
||||
Coef: s.coef,
|
||||
ToFloat64: s.toFloat64,
|
||||
H: s.buf[s.pos-1],
|
||||
LastDelta: s.lastDelta,
|
||||
Payload: s.buf[:s.pos],
|
||||
})
|
||||
d := NewValueDeltaDecompressor(metricType, fracDigits)
|
||||
if s.state != nil {
|
||||
d.RestoreFromState(*s.state)
|
||||
} else {
|
||||
d.RestoreFromState(s.getState())
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// DECOMPRESSOR
|
||||
@@ -255,78 +267,62 @@ type ValueDeltaDecompressor struct {
|
||||
toFloat64 func(uint64, float64) float64
|
||||
}
|
||||
|
||||
func NewValueDeltaDecompressor(fracDigits byte, buf []byte, toFloat64 func(uint64, float64) float64) *ValueDeltaDecompressor {
|
||||
var coef float64 = 1
|
||||
if fracDigits > 0 {
|
||||
coef = math.Pow(10, float64(fracDigits))
|
||||
}
|
||||
u64, n, err := bin.GetVarUint64(buf)
|
||||
if err != nil {
|
||||
log.Fatalf("bug: get base value: %s", err)
|
||||
}
|
||||
func NewValueDeltaDecompressor(metricType qb.MetricType, fracDigits byte) *ValueDeltaDecompressor {
|
||||
s := &ValueDeltaDecompressor{
|
||||
buf: buf,
|
||||
coef: coef,
|
||||
pos: len(buf), // first free
|
||||
bound: n,
|
||||
toFloat64: toFloat64,
|
||||
coef: 1,
|
||||
}
|
||||
s.baseValue = s.toFloat64(u64, coef)
|
||||
// читаю заголовок наступної серії
|
||||
if fracDigits > 0 {
|
||||
s.coef = math.Pow(10, float64(fracDigits))
|
||||
}
|
||||
if metricType == qb.Cumulative {
|
||||
s.toFloat64 = toCumulativeFloat64
|
||||
} else {
|
||||
s.toFloat64 = toInstantFloat64
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *ValueDeltaDecompressor) RestoreFromEnd(buf []byte) {
|
||||
s.buf = buf
|
||||
s.pos = len(buf) // first free
|
||||
s.readBaseValue()
|
||||
s.readHeader()
|
||||
s.readValue()
|
||||
return s
|
||||
}
|
||||
|
||||
type ValueDeltaDecompressorFromStateOptions struct {
|
||||
Coef float64
|
||||
// (value, coef) => uint64
|
||||
ToFloat64 func(uint64, float64) float64
|
||||
H byte
|
||||
LastDelta uint64
|
||||
Payload []byte
|
||||
func (s *ValueDeltaDecompressor) RestoreFromState(state qb.ValueDeltaCapturedState) {
|
||||
s.buf = state.Payload
|
||||
s.pos = len(s.buf) // first free
|
||||
s.readBaseValue()
|
||||
s.lastValue = s.baseValue + s.toFloat64(state.LastDelta, s.coef)
|
||||
s.decodeHeaderByte(state.H)
|
||||
}
|
||||
|
||||
func NewValueDeltaDecompressorFromState(opt ValueDeltaDecompressorFromStateOptions) *ValueDeltaDecompressor {
|
||||
u64, n, err := bin.GetVarUint64(opt.Payload)
|
||||
func (s *ValueDeltaDecompressor) readBaseValue() {
|
||||
u64, n, err := bin.GetVarUint64(s.buf)
|
||||
if err != nil {
|
||||
log.Fatalf("bug: get base value: %s", err)
|
||||
}
|
||||
s := &ValueDeltaDecompressor{
|
||||
buf: opt.Payload,
|
||||
coef: opt.Coef,
|
||||
pos: len(opt.Payload), // first free
|
||||
bound: n,
|
||||
toFloat64: opt.ToFloat64,
|
||||
}
|
||||
s.baseValue = s.toFloat64(u64, s.coef)
|
||||
s.lastValue = s.baseValue + s.toFloat64(opt.LastDelta, s.coef)
|
||||
s.decodeHeaderByte(opt.H)
|
||||
return s
|
||||
s.bound = n
|
||||
}
|
||||
|
||||
func (s *ValueDeltaDecompressor) NextValue() (value float64, done bool) {
|
||||
//fmt.Printf("NextValue(): bound: %d, pos: %d, pending: %d\n", s.bound, s.pos, s.pending)
|
||||
if s.done {
|
||||
return 0, true
|
||||
}
|
||||
// метод працює як do while - спочатку значення, а потім перевірка умови
|
||||
value = s.lastValue
|
||||
s.pending--
|
||||
if s.pending > 0 {
|
||||
// якщо в серії залишаються елементи
|
||||
if !s.isRun {
|
||||
s.readValue()
|
||||
}
|
||||
} else if s.pos > s.bound {
|
||||
// в серії більше немає елементів, отже перевіряє чи є ще дані в буфері.
|
||||
// дані є - читаю заголовок наступної серії
|
||||
s.readHeader()
|
||||
s.readValue()
|
||||
} else {
|
||||
s.done = true
|
||||
}
|
||||
// серія завершена - перевіряю чи є ще серії
|
||||
return value, false
|
||||
}
|
||||
|
||||
@@ -335,6 +331,7 @@ func (s *ValueDeltaDecompressor) readHeader() {
|
||||
h := s.buf[s.pos]
|
||||
s.decodeHeaderByte(h)
|
||||
}
|
||||
|
||||
func (s *ValueDeltaDecompressor) readValue() {
|
||||
u64, n, err := bin.ReverseGetVarUint64(s.buf[:s.pos])
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user