github.com/matrixorigin/matrixone@v0.7.0/pkg/lockservice/lock.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 lockservice
    16  
    17  const (
    18  	flagLockRow byte = 1 << iota
    19  	flagLockRangeStart
    20  	flagLockRangeEnd
    21  	flagLockExclusiveMode
    22  	flagLockSharedMode
    23  )
    24  
    25  func newRangeLock(txnID []byte, mode LockMode) (Lock, Lock) {
    26  	l := newLock(txnID, mode)
    27  	return l.toRangeStartLock(), l.toRangeEndLock()
    28  }
    29  
    30  func newRowLock(txnID []byte, mode LockMode) Lock {
    31  	l := newLock(txnID, mode)
    32  	return l.toRowLock()
    33  }
    34  
    35  func newLock(txnID []byte, mode LockMode) Lock {
    36  	l := Lock{txnID: txnID}
    37  	if mode == Exclusive {
    38  		l.value |= flagLockExclusiveMode
    39  	} else {
    40  		l.value |= flagLockSharedMode
    41  	}
    42  	return l
    43  }
    44  
    45  func (l Lock) toRowLock() Lock {
    46  	l.value |= flagLockRow
    47  	return l
    48  }
    49  
    50  func (l Lock) toRangeStartLock() Lock {
    51  	l.value |= flagLockRangeStart
    52  	return l
    53  }
    54  
    55  func (l Lock) toRangeEndLock() Lock {
    56  	l.value |= flagLockRangeEnd
    57  	return l
    58  }
    59  
    60  func (l Lock) isLockRow() bool {
    61  	return l.value&flagLockRow != 0
    62  }
    63  
    64  func (l Lock) isLockRange() bool {
    65  	return !l.isLockRow()
    66  }
    67  
    68  func (l Lock) isLockRangeStart() bool {
    69  	return l.value&flagLockRangeStart != 0
    70  }
    71  
    72  func (l Lock) isLockRangeEnd() bool {
    73  	return l.value&flagLockRangeEnd != 0
    74  }
    75  
    76  func (l Lock) getLockMode() LockMode {
    77  	if l.value&flagLockExclusiveMode != 0 {
    78  		return Exclusive
    79  	}
    80  	return Shared
    81  }