go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butler/bundler/binaryParser.go (about)

     1  // Copyright 2015 The LUCI 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 bundler
    16  
    17  import (
    18  	"errors"
    19  
    20  	"go.chromium.org/luci/logdog/api/logpb"
    21  )
    22  
    23  // binaryThreshold is the amount of binary data that we will willingly yield
    24  // if not allowed to split. This helps build larger binary stream chunks.
    25  const defaultBinaryThreshold = 8 * 1024
    26  
    27  // binaryParser is a parser implementation for the LogDog BINARY stream type.
    28  type binaryParser struct {
    29  	baseParser
    30  
    31  	offset    int64
    32  	threshold int
    33  }
    34  
    35  var _ parser = (*binaryParser)(nil)
    36  
    37  func (p *binaryParser) nextEntry(c *constraints) (*logpb.LogEntry, error) {
    38  	threshold := p.getThreshold()
    39  	if c.allowSplit {
    40  		// If we're allowed to split, return _any_ available data.
    41  		threshold = 0
    42  	}
    43  
    44  	count := p.Len()
    45  	if count <= int64(threshold) {
    46  		return nil, nil
    47  	}
    48  	if count > int64(c.limit) {
    49  		count = int64(c.limit)
    50  	}
    51  
    52  	// The integer conversion, since count has been bounded by our "int" limit.
    53  	size := int(count)
    54  
    55  	data := make([]byte, size)
    56  	size, _ = p.View().Read(data)
    57  	memoryCorruptionIf(int64(size) != count, errors.New("partial buffer read"))
    58  
    59  	ts, _ := p.firstChunkTime()
    60  	e := p.baseLogEntry(ts)
    61  	e.Content = &logpb.LogEntry_Binary{Binary: &logpb.Binary{
    62  		Data: data[:size],
    63  	}}
    64  	e.Sequence = uint64(p.offset)
    65  
    66  	p.Consume(int64(size))
    67  	p.offset += int64(size)
    68  	return e, nil
    69  }
    70  
    71  func (p *binaryParser) getThreshold() int {
    72  	result := p.threshold
    73  	if result == 0 {
    74  		result = defaultBinaryThreshold
    75  	}
    76  	return result
    77  }