github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/common/id.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  
    22  // ID is the general identifier type shared by different types like
    23  // table, segment, block, etc.
    24  //
    25  // We could wrap info from upper level via ID, for instance, get the table id,
    26  // segment id, and the block id for one block by ID.AsBlockID, which made
    27  // the resource management easier.
    28  type ID struct {
    29  	// Internal table id
    30  	TableID uint64
    31  	// Internal segment id
    32  	SegmentID uint64
    33  	// Internal block id
    34  	BlockID uint64
    35  	// Internal column part id
    36  	PartID uint32
    37  	// Column index for the column part above
    38  	Idx uint16
    39  	// Iter is used for MVCC
    40  	Iter uint8
    41  }
    42  
    43  func (id *ID) AsBlockID() ID {
    44  	return ID{
    45  		TableID:   id.TableID,
    46  		SegmentID: id.SegmentID,
    47  		BlockID:   id.BlockID,
    48  	}
    49  }
    50  
    51  func (id *ID) AsSegmentID() ID {
    52  	return ID{
    53  		TableID:   id.TableID,
    54  		SegmentID: id.SegmentID,
    55  	}
    56  }
    57  
    58  func (id *ID) String() string {
    59  	return fmt.Sprintf("<%d:%d-%d-%d-%d-%d>", id.Idx, id.TableID, id.SegmentID, id.BlockID, id.PartID, id.Iter)
    60  }
    61  
    62  func (id *ID) TableString() string {
    63  	return fmt.Sprintf("TBL<%d:%d>", id.Idx, id.TableID)
    64  }
    65  func (id *ID) SegmentString() string {
    66  	return fmt.Sprintf("SEG<%d:%d-%d>", id.Idx, id.TableID, id.SegmentID)
    67  }
    68  
    69  func (id *ID) BlockString() string {
    70  	return fmt.Sprintf("BLK<%d:%d-%d-%d>", id.Idx, id.TableID, id.SegmentID, id.BlockID)
    71  }
    72  
    73  func (id *ID) IsSameSegment(o ID) bool {
    74  	return id.TableID == o.TableID && id.SegmentID == o.SegmentID
    75  }
    76  
    77  func (id *ID) IsSameBlock(o ID) bool {
    78  	return id.TableID == o.TableID && id.SegmentID == o.SegmentID && id.BlockID == o.BlockID
    79  }
    80  
    81  func (id *ID) NextBlock() ID {
    82  	newId := atomic.AddUint64(&id.BlockID, uint64(1))
    83  	bid := *id
    84  	bid.BlockID = newId - 1
    85  	return bid
    86  }
    87  
    88  func (id *ID) NextSegment() ID {
    89  	newId := atomic.AddUint64(&id.SegmentID, uint64(1))
    90  	bid := *id
    91  	bid.SegmentID = newId - 1
    92  	return bid
    93  }
    94  
    95  func IDArraryString(ids []ID) string {
    96  	str := "["
    97  	for _, id := range ids {
    98  		str = fmt.Sprintf("%s%s,", str, id.String())
    99  	}
   100  	str = fmt.Sprintf("%s]", str)
   101  	return str
   102  }
   103  
   104  func BlockIDArraryString(ids []ID) string {
   105  	str := "["
   106  	for _, id := range ids {
   107  		str = fmt.Sprintf("%s%d,", str, id.BlockID)
   108  	}
   109  	str = fmt.Sprintf("%s]", str)
   110  	return str
   111  }