This commit is contained in:
Brian Hendriks
2022-08-23 11:40:27 -07:00
parent 33320070f2
commit 30722658b3
3 changed files with 65 additions and 45 deletions

View File

@@ -15,11 +15,10 @@
package sqlserver
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/dolthub/dolt/go/libraries/utils/version"
"github.com/dolthub/go-mysql-server/server"
"github.com/prometheus/client_golang/prometheus"
)
@@ -70,7 +69,7 @@ func newMetricsListener(labels prometheus.Labels, versionStr string) (*metricsLi
}),
}
version, err := encodeVersion(versionStr)
version, err := version.Encode(versionStr)
if err != nil {
return nil, err
}
@@ -114,40 +113,3 @@ func (ml *metricsListener) Close() {
prometheus.Unregister(ml.gaugeConcurrentQueries)
prometheus.Unregister(ml.histQueryDur)
}
func encodeVersion(version string) (float64, error) {
parts := strings.Split(version, ".")
if len(parts) != 3 {
return 0, fmt.Errorf("version '%s' is not in the format X.X.X", version)
}
partVals := make([]uint64, 3)
for i := 0; i < 3; i++ {
var err error
partVals[i], err = strconv.ParseUint(parts[i], 10, 32)
if err != nil {
return 0, fmt.Errorf("failed to parse version '%s'. error at '%s': %w", version, parts[i], err)
}
}
if partVals[0] > 255 || partVals[1] > 255 || partVals[2] > 65535 {
return 0, fmt.Errorf("version '%s' cannot be encoded with 8 bits for major, 8 bits for minor, 16 bits for build", version)
}
versionUint32 := (uint32(partVals[0]&0xFF) << 24) | (uint32(partVals[1]&0xFF) << 16) | uint32(partVals[2]&0xFFFF)
return float64(versionUint32), nil
}
func decodeVersion(version float64) string {
versInt32 := uint32(version)
major := (versInt32 & 0xFF000000) >> 24
minor := (versInt32 & 0x00FF0000) >> 16
build := versInt32 & 0x0000FFFF
majorStr := strconv.FormatUint(uint64(major), 10)
minorStr := strconv.FormatUint(uint64(minor), 10)
buildStr := strconv.FormatUint(uint64(build), 10)
return majorStr + "." + minorStr + "." + buildStr
}

View File

@@ -0,0 +1,58 @@
// Copyright 2022 Dolthub, Inc.
//
// 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 version
import (
"fmt"
"strconv"
"strings"
)
func Encode(version string) (float64, error) {
parts := strings.Split(version, ".")
if len(parts) != 3 {
return 0, fmt.Errorf("version '%s' is not in the format X.X.X", version)
}
partVals := make([]uint64, 3)
for i := 0; i < 3; i++ {
var err error
partVals[i], err = strconv.ParseUint(parts[i], 10, 32)
if err != nil {
return 0, fmt.Errorf("failed to parse version '%s'. error at '%s': %w", version, parts[i], err)
}
}
if partVals[0] > 255 || partVals[1] > 255 || partVals[2] > 65535 {
return 0, fmt.Errorf("version '%s' cannot be encoded with 8 bits for major, 8 bits for minor, 16 bits for build", version)
}
versionUint32 := (uint32(partVals[0]&0xFF) << 24) | (uint32(partVals[1]&0xFF) << 16) | uint32(partVals[2]&0xFFFF)
return float64(versionUint32), nil
}
func Decode(version float64) string {
versInt32 := uint32(version)
major := (versInt32 & 0xFF000000) >> 24
minor := (versInt32 & 0x00FF0000) >> 16
build := versInt32 & 0x0000FFFF
majorStr := strconv.FormatUint(uint64(major), 10)
minorStr := strconv.FormatUint(uint64(minor), 10)
buildStr := strconv.FormatUint(uint64(build), 10)
return majorStr + "." + minorStr + "." + buildStr
}

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlserver
package version
import (
"testing"
@@ -30,10 +30,10 @@ func TestEncodeDecodeVersion(t *testing.T) {
for _, version := range versions {
t.Run(version, func(t *testing.T) {
encoded, err := encodeVersion(version)
encoded, err := Encode(version)
require.NoError(t, err)
decoded := decodeVersion(encoded)
decoded := Decode(encoded)
require.Equal(t, version, decoded)
})
}
@@ -54,7 +54,7 @@ func TestBadVersionEncodeFailure(t *testing.T) {
for _, version := range versions {
t.Run(version, func(t *testing.T) {
_, err := encodeVersion(version)
_, err := Encode(version)
require.Error(t, err)
})
}