2016-05-17 23:55:50 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// 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 keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2016-10-14 06:14:34 +00:00
|
|
|
"math"
|
|
|
|
"reflect"
|
2016-05-17 23:55:50 +00:00
|
|
|
"time"
|
2016-10-14 06:14:34 +00:00
|
|
|
"unsafe"
|
2016-05-17 23:55:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type writer struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWriter(w io.Writer) *writer {
|
|
|
|
return &writer{
|
|
|
|
w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
func (w *writer) write(i interface{}) {
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
switch v := i.(type) {
|
2016-09-09 23:21:58 +00:00
|
|
|
|
2016-05-17 23:55:50 +00:00
|
|
|
case []byte:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.Write(v)
|
2016-09-09 23:21:58 +00:00
|
|
|
|
2016-05-17 23:55:50 +00:00
|
|
|
case string:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeStr(v)
|
2016-09-09 23:21:58 +00:00
|
|
|
|
|
|
|
case time.Time:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeTme(v)
|
2016-09-09 23:21:58 +00:00
|
|
|
|
2016-05-17 23:55:50 +00:00
|
|
|
case uint:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case uint8:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case uint16:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case uint32:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case uint64:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-09-09 23:21:58 +00:00
|
|
|
|
2016-05-17 23:55:50 +00:00
|
|
|
case int:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case int8:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case int16:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case int32:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case int64:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-09-09 23:21:58 +00:00
|
|
|
|
2016-05-17 23:55:50 +00:00
|
|
|
case float32:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
case float64:
|
2016-10-14 06:52:00 +00:00
|
|
|
w.writeNum(float64(v))
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-10-14 06:14:34 +00:00
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
func (w *writer) writeStr(v string) {
|
2016-10-14 06:14:34 +00:00
|
|
|
b := *(*[]byte)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&v))))
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
func (w *writer) writeTme(v time.Time) {
|
2016-10-14 06:14:34 +00:00
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(b, uint64(v.UTC().UnixNano()))
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
func (w *writer) writeNum(v float64) {
|
2016-10-14 06:14:34 +00:00
|
|
|
b := make([]byte, 8)
|
|
|
|
if v < 0 {
|
|
|
|
w.Write(bNEG)
|
|
|
|
binary.BigEndian.PutUint64(b, ^math.Float64bits(v))
|
|
|
|
} else {
|
|
|
|
w.Write(bPOS)
|
|
|
|
binary.BigEndian.PutUint64(b, math.Float64bits(v))
|
|
|
|
}
|
|
|
|
w.Write(b)
|
|
|
|
}
|