github.com/KinWaiYuen/client-go/v2@v2.5.4/kv/keyflags.go (about) 1 // Copyright 2021 TiKV Authors 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 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/kv/keyflags.go 19 // 20 21 // Copyright 2021 PingCAP, Inc. 22 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package kv 36 37 // KeyFlags are metadata associated with key. 38 // Notice that the highest bit is used by red black tree, do not set flags on it. 39 type KeyFlags uint16 40 41 const ( 42 // FlagBytes is the byte size of type KeyFlags 43 FlagBytes = 2 44 flagPresumeKNE KeyFlags = 1 << iota 45 flagKeyLocked 46 flagNeedLocked 47 flagKeyLockedValExist 48 flagNeedCheckExists 49 flagPrewriteOnly 50 flagIgnoredIn2PC 51 flagReadable 52 53 persistentFlags = flagKeyLocked | flagKeyLockedValExist 54 ) 55 56 // HasPresumeKeyNotExists returns whether the associated key use lazy check. 57 func (f KeyFlags) HasPresumeKeyNotExists() bool { 58 return f&flagPresumeKNE != 0 59 } 60 61 // HasLocked returns whether the associated key has acquired pessimistic lock. 62 func (f KeyFlags) HasLocked() bool { 63 return f&flagKeyLocked != 0 64 } 65 66 // HasNeedLocked return whether the key needed to be locked 67 func (f KeyFlags) HasNeedLocked() bool { 68 return f&flagNeedLocked != 0 69 } 70 71 // HasLockedValueExists returns whether the value exists when key locked. 72 func (f KeyFlags) HasLockedValueExists() bool { 73 return f&flagKeyLockedValExist != 0 74 } 75 76 // HasNeedCheckExists returns whether the key need to check existence when it has been locked. 77 func (f KeyFlags) HasNeedCheckExists() bool { 78 return f&flagNeedCheckExists != 0 79 } 80 81 // HasPrewriteOnly returns whether the key should be used in 2pc commit phase. 82 func (f KeyFlags) HasPrewriteOnly() bool { 83 return f&flagPrewriteOnly != 0 84 } 85 86 // HasIgnoredIn2PC returns whether the key will be ignored in 2pc. 87 func (f KeyFlags) HasIgnoredIn2PC() bool { 88 return f&flagIgnoredIn2PC != 0 89 } 90 91 // HasReadable returns whether the in-transaction operations is able to read the key. 92 func (f KeyFlags) HasReadable() bool { 93 return f&flagReadable != 0 94 } 95 96 // AndPersistent returns the value of current flags&persistentFlags 97 func (f KeyFlags) AndPersistent() KeyFlags { 98 return f & persistentFlags 99 } 100 101 // ApplyFlagsOps applys flagspos to origin. 102 func ApplyFlagsOps(origin KeyFlags, ops ...FlagsOp) KeyFlags { 103 for _, op := range ops { 104 switch op { 105 case SetPresumeKeyNotExists: 106 origin |= flagPresumeKNE | flagNeedCheckExists 107 case DelPresumeKeyNotExists: 108 origin &= ^(flagPresumeKNE | flagNeedCheckExists) 109 case SetKeyLocked: 110 origin |= flagKeyLocked 111 case DelKeyLocked: 112 origin &= ^flagKeyLocked 113 case SetNeedLocked: 114 origin |= flagNeedLocked 115 case DelNeedLocked: 116 origin &= ^flagNeedLocked 117 case SetKeyLockedValueExists: 118 origin |= flagKeyLockedValExist 119 case DelNeedCheckExists: 120 origin &= ^flagNeedCheckExists 121 case SetKeyLockedValueNotExists: 122 origin &= ^flagKeyLockedValExist 123 case SetPrewriteOnly: 124 origin |= flagPrewriteOnly 125 case SetIgnoredIn2PC: 126 origin |= flagIgnoredIn2PC 127 case SetReadable: 128 origin |= flagReadable 129 } 130 } 131 return origin 132 } 133 134 // FlagsOp describes KeyFlags modify operation. 135 type FlagsOp uint16 136 137 const ( 138 // SetPresumeKeyNotExists marks the existence of the associated key is checked lazily. 139 // Implies KeyFlags.HasNeedCheckExists() == true. 140 SetPresumeKeyNotExists FlagsOp = 1 << iota 141 // DelPresumeKeyNotExists reverts SetPresumeKeyNotExists. 142 DelPresumeKeyNotExists 143 // SetKeyLocked marks the associated key has acquired lock. 144 SetKeyLocked 145 // DelKeyLocked reverts SetKeyLocked. 146 DelKeyLocked 147 // SetNeedLocked marks the associated key need to be acquired lock. 148 SetNeedLocked 149 // DelNeedLocked reverts SetKeyNeedLocked. 150 DelNeedLocked 151 // SetKeyLockedValueExists marks the value exists when key has been locked in Transaction.LockKeys. 152 SetKeyLockedValueExists 153 // SetKeyLockedValueNotExists marks the value doesn't exists when key has been locked in Transaction.LockKeys. 154 SetKeyLockedValueNotExists 155 // DelNeedCheckExists marks the key no need to be checked in Transaction.LockKeys. 156 DelNeedCheckExists 157 // SetPrewriteOnly marks the key shouldn't be used in 2pc commit phase. 158 SetPrewriteOnly 159 // SetIgnoredIn2PC marks the key will be ignored in 2pc. 160 SetIgnoredIn2PC 161 // SetReadable marks the key is readable by in-transaction read. 162 SetReadable 163 )