github.com/matrixorigin/matrixone@v0.7.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  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/logutil"
    23  )
    24  
    25  type ClosedInterval struct {
    26  	Start, End uint64
    27  }
    28  
    29  func (i *ClosedInterval) String() string {
    30  	return fmt.Sprintf("[%d,%d]", i.Start, i.End)
    31  }
    32  
    33  func (i *ClosedInterval) Append(id uint64) error {
    34  	if i.Start == i.End && i.Start == 0 {
    35  		i.Start = id
    36  		i.End = id
    37  		return nil
    38  	}
    39  	if id != i.End+1 {
    40  		logutil.Infof("invalid interval %v %v", i, id)
    41  		return moerr.NewInternalErrorNoCtx("invalid interval")
    42  	}
    43  	i.End = id
    44  	return nil
    45  }
    46  
    47  func (i *ClosedInterval) Contains(o ClosedInterval) bool {
    48  	if i == nil {
    49  		return false
    50  	}
    51  	return i.Start <= o.Start && i.End >= o.End
    52  }
    53  
    54  func (i *ClosedInterval) TryMerge(o ClosedInterval) bool {
    55  	if o.Start > i.End+1 || i.Start > o.End+1 {
    56  		return false
    57  	}
    58  	if i.Start > o.Start {
    59  		i.Start = o.Start
    60  	}
    61  	if i.End < o.End {
    62  		i.End = o.End
    63  	}
    64  
    65  	return true
    66  }
    67  
    68  func (i *ClosedInterval) IsCoveredByInt(idx uint64) bool {
    69  	return idx >= i.End
    70  }
    71  
    72  func (i *ClosedInterval) AtomicUpdateEnd(v uint64) {
    73  	atomic.StoreUint64(&i.End, v)
    74  }
    75  
    76  func (i *ClosedInterval) LT(o *ClosedInterval) bool {
    77  	return i.End < o.Start
    78  }
    79  
    80  func (i *ClosedInterval) GT(o *ClosedInterval) bool {
    81  	return i.Start < o.End
    82  }