/
bytes.go
136 lines (119 loc) · 3.37 KB
/
bytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2021 Matrix Origin
//
// 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 types
import (
"bytes"
"unsafe"
"github.com/matrixorigin/matrixone/pkg/common/mpool"
"github.com/matrixorigin/matrixone/pkg/common/util"
)
const (
VarlenaInlineSize = 23
VarlenaSize = 24
MaxStringSize = 10485760
VarlenaBigHdr = 0xffffffff
MaxVarcharLen = 65535
MaxCharLen = 255
MaxBinaryLen = 255
MaxVarBinaryLen = 65535
MaxEnumLen = 65535
)
func (v *Varlena) UnsafePtr() unsafe.Pointer {
return unsafe.Pointer(&v[0])
}
func (v *Varlena) ByteSlice() []byte {
svlen := (*v)[0]
ptr := unsafe.Add(unsafe.Pointer(&v[0]), 1)
return unsafe.Slice((*byte)(ptr), svlen)
}
func (v *Varlena) U32Slice() []uint32 {
ptr := (*uint32)(v.UnsafePtr())
return unsafe.Slice(ptr, 6)
}
func (v *Varlena) OffsetLen() (uint32, uint32) {
s := v.U32Slice()
return s[1], s[2]
}
func (v *Varlena) SetOffsetLen(voff, vlen uint32) {
s := v.U32Slice()
s[0] = VarlenaBigHdr
s[1] = voff
s[2] = vlen
}
// do not use this function, will be deleted in the future
// use BuildVarlenaFromValena or BuildVarlenaFromByteSlice instead
func BuildVarlena(bs []byte, area []byte, m *mpool.MPool) (Varlena, []byte, error) {
var err error
var v Varlena
vlen := len(bs)
if vlen <= VarlenaInlineSize {
v[0] = byte(vlen)
copy(v[1:1+vlen], bs)
return v, area, nil
} else {
voff := len(area)
if voff+vlen < cap(area) || m == nil {
area = append(area, bs...)
} else {
area, err = m.Grow2(area, bs, voff+vlen)
if err != nil {
return v, nil, err
}
}
v.SetOffsetLen(uint32(voff), uint32(vlen))
return v, area, nil
}
}
func (v *Varlena) IsSmall() bool {
return (*v)[0] <= VarlenaInlineSize
}
// For short slice, this one will return a slice stored internal
// in the varlena. Caller must ensure that v has a longer life
// span than the returned byte slice.
//
// Main user of Varlena is vector. v that comes from vector.Data
// will be fine.
func (v *Varlena) GetByteSlice(area []byte) []byte {
svlen := (*v)[0]
if svlen <= VarlenaInlineSize {
return v.ByteSlice()
}
voff, vlen := v.OffsetLen()
return area[voff : voff+vlen]
}
// GetArray Returns []T from Varlena. If the Varlena size is less than Inline size,
// it returns the value from the Varlena header.
// Else, it returns the value from the area.
func GetArray[T RealNumbers](v *Varlena, area []byte) []T {
svlen := (*v)[0]
if svlen <= VarlenaInlineSize {
return BytesToArray[T](v.ByteSlice())
}
voff, vlen := v.OffsetLen()
return BytesToArray[T](area[voff : voff+vlen])
}
// See the lifespan comment above.
func (v *Varlena) GetString(area []byte) string {
return util.UnsafeBytesToString(v.GetByteSlice(area))
}
func (v *Varlena) Reset() {
var vzero Varlena
*v = vzero
}
func PrefixCompare(lhs, rhs []byte) int {
if len(lhs) > len(rhs) {
lhs = lhs[:len(rhs)]
}
return bytes.Compare(lhs, rhs)
}