github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/types/sequence.go (about)

     1  // Copyright 2019 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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package types
    23  
    24  import (
    25  	"context"
    26  
    27  	"github.com/dolthub/dolt/go/store/d"
    28  	"github.com/dolthub/dolt/go/store/hash"
    29  )
    30  
    31  type sequenceItem interface{}
    32  
    33  type compareFn func(x int, y int) (bool, error)
    34  
    35  type sequence interface {
    36  	asValueImpl() valueImpl
    37  	cumulativeNumberOfLeaves(idx int) (uint64, error)
    38  	Empty() bool
    39  	Equals(other Value) bool
    40  	format() *NomsBinFormat
    41  	getChildSequence(ctx context.Context, idx int) (sequence, error)
    42  	getCompareFn(other sequence) compareFn
    43  	getCompositeChildSequence(ctx context.Context, start uint64, length uint64) (sequence, error)
    44  	getItem(idx int) (sequenceItem, error)
    45  	Hash(*NomsBinFormat) (hash.Hash, error)
    46  	isLeaf() bool
    47  	Kind() NomsKind
    48  	Len() uint64
    49  	Less(nbf *NomsBinFormat, other LesserValuable) (bool, error)
    50  	numLeaves() uint64
    51  	seqLen() int
    52  	treeLevel() uint64
    53  	typeOf() (*Type, error)
    54  	valueReadWriter() ValueReadWriter
    55  	valuesSlice(from, to uint64) ([]Value, error)
    56  	kvTuples(from, to uint64, dest []Tuple) ([]Tuple, error)
    57  	WalkRefs(nbf *NomsBinFormat, cb RefCallback) error
    58  	writeTo(nomsWriter, *NomsBinFormat) error
    59  }
    60  
    61  const (
    62  	sequencePartKind   = 0
    63  	sequencePartLevel  = 1
    64  	sequencePartCount  = 2
    65  	sequencePartValues = 3
    66  )
    67  
    68  type sequenceImpl struct {
    69  	valueImpl
    70  	len uint64
    71  }
    72  
    73  func newSequenceImpl(vrw ValueReadWriter, buff []byte, offsets []uint32, len uint64) sequenceImpl {
    74  	return sequenceImpl{valueImpl{vrw, vrw.Format(), buff, offsets}, len}
    75  }
    76  
    77  func (seq sequenceImpl) decoderSkipToValues() (valueDecoder, uint64) {
    78  	dec := seq.decoderAtPart(sequencePartCount)
    79  	count := dec.readCount()
    80  	return dec, count
    81  }
    82  
    83  func (seq sequenceImpl) decoderAtPart(part uint32) valueDecoder {
    84  	offset := seq.offsets[part] - seq.offsets[sequencePartKind]
    85  	return newValueDecoder(seq.buff[offset:], seq.vrw)
    86  }
    87  
    88  func (seq sequenceImpl) Empty() bool {
    89  	return seq.Len() == 0
    90  }
    91  
    92  func (seq sequenceImpl) Len() uint64 {
    93  	return seq.len
    94  }
    95  
    96  func (seq sequenceImpl) seqLen() int {
    97  	_, count := seq.decoderSkipToValues()
    98  	return int(count)
    99  }
   100  
   101  func (seq sequenceImpl) getItemOffset(idx int) int {
   102  	// kind, level, count, elements...
   103  	// 0     1      2      3          n+1
   104  	d.PanicIfTrue(idx+sequencePartValues+1 > len(seq.offsets))
   105  	return int(seq.offsets[idx+sequencePartValues] - seq.offsets[sequencePartKind])
   106  }
   107  
   108  func (seq sequenceImpl) decoderSkipToIndex(idx int) valueDecoder {
   109  	offset := seq.getItemOffset(idx)
   110  	return seq.decoderAtOffset(offset)
   111  }