add readable flag (#320)

Signed-off-by: you06 <you1474600@gmail.com>
This commit is contained in:
you06 2021-10-09 12:30:03 +08:00 committed by GitHub
parent 729d741bfd
commit 843a5378aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 8 deletions

View File

@ -792,7 +792,7 @@ type memdbNode struct {
right memdbArenaAddr
vptr memdbArenaAddr
klen uint16
flags uint8
flags uint16
}
func (n *memdbNode) isRed() bool {
@ -814,7 +814,7 @@ func (n *memdbNode) setBlack() {
func (n *memdbNode) getKey() []byte {
var ret []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&ret))
hdr.Data = uintptr(unsafe.Pointer(&n.flags)) + 1
hdr.Data = uintptr(unsafe.Pointer(&n.flags)) + kv.FlagBytes
hdr.Len = int(n.klen)
hdr.Cap = int(n.klen)
return ret
@ -822,7 +822,7 @@ func (n *memdbNode) getKey() []byte {
const (
// bit 1 => red, bit 0 => black
nodeColorBit uint8 = 0x80
nodeColorBit uint16 = 0x8000
nodeFlagsMask = ^nodeColorBit
)
@ -831,5 +831,5 @@ func (n *memdbNode) getKeyFlags() kv.KeyFlags {
}
func (n *memdbNode) setKeyFlags(f kv.KeyFlags) {
n.flags = (^nodeFlagsMask & n.flags) | uint8(f)
n.flags = (^nodeFlagsMask & n.flags) | uint16(f)
}

View File

@ -214,7 +214,7 @@ func (a *nodeAllocator) getNode(addr memdbArenaAddr) *memdbNode {
}
func (a *nodeAllocator) allocNode(key []byte) (memdbArenaAddr, *memdbNode) {
nodeSize := 8*4 + 2 + 1 + len(key)
nodeSize := 8*4 + 2 + kv.FlagBytes + len(key)
addr, mem := a.alloc(nodeSize, true)
n := (*memdbNode)(unsafe.Pointer(&mem[0]))
n.vptr = nullAddr

View File

@ -34,10 +34,13 @@
package kv
// KeyFlags are metadata associated with key
type KeyFlags uint8
// KeyFlags are metadata associated with key.
// Notice that the highest bit is used by red black tree, do not set flags on it.
type KeyFlags uint16
const (
// FlagBytes is the byte size of type KeyFlags
FlagBytes = 2
flagPresumeKNE KeyFlags = 1 << iota
flagKeyLocked
flagNeedLocked
@ -45,6 +48,7 @@ const (
flagNeedCheckExists
flagPrewriteOnly
flagIgnoredIn2PC
flagReadable
persistentFlags = flagKeyLocked | flagKeyLockedValExist
)
@ -84,6 +88,11 @@ func (f KeyFlags) HasIgnoredIn2PC() bool {
return f&flagIgnoredIn2PC != 0
}
// HasReadable returns whether the in-transaction operations is able to read the key.
func (f KeyFlags) HasReadable() bool {
return f&flagReadable != 0
}
// AndPersistent returns the value of current flags&persistentFlags
func (f KeyFlags) AndPersistent() KeyFlags {
return f & persistentFlags
@ -115,6 +124,8 @@ func ApplyFlagsOps(origin KeyFlags, ops ...FlagsOp) KeyFlags {
origin |= flagPrewriteOnly
case SetIgnoredIn2PC:
origin |= flagIgnoredIn2PC
case SetReadable:
origin |= flagReadable
}
}
return origin
@ -147,4 +158,6 @@ const (
SetPrewriteOnly
// SetIgnoredIn2PC marks the key will be ignored in 2pc.
SetIgnoredIn2PC
// SetReadable marks the key is readable by in-transaction read.
SetReadable
)