aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/col_uuid_unsafe.go
blob: 18fa73fc8b2c4ea82266605481663ba8c052d95b (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
//go:build (amd64 || arm64 || riscv64) && !purego

package proto

import (
	"unsafe"

	"github.com/go-faster/errors"
	"github.com/google/uuid"
	"github.com/segmentio/asm/bswap"
)

func (c *ColUUID) DecodeColumn(r *Reader, rows int) error {
	if rows == 0 {
		return nil
	}
	*c = append(*c, make([]uuid.UUID, rows)...)

	// Memory layout of [N]UUID is same as [N*sizeof(UUID)]byte.
	// So just interpret c as byte slice and read data into it.
	s := *(*slice)(unsafe.Pointer(c)) // #nosec: G103 // memory layout matches
	const size = 16
	s.Len *= size
	s.Cap *= size
	dst := *(*[]byte)(unsafe.Pointer(&s)) // #nosec: G103 // memory layout matches
	if err := r.ReadFull(dst); err != nil {
		return errors.Wrap(err, "read full")
	}
	bswap.Swap64(dst) // BE <-> LE

	return nil
}

// EncodeColumn encodes ColUUID rows to *Buffer.
func (c ColUUID) EncodeColumn(b *Buffer) {
	if len(c) == 0 {
		return
	}
	offset := len(b.Buf)
	const size = 16
	b.Buf = append(b.Buf, make([]byte, size*len(c))...)
	s := *(*slice)(unsafe.Pointer(&c)) // #nosec: G103 // memory layout matches
	s.Len *= size
	s.Cap *= size
	src := *(*[]byte)(unsafe.Pointer(&s)) // #nosec: G103 // memory layout matches
	dst := b.Buf[offset:]
	copy(dst, src)
	bswap.Swap64(dst) // BE <-> LE
}