forked from forgejo/forgejo
Integrate public as bindata optionally (#293)
* Dropped unused codekit config * Integrated dynamic and static bindata for public * Ignore public bindata * Add a general generate make task * Integrated flexible public assets into web command * Updated vendoring, added all missiong govendor deps * Made the linter happy with the bindata and dynamic code * Moved public bindata definition to modules directory * Ignoring the new bindata path now * Updated to the new public modules import path * Updated public bindata command and drop the new prefix
This commit is contained in:
parent
4680c349dd
commit
b6a95a8cb3
691 changed files with 305318 additions and 1272 deletions
197
vendor/github.com/pingcap/tidb/util/codec/bytes.go
generated
vendored
Normal file
197
vendor/github.com/pingcap/tidb/util/codec/bytes.go
generated
vendored
Normal file
|
@ -0,0 +1,197 @@
|
|||
// Copyright 2015 PingCAP, 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package codec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
encGroupSize = 8
|
||||
encMarker = byte(0xFF)
|
||||
encPad = byte(0x0)
|
||||
)
|
||||
|
||||
var (
|
||||
pads = make([]byte, encGroupSize)
|
||||
encPads = []byte{encPad}
|
||||
)
|
||||
|
||||
// EncodeBytes guarantees the encoded value is in ascending order for comparison,
|
||||
// encoding with the following rule:
|
||||
// [group1][marker1]...[groupN][markerN]
|
||||
// group is 8 bytes slice which is padding with 0.
|
||||
// marker is `0xFF - padding 0 count`
|
||||
// For example:
|
||||
// [] -> [0, 0, 0, 0, 0, 0, 0, 0, 247]
|
||||
// [1, 2, 3] -> [1, 2, 3, 0, 0, 0, 0, 0, 250]
|
||||
// [1, 2, 3, 0] -> [1, 2, 3, 0, 0, 0, 0, 0, 251]
|
||||
// [1, 2, 3, 4, 5, 6, 7, 8] -> [1, 2, 3, 4, 5, 6, 7, 8, 255, 0, 0, 0, 0, 0, 0, 0, 0, 247]
|
||||
// Refer: https://github.com/facebook/mysql-5.6/wiki/MyRocks-record-format#memcomparable-format
|
||||
func EncodeBytes(b []byte, data []byte) []byte {
|
||||
// Allocate more space to avoid unnecessary slice growing.
|
||||
// Assume that the byte slice size is about `(len(data) / encGroupSize + 1) * (encGroupSize + 1)` bytes,
|
||||
// that is `(len(data) / 8 + 1) * 9` in our implement.
|
||||
dLen := len(data)
|
||||
reallocSize := (dLen/encGroupSize + 1) * (encGroupSize + 1)
|
||||
result := reallocBytes(b, reallocSize)
|
||||
for idx := 0; idx <= dLen; idx += encGroupSize {
|
||||
remain := dLen - idx
|
||||
padCount := 0
|
||||
if remain >= encGroupSize {
|
||||
result = append(result, data[idx:idx+encGroupSize]...)
|
||||
} else {
|
||||
padCount = encGroupSize - remain
|
||||
result = append(result, data[idx:]...)
|
||||
result = append(result, pads[:padCount]...)
|
||||
}
|
||||
|
||||
marker := encMarker - byte(padCount)
|
||||
result = append(result, marker)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func decodeBytes(b []byte, reverse bool) ([]byte, []byte, error) {
|
||||
data := make([]byte, 0, len(b))
|
||||
for {
|
||||
if len(b) < encGroupSize+1 {
|
||||
return nil, nil, errors.New("insufficient bytes to decode value")
|
||||
}
|
||||
|
||||
groupBytes := b[:encGroupSize+1]
|
||||
if reverse {
|
||||
reverseBytes(groupBytes)
|
||||
}
|
||||
|
||||
group := groupBytes[:encGroupSize]
|
||||
marker := groupBytes[encGroupSize]
|
||||
|
||||
// Check validity of marker.
|
||||
padCount := encMarker - marker
|
||||
realGroupSize := encGroupSize - padCount
|
||||
if padCount > encGroupSize {
|
||||
return nil, nil, errors.Errorf("invalid marker byte, group bytes %q", groupBytes)
|
||||
}
|
||||
|
||||
data = append(data, group[:realGroupSize]...)
|
||||
b = b[encGroupSize+1:]
|
||||
|
||||
if marker != encMarker {
|
||||
// Check validity of padding bytes.
|
||||
if bytes.Count(group[realGroupSize:], encPads) != int(padCount) {
|
||||
return nil, nil, errors.Errorf("invalid padding byte, group bytes %q", groupBytes)
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return b, data, nil
|
||||
}
|
||||
|
||||
// DecodeBytes decodes bytes which is encoded by EncodeBytes before,
|
||||
// returns the leftover bytes and decoded value if no error.
|
||||
func DecodeBytes(b []byte) ([]byte, []byte, error) {
|
||||
return decodeBytes(b, false)
|
||||
}
|
||||
|
||||
// EncodeBytesDesc first encodes bytes using EncodeBytes, then bitwise reverses
|
||||
// encoded value to guarantee the encoded value is in descending order for comparison.
|
||||
func EncodeBytesDesc(b []byte, data []byte) []byte {
|
||||
n := len(b)
|
||||
b = EncodeBytes(b, data)
|
||||
reverseBytes(b[n:])
|
||||
return b
|
||||
}
|
||||
|
||||
// DecodeBytesDesc decodes bytes which is encoded by EncodeBytesDesc before,
|
||||
// returns the leftover bytes and decoded value if no error.
|
||||
func DecodeBytesDesc(b []byte) ([]byte, []byte, error) {
|
||||
return decodeBytes(b, true)
|
||||
}
|
||||
|
||||
// EncodeCompactBytes joins bytes with its length into a byte slice. It is more
|
||||
// efficient in both space and time compare to EncodeBytes. Note that the encoded
|
||||
// result is not memcomparable.
|
||||
func EncodeCompactBytes(b []byte, data []byte) []byte {
|
||||
b = reallocBytes(b, binary.MaxVarintLen64+len(data))
|
||||
b = EncodeVarint(b, int64(len(data)))
|
||||
return append(b, data...)
|
||||
}
|
||||
|
||||
// DecodeCompactBytes decodes bytes which is encoded by EncodeCompactBytes before.
|
||||
func DecodeCompactBytes(b []byte) ([]byte, []byte, error) {
|
||||
b, n, err := DecodeVarint(b)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Trace(err)
|
||||
}
|
||||
if int64(len(b)) < n {
|
||||
return nil, nil, errors.Errorf("insufficient bytes to decode value, expected length: %v", n)
|
||||
}
|
||||
return b[n:], b[:n], nil
|
||||
}
|
||||
|
||||
// See https://golang.org/src/crypto/cipher/xor.go
|
||||
const wordSize = int(unsafe.Sizeof(uintptr(0)))
|
||||
const supportsUnaligned = runtime.GOARCH == "386" || runtime.GOARCH == "amd64"
|
||||
|
||||
func fastReverseBytes(b []byte) {
|
||||
n := len(b)
|
||||
w := n / wordSize
|
||||
if w > 0 {
|
||||
bw := *(*[]uintptr)(unsafe.Pointer(&b))
|
||||
for i := 0; i < w; i++ {
|
||||
bw[i] = ^bw[i]
|
||||
}
|
||||
}
|
||||
|
||||
for i := w * wordSize; i < n; i++ {
|
||||
b[i] = ^b[i]
|
||||
}
|
||||
}
|
||||
|
||||
func safeReverseBytes(b []byte) {
|
||||
for i := range b {
|
||||
b[i] = ^b[i]
|
||||
}
|
||||
}
|
||||
|
||||
func reverseBytes(b []byte) {
|
||||
if supportsUnaligned {
|
||||
fastReverseBytes(b)
|
||||
return
|
||||
}
|
||||
|
||||
safeReverseBytes(b)
|
||||
}
|
||||
|
||||
// like realloc.
|
||||
func reallocBytes(b []byte, n int) []byte {
|
||||
newSize := len(b) + n
|
||||
if cap(b) < newSize {
|
||||
bs := make([]byte, len(b), newSize)
|
||||
copy(bs, b)
|
||||
return bs
|
||||
}
|
||||
|
||||
// slice b has capability to store n bytes
|
||||
return b
|
||||
}
|
165
vendor/github.com/pingcap/tidb/util/codec/codec.go
generated
vendored
Normal file
165
vendor/github.com/pingcap/tidb/util/codec/codec.go
generated
vendored
Normal file
|
@ -0,0 +1,165 @@
|
|||
// Copyright 2015 PingCAP, 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package codec
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"github.com/pingcap/tidb/mysql"
|
||||
"github.com/pingcap/tidb/util/types"
|
||||
)
|
||||
|
||||
const (
|
||||
nilFlag byte = iota
|
||||
bytesFlag
|
||||
compactBytesFlag
|
||||
intFlag
|
||||
uintFlag
|
||||
floatFlag
|
||||
decimalFlag
|
||||
durationFlag
|
||||
)
|
||||
|
||||
func encode(b []byte, vals []types.Datum, comparable bool) ([]byte, error) {
|
||||
for _, val := range vals {
|
||||
switch val.Kind() {
|
||||
case types.KindInt64:
|
||||
b = append(b, intFlag)
|
||||
b = EncodeInt(b, val.GetInt64())
|
||||
case types.KindUint64:
|
||||
b = append(b, uintFlag)
|
||||
b = EncodeUint(b, val.GetUint64())
|
||||
case types.KindFloat32, types.KindFloat64:
|
||||
b = append(b, floatFlag)
|
||||
b = EncodeFloat(b, val.GetFloat64())
|
||||
case types.KindString, types.KindBytes:
|
||||
b = encodeBytes(b, val.GetBytes(), comparable)
|
||||
case types.KindMysqlTime:
|
||||
b = encodeBytes(b, []byte(val.GetMysqlTime().String()), comparable)
|
||||
case types.KindMysqlDuration:
|
||||
// duration may have negative value, so we cannot use String to encode directly.
|
||||
b = append(b, durationFlag)
|
||||
b = EncodeInt(b, int64(val.GetMysqlDuration().Duration))
|
||||
case types.KindMysqlDecimal:
|
||||
b = append(b, decimalFlag)
|
||||
b = EncodeDecimal(b, val.GetMysqlDecimal())
|
||||
case types.KindMysqlHex:
|
||||
b = append(b, intFlag)
|
||||
b = EncodeInt(b, int64(val.GetMysqlHex().ToNumber()))
|
||||
case types.KindMysqlBit:
|
||||
b = append(b, uintFlag)
|
||||
b = EncodeUint(b, uint64(val.GetMysqlBit().ToNumber()))
|
||||
case types.KindMysqlEnum:
|
||||
b = append(b, uintFlag)
|
||||
b = EncodeUint(b, uint64(val.GetMysqlEnum().ToNumber()))
|
||||
case types.KindMysqlSet:
|
||||
b = append(b, uintFlag)
|
||||
b = EncodeUint(b, uint64(val.GetMysqlSet().ToNumber()))
|
||||
case types.KindNull:
|
||||
b = append(b, nilFlag)
|
||||
default:
|
||||
return nil, errors.Errorf("unsupport encode type %d", val.Kind())
|
||||
}
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func encodeBytes(b []byte, v []byte, comparable bool) []byte {
|
||||
if comparable {
|
||||
b = append(b, bytesFlag)
|
||||
b = EncodeBytes(b, v)
|
||||
} else {
|
||||
b = append(b, compactBytesFlag)
|
||||
b = EncodeCompactBytes(b, v)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// EncodeKey appends the encoded values to byte slice b, returns the appended
|
||||
// slice. It guarantees the encoded value is in ascending order for comparison.
|
||||
func EncodeKey(b []byte, v ...types.Datum) ([]byte, error) {
|
||||
return encode(b, v, true)
|
||||
}
|
||||
|
||||
// EncodeValue appends the encoded values to byte slice b, returning the appended
|
||||
// slice. It does not guarantee the order for comparison.
|
||||
func EncodeValue(b []byte, v ...types.Datum) ([]byte, error) {
|
||||
return encode(b, v, false)
|
||||
}
|
||||
|
||||
// Decode decodes values from a byte slice generated with EncodeKey or EncodeValue
|
||||
// before.
|
||||
func Decode(b []byte) ([]types.Datum, error) {
|
||||
if len(b) < 1 {
|
||||
return nil, errors.New("invalid encoded key")
|
||||
}
|
||||
|
||||
var (
|
||||
flag byte
|
||||
err error
|
||||
values = make([]types.Datum, 0, 1)
|
||||
)
|
||||
|
||||
for len(b) > 0 {
|
||||
flag = b[0]
|
||||
b = b[1:]
|
||||
var d types.Datum
|
||||
switch flag {
|
||||
case intFlag:
|
||||
var v int64
|
||||
b, v, err = DecodeInt(b)
|
||||
d.SetInt64(v)
|
||||
case uintFlag:
|
||||
var v uint64
|
||||
b, v, err = DecodeUint(b)
|
||||
d.SetUint64(v)
|
||||
case floatFlag:
|
||||
var v float64
|
||||
b, v, err = DecodeFloat(b)
|
||||
d.SetFloat64(v)
|
||||
case bytesFlag:
|
||||
var v []byte
|
||||
b, v, err = DecodeBytes(b)
|
||||
d.SetBytes(v)
|
||||
case compactBytesFlag:
|
||||
var v []byte
|
||||
b, v, err = DecodeCompactBytes(b)
|
||||
d.SetBytes(v)
|
||||
case decimalFlag:
|
||||
var v mysql.Decimal
|
||||
b, v, err = DecodeDecimal(b)
|
||||
d.SetValue(v)
|
||||
case durationFlag:
|
||||
var r int64
|
||||
b, r, err = DecodeInt(b)
|
||||
if err == nil {
|
||||
// use max fsp, let outer to do round manually.
|
||||
v := mysql.Duration{Duration: time.Duration(r), Fsp: mysql.MaxFsp}
|
||||
d.SetValue(v)
|
||||
}
|
||||
case nilFlag:
|
||||
default:
|
||||
return nil, errors.Errorf("invalid encoded key flag %v", flag)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
|
||||
values = append(values, d)
|
||||
}
|
||||
|
||||
return values, nil
|
||||
}
|
183
vendor/github.com/pingcap/tidb/util/codec/decimal.go
generated
vendored
Normal file
183
vendor/github.com/pingcap/tidb/util/codec/decimal.go
generated
vendored
Normal file
|
@ -0,0 +1,183 @@
|
|||
// Copyright 2015 PingCAP, 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package codec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/big"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"github.com/pingcap/tidb/mysql"
|
||||
)
|
||||
|
||||
const (
|
||||
negativeSign int64 = 8
|
||||
zeroSign int64 = 16
|
||||
positiveSign int64 = 24
|
||||
)
|
||||
|
||||
func codecSign(value int64) int64 {
|
||||
if value < 0 {
|
||||
return negativeSign
|
||||
}
|
||||
|
||||
return positiveSign
|
||||
}
|
||||
|
||||
func encodeExp(expValue int64, expSign int64, valSign int64) int64 {
|
||||
if expSign == negativeSign {
|
||||
expValue = -expValue
|
||||
}
|
||||
|
||||
if expSign != valSign {
|
||||
expValue = ^expValue
|
||||
}
|
||||
|
||||
return expValue
|
||||
}
|
||||
|
||||
func decodeExp(expValue int64, expSign int64, valSign int64) int64 {
|
||||
if expSign != valSign {
|
||||
expValue = ^expValue
|
||||
}
|
||||
|
||||
if expSign == negativeSign {
|
||||
expValue = -expValue
|
||||
}
|
||||
|
||||
return expValue
|
||||
}
|
||||
|
||||
func codecValue(value []byte, valSign int64) {
|
||||
if valSign == negativeSign {
|
||||
reverseBytes(value)
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeDecimal encodes a decimal d into a byte slice which can be sorted lexicographically later.
|
||||
// EncodeDecimal guarantees that the encoded value is in ascending order for comparison.
|
||||
// Decimal encoding:
|
||||
// Byte -> value sign
|
||||
// Byte -> exp sign
|
||||
// EncodeInt -> exp value
|
||||
// EncodeBytes -> abs value bytes
|
||||
func EncodeDecimal(b []byte, d mysql.Decimal) []byte {
|
||||
if d.Equals(mysql.ZeroDecimal) {
|
||||
return append(b, byte(zeroSign))
|
||||
}
|
||||
|
||||
v := d.BigIntValue()
|
||||
valSign := codecSign(int64(v.Sign()))
|
||||
|
||||
absVal := new(big.Int)
|
||||
absVal.Abs(v)
|
||||
|
||||
value := []byte(absVal.String())
|
||||
|
||||
// Trim right side "0", like "12.34000" -> "12.34" or "0.1234000" -> "0.1234".
|
||||
if d.Exponent() != 0 {
|
||||
value = bytes.TrimRight(value, "0")
|
||||
}
|
||||
|
||||
// Get exp and value, format is "value":"exp".
|
||||
// like "12.34" -> "0.1234":"2".
|
||||
// like "-0.01234" -> "-0.1234":"-1".
|
||||
exp := int64(0)
|
||||
div := big.NewInt(10)
|
||||
for ; ; exp++ {
|
||||
if absVal.Sign() == 0 {
|
||||
break
|
||||
}
|
||||
absVal = absVal.Div(absVal, div)
|
||||
}
|
||||
|
||||
expVal := exp + int64(d.Exponent())
|
||||
expSign := codecSign(expVal)
|
||||
|
||||
// For negtive exp, do bit reverse for exp.
|
||||
// For negtive decimal, do bit reverse for exp and value.
|
||||
expVal = encodeExp(expVal, expSign, valSign)
|
||||
codecValue(value, valSign)
|
||||
|
||||
b = append(b, byte(valSign))
|
||||
b = append(b, byte(expSign))
|
||||
b = EncodeInt(b, expVal)
|
||||
b = EncodeBytes(b, value)
|
||||
return b
|
||||
}
|
||||
|
||||
// DecodeDecimal decodes bytes to decimal.
|
||||
// DecodeFloat decodes a float from a byte slice
|
||||
// Decimal decoding:
|
||||
// Byte -> value sign
|
||||
// Byte -> exp sign
|
||||
// DecodeInt -> exp value
|
||||
// DecodeBytes -> abs value bytes
|
||||
func DecodeDecimal(b []byte) ([]byte, mysql.Decimal, error) {
|
||||
var (
|
||||
r = b
|
||||
d mysql.Decimal
|
||||
err error
|
||||
)
|
||||
|
||||
// Decode value sign.
|
||||
valSign := int64(r[0])
|
||||
r = r[1:]
|
||||
if valSign == zeroSign {
|
||||
d, err = mysql.ParseDecimal("0")
|
||||
return r, d, errors.Trace(err)
|
||||
}
|
||||
|
||||
// Decode exp sign.
|
||||
expSign := int64(r[0])
|
||||
r = r[1:]
|
||||
|
||||
// Decode exp value.
|
||||
expVal := int64(0)
|
||||
r, expVal, err = DecodeInt(r)
|
||||
if err != nil {
|
||||
return r, d, errors.Trace(err)
|
||||
}
|
||||
expVal = decodeExp(expVal, expSign, valSign)
|
||||
|
||||
// Decode abs value bytes.
|
||||
value := []byte{}
|
||||
r, value, err = DecodeBytes(r)
|
||||
if err != nil {
|
||||
return r, d, errors.Trace(err)
|
||||
}
|
||||
codecValue(value, valSign)
|
||||
|
||||
// Generate decimal string value.
|
||||
var decimalStr []byte
|
||||
if valSign == negativeSign {
|
||||
decimalStr = append(decimalStr, '-')
|
||||
}
|
||||
|
||||
if expVal <= 0 {
|
||||
// Like decimal "0.1234" or "0.01234".
|
||||
decimalStr = append(decimalStr, '0')
|
||||
decimalStr = append(decimalStr, '.')
|
||||
decimalStr = append(decimalStr, bytes.Repeat([]byte{'0'}, -int(expVal))...)
|
||||
decimalStr = append(decimalStr, value...)
|
||||
} else {
|
||||
// Like decimal "12.34".
|
||||
decimalStr = append(decimalStr, value[:expVal]...)
|
||||
decimalStr = append(decimalStr, '.')
|
||||
decimalStr = append(decimalStr, value[expVal:]...)
|
||||
}
|
||||
|
||||
d, err = mysql.ParseDecimal(string(decimalStr))
|
||||
return r, d, errors.Trace(err)
|
||||
}
|
65
vendor/github.com/pingcap/tidb/util/codec/float.go
generated
vendored
Normal file
65
vendor/github.com/pingcap/tidb/util/codec/float.go
generated
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
// Copyright 2015 PingCAP, 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package codec
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
func encodeFloatToCmpUint64(f float64) uint64 {
|
||||
u := math.Float64bits(f)
|
||||
if f >= 0 {
|
||||
u |= signMask
|
||||
} else {
|
||||
u = ^u
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func decodeCmpUintToFloat(u uint64) float64 {
|
||||
if u&signMask > 0 {
|
||||
u &= ^signMask
|
||||
} else {
|
||||
u = ^u
|
||||
}
|
||||
return math.Float64frombits(u)
|
||||
}
|
||||
|
||||
// EncodeFloat encodes a float v into a byte slice which can be sorted lexicographically later.
|
||||
// EncodeFloat guarantees that the encoded value is in ascending order for comparison.
|
||||
func EncodeFloat(b []byte, v float64) []byte {
|
||||
u := encodeFloatToCmpUint64(v)
|
||||
return EncodeUint(b, u)
|
||||
}
|
||||
|
||||
// DecodeFloat decodes a float from a byte slice generated with EncodeFloat before.
|
||||
func DecodeFloat(b []byte) ([]byte, float64, error) {
|
||||
b, u, err := DecodeUint(b)
|
||||
return b, decodeCmpUintToFloat(u), errors.Trace(err)
|
||||
}
|
||||
|
||||
// EncodeFloatDesc encodes a float v into a byte slice which can be sorted lexicographically later.
|
||||
// EncodeFloatDesc guarantees that the encoded value is in descending order for comparison.
|
||||
func EncodeFloatDesc(b []byte, v float64) []byte {
|
||||
u := encodeFloatToCmpUint64(v)
|
||||
return EncodeUintDesc(b, u)
|
||||
}
|
||||
|
||||
// DecodeFloatDesc decodes a float from a byte slice generated with EncodeFloatDesc before.
|
||||
func DecodeFloatDesc(b []byte) ([]byte, float64, error) {
|
||||
b, u, err := DecodeUintDesc(b)
|
||||
return b, decodeCmpUintToFloat(u), errors.Trace(err)
|
||||
}
|
170
vendor/github.com/pingcap/tidb/util/codec/number.go
generated
vendored
Normal file
170
vendor/github.com/pingcap/tidb/util/codec/number.go
generated
vendored
Normal file
|
@ -0,0 +1,170 @@
|
|||
// Copyright 2015 PingCAP, 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,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package codec
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
const signMask uint64 = 0x8000000000000000
|
||||
|
||||
func encodeIntToCmpUint(v int64) uint64 {
|
||||
u := uint64(v)
|
||||
if u&signMask > 0 {
|
||||
u &= ^signMask
|
||||
} else {
|
||||
u |= signMask
|
||||
}
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
func decodeCmpUintToInt(u uint64) int64 {
|
||||
if u&signMask > 0 {
|
||||
u &= ^signMask
|
||||
} else {
|
||||
u |= signMask
|
||||
}
|
||||
|
||||
return int64(u)
|
||||
}
|
||||
|
||||
// EncodeInt appends the encoded value to slice b and returns the appended slice.
|
||||
// EncodeInt guarantees that the encoded value is in ascending order for comparison.
|
||||
func EncodeInt(b []byte, v int64) []byte {
|
||||
var data [8]byte
|
||||
u := encodeIntToCmpUint(v)
|
||||
binary.BigEndian.PutUint64(data[:], u)
|
||||
return append(b, data[:]...)
|
||||
}
|
||||
|
||||
// EncodeIntDesc appends the encoded value to slice b and returns the appended slice.
|
||||
// EncodeIntDesc guarantees that the encoded value is in descending order for comparison.
|
||||
func EncodeIntDesc(b []byte, v int64) []byte {
|
||||
var data [8]byte
|
||||
u := encodeIntToCmpUint(v)
|
||||
binary.BigEndian.PutUint64(data[:], ^u)
|
||||
return append(b, data[:]...)
|
||||
}
|
||||
|
||||
// DecodeInt decodes value encoded by EncodeInt before.
|
||||
// It returns the leftover un-decoded slice, decoded value if no error.
|
||||
func DecodeInt(b []byte) ([]byte, int64, error) {
|
||||
if len(b) < 8 {
|
||||
return nil, 0, errors.New("insufficient bytes to decode value")
|
||||
}
|
||||
|
||||
u := binary.BigEndian.Uint64(b[:8])
|
||||
v := decodeCmpUintToInt(u)
|
||||
b = b[8:]
|
||||
return b, v, nil
|
||||
}
|
||||
|
||||
// DecodeIntDesc decodes value encoded by EncodeInt before.
|
||||
// It returns the leftover un-decoded slice, decoded value if no error.
|
||||
func DecodeIntDesc(b []byte) ([]byte, int64, error) {
|
||||
if len(b) < 8 {
|
||||
return nil, 0, errors.New("insufficient bytes to decode value")
|
||||
}
|
||||
|
||||
u := binary.BigEndian.Uint64(b[:8])
|
||||
v := decodeCmpUintToInt(^u)
|
||||
b = b[8:]
|
||||
return b, v, nil
|
||||
}
|
||||
|
||||
// EncodeUint appends the encoded value to slice b and returns the appended slice.
|
||||
// EncodeUint guarantees that the encoded value is in ascending order for comparison.
|
||||
func EncodeUint(b []byte, v uint64) []byte {
|
||||
var data [8]byte
|
||||
binary.BigEndian.PutUint64(data[:], v)
|
||||
return append(b, data[:]...)
|
||||
}
|
||||
|
||||
// EncodeUintDesc appends the encoded value to slice b and returns the appended slice.
|
||||
// EncodeUintDesc guarantees that the encoded value is in descending order for comparison.
|
||||
func EncodeUintDesc(b []byte, v uint64) []byte {
|
||||
var data [8]byte
|
||||
binary.BigEndian.PutUint64(data[:], ^v)
|
||||
return append(b, data[:]...)
|
||||
}
|
||||
|
||||
// DecodeUint decodes value encoded by EncodeUint before.
|
||||
// It returns the leftover un-decoded slice, decoded value if no error.
|
||||
func DecodeUint(b []byte) ([]byte, uint64, error) {
|
||||
if len(b) < 8 {
|
||||
return nil, 0, errors.New("insufficient bytes to decode value")
|
||||
}
|
||||
|
||||
v := binary.BigEndian.Uint64(b[:8])
|
||||
b = b[8:]
|
||||
return b, v, nil
|
||||
}
|
||||
|
||||
// DecodeUintDesc decodes value encoded by EncodeInt before.
|
||||
// It returns the leftover un-decoded slice, decoded value if no error.
|
||||
func DecodeUintDesc(b []byte) ([]byte, uint64, error) {
|
||||
if len(b) < 8 {
|
||||
return nil, 0, errors.New("insufficient bytes to decode value")
|
||||
}
|
||||
|
||||
data := b[:8]
|
||||
v := binary.BigEndian.Uint64(data)
|
||||
b = b[8:]
|
||||
return b, ^v, nil
|
||||
}
|
||||
|
||||
// EncodeVarint appends the encoded value to slice b and returns the appended slice.
|
||||
// Note that the encoded result is not memcomparable.
|
||||
func EncodeVarint(b []byte, v int64) []byte {
|
||||
var data [binary.MaxVarintLen64]byte
|
||||
n := binary.PutVarint(data[:], v)
|
||||
return append(b, data[:n]...)
|
||||
}
|
||||
|
||||
// DecodeVarint decodes value encoded by EncodeVarint before.
|
||||
// It returns the leftover un-decoded slice, decoded value if no error.
|
||||
func DecodeVarint(b []byte) ([]byte, int64, error) {
|
||||
v, n := binary.Varint(b)
|
||||
if n > 0 {
|
||||
return b[n:], v, nil
|
||||
}
|
||||
if n < 0 {
|
||||
return nil, 0, errors.New("value larger than 64 bits")
|
||||
}
|
||||
return nil, 0, errors.New("insufficient bytes to decode value")
|
||||
}
|
||||
|
||||
// EncodeUvarint appends the encoded value to slice b and returns the appended slice.
|
||||
// Note that the encoded result is not memcomparable.
|
||||
func EncodeUvarint(b []byte, v uint64) []byte {
|
||||
var data [binary.MaxVarintLen64]byte
|
||||
n := binary.PutUvarint(data[:], v)
|
||||
return append(b, data[:n]...)
|
||||
}
|
||||
|
||||
// DecodeUvarint decodes value encoded by EncodeUvarint before.
|
||||
// It returns the leftover un-decoded slice, decoded value if no error.
|
||||
func DecodeUvarint(b []byte) ([]byte, uint64, error) {
|
||||
v, n := binary.Uvarint(b)
|
||||
if n > 0 {
|
||||
return b[n:], v, nil
|
||||
}
|
||||
if n < 0 {
|
||||
return nil, 0, errors.New("value larger than 64 bits")
|
||||
}
|
||||
return nil, 0, errors.New("insufficient bytes to decode value")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue