aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/col_raw_of.go
blob: 325a17b3061e9a1a5be375f6dbddc3688ac53fa2 (plain) (blame)
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
//go:build (amd64 || arm64 || riscv64) && !purego

package proto

import (
	"strconv"
	"unsafe"

	"github.com/go-faster/errors"
)

// ColRawOf is generic raw column.
type ColRawOf[X comparable] []X

func (c *ColRawOf[X]) AppendArr(v []X) {
	for _, x := range v {
		c.Append(x)
	}
}

func (c ColRawOf[X]) Size() int {
	var x X
	return int(unsafe.Sizeof(x)) // #nosec G103
}

// Type returns ColumnType of ColRawOf.
func (c ColRawOf[X]) Type() ColumnType {
	return ColumnTypeFixedString.With(strconv.Itoa(c.Size()))
}

// Rows returns count of rows in column.
func (c ColRawOf[X]) Rows() int {
	return len(c)
}

// Row returns value of "i" row.
func (c ColRawOf[X]) Row(i int) X {
	return c[i]
}

// Reset resets data in row, preserving capacity for efficiency.
func (c *ColRawOf[X]) Reset() {
	*c = (*c)[:0]
}

// Append value to column.
func (c *ColRawOf[X]) Append(v X) {
	*c = append(*c, v)
}

// EncodeColumn encodes ColRawOf rows to *Buffer.
func (c ColRawOf[X]) EncodeColumn(b *Buffer) {
	if len(c) == 0 {
		return
	}
	offset := len(b.Buf)
	var x X
	size := unsafe.Sizeof(x) // #nosec G103
	b.Buf = append(b.Buf, make([]byte, int(size)*len(c))...)
	s := *(*slice)(unsafe.Pointer(&c)) // #nosec G103
	s.Len *= size
	s.Cap *= size
	src := *(*[]byte)(unsafe.Pointer(&s)) // #nosec G103
	dst := b.Buf[offset:]
	copy(dst, src)
}

// DecodeColumn decodes ColRawOf rows from *Reader.
func (c *ColRawOf[X]) DecodeColumn(r *Reader, rows int) error {
	if rows == 0 {
		return nil
	}
	*c = append(*c, make([]X, rows)...)
	s := *(*slice)(unsafe.Pointer(c)) // #nosec G103
	var x X
	size := unsafe.Sizeof(x) // #nosec G103
	s.Len *= size
	s.Cap *= size
	dst := *(*[]byte)(unsafe.Pointer(&s)) // #nosec G103
	if err := r.ReadFull(dst); err != nil {
		return errors.Wrap(err, "read full")
	}
	return nil
}