From 843a5378aa9101c0e2aba61e0c2c11b3f122f08f Mon Sep 17 00:00:00 2001 From: you06 Date: Sat, 9 Oct 2021 12:30:03 +0800 Subject: [PATCH] add readable flag (#320) Signed-off-by: you06 --- internal/unionstore/memdb.go | 10 +++++----- internal/unionstore/memdb_arena.go | 2 +- kv/keyflags.go | 17 +++++++++++++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/internal/unionstore/memdb.go b/internal/unionstore/memdb.go index 7833d966..cd0703e0 100644 --- a/internal/unionstore/memdb.go +++ b/internal/unionstore/memdb.go @@ -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,8 +822,8 @@ func (n *memdbNode) getKey() []byte { const ( // bit 1 => red, bit 0 => black - nodeColorBit uint8 = 0x80 - nodeFlagsMask = ^nodeColorBit + nodeColorBit uint16 = 0x8000 + nodeFlagsMask = ^nodeColorBit ) func (n *memdbNode) getKeyFlags() kv.KeyFlags { @@ -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) } diff --git a/internal/unionstore/memdb_arena.go b/internal/unionstore/memdb_arena.go index 0db8090b..5addc883 100644 --- a/internal/unionstore/memdb_arena.go +++ b/internal/unionstore/memdb_arena.go @@ -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 diff --git a/kv/keyflags.go b/kv/keyflags.go index 64146779..133e5079 100644 --- a/kv/keyflags.go +++ b/kv/keyflags.go @@ -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 )