func (d *TimeDecoder) Read() int64 {
	return d.v
}
Read returns the next timestamp from the decoder.
package tsm1
import (
	"encoding/binary"
	"fmt"
	"math"
	"github.com/jwilder/encoding/simple8b"
)
const (
	timeUncompressed = 0
	timeCompressedPackedSimple = 1
	timeCompressedRLE = 2
)
type TimeEncoder interface {
	Write(t int64)
	Bytes() ([]byte, error)
	Reset()
}
type encoder struct {
	ts    []uint64
	bytes []byte
	enc   *simple8b.Encoder
}
func NewTimeEncoder(sz int) TimeEncoder {
	return &encoder{
		ts:  make([]uint64, 0, sz),
		enc: simple8b.NewEncoder(),
	}
}
func (e *encoder) Reset() {
	e.ts = e.ts[:0]
	e.bytes = e.bytes[:0]
	e.enc.Reset()
}
func (e *encoder) Write(t int64) {
	e.ts = append(e.ts, uint64(t))
}
func (e *encoder) reduce() (max, divisor uint64, rle bool, deltas []uint64) {
	deltas = e.ts
	max, divisor = 0, 1e12
	rle = true
	for i := len(deltas) - 1; i > 0; i-- {
		deltas[i] = deltas[i] - deltas[i-1]
		v := deltas[i]
		if v > max {
			max = v
		}
		for divisor > 1 && v%divisor != 0 {
			divisor /= 10
		}
		rle = i == len(deltas)-1 || rle && (deltas[i+1] == deltas[i])
	}
	return
}
func (e *encoder) Bytes() ([]byte, error) {
	if len(e.ts) == 0 {
		return e.bytes[:0], nil
	}
	max, div, rle, dts := e.reduce()
	if rle && len(e.ts) > 1 {
		return e.encodeRLE(e.ts[0], e.ts[1], div, len(e.ts))
	}
	if max > simple8b.MaxValue {
		return e.encodeRaw()
	}
	return e.encodePacked(div, dts)
}
func (e *encoder) encodePacked(div uint64, dts []uint64) ([]byte, error) {
	if div > 1 {
		for _, v := range dts[1:] {
			if err := e.enc.Write(v / div); err != nil {
				return nil, err
			}
		}
	} else {
		for _, v := range dts[1:] {
			if err := e.enc.Write(v); err != nil {
				return nil, err
			}
		}
	}
	deltas, err := e.enc.Bytes()
	if err != nil {
		return nil, err
	}
	sz := 8 + 1 + len(deltas)
	if cap(e.bytes) < sz {
		e.bytes = make([]byte, sz)
	}
	b := e.bytes[:sz]
	b[0] = byte(timeCompressedPackedSimple) << 4
	b[0] |= byte(math.Log10(float64(div)))
	binary.BigEndian.PutUint64(b[1:9], uint64(dts[0]))
	copy(b[9:], deltas)
	return b[:9+len(deltas)], nil
}
func (e *encoder) encodeRaw() ([]byte, error) {
	sz := 1 + len(e.ts)*8
	if cap(e.bytes) < sz {
		e.bytes = make([]byte, sz)
	}
	b := e.bytes[:sz]
	b[0] = byte(timeUncompressed) << 4
	for i, v := range e.ts {
		binary.BigEndian.PutUint64(b[1+i*8:1+i*8+8], uint64(v))
	}
	return b, nil
}
func (e *encoder) encodeRLE(first, delta, div uint64, n int) ([]byte, error) {
	sz := 31
	if cap(e.bytes) < sz {
		e.bytes = make([]byte, sz)
	}
	b := e.bytes[:sz]
	b[0] = byte(timeCompressedRLE) << 4
	b[0] |= byte(math.Log10(float64(div)))
	i := 1
	binary.BigEndian.PutUint64(b[i:], uint64(first))
	i += 8
	i += binary.PutUvarint(b[i:], uint64(delta/div))
	i += binary.PutUvarint(b[i:], uint64(n))
	return b[:i], nil
}
type TimeDecoder struct {
	v    int64
	i, n int
	ts   []uint64
	dec  simple8b.Decoder
	err  error
	rleDelta int64
	encoding byte
}
func (d *TimeDecoder) Init(b []byte) {
	d.v = 0
	d.i = 0
	d.ts = d.ts[:0]
	d.err = nil
	if len(b) > 0 {
		d.encoding = b[0] >> 4
	}
	d.decode(b)
}
func (d *TimeDecoder) Next() bool {
	if d.err != nil {
		return false
	}
	if d.encoding == timeCompressedRLE {
		if d.i >= d.n {
			return false
		}
		d.i++
		d.v += d.rleDelta
		return d.i < d.n
	}
	if d.i >= len(d.ts) {
		return false
	}
	d.v = int64(d.ts[d.i])
	d.i++
	return true
}