MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / Serialize

Method Serialize

pkg/col/coldata/bytes.go:581–618  ·  view source on GitHub ↗

Serialize converts b into the "arrow-like" (which is arrow-compatible) format. We call this "arrow-like" because we're abusing the arrow format to get the best speed, possibly at the cost of increased allocations (when Bytes vector has been modified in-place many times via Sets at arbitrary positio

(n int, dataScratch []byte, offsetsScratch []int32)

Source from the content-addressed store, hash-verified

579//
580// Note: it is assumed that n is not larger than MaxBatchSize.
581func (b *Bytes) Serialize(n int, dataScratch []byte, offsetsScratch []int32) ([]byte, []int32) {
582 if buildutil.CrdbTestBuild {
583 if n > MaxBatchSize {
584 colexecerror.InternalError(errors.AssertionFailedf(
585 "too many bytes elements to serialize: %d vs MaxBatchSize of %d", n, MaxBatchSize,
586 ))
587 }
588 }
589 data := dataScratch[:0]
590 offsets := offsetsScratch[:0]
591
592 // Handle the cases of 0 and 1 elements separately since then we cannot
593 // store two offsets that we need.
594 if n == 0 {
595 offsets = append(offsets, 0)
596 return data, offsets
597 } else if n == 1 {
598 data = append(data, b.Get(0)...)
599 offsets = append(offsets, 0)
600 offsets = append(offsets, int32(len(data)))
601 return data, offsets
602 }
603
604 // Copy over b.elements treated as []byte as well as b.buffer into data.
605 bytes := b.elementsAsBytes(n)
606 if cap(data) < len(bytes)+len(b.buffer) {
607 data = make([]byte, 0, len(bytes)+len(b.buffer))
608 }
609 data = append(data, bytes...)
610 data = append(data, b.buffer...)
611
612 // Now populate the offsets slice which conforms to the arrow format and has
613 // the correct length.
614 offsets = append(offsets, zeroInt32Slice[:n-1]...)
615 offsets = append(offsets, int32(len(bytes)))
616 offsets = append(offsets, int32(len(data)))
617 return data, offsets
618}
619
620// Deserialize updates b according to the "arrow-like" format that was produced
621// by Serialize.

Callers

nothing calls this directly

Calls 3

GetMethod · 0.95
elementsAsBytesMethod · 0.95
InternalErrorFunction · 0.92

Tested by

no test coverage detected