2021-12-14 08:12:26 +00:00
|
|
|
// Copyright © 2016 SurrealDB Ltd.
|
2016-05-17 23:55:50 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2016-10-14 06:14:34 +00:00
|
|
|
"math"
|
2016-05-17 23:55:50 +00:00
|
|
|
"time"
|
2017-11-16 20:18:42 +00:00
|
|
|
|
2021-12-14 08:12:26 +00:00
|
|
|
"github.com/surrealdb/bump"
|
2016-05-17 23:55:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type writer struct {
|
2017-11-16 20:18:42 +00:00
|
|
|
w *bump.Writer
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:18:42 +00:00
|
|
|
func newWriter() *writer {
|
2016-05-17 23:55:50 +00:00
|
|
|
return &writer{
|
2017-11-16 20:18:42 +00:00
|
|
|
w: bump.NewWriter(nil),
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:18:42 +00:00
|
|
|
func (w *writer) writeOne(v byte) {
|
|
|
|
w.w.WriteByte(v)
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
2016-10-14 06:14:34 +00:00
|
|
|
|
2017-11-16 20:18:42 +00:00
|
|
|
func (w *writer) writeMany(v []byte) {
|
|
|
|
w.w.WriteBytes(v)
|
2016-10-14 06:14:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:18:42 +00:00
|
|
|
func (w *writer) writeTime(v time.Time) {
|
2016-10-14 06:14:34 +00:00
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(b, uint64(v.UTC().UnixNano()))
|
2017-11-16 20:18:42 +00:00
|
|
|
w.w.WriteBytes(b)
|
2016-10-14 06:14:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:18:42 +00:00
|
|
|
func (w *writer) writeFloat(v float64) {
|
2016-10-14 06:14:34 +00:00
|
|
|
b := make([]byte, 8)
|
|
|
|
if v < 0 {
|
2017-11-16 20:18:42 +00:00
|
|
|
w.w.WriteByte(bNEG)
|
2016-10-14 06:14:34 +00:00
|
|
|
binary.BigEndian.PutUint64(b, ^math.Float64bits(v))
|
|
|
|
} else {
|
2017-11-16 20:18:42 +00:00
|
|
|
w.w.WriteByte(bPOS)
|
2016-10-14 06:14:34 +00:00
|
|
|
binary.BigEndian.PutUint64(b, math.Float64bits(v))
|
|
|
|
}
|
2017-11-16 20:18:42 +00:00
|
|
|
w.w.WriteBytes(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) writeString(v string) {
|
2017-11-30 00:59:10 +00:00
|
|
|
w.w.WriteString(v)
|
2016-10-14 06:14:34 +00:00
|
|
|
}
|