2015-10-01 19:23:38 +00:00
|
|
|
package tsm1_test
|
2015-09-02 22:05:07 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-24 20:29:51 +00:00
|
|
|
// "math/rand"
|
|
|
|
|
2015-09-29 22:08:18 +00:00
|
|
|
"fmt"
|
2015-09-17 19:46:37 +00:00
|
|
|
"reflect"
|
2015-09-02 22:05:07 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-10-01 19:23:38 +00:00
|
|
|
"github.com/influxdb/influxdb/tsdb/engine/tsm1"
|
2015-09-02 22:05:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncoding_FloatBlock(t *testing.T) {
|
2015-09-17 19:46:37 +00:00
|
|
|
valueCount := 1000
|
|
|
|
times := getTimes(valueCount, 60, time.Second)
|
2015-10-01 19:23:38 +00:00
|
|
|
values := make(tsm1.Values, len(times))
|
2015-09-17 19:46:37 +00:00
|
|
|
for i, t := range times {
|
2015-10-01 19:23:38 +00:00
|
|
|
values[i] = tsm1.NewValue(t, float64(i))
|
2015-09-17 19:46:37 +00:00
|
|
|
}
|
2015-09-02 22:05:07 +00:00
|
|
|
|
2015-10-02 16:46:58 +00:00
|
|
|
b, err := values.Encode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-09-02 22:05:07 +00:00
|
|
|
|
2015-09-17 19:46:37 +00:00
|
|
|
decodedValues := values.DecodeSameTypeBlock(b)
|
2015-09-06 22:49:15 +00:00
|
|
|
|
2015-09-17 19:46:37 +00:00
|
|
|
if !reflect.DeepEqual(decodedValues, values) {
|
|
|
|
t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)
|
|
|
|
}
|
2015-09-02 22:05:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 02:01:43 +00:00
|
|
|
func TestEncoding_FloatBlock_ZeroTime(t *testing.T) {
|
2015-10-01 19:23:38 +00:00
|
|
|
values := make(tsm1.Values, 3)
|
2015-09-30 02:01:43 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
2015-10-01 19:23:38 +00:00
|
|
|
values[i] = tsm1.NewValue(time.Unix(0, 0), float64(i))
|
2015-09-30 02:01:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 16:46:58 +00:00
|
|
|
b, err := values.Encode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-09-30 02:01:43 +00:00
|
|
|
|
|
|
|
decodedValues := values.DecodeSameTypeBlock(b)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(decodedValues, values) {
|
|
|
|
t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 20:22:07 +00:00
|
|
|
func TestEncoding_FloatBlock_SimilarFloats(t *testing.T) {
|
|
|
|
values := make(tsm1.Values, 5)
|
|
|
|
values[0] = tsm1.NewValue(time.Unix(0, 1444238178437870000), 6.00065e+06)
|
|
|
|
values[1] = tsm1.NewValue(time.Unix(0, 1444238185286830000), 6.000656e+06)
|
|
|
|
values[2] = tsm1.NewValue(time.Unix(0, 1444238188441501000), 6.000657e+06)
|
|
|
|
values[3] = tsm1.NewValue(time.Unix(0, 1444238195286811000), 6.000659e+06)
|
|
|
|
values[4] = tsm1.NewValue(time.Unix(0, 1444238198439917000), 6.000661e+06)
|
|
|
|
|
|
|
|
b, err := values.Encode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedValues := values.DecodeSameTypeBlock(b)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(decodedValues, values) {
|
|
|
|
t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:42:48 +00:00
|
|
|
func TestEncoding_IntBlock_Basic(t *testing.T) {
|
2015-09-24 20:29:51 +00:00
|
|
|
valueCount := 1000
|
|
|
|
times := getTimes(valueCount, 60, time.Second)
|
2015-10-01 19:23:38 +00:00
|
|
|
values := make(tsm1.Values, len(times))
|
2015-09-24 20:29:51 +00:00
|
|
|
for i, t := range times {
|
2015-10-01 19:23:38 +00:00
|
|
|
values[i] = tsm1.NewValue(t, int64(i))
|
2015-09-24 20:29:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 16:46:58 +00:00
|
|
|
b, err := values.Encode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-09-24 20:29:51 +00:00
|
|
|
|
|
|
|
decodedValues := values.DecodeSameTypeBlock(b)
|
|
|
|
|
2015-09-24 22:42:48 +00:00
|
|
|
if len(decodedValues) != len(values) {
|
|
|
|
t.Fatalf("unexpected results length:\n\tgot: %v\n\texp: %v\n", len(decodedValues), len(values))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(decodedValues); i++ {
|
|
|
|
|
|
|
|
if decodedValues[i].Time() != values[i].Time() {
|
|
|
|
t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues[i].Time(), values[i].Time())
|
|
|
|
}
|
|
|
|
|
|
|
|
if decodedValues[i].Value() != values[i].Value() {
|
|
|
|
t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues[i].Value(), values[i].Value())
|
|
|
|
}
|
2015-09-24 20:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncoding_IntBlock_Negatives(t *testing.T) {
|
|
|
|
valueCount := 1000
|
|
|
|
times := getTimes(valueCount, 60, time.Second)
|
2015-10-01 19:23:38 +00:00
|
|
|
values := make(tsm1.Values, len(times))
|
2015-09-24 20:29:51 +00:00
|
|
|
for i, t := range times {
|
|
|
|
v := int64(i)
|
|
|
|
if i%2 == 0 {
|
|
|
|
v = -v
|
|
|
|
}
|
2015-10-01 19:23:38 +00:00
|
|
|
values[i] = tsm1.NewValue(t, int64(v))
|
2015-09-24 20:29:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 16:46:58 +00:00
|
|
|
b, err := values.Encode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-09-24 20:29:51 +00:00
|
|
|
|
|
|
|
decodedValues := values.DecodeSameTypeBlock(b)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(decodedValues, values) {
|
|
|
|
t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 18:53:27 +00:00
|
|
|
func TestEncoding_BoolBlock_Basic(t *testing.T) {
|
|
|
|
valueCount := 1000
|
|
|
|
times := getTimes(valueCount, 60, time.Second)
|
2015-10-01 19:23:38 +00:00
|
|
|
values := make(tsm1.Values, len(times))
|
2015-09-28 18:53:27 +00:00
|
|
|
for i, t := range times {
|
|
|
|
v := true
|
|
|
|
if i%2 == 0 {
|
|
|
|
v = false
|
|
|
|
}
|
2015-10-01 19:23:38 +00:00
|
|
|
values[i] = tsm1.NewValue(t, v)
|
2015-09-28 18:53:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 16:46:58 +00:00
|
|
|
b, err := values.Encode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-09-28 18:53:27 +00:00
|
|
|
|
|
|
|
decodedValues := values.DecodeSameTypeBlock(b)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(decodedValues, values) {
|
|
|
|
t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 22:08:18 +00:00
|
|
|
func TestEncoding_StringBlock_Basic(t *testing.T) {
|
|
|
|
valueCount := 1000
|
|
|
|
times := getTimes(valueCount, 60, time.Second)
|
2015-10-01 19:23:38 +00:00
|
|
|
values := make(tsm1.Values, len(times))
|
2015-09-29 22:08:18 +00:00
|
|
|
for i, t := range times {
|
2015-10-01 19:23:38 +00:00
|
|
|
values[i] = tsm1.NewValue(t, fmt.Sprintf("value %d", i))
|
2015-09-29 22:08:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 16:46:58 +00:00
|
|
|
b, err := values.Encode(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-09-29 22:08:18 +00:00
|
|
|
|
|
|
|
decodedValues := values.DecodeSameTypeBlock(b)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(decodedValues, values) {
|
|
|
|
t.Fatalf("unexpected results:\n\tgot: %v\n\texp: %v\n", decodedValues, values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 22:49:15 +00:00
|
|
|
func getTimes(n, step int, precision time.Duration) []time.Time {
|
2015-09-02 22:05:07 +00:00
|
|
|
t := time.Now().Round(precision)
|
2015-09-06 22:49:15 +00:00
|
|
|
a := make([]time.Time, n)
|
2015-09-02 22:05:07 +00:00
|
|
|
for i := 0; i < n; i++ {
|
2015-09-24 22:42:48 +00:00
|
|
|
a[i] = t.Add(time.Duration(i*60) * precision)
|
2015-09-02 22:05:07 +00:00
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|