Skip to content

Commit

Permalink
Remove custom binary-conversion functions
Browse files Browse the repository at this point in the history
Also cleaned up some excess allocations, and other cruft from the code
  • Loading branch information
joelegasse committed Feb 11, 2016
1 parent 8d7a4a9 commit c360bea
Show file tree
Hide file tree
Showing 18 changed files with 271 additions and 335 deletions.
23 changes: 0 additions & 23 deletions cmd/influx_inspect/info.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/binary"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -90,28 +89,6 @@ func countSeries(tstore *tsdb.Store) int {
return count
}

func btou64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}

// u64tob converts a uint64 into an 8-byte slice.
func u64tob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}

func btou32(b []byte) uint32 {
return binary.BigEndian.Uint32(b)
}

// u32tob converts a uint32 into an 4-byte slice.
func u32tob(v uint32) []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, v)
return b
}

// ShardIDs is a collection of UINT 64 that represent shard ids.
type ShardIDs []uint64

Expand Down
10 changes: 5 additions & 5 deletions cmd/influx_inspect/tsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ func readIndex(f *os.File) (*tsmIndex, error) {
// Get the min time
f.Seek(-20, os.SEEK_END)
f.Read(b)
minTime := time.Unix(0, int64(btou64(b)))
minTime := time.Unix(0, int64(binary.BigEndian.Uint64(b)))

// Get max time
f.Seek(-12, os.SEEK_END)
f.Read(b)
maxTime := time.Unix(0, int64(btou64(b)))
maxTime := time.Unix(0, int64(binary.BigEndian.Uint64(b)))

// Figure out where the index starts
indexStart := stat.Size() - int64(seriesCount*12+20)
Expand Down Expand Up @@ -356,15 +356,15 @@ func cmdDumpTsm1(opts *tsdmDumpOpts) {
f.Seek(int64(i), 0)

f.Read(b)
id := btou64(b)
id := binary.BigEndian.Uint64(b)
f.Read(b[:4])
length := binary.BigEndian.Uint32(b[:4])
buf := make([]byte, length)
f.Read(buf)

blockSize += int64(len(buf)) + 12

startTime := time.Unix(0, int64(btou64(buf[:8])))
startTime := time.Unix(0, int64(binary.BigEndian.Uint64(buf[:8])))
blockType := buf[8]

encoded := buf[9:]
Expand Down Expand Up @@ -544,7 +544,7 @@ func cmdDumpTsm1dev(opts *tsdmDumpOpts) {
f.Seek(int64(e.Offset), 0)
f.Read(b[:4])

chksum := btou32(b[:4])
chksum := binary.BigEndian.Uint32(b[:4])

buf := make([]byte, e.Size-4)
f.Read(buf)
Expand Down
14 changes: 3 additions & 11 deletions cmd/influx_tsm/b1/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ func newCursor(tx *bolt.Tx, series string, field string, dec *tsdb.FieldCodec) *

// Seek moves the cursor to a position.
func (c cursor) SeekTo(seek int64) {
k, v := c.cursor.Seek(u64tob(uint64(seek)))
var seekBytes [8]byte
binary.BigEndian.PutUint64(seekBytes[:], uint64(seek))
k, v := c.cursor.Seek(seekBytes[:])
c.keyBuf, c.valBuf = tsdb.DecodeKeyValue(c.field, c.dec, k, v)
}

Expand Down Expand Up @@ -234,13 +236,3 @@ func (a cursors) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a cursors) Less(i, j int) bool {
return tsm1.SeriesFieldKey(a[i].series, a[i].field) < tsm1.SeriesFieldKey(a[j].series, a[j].field)
}

// u64tob converts a uint64 into an 8-byte slice.
func u64tob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}

// btou64 converts an 8-byte slice to a uint64.
func btou64(b []byte) uint64 { return binary.BigEndian.Uint64(b) }
23 changes: 7 additions & 16 deletions cmd/influx_tsm/bz1/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,25 @@ func newCursor(tx *bolt.Tx, series string, field string, dec *tsdb.FieldCodec) *

// Seek moves the cursor to a position.
func (c *cursor) SeekTo(seek int64) {
seekBytes := u64tob(uint64(seek))
var seekBytes [8]byte
binary.BigEndian.PutUint64(seekBytes[:], uint64(seek))

// Move cursor to appropriate block and set to buffer.
k, v := c.cursor.Seek(seekBytes)
k, v := c.cursor.Seek(seekBytes[:])
if v == nil { // get the last block, it might have this time
_, v = c.cursor.Last()
} else if seek < int64(btou64(k)) { // the seek key is less than this block, go back one and check
} else if seek < int64(binary.BigEndian.Uint64(k)) { // the seek key is less than this block, go back one and check
_, v = c.cursor.Prev()

// if the previous block max time is less than the seek value, reset to where we were originally
if v == nil || seek > int64(btou64(v[0:8])) {
_, v = c.cursor.Seek(seekBytes)
if v == nil || seek > int64(binary.BigEndian.Uint64(v[0:8])) {
_, v = c.cursor.Seek(seekBytes[:])
}
}
c.setBuf(v)

// Read current block up to seek position.
c.seekBuf(seekBytes)
c.seekBuf(seekBytes[:])

// Return current entry.
c.keyBuf, c.valBuf = c.read()
Expand Down Expand Up @@ -335,16 +336,6 @@ func (a cursors) Less(i, j int) bool {
return tsm.SeriesFieldKey(a[i].series, a[i].field) < tsm.SeriesFieldKey(a[j].series, a[j].field)
}

// btou64 converts an 8-byte slice into an uint64.
func btou64(b []byte) uint64 { return binary.BigEndian.Uint64(b) }

// u64tob converts a uint64 into an 8-byte slice.
func u64tob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}

// entryHeaderSize is the number of bytes required for the header.
const entryHeaderSize = 8 + 4

Expand Down
5 changes: 1 addition & 4 deletions cmd/influx_tsm/tsdb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,11 @@ func MeasurementFromSeriesKey(key string) string {
// DecodeKeyValue decodes the key and value from bytes.
func DecodeKeyValue(field string, dec *FieldCodec, k, v []byte) (int64, interface{}) {
// Convert key to a timestamp.
key := int64(btou64(k[0:8]))
key := int64(binary.BigEndian.Uint64(k[0:8]))

decValue, err := dec.DecodeByName(field, v)
if err != nil {
return key, nil
}
return key, decValue
}

// btou64 converts an 8-byte slice into an uint64.
func btou64(b []byte) uint64 { return binary.BigEndian.Uint64(b) }
6 changes: 1 addition & 5 deletions cmd/influxd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (cmd *Command) backupMetastore() error {
return err
}

magic := btou64(binData[:8])
magic := binary.BigEndian.Uint64(binData[:8])
if magic != snapshotter.BackupMagicHeader {
cmd.Logger.Println("Invalid metadata blob, ensure the metadata service is running (default port 8088)")
return errors.New("invalid metadata received")
Expand Down Expand Up @@ -365,7 +365,3 @@ func retentionAndShardFromPath(path string) (retention, shard string, err error)

return a[1], a[2], nil
}

func btou64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
17 changes: 3 additions & 14 deletions cmd/influxd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,20 @@ func (cmd *Command) unpackMeta() error {
var i int

// Make sure the file is actually a meta store backup file
magic := btou64(b[:8])
magic := binary.BigEndian.Uint64(b[:8])
if magic != snapshotter.BackupMagicHeader {
return fmt.Errorf("invalid metadata file")
}
i += 8

// Size of the meta store bytes
length := int(btou64(b[i : i+8]))
length := int(binary.BigEndian.Uint64(b[i : i+8]))
i += 8
metaBytes := b[i : i+length]
i += int(length)

// Size of the node.json bytes
length = int(btou64(b[i : i+8]))
length = int(binary.BigEndian.Uint64(b[i : i+8]))
i += 8
nodeBytes := b[i:]

Expand Down Expand Up @@ -399,14 +399,3 @@ func (ln *nopListener) Close() error {
}

func (ln *nopListener) Addr() net.Addr { return &net.TCPAddr{} }

// u64tob converts a uint64 into an 8-byte slice.
func u64tob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}

func btou64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
2 changes: 1 addition & 1 deletion influxql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2268,7 +2268,7 @@ func TestDropSeriesStatement_String(t *testing.T) {

func BenchmarkParserParseStatement(b *testing.B) {
b.ReportAllocs()
s := `SELECT field FROM "series" WHERE value > 10`
s := `SELECT "field" FROM "series" WHERE value > 10`
for i := 0; i < b.N; i++ {
if stmt, err := influxql.NewParser(strings.NewReader(s)).ParseStatement(); err != nil {
b.Fatalf("unexpected error: %s", err)
Expand Down
36 changes: 17 additions & 19 deletions models/points.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,29 +1247,37 @@ func (p *point) String() string {
}

func (p *point) MarshalBinary() ([]byte, error) {
b := u32tob(uint32(len(p.Key())))
b = append(b, p.Key()...)

b = append(b, u32tob(uint32(len(p.fields)))...)
b = append(b, p.fields...)

tb, err := p.time.MarshalBinary()
if err != nil {
return nil, err
}
b = append(b, tb...)

b := make([]byte, 8+len(p.key)+len(p.fields)+len(tb))
i := 0

binary.BigEndian.PutUint32(b[i:], uint32(len(p.key)))
i += 4

i += copy(b[i:], p.key)

binary.BigEndian.PutUint32(b[i:i+4], uint32(len(p.fields)))
i += 4

i += copy(b[i:], p.fields)

copy(b[i:], tb)
return b, nil
}

func (p *point) UnmarshalBinary(b []byte) error {
var i int
keyLen := int(btou32(b[:4]))
keyLen := int(binary.BigEndian.Uint32(b[:4]))
i += int(4)

p.key = b[i : i+keyLen]
i += keyLen

fieldLen := int(btou32(b[i : i+4]))
fieldLen := int(binary.BigEndian.Uint32(b[i : i+4]))
i += int(4)

p.fields = b[i : i+fieldLen]
Expand Down Expand Up @@ -1528,13 +1536,3 @@ func (s *indexedSlice) Swap(i, j int) {
func (s *indexedSlice) Len() int {
return len(s.indices)
}

func u32tob(v uint32) []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, v)
return b
}

func btou32(b []byte) uint32 {
return uint32(binary.BigEndian.Uint32(b))
}
16 changes: 4 additions & 12 deletions services/hh/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,13 @@ func (l *segment) readUint64() (uint64, error) {
if err := l.readBytes(b); err != nil {
return 0, err
}
return btou64(b), nil
return binary.BigEndian.Uint64(b), nil
}

func (l *segment) writeUint64(sz uint64) error {
return l.writeBytes(u64tob(sz))
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], sz)
return l.writeBytes(buf[:])
}

func (l *segment) writeBytes(b []byte) error {
Expand All @@ -706,13 +708,3 @@ func (l *segment) readBytes(b []byte) error {
}
return nil
}

func u64tob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}

func btou64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
35 changes: 13 additions & 22 deletions services/snapshotter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,36 @@ func (s *Service) handleConn(conn net.Conn) error {

func (s *Service) writeMetaStore(conn net.Conn) error {
// Retrieve and serialize the current meta data.
buf, err := s.MetaClient.MarshalBinary()
metaBlob, err := s.MetaClient.MarshalBinary()
if err != nil {
return fmt.Errorf("marshal meta: %s", err)
}

var b bytes.Buffer
enc := json.NewEncoder(&b)
if err := enc.Encode(s.Node); err != nil {
var nodeBytes bytes.Buffer
if err := json.NewEncoder(&nodeBytes).Encode(s.Node); err != nil {
return err
}

if _, err := conn.Write(u64tob(BackupMagicHeader)); err != nil {
return err
}
var numBytes [24]byte

if _, err := conn.Write(u64tob(uint64(len(buf)))); err != nil {
binary.BigEndian.PutUint64(numBytes[:8], BackupMagicHeader)
binary.BigEndian.PutUint64(numBytes[8:16], uint64(len(metaBlob)))
binary.BigEndian.PutUint64(numBytes[16:24], uint64(nodeBytes.Len()))

// backup header followed by meta blob length
if _, err := conn.Write(numBytes[:16]); err != nil {
return err
}

if _, err := conn.Write(buf); err != nil {
if _, err := conn.Write(metaBlob); err != nil {
return err
}

if _, err := conn.Write(u64tob(uint64(b.Len()))); err != nil {
if _, err := conn.Write(numBytes[16:24]); err != nil {
return err
}

if _, err := b.WriteTo(conn); err != nil {
if _, err := nodeBytes.WriteTo(conn); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -286,14 +288,3 @@ type Request struct {
type Response struct {
Paths []string
}

// u64tob converts a uint64 into an 8-byte slice.
func u64tob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}

func btou64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
Loading

0 comments on commit c360bea

Please sign in to comment.