github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/prolly/message/item_access.go (about)

     1  // Copyright 2021 Dolthub, Inc.
     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 message
    16  
    17  import (
    18  	"github.com/dolthub/dolt/go/gen/fb/serial"
    19  	"github.com/dolthub/dolt/go/store/val"
    20  )
    21  
    22  // ItemAccess accesses items in a serial.Message.
    23  type ItemAccess struct {
    24  	// bufStart is the offset to the start of the
    25  	// Item buffer within a serial.Message.
    26  	// bufLen is the length of the Item buffer.
    27  	bufStart, bufLen uint16
    28  
    29  	// offStart, if nonzero, is the offset to the
    30  	// start of the uin16 offset buffer within a
    31  	// serial.Message. A zero value for offStart
    32  	// indicates an empty offset buffer.
    33  	// bufLen is the length of the Item buffer.
    34  	offStart, offLen uint16
    35  
    36  	// If the serial.Message does not contain an
    37  	// offset buffer (offStart is zero), then
    38  	// Items have a fixed width equal to itemWidth.
    39  	itemWidth uint16
    40  }
    41  
    42  // GetItem returns the ith Item from the buffer.
    43  func (acc ItemAccess) GetItem(i int, msg serial.Message) []byte {
    44  	buf := msg[acc.bufStart : acc.bufStart+acc.bufLen]
    45  	off := msg[acc.offStart : acc.offStart+acc.offLen]
    46  	if acc.offStart != 0 {
    47  		stop := val.ReadUint16(off[(i*2)+2 : (i*2)+4])
    48  		start := val.ReadUint16(off[(i * 2) : (i*2)+2])
    49  		return buf[start:stop]
    50  	} else {
    51  		stop := int(acc.itemWidth) * (i + 1)
    52  		start := int(acc.itemWidth) * i
    53  		return buf[start:stop]
    54  	}
    55  }