github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/catalog/idalloc.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 catalog
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    21  )
    22  
    23  type IDAlloctor struct {
    24  	dbAlloc  *common.IdAlloctor
    25  	tblAlloc *common.IdAlloctor
    26  	objAlloc *common.IdAlloctor
    27  	blkAlloc *common.IdAlloctor
    28  }
    29  
    30  func NewIDAllocator() *IDAlloctor {
    31  	return &IDAlloctor{
    32  		dbAlloc:  common.NewIdAlloctor(1000),
    33  		tblAlloc: common.NewIdAlloctor(1000),
    34  		objAlloc: common.NewIdAlloctor(1000),
    35  		blkAlloc: common.NewIdAlloctor(1000),
    36  	}
    37  }
    38  
    39  func (alloc *IDAlloctor) Init(prevDb, prevTbl, prevObj, prevBlk uint64) {
    40  	alloc.dbAlloc.SetStart(prevDb)
    41  	alloc.tblAlloc.SetStart(prevTbl)
    42  	alloc.objAlloc.SetStart(prevObj)
    43  	alloc.blkAlloc.SetStart(prevBlk)
    44  }
    45  
    46  func (alloc *IDAlloctor) NextDB() uint64     { return alloc.dbAlloc.Alloc() }
    47  func (alloc *IDAlloctor) NextTable() uint64  { return alloc.tblAlloc.Alloc() }
    48  func (alloc *IDAlloctor) NextObject() uint64 { return alloc.objAlloc.Alloc() }
    49  func (alloc *IDAlloctor) NextBlock() uint64  { return alloc.blkAlloc.Alloc() }
    50  
    51  func (alloc *IDAlloctor) CurrDB() uint64     { return alloc.dbAlloc.Get() }
    52  func (alloc *IDAlloctor) CurrTable() uint64  { return alloc.tblAlloc.Get() }
    53  func (alloc *IDAlloctor) CurrObject() uint64 { return alloc.objAlloc.Get() }
    54  func (alloc *IDAlloctor) CurrBlock() uint64  { return alloc.blkAlloc.Get() }
    55  
    56  func (alloc *IDAlloctor) OnReplayBlockID(id uint64) {
    57  	if alloc.CurrBlock() < id {
    58  		alloc.blkAlloc.SetStart(id)
    59  	}
    60  }
    61  
    62  func (alloc *IDAlloctor) OnReplayObjectID(id uint64) {
    63  	if alloc.CurrObject() < id {
    64  		alloc.objAlloc.SetStart(id)
    65  	}
    66  }
    67  func (alloc *IDAlloctor) OnReplayTableID(id uint64) {
    68  	if alloc.CurrTable() < id {
    69  		alloc.tblAlloc.SetStart(id)
    70  	}
    71  }
    72  func (alloc *IDAlloctor) OnReplayDBID(id uint64) {
    73  	if alloc.CurrDB() < id {
    74  		alloc.dbAlloc.SetStart(id)
    75  	}
    76  }
    77  
    78  func (alloc *IDAlloctor) IDStates() string {
    79  	return fmt.Sprintf("Current DBID=%d,TID=%d,SID=%d,BID=%d",
    80  		alloc.CurrDB(), alloc.CurrTable(), alloc.CurrObject(), alloc.CurrBlock())
    81  }