github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/types/inlineblob.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  package types
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"encoding/hex"
    21  	"fmt"
    22  	"math"
    23  	"strings"
    24  
    25  	"github.com/dolthub/dolt/go/store/hash"
    26  )
    27  
    28  type InlineBlob []byte
    29  
    30  func (v InlineBlob) Value(ctx context.Context) (Value, error) {
    31  	return v, nil
    32  }
    33  
    34  func (v InlineBlob) Equals(other Value) bool {
    35  	v2, ok := other.(InlineBlob)
    36  	if !ok {
    37  		return false
    38  	}
    39  
    40  	return bytes.Equal(v, v2)
    41  }
    42  
    43  func (v InlineBlob) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error) {
    44  	if v2, ok := other.(InlineBlob); ok {
    45  		return bytes.Compare(v, v2) == -1, nil
    46  	}
    47  	return InlineBlobKind < other.Kind(), nil
    48  }
    49  
    50  func (v InlineBlob) Hash(nbf *NomsBinFormat) (hash.Hash, error) {
    51  	return getHash(v, nbf)
    52  }
    53  
    54  func (v InlineBlob) isPrimitive() bool {
    55  	return true
    56  }
    57  
    58  func (v InlineBlob) WalkValues(ctx context.Context, cb ValueCallback) error {
    59  	return nil
    60  }
    61  
    62  func (v InlineBlob) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error {
    63  	return nil
    64  }
    65  
    66  func (v InlineBlob) typeOf() (*Type, error) {
    67  	return PrimitiveTypeMap[InlineBlobKind], nil
    68  }
    69  
    70  func (v InlineBlob) Kind() NomsKind {
    71  	return InlineBlobKind
    72  }
    73  
    74  func (v InlineBlob) valueReadWriter() ValueReadWriter {
    75  	return nil
    76  }
    77  
    78  func (v InlineBlob) writeTo(w nomsWriter, nbf *NomsBinFormat) error {
    79  	byteLen := len(v)
    80  	if byteLen > math.MaxUint16 {
    81  		return fmt.Errorf("InlineBlob has length %v when max is %v", byteLen, math.MaxUint16)
    82  	}
    83  
    84  	err := InlineBlobKind.writeTo(w, nbf)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	w.writeUint16(uint16(byteLen))
    90  	w.writeRaw(v)
    91  	return nil
    92  }
    93  
    94  func (v InlineBlob) readFrom(nbf *NomsBinFormat, b *binaryNomsReader) (Value, error) {
    95  	bytes := b.ReadInlineBlob()
    96  	return InlineBlob(bytes), nil
    97  }
    98  
    99  func (v InlineBlob) skip(nbf *NomsBinFormat, b *binaryNomsReader) {
   100  	size := uint32(b.readUint16())
   101  	b.skipBytes(size)
   102  }
   103  
   104  func (v InlineBlob) HumanReadableString() string {
   105  	return strings.ToUpper(hex.EncodeToString(v))
   106  }