Skip to content

Commit

Permalink
tsm1 meta lint
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed Oct 15, 2015
1 parent ae92562 commit c27f8ae
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 147 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ packages/
autom4te.cache/
config.log
config.status
Makefile

# log file
influxdb.log
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
- [#4431](https://github.com/influxdb/influxdb/issues/4431): Add tsm1 WAL QuickCheck
- [#4438](https://github.com/influxdb/influxdb/pull/4438): openTSDB service shutdown fixes
- [#3820](https://github.com/influxdb/influxdb/issues/3820): Fix js error in admin UI.
- [#4460](https://github.com/influxdb/influxdb/issues/4460): tsm1 meta lint

## v0.9.4 [2015-09-14]

Expand Down
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
PACKAGES=$(shell find . -name '*.go' -print0 | xargs -0 -n1 dirname | sort --unique)

default:

metalint: deadcode cyclo aligncheck defercheck structcheck lint errcheck

deadcode:
@deadcode $(PACKAGES) 2>&1

cyclo:
@gocyclo -over 10 $(PACKAGES)

aligncheck:
@aligncheck $(PACKAGES)

defercheck:
@defercheck $(PACKAGES)


structcheck:
@structcheck $(PACKAGES)

lint:
@for pkg in $(PACKAGES); do golint $$pkg; done

errcheck:
@for pkg in $(PACKAGES); do \
errcheck -ignorepkg=bytes,fmt -ignore=":(Rollback|Close)" $$pkg \
done

tools:
go get github.com/remyoudompheng/go-misc/deadcode
go get github.com/alecthomas/gocyclo
go get github.com/opennota/check/...
go get github.com/golang/lint/golint
go get github.com/kisielk/errcheck

.PHONY: default,metalint,deadcode,cyclo,aligncheck,defercheck,structcheck,lint,errcheck,tools
41 changes: 0 additions & 41 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package influxdb

import (
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
)

Expand All @@ -26,18 +24,6 @@ func ErrRetentionPolicyNotFound(name string) error {
return fmt.Errorf("retention policy not found: %s", name)
}

func errMeasurementNotFound(name string) error { return fmt.Errorf("measurement not found: %s", name) }

func errorf(format string, a ...interface{}) (err error) {
if _, file, line, ok := runtime.Caller(2); ok {
a = append(a, file, line)
err = fmt.Errorf(format+" (%s:%d)", a...)
} else {
err = fmt.Errorf(format, a...)
}
return
}

// IsClientError indicates whether an error is a known client error.
func IsClientError(err error) bool {
if err == nil {
Expand All @@ -57,30 +43,3 @@ func IsClientError(err error) bool {

return false
}

// mustMarshal encodes a value to JSON.
// This will panic if an error occurs. This should only be used internally when
// an invalid marshal will cause corruption and a panic is appropriate.
func mustMarshalJSON(v interface{}) []byte {
b, err := json.Marshal(v)
if err != nil {
panic("marshal: " + err.Error())
}
return b
}

// mustUnmarshalJSON decodes a value from JSON.
// This will panic if an error occurs. This should only be used internally when
// an invalid unmarshal will cause corruption and a panic is appropriate.
func mustUnmarshalJSON(b []byte, v interface{}) {
if err := json.Unmarshal(b, v); err != nil {
panic("unmarshal: " + err.Error())
}
}

// assert will panic with a given formatted message if the given condition is false.
func assert(condition bool, msg string, v ...interface{}) {
if !condition {
panic(fmt.Sprintf("assert failed: "+msg, v...))
}
}
28 changes: 17 additions & 11 deletions tsdb/engine/tsm1/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ package tsm1
import "encoding/binary"

const (
// boolUncompressed is an uncompressed boolean format
// boolUncompressed is an uncompressed boolean format.
// Not yet implemented.
boolUncompressed = 0

// boolCompressedBitPacked is an bit packed format using 1 bit per boolean
boolCompressedBitPacked = 1
)

// BoolEncoder encodes a series of bools to an in-memory buffer.
type BoolEncoder interface {
Write(b bool)
Bytes() ([]byte, error)
}

type BoolDecoder interface {
Next() bool
Read() bool
Error() error
}

type boolEncoder struct {
// The encoded bytes
bytes []byte
Expand All @@ -39,6 +36,7 @@ type boolEncoder struct {
n int
}

// NewBoolEncoder returns a new instance of BoolEncoder.
func NewBoolEncoder() BoolEncoder {
return &boolEncoder{}
}
Expand All @@ -57,16 +55,16 @@ func (e *boolEncoder) Write(b bool) {
}

// Increment the current bool count
e.i += 1
e.i++
// Increment the total bool count
e.n += 1
e.n++
}

func (e *boolEncoder) flush() {
// Pad remaining byte w/ 0s
for e.i < 8 {
e.b = e.b << 1
e.i += 1
e.i++
}

// If we have bits set, append them to the byte slice
Expand All @@ -93,13 +91,21 @@ func (e *boolEncoder) Bytes() ([]byte, error) {
return append(b[:i], e.bytes...), nil
}

// BoolDecoder decodes a series of bools from an in-memory buffer.
type BoolDecoder interface {
Next() bool
Read() bool
Error() error
}

type boolDecoder struct {
b []byte
i int
n int
err error
}

// NewBoolDecoder returns a new instance of BoolDecoder.
func NewBoolDecoder(b []byte) BoolDecoder {
// First byte stores the encoding type, only have 1 bit-packet format
// currently ignore for now.
Expand All @@ -109,7 +115,7 @@ func NewBoolDecoder(b []byte) BoolDecoder {
}

func (e *boolDecoder) Next() bool {
e.i += 1
e.i++
return e.i < e.n
}

Expand Down
6 changes: 4 additions & 2 deletions tsdb/engine/tsm1/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Test_BoolEncoder_Multi_Compressed(t *testing.T) {
}

func Test_BoolEncoder_Quick(t *testing.T) {
quick.Check(func(values []bool) bool {
if err := quick.Check(func(values []bool) bool {
// Write values to encoder.
enc := tsm1.NewBoolEncoder()
for _, v := range values {
Expand All @@ -101,5 +101,7 @@ func Test_BoolEncoder_Quick(t *testing.T) {
}

return true
}, nil)
}, nil); err != nil {
t.Fatal(err)
}
}
3 changes: 3 additions & 0 deletions tsdb/engine/tsm1/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type combinedEngineCursor struct {
ascending bool
}

// NewCombinedEngineCursor returns a Cursor that joins wc and ec.
// Values from wc take precedence over ec when identical timestamps are returned.
func NewCombinedEngineCursor(wc, ec tsdb.Cursor, ascending bool) tsdb.Cursor {
return &combinedEngineCursor{
walCursor: wc,
Expand Down Expand Up @@ -105,6 +107,7 @@ type multiFieldCursor struct {
valueBuffer []interface{}
}

// NewMultiFieldCursor returns an instance of Cursor that joins the results of cursors.
func NewMultiFieldCursor(fields []string, cursors []tsdb.Cursor, ascending bool) tsdb.Cursor {
return &multiFieldCursor{
fields: fields,
Expand Down
40 changes: 20 additions & 20 deletions tsdb/engine/tsm1/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,33 @@ func (e *EmptyValue) Size() int { return 0 }
// makes the code cleaner.
type Values []Value

func (v Values) MinTime() int64 {
return v[0].Time().UnixNano()
func (a Values) MinTime() int64 {
return a[0].Time().UnixNano()
}

func (v Values) MaxTime() int64 {
return v[len(v)-1].Time().UnixNano()
func (a Values) MaxTime() int64 {
return a[len(a)-1].Time().UnixNano()
}

// Encode converts the values to a byte slice. If there are no values,
// this function panics.
func (v Values) Encode(buf []byte) ([]byte, error) {
if len(v) == 0 {
func (a Values) Encode(buf []byte) ([]byte, error) {
if len(a) == 0 {
panic("unable to encode block type")
}

switch v[0].(type) {
switch a[0].(type) {
case *FloatValue:
return encodeFloatBlock(buf, v)
return encodeFloatBlock(buf, a)
case *Int64Value:
return encodeInt64Block(buf, v)
return encodeInt64Block(buf, a)
case *BoolValue:
return encodeBoolBlock(buf, v)
return encodeBoolBlock(buf, a)
case *StringValue:
return encodeStringBlock(buf, v)
return encodeStringBlock(buf, a)
}

return nil, fmt.Errorf("unsupported value type %T", v[0])
return nil, fmt.Errorf("unsupported value type %T", a[0])
}

// DecodeBlock takes a byte array and will decode into values of the appropriate type
Expand Down Expand Up @@ -115,19 +115,19 @@ func DecodeBlock(block []byte) (Values, error) {
// Deduplicate returns a new Values slice with any values
// that have the same timestamp removed. The Value that appears
// last in the slice is the one that is kept. The returned slice is in ascending order
func (v Values) Deduplicate() Values {
func (a Values) Deduplicate() Values {
m := make(map[int64]Value)
for _, val := range v {
for _, val := range a {
m[val.UnixNano()] = val
}

a := make([]Value, 0, len(m))
other := make([]Value, 0, len(m))
for _, val := range m {
a = append(a, val)
other = append(other, val)
}
sort.Sort(Values(a))
sort.Sort(Values(other))

return a
return other
}

// Sort methods
Expand Down Expand Up @@ -340,8 +340,8 @@ func (v *Int64Value) Value() interface{} {
return v.value
}

func (f *Int64Value) UnixNano() int64 {
return f.time.UnixNano()
func (v *Int64Value) UnixNano() int64 {
return v.time.UnixNano()
}

func (v *Int64Value) Size() int {
Expand Down
4 changes: 3 additions & 1 deletion tsdb/engine/tsm1/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
)

const (
// floatUncompressed is an uncompressed format using 8 bytes per value
// floatUncompressed is an uncompressed format using 8 bytes per value.
// Not yet implemented.
floatUncompressed = 0

// floatCompressedGorilla is a compressed format using the gorilla paper encoding
floatCompressedGorilla = 1
)
Expand Down
2 changes: 1 addition & 1 deletion tsdb/engine/tsm1/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (d *int64Decoder) Next() bool {
return false
}

d.i += 1
d.i++

if d.i >= d.n {
switch d.encoding {
Expand Down
4 changes: 3 additions & 1 deletion tsdb/engine/tsm1/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
)

const (
// stringUncompressed is a an uncompressed format encoding strings as raw bytes
// stringUncompressed is a an uncompressed format encoding strings as raw bytes.
// Not yet implemented.
stringUncompressed = 0

// stringCompressedSnappy is a compressed encoding using Snappy compression
stringCompressedSnappy = 1
)
Expand Down
6 changes: 3 additions & 3 deletions tsdb/engine/tsm1/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type TimeEncoder interface {
Bytes() ([]byte, error)
}

// TimeEncoder decodes byte slices to time.Time values.
// TimeDecoder decodes byte slices to time.Time values.
type TimeDecoder interface {
Next() bool
Read() time.Time
Expand Down Expand Up @@ -264,7 +264,7 @@ func (d *decoder) decodeRLE(b []byte) {

// Lower 4 bits hold the 10 based exponent so we can scale the values back up
mod := int64(math.Pow10(int(b[i] & 0xF)))
i += 1
i++

// Next 8 bytes is the starting timestamp
first := binary.BigEndian.Uint64(b[i : i+8])
Expand All @@ -278,7 +278,7 @@ func (d *decoder) decodeRLE(b []byte) {
i += n

// Last 1-10 bytes is how many times the value repeats
count, n := binary.Uvarint(b[i:])
count, _ := binary.Uvarint(b[i:])

// Rebuild construct the original values now
deltas := make([]uint64, count)
Expand Down
4 changes: 2 additions & 2 deletions tsdb/engine/tsm1/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func Test_TimeEncoder_Reverse(t *testing.T) {
if ts[i] != dec.Read() {
t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), ts[i])
}
i += 1
i++
}
}

Expand Down Expand Up @@ -343,7 +343,7 @@ func Test_TimeEncoder_220SecondDelta(t *testing.T) {
if ts[i] != dec.Read() {
t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), ts[i])
}
i += 1
i++
}

if i != len(ts) {
Expand Down
Loading

0 comments on commit c27f8ae

Please sign in to comment.