github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/storage/kv/keyflags.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     2  //
     3  // Copyright 2021 PingCAP, Inc.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package kv
    18  
    19  // KeyFlags are metadata associated with key.
    20  // Notice that the highest bit is used by red black tree, do not set flags on it.
    21  type KeyFlags uint16
    22  
    23  const (
    24  	// FlagBytes is the byte size of type KeyFlags
    25  	FlagBytes               = 2
    26  	flagPresumeKNE KeyFlags = 1 << iota
    27  	flagKeyLocked
    28  	flagNeedLocked
    29  	flagKeyLockedValExist
    30  	flagNeedCheckExists
    31  	flagPrewriteOnly
    32  	flagIgnoredIn2PC
    33  	flagReadable
    34  	flagNewlyInserted
    35  
    36  	// The following are assertion related flags.
    37  	// There are four choices of the two bits:
    38  	// * 0: Assertion is not set and can be set later.
    39  	// * flagAssertExists: We assert the key exists.
    40  	// * flagAssertNotExists: We assert the key doesn't exist.
    41  	// * flagAssertExists | flagAssertNotExists: Assertion cannot be made on this key (unknown).
    42  	// Once either (or both) of the two flags is set, we say assertion is set (`HasAssertionFlags` becomes true), and
    43  	// it's expected to be unchangeable within the current transaction.
    44  	flagAssertExist
    45  	flagAssertNotExist
    46  
    47  	// It marks the conflict check of the key is postponed to prewrite. This is only used in pessimistic transactions.
    48  	// When the key gets locked (and the existence is checked), the flag should be removed.
    49  	// It should only be applied to keys with PresumeNotExist, so that an in-place constraint check becomes
    50  	// (a conflict check + a constraint check) in prewrite.
    51  	flagNeedConstraintCheckInPrewrite
    52  
    53  	// A PresumeKNE that should not be unset. It's used by lazy uniqueness checks. So that PresumeKNE flags in previous
    54  	// statement should not be mistakenly unset.
    55  	// It's only set when a stmt that sets PresumeKNE finishes.
    56  	// It's only read together with PresumeKNE
    57  	// It doesn't have to be persistent, since the only place where it is used is in statement retry where it helps set
    58  	// the correct assert_not_exist option. The life of the flag begins after stmt which sets the PresumeKNE flag, and
    59  	// ends when the lock is acquired.
    60  	flagPreviousPresumeKNE
    61  
    62  	persistentFlags = flagKeyLocked | flagKeyLockedValExist | flagNeedConstraintCheckInPrewrite
    63  )
    64  
    65  // HasAssertExist returns whether the key need ensure exists in 2pc.
    66  func (f KeyFlags) HasAssertExist() bool {
    67  	return f&flagAssertExist != 0 && f&flagAssertNotExist == 0
    68  }
    69  
    70  // HasAssertNotExist returns whether the key need ensure non-exists in 2pc.
    71  func (f KeyFlags) HasAssertNotExist() bool {
    72  	return f&flagAssertNotExist != 0 && f&flagAssertExist == 0
    73  }
    74  
    75  // HasAssertUnknown returns whether the key is marked unable to do any assertion.
    76  func (f KeyFlags) HasAssertUnknown() bool {
    77  	return f&flagAssertExist != 0 && f&flagAssertNotExist != 0
    78  }
    79  
    80  // HasAssertionFlags returns whether the key's assertion is set.
    81  func (f KeyFlags) HasAssertionFlags() bool {
    82  	return f&flagAssertExist != 0 || f&flagAssertNotExist != 0
    83  }
    84  
    85  // HasPresumeKeyNotExists returns whether the associated key use lazy check.
    86  func (f KeyFlags) HasPresumeKeyNotExists() bool {
    87  	return f&(flagPresumeKNE|flagPreviousPresumeKNE) != 0
    88  }
    89  
    90  // HasLocked returns whether the associated key has acquired pessimistic lock.
    91  func (f KeyFlags) HasLocked() bool {
    92  	return f&flagKeyLocked != 0
    93  }
    94  
    95  // HasNeedLocked return whether the key needed to be locked
    96  func (f KeyFlags) HasNeedLocked() bool {
    97  	return f&flagNeedLocked != 0
    98  }
    99  
   100  // HasLockedValueExists returns whether the value exists when key locked.
   101  func (f KeyFlags) HasLockedValueExists() bool {
   102  	return f&flagKeyLockedValExist != 0
   103  }
   104  
   105  // HasNeedCheckExists returns whether the key need to check existence when it has been locked.
   106  func (f KeyFlags) HasNeedCheckExists() bool {
   107  	return f&flagNeedCheckExists != 0
   108  }
   109  
   110  // HasPrewriteOnly returns whether the key should be used in 2pc commit phase.
   111  func (f KeyFlags) HasPrewriteOnly() bool {
   112  	return f&flagPrewriteOnly != 0
   113  }
   114  
   115  // HasIgnoredIn2PC returns whether the key will be ignored in 2pc.
   116  func (f KeyFlags) HasIgnoredIn2PC() bool {
   117  	return f&flagIgnoredIn2PC != 0
   118  }
   119  
   120  // HasReadable returns whether the in-transaction operations is able to read the key.
   121  func (f KeyFlags) HasReadable() bool {
   122  	return f&flagReadable != 0
   123  }
   124  
   125  // HasNeedConstraintCheckInPrewrite returns whether the key needs to check conflict in prewrite.
   126  func (f KeyFlags) HasNeedConstraintCheckInPrewrite() bool {
   127  	return f&flagNeedConstraintCheckInPrewrite != 0
   128  }
   129  
   130  // AndPersistent returns the value of current flags&persistentFlags
   131  func (f KeyFlags) AndPersistent() KeyFlags {
   132  	return f & persistentFlags
   133  }
   134  
   135  // HasNewlyInserted returns whether the in-transaction key is generated by an "insert" operation.
   136  func (f KeyFlags) HasNewlyInserted() bool {
   137  	return f&flagNewlyInserted != 0
   138  }
   139  
   140  // ApplyFlagsOps applys flagspos to origin.
   141  func ApplyFlagsOps(origin KeyFlags, ops ...FlagsOp) KeyFlags {
   142  	for _, op := range ops {
   143  		switch op {
   144  		case SetPresumeKeyNotExists:
   145  			origin |= flagPresumeKNE | flagNeedCheckExists
   146  		case DelPresumeKeyNotExists:
   147  			origin &= ^(flagPresumeKNE | flagNeedCheckExists)
   148  		case SetKeyLocked:
   149  			origin |= flagKeyLocked
   150  		case DelKeyLocked:
   151  			origin &= ^flagKeyLocked
   152  		case SetNeedLocked:
   153  			origin |= flagNeedLocked
   154  		case DelNeedLocked:
   155  			origin &= ^flagNeedLocked
   156  		case SetKeyLockedValueExists:
   157  			origin |= flagKeyLockedValExist
   158  			origin &= ^flagNeedConstraintCheckInPrewrite
   159  		case DelNeedCheckExists:
   160  			origin &= ^flagNeedCheckExists
   161  		case SetKeyLockedValueNotExists:
   162  			origin &= ^flagKeyLockedValExist
   163  			origin &= ^flagNeedConstraintCheckInPrewrite
   164  		case SetPrewriteOnly:
   165  			origin |= flagPrewriteOnly
   166  		case SetIgnoredIn2PC:
   167  			origin |= flagIgnoredIn2PC
   168  		case SetReadable:
   169  			origin |= flagReadable
   170  		case SetNewlyInserted:
   171  			origin |= flagNewlyInserted
   172  		case SetAssertExist:
   173  			origin &= ^flagAssertNotExist
   174  			origin |= flagAssertExist
   175  		case SetAssertNotExist:
   176  			origin &= ^flagAssertExist
   177  			origin |= flagAssertNotExist
   178  		case SetAssertUnknown:
   179  			origin |= flagAssertNotExist
   180  			origin |= flagAssertExist
   181  		case SetAssertNone:
   182  			origin &= ^flagAssertExist
   183  			origin &= ^flagAssertNotExist
   184  		case SetNeedConstraintCheckInPrewrite:
   185  			origin |= flagNeedConstraintCheckInPrewrite
   186  		case DelNeedConstraintCheckInPrewrite:
   187  			origin &= ^flagNeedConstraintCheckInPrewrite
   188  		case SetPreviousPresumeKNE:
   189  			origin |= flagPreviousPresumeKNE
   190  		}
   191  	}
   192  	return origin
   193  }
   194  
   195  // FlagsOp describes KeyFlags modify operation.
   196  type FlagsOp uint32
   197  
   198  const (
   199  	// SetPresumeKeyNotExists marks the existence of the associated key is checked lazily.
   200  	// Implies KeyFlags.HasNeedCheckExists() == true.
   201  	SetPresumeKeyNotExists FlagsOp = 1 << iota
   202  	// DelPresumeKeyNotExists reverts SetPresumeKeyNotExists.
   203  	DelPresumeKeyNotExists
   204  	// SetKeyLocked marks the associated key has acquired lock.
   205  	SetKeyLocked
   206  	// DelKeyLocked reverts SetKeyLocked.
   207  	DelKeyLocked
   208  	// SetNeedLocked marks the associated key need to be acquired lock.
   209  	SetNeedLocked
   210  	// DelNeedLocked reverts SetKeyNeedLocked.
   211  	DelNeedLocked
   212  	// SetKeyLockedValueExists marks the value exists when key has been locked in Transaction.LockKeys.
   213  	SetKeyLockedValueExists
   214  	// SetKeyLockedValueNotExists marks the value doesn't exists when key has been locked in Transaction.LockKeys.
   215  	SetKeyLockedValueNotExists
   216  	// DelNeedCheckExists marks the key no need to be checked in Transaction.LockKeys.
   217  	DelNeedCheckExists
   218  	// SetPrewriteOnly marks the key shouldn't be used in 2pc commit phase.
   219  	SetPrewriteOnly
   220  	// SetIgnoredIn2PC marks the key will be ignored in 2pc.
   221  	SetIgnoredIn2PC
   222  	// SetReadable marks the key is readable by in-transaction read.
   223  	SetReadable
   224  	// SetNewlyInserted marks the key is newly inserted with value length greater than zero.
   225  	SetNewlyInserted
   226  	// SetAssertExist marks the key must exist.
   227  	SetAssertExist
   228  	// SetAssertNotExist marks the key must not exist.
   229  	SetAssertNotExist
   230  	// SetAssertUnknown mark the key maybe exists or not exists.
   231  	SetAssertUnknown
   232  	// SetAssertNone cleans up the key's assert.
   233  	SetAssertNone
   234  	// SetNeedConstraintCheckInPrewrite marks the key needs to check conflict in prewrite.
   235  	SetNeedConstraintCheckInPrewrite
   236  	// DelNeedConstraintCheckInPrewrite reverts SetNeedConstraintCheckInPrewrite. This is required when we decide to
   237  	// make up the pessimistic lock.
   238  	DelNeedConstraintCheckInPrewrite
   239  	// SetPreviousPresumeKNE sets flagPreviousPresumeKNE.
   240  	SetPreviousPresumeKNE
   241  )