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

     1  // Copyright 2022 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  	"sync/atomic"
    19  
    20  	"github.com/google/uuid"
    21  )
    22  
    23  var (
    24  	GlobalSeqNum atomic.Uint64
    25  )
    26  
    27  func NextGlobalSeqNum() uint64 {
    28  	return GlobalSeqNum.Add(1)
    29  }
    30  
    31  func GetGlobalSeqNum() uint64 {
    32  	return GlobalSeqNum.Load()
    33  }
    34  
    35  type IdAlloctor struct {
    36  	id atomic.Uint64
    37  }
    38  
    39  func NewIdAlloctor(from uint64) *IdAlloctor {
    40  	if from == 0 {
    41  		panic("should not be 0")
    42  	}
    43  
    44  	alloc := &IdAlloctor{}
    45  	alloc.id.Store(from - 1)
    46  	return alloc
    47  }
    48  
    49  func (alloc *IdAlloctor) Alloc() uint64 {
    50  	return alloc.id.Add(1)
    51  }
    52  
    53  func (alloc *IdAlloctor) Get() uint64 {
    54  	return alloc.id.Load()
    55  }
    56  
    57  func (alloc *IdAlloctor) SetStart(start uint64) {
    58  	alloc.id.Store(start)
    59  }
    60  
    61  type TxnIDAllocator struct {
    62  }
    63  
    64  func NewTxnIDAllocator() *TxnIDAllocator {
    65  	return &TxnIDAllocator{}
    66  }
    67  
    68  func (alloc *TxnIDAllocator) Alloc() []byte {
    69  	v := uuid.Must(uuid.NewV7())
    70  	return v[:]
    71  }