Skip to content

Commit 4dfbb8f

Browse files
authored
feat: porting kernel record module to internal (#204)
`record.Record` is a kernel module and needs to be ported to the SDK. Signed-off-by: Young Xu <[email protected]>
1 parent 8eb2abd commit 4dfbb8f

15 files changed

+2299
-2
lines changed

.github/workflows/license_lint.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ jobs:
2323
runs-on: ubuntu-latest
2424
steps:
2525
- uses: actions/checkout@v4
26-
- name: skip generated files
27-
run: rm -rf proto/*.pb.go
26+
- name: skip generated files and dot folders
27+
run: rm -rf proto/*.pb.go && rm -rf .idea
2828
- name: licenselint
2929
uses: shoothzj/licenselint@main
3030
with:

internal/record/column_boolean.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 openGemini Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package record
16+
17+
func (cv *ColVal) AppendBooleanNull() {
18+
appendNull(cv)
19+
}
20+
21+
func (cv *ColVal) AppendBooleanNulls(count int) {
22+
appendNulls(cv, count)
23+
}
24+
25+
func (cv *ColVal) AppendBooleans(values ...bool) {
26+
appendValues(cv, values...)
27+
}
28+
29+
func (cv *ColVal) AppendBoolean(v bool) {
30+
appendValue(cv, v)
31+
}

internal/record/column_float.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 openGemini Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package record
16+
17+
func (cv *ColVal) AppendFloatNull() {
18+
appendNull(cv)
19+
}
20+
21+
func (cv *ColVal) AppendFloatNulls(count int) {
22+
appendNulls(cv, count)
23+
}
24+
25+
func (cv *ColVal) AppendFloats(values ...float64) {
26+
appendValues(cv, values...)
27+
}
28+
29+
func (cv *ColVal) AppendFloat(v float64) {
30+
appendValue(cv, v)
31+
}

internal/record/column_integer.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 openGemini Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package record
16+
17+
func (cv *ColVal) AppendIntegerNulls(count int) {
18+
appendNulls(cv, count)
19+
}
20+
21+
func (cv *ColVal) AppendIntegerNull() {
22+
appendNull(cv)
23+
}
24+
25+
func (cv *ColVal) AppendIntegers(values ...int64) {
26+
appendValues(cv, values...)
27+
}
28+
29+
func (cv *ColVal) AppendInteger(v int64) {
30+
appendValue(cv, v)
31+
}

internal/record/column_string.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2024 openGemini Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package record
16+
17+
func (cv *ColVal) AppendStringNulls(count int) {
18+
for i := 0; i < count; i++ {
19+
cv.AppendStringNull()
20+
}
21+
}
22+
23+
func (cv *ColVal) AppendStringNull() {
24+
cv.reserveOffset(1)
25+
cv.Offset[cv.Len] = uint32(len(cv.Val))
26+
cv.resetBitMap(cv.Len)
27+
cv.Len++
28+
cv.NilCount++
29+
}
30+
31+
func (cv *ColVal) AppendStrings(values ...string) {
32+
for _, v := range values {
33+
cv.AppendString(v)
34+
}
35+
}
36+
37+
func (cv *ColVal) AppendString(v string) {
38+
index := len(cv.Val)
39+
cv.reserveVal(len(v))
40+
copy(cv.Val[index:], v)
41+
cv.reserveOffset(1)
42+
cv.Offset[cv.Len] = uint32(index)
43+
cv.setBitMap(cv.Len)
44+
cv.Len++
45+
}

internal/record/column_util.go

+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
// Copyright 2024 openGemini Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package record
16+
17+
import (
18+
"unsafe"
19+
)
20+
21+
var (
22+
BitMask = [8]byte{1, 2, 4, 8, 16, 32, 64, 128}
23+
FlippedBitMask = [8]byte{254, 253, 251, 247, 239, 223, 191, 127}
24+
)
25+
26+
type ColVal struct {
27+
Val []byte
28+
Offset []uint32
29+
Bitmap []byte
30+
BitMapOffset int
31+
Len int
32+
NilCount int
33+
}
34+
35+
func (cv *ColVal) Init() {
36+
cv.Val = cv.Val[:0]
37+
cv.Offset = cv.Offset[:0]
38+
cv.Bitmap = cv.Bitmap[:0]
39+
cv.Len = 0
40+
cv.NilCount = 0
41+
cv.BitMapOffset = 0
42+
}
43+
44+
func (cv *ColVal) reserveOffset(size int) {
45+
offsetCap := cap(cv.Offset)
46+
offsetLen := len(cv.Offset)
47+
remain := offsetCap - offsetLen
48+
if delta := size - remain; delta > 0 {
49+
cv.Offset = append(cv.Offset[:offsetCap], make([]uint32, delta)...)
50+
}
51+
cv.Offset = cv.Offset[:offsetLen+size]
52+
}
53+
54+
func (cv *ColVal) resetBitMap(index int) {
55+
if (cv.Len+cv.BitMapOffset)>>3 >= len(cv.Bitmap) {
56+
cv.Bitmap = append(cv.Bitmap, 0)
57+
return
58+
}
59+
60+
index += cv.BitMapOffset
61+
cv.Bitmap[index>>3] &= FlippedBitMask[index&0x07]
62+
}
63+
64+
func appendNulls(cv *ColVal, count int) {
65+
for i := 0; i < count; i++ {
66+
appendNull(cv)
67+
}
68+
}
69+
70+
func appendNull(cv *ColVal) {
71+
cv.resetBitMap(cv.Len)
72+
cv.Len++
73+
cv.NilCount++
74+
}
75+
76+
func (cv *ColVal) reserveVal(size int) {
77+
cv.Val = reserveBytes(cv.Val, size)
78+
}
79+
80+
func reserveBytes(b []byte, size int) []byte {
81+
valCap := cap(b)
82+
if valCap == 0 {
83+
return make([]byte, size)
84+
}
85+
86+
valLen := len(b)
87+
remain := valCap - valLen
88+
if delta := size - remain; delta > 0 {
89+
if delta <= len(zeroBuf) {
90+
b = append(b[:valCap], zeroBuf[:delta]...)
91+
} else {
92+
b = append(b[:valCap], make([]byte, delta)...)
93+
}
94+
}
95+
return b[:valLen+size]
96+
}
97+
98+
func (cv *ColVal) setBitMap(index int) {
99+
if (cv.Len+cv.BitMapOffset)>>3 >= len(cv.Bitmap) {
100+
cv.Bitmap = append(cv.Bitmap, 1)
101+
return
102+
}
103+
104+
index += cv.BitMapOffset
105+
cv.Bitmap[index>>3] |= BitMask[index&0x07]
106+
}
107+
108+
func appendValues[T ExceptString](cv *ColVal, values ...T) {
109+
for _, v := range values {
110+
appendValue(cv, v)
111+
}
112+
}
113+
114+
func appendValue[T ExceptString](cv *ColVal, v T) {
115+
index := len(cv.Val)
116+
cv.reserveVal(int(unsafe.Sizeof(v)))
117+
*(*T)(unsafe.Pointer(&cv.Val[index])) = v
118+
cv.setBitMap(cv.Len)
119+
cv.Len++
120+
}
121+
122+
func values[T ExceptString](cv *ColVal) []T {
123+
valueLen := int(unsafe.Sizeof(*new(T)))
124+
if cv.Val == nil {
125+
return nil
126+
}
127+
data := unsafe.Slice((*T)(unsafe.Pointer(&cv.Val[0])), len(cv.Val)/valueLen)
128+
return data
129+
}
130+
131+
func (cv *ColVal) FloatValues() []float64 {
132+
return values[float64](cv)
133+
}
134+
135+
func (cv *ColVal) IsNil(i int) bool {
136+
if i >= cv.Len || len(cv.Bitmap) == 0 {
137+
return true
138+
}
139+
if cv.NilCount == 0 {
140+
return false
141+
}
142+
idx := cv.BitMapOffset + i
143+
return !((cv.Bitmap[idx>>3] & BitMask[idx&0x07]) != 0)
144+
}
145+
146+
func (cv *ColVal) StringValues(dst []string) []string {
147+
if len(cv.Offset) == 0 {
148+
return dst
149+
}
150+
151+
offs := cv.Offset
152+
for i := 0; i < len(offs); i++ {
153+
if cv.IsNil(i) {
154+
continue
155+
}
156+
off := offs[i]
157+
if i == len(offs)-1 {
158+
dst = append(dst, Bytes2str(cv.Val[off:]))
159+
} else {
160+
dst = append(dst, Bytes2str(cv.Val[off:offs[i+1]]))
161+
}
162+
}
163+
164+
return dst
165+
}
166+
167+
func (cv *ColVal) BooleanValues() []bool {
168+
return values[bool](cv)
169+
}
170+
171+
func (cv *ColVal) IntegerValues() []int64 {
172+
return values[int64](cv)
173+
}
174+
175+
func (cv *ColVal) appendAll(src *ColVal) {
176+
cv.Val = append(cv.Val, src.Val...)
177+
cv.Offset = append(cv.Offset, src.Offset...)
178+
bitmap, bitMapOffset := subBitmapBytes(src.Bitmap, src.BitMapOffset, src.Len)
179+
cv.Bitmap = append(cv.Bitmap, bitmap...)
180+
cv.BitMapOffset = bitMapOffset
181+
cv.Len = src.Len
182+
cv.NilCount = src.NilCount
183+
}
184+
185+
func subBitmapBytes(bitmap []byte, bitMapOffset int, length int) ([]byte, int) {
186+
if ((bitMapOffset + length) & 0x7) != 0 {
187+
return bitmap[bitMapOffset>>3 : ((bitMapOffset+length)>>3 + 1)], bitMapOffset & 0x7
188+
}
189+
190+
return bitmap[bitMapOffset>>3 : (bitMapOffset+length)>>3], bitMapOffset & 0x7
191+
}
192+
193+
func (cv *ColVal) appendString(src *ColVal, start, end int) {
194+
offset := uint32(len(cv.Val))
195+
for i := start; i < end; i++ {
196+
if i != start {
197+
offset += src.Offset[i] - src.Offset[i-1]
198+
}
199+
cv.Offset = append(cv.Offset, offset)
200+
}
201+
202+
if end == src.Len {
203+
cv.Val = append(cv.Val, src.Val[src.Offset[start]:]...)
204+
} else {
205+
cv.Val = append(cv.Val, src.Val[src.Offset[start]:src.Offset[end]]...)
206+
}
207+
}
208+
209+
func (cv *ColVal) Size() int {
210+
size := 0
211+
size += SizeOfInt() // Len
212+
size += SizeOfInt() // NilCount
213+
size += SizeOfInt() // BitMapOffset
214+
size += SizeOfByteSlice(cv.Val) // Val
215+
size += SizeOfByteSlice(cv.Bitmap) // Bitmap
216+
size += SizeOfUint32Slice(cv.Offset) // Offset
217+
return size
218+
}
219+
220+
func (cv *ColVal) Marshal(buf []byte) ([]byte, error) {
221+
buf = AppendInt(buf, cv.Len)
222+
buf = AppendInt(buf, cv.NilCount)
223+
buf = AppendInt(buf, cv.BitMapOffset)
224+
buf = AppendBytes(buf, cv.Val)
225+
buf = AppendBytes(buf, cv.Bitmap)
226+
buf = AppendUint32Slice(buf, cv.Offset)
227+
return buf, nil
228+
}

0 commit comments

Comments
 (0)