Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Move filter.fastParseInt to convert.ToInt
Browse files Browse the repository at this point in the history
This has been extracted so that it can be used outside of the filter
package.
  • Loading branch information
mjs committed Mar 20, 2018
1 parent 34be8c8 commit 8c088e7
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 61 deletions.
46 changes: 46 additions & 0 deletions convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2018 Jump Trading
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package convert

import "errors"

const int64Max = (1 << 63) - 1

// ToInt is a simpler, faster version of strconv.ParseInt().
// Differences to ParseInt:
// - input is []byte instead of a string (no type conversion required
// by caller)
// - only supports base 10 input
// - only handles positive values
func ToInt(s []byte) (int64, error) {
if len(s) == 0 {
return 0, errors.New("empty")
}

var n uint64
for _, c := range s {
if '0' <= c && c <= '9' {
c -= '0'
} else {
return 0, errors.New("invalid char")
}
n = n*10 + uint64(c)
}

if n > int64Max {
return 0, errors.New("overflow")
}
return int64(n), nil
}
51 changes: 51 additions & 0 deletions convert/convert_small_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2018 Jump Trading
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build small

package convert_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/jumptrading/influx-spout/convert"
)

func TestToInt(t *testing.T) {
check := func(input string, expected int64) {
actual, err := convert.ToInt([]byte(input))
require.NoError(t, err)
assert.Equal(t, expected, actual, "ToInt(%q)", input)
}

shouldFail := func(input string) {
_, err := convert.ToInt([]byte(input))
assert.Error(t, err)
}

check("0", 0)
check("1", 1)
check("9", 9)
check("10", 10)
check("99", 99)
check("101", 101)
check("9223372036854775807", (1<<63)-1) // max int64 value

shouldFail("9223372036854775808") // max int64 value + 1
shouldFail("-1") // negatives not supported
shouldFail("x")
}
26 changes: 0 additions & 26 deletions filter/timestamp_small_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestExtractTimestamp(t *testing.T) {
Expand Down Expand Up @@ -56,28 +55,3 @@ func TestExtractTimestamp(t *testing.T) {
check("weather temp=99 -"+tsStr, defaultTs)
check(tsStr, defaultTs)
}

func TestFastParseInt(t *testing.T) {
check := func(input string, expected int64) {
actual, err := fastParseInt([]byte(input))
require.NoError(t, err)
assert.Equal(t, expected, actual, "fastParseInt(%q)", input)
}

shouldFail := func(input string) {
_, err := fastParseInt([]byte(input))
assert.Error(t, err)
}

check("0", 0)
check("1", 1)
check("9", 9)
check("10", 10)
check("99", 99)
check("101", 101)
check("9223372036854775807", (1<<63)-1) // max int64 value

shouldFail("9223372036854775808") // max int64 value + 1
shouldFail("-1") // negatives not supported
shouldFail("x")
}
33 changes: 2 additions & 31 deletions filter/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package filter

import (
"bytes"
"errors"
"fmt"
"log"
"sync"
"time"

"github.com/jumptrading/influx-spout/convert"
"github.com/jumptrading/influx-spout/stats"
)

Expand Down Expand Up @@ -161,7 +161,7 @@ func extractTimestamp(line []byte, defaultTs int64) int64 {
// Expect a space just before the timestamp.
for i := length - maxTsLen - 1; i < length-minTsLen; i++ {
if line[i] == ' ' {
out, err := fastParseInt(line[i+1:])
out, err := convert.ToInt(line[i+1:])
if err != nil {
return defaultTs
}
Expand All @@ -170,32 +170,3 @@ func extractTimestamp(line []byte, defaultTs int64) int64 {
}
return defaultTs
}

const int64Max = (1 << 63) - 1

// fastParseInt is a simpler, faster version of strconv.ParseInt().
// Differences to ParseInt:
// - input is []byte instead of a string (no type conversion required
// by caller)
// - only supports base 10 input
// - only handles positive values
func fastParseInt(s []byte) (int64, error) {
if len(s) == 0 {
return 0, errors.New("empty")
}

var n uint64
for _, c := range s {
if '0' <= c && c <= '9' {
c -= '0'
} else {
return 0, errors.New("invalid char")
}
n = n*10 + uint64(c)
}

if n > int64Max {
return 0, errors.New("overflow")
}
return int64(n), nil
}
9 changes: 5 additions & 4 deletions monitor/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package monitor
import (
"bytes"
"errors"
"strconv"

"github.com/jumptrading/influx-spout/convert"
)

// Metric represents a single Prometheus metric line, including its
// labels and timestamp.
type Metric struct {
Name []byte
Labels LabelPairs
Value int
Value int64
Milliseconds int64
}

Expand Down Expand Up @@ -60,14 +61,14 @@ func ParseMetric(s []byte) (*Metric, error) {
if j == -1 {
j = len(s[i:]) // No timestamp
}
out.Value, err = strconv.Atoi(string(s[i : i+j]))
out.Value, err = convert.ToInt(s[i : i+j])
if err != nil {
return nil, errors.New("invalid value")
}

i += j
if i < len(s) {
out.Milliseconds, err = strconv.ParseInt(string(s[i+1:]), 10, 64)
out.Milliseconds, err = convert.ToInt(s[i+1:])
if err != nil {
return nil, errors.New("invalid timestamp")
}
Expand Down

0 comments on commit 8c088e7

Please sign in to comment.