github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/common/interval.go (about)

     1  // Copyright 2021 Matrix Origin
     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 common
    16  
    17  import (
    18  	"fmt"
    19  	"sync/atomic"
    20  	"unsafe"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  )
    24  
    25  type ClosedInterval struct {
    26  	Start, End uint64
    27  }
    28  
    29  const (
    30  	CloseIntervalSize int64 = int64(unsafe.Sizeof(ClosedInterval{}))
    31  )
    32  
    33  func EncodeCloseInterval(i *ClosedInterval) []byte {
    34  	return unsafe.Slice((*byte)(unsafe.Pointer(i)), CloseIntervalSize)
    35  }
    36  
    37  func (i *ClosedInterval) String() string {
    38  	return fmt.Sprintf("[%d,%d]", i.Start, i.End)
    39  }
    40  
    41  func (i *ClosedInterval) Append(id uint64) error {
    42  	if i.Start == i.End && i.Start == 0 {
    43  		i.Start = id
    44  		i.End = id
    45  		return nil
    46  	}
    47  	if id != i.End+1 {
    48  		// logutil.Debugf("invalid interval %v %v", i, id)
    49  		return moerr.NewInternalErrorNoCtx("invalid interval")
    50  	}
    51  	i.End = id
    52  	return nil
    53  }
    54  
    55  func (i *ClosedInterval) Contains(o ClosedInterval) bool {
    56  	if i == nil {
    57  		return false
    58  	}
    59  	return i.Start <= o.Start && i.End >= o.End
    60  }
    61  
    62  func (i *ClosedInterval) Uint64Contains(start, end uint64) bool {
    63  	if i == nil {
    64  		return false
    65  	}
    66  	return i.Start <= start && i.End >= end
    67  }
    68  
    69  func (i *ClosedInterval) TryMerge(o ClosedInterval) bool {
    70  	if o.Start > i.End+1 || i.Start > o.End+1 {
    71  		return false
    72  	}
    73  	if i.Start > o.Start {
    74  		i.Start = o.Start
    75  	}
    76  	if i.End < o.End {
    77  		i.End = o.End
    78  	}
    79  
    80  	return true
    81  }
    82  
    83  func (i *ClosedInterval) IsCoveredByInt(idx uint64) bool {
    84  	return idx >= i.End
    85  }
    86  
    87  func (i *ClosedInterval) AtomicUpdateEnd(v uint64) {
    88  	atomic.StoreUint64(&i.End, v)
    89  }
    90  
    91  func (i *ClosedInterval) LT(o *ClosedInterval) bool {
    92  	return i.End < o.Start
    93  }
    94  
    95  func (i *ClosedInterval) GT(o *ClosedInterval) bool {
    96  	return i.Start < o.End
    97  }