github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/lookup/above.go (about)

     1  // Copyright 2020 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 lookup
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/dolthub/dolt/go/store/types"
    21  )
    22  
    23  // Above represents the position immediately above the contained key.
    24  type Above struct {
    25  	key types.Tuple
    26  }
    27  
    28  var _ Cut = Above{}
    29  
    30  // Compare implements Cut.
    31  func (a Above) Compare(c Cut) (int, error) {
    32  	switch c := c.(type) {
    33  	case AboveAll:
    34  		return -1, nil
    35  	case BelowAll:
    36  		return 1, nil
    37  	case Above:
    38  		ok, err := a.key.Less(a.key.Format(), c.key)
    39  		if err != nil {
    40  			return 0, err
    41  		}
    42  		if ok {
    43  			return -1, nil
    44  		}
    45  		if a.key.Equals(c.key) {
    46  			return 0, nil
    47  		}
    48  		return 1, nil
    49  	case Below:
    50  		ok, err := a.key.Less(a.key.Format(), c.key)
    51  		if err != nil {
    52  			return 0, err
    53  		}
    54  		if ok {
    55  			return -1, nil
    56  		}
    57  		return 1, nil
    58  	default:
    59  		panic(fmt.Errorf("unrecognized Cut type '%T'", c))
    60  	}
    61  }
    62  
    63  // Equals implements Cut.
    64  func (a Above) Equals(c Cut) bool {
    65  	other, ok := c.(Above)
    66  	if !ok {
    67  		return false
    68  	}
    69  	return a.key.Equals(other.key)
    70  }
    71  
    72  // Format implements Cut.
    73  func (a Above) Format() *types.NomsBinFormat {
    74  	if a.key.Empty() {
    75  		return types.Format_Default
    76  	}
    77  	return a.key.Format()
    78  }
    79  
    80  // Less implements Cut.
    81  func (a Above) Less(tpl types.Tuple) (bool, error) {
    82  	return a.key.Less(a.key.Format(), tpl)
    83  }
    84  
    85  // String implements Cut.
    86  func (a Above) String() string {
    87  	return fmt.Sprintf("Above[%s]", cutKeyToString(a.key))
    88  }
    89  
    90  // TypeAsLowerBound implements Cut.
    91  func (Above) TypeAsLowerBound() BoundType {
    92  	return Open
    93  }
    94  
    95  // TypeAsUpperBound implements Cut.
    96  func (Above) TypeAsUpperBound() BoundType {
    97  	return Closed
    98  }