From 008ef13a0c9f9517879ae48bc503d678a06bac0b Mon Sep 17 00:00:00 2001 From: snower Date: Sun, 21 Apr 2024 14:56:03 +0800 Subject: [PATCH] Optimize lock data and add unset instruction logic --- protocol/command.go | 5 ++++- server/db.go | 10 +++++++--- server/lock.go | 25 ++++++++++++++++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/protocol/command.go b/protocol/command.go index 50abb0f..0c51142 100755 --- a/protocol/command.go +++ b/protocol/command.go @@ -93,7 +93,10 @@ const ( CALL_COMMAND_CHARSET_UTF8 = 1 ) -const LOCK_DATA_COMMAND_TYPE_SET = 0 +const ( + LOCK_DATA_COMMAND_TYPE_SET = 0 + LOCK_DATA_COMMAND_TYPE_UNSET = 1 +) var ERROR_MSG []string = []string{ "OK", diff --git a/server/db.go b/server/db.go index 000c628..c3294f1 100755 --- a/server/db.go +++ b/server/db.go @@ -2230,12 +2230,12 @@ func (self *LockDB) DoAckLock(lock *Lock, succed bool) { lock.expriedTime = lock.startTime + int64(lock.command.Expried)/1000 + 1 } - var lockData []byte + var lockData []byte = nil if lockManager.currentData != nil { currentData := lockManager.currentData if currentData.recoverLock == lock { if currentData.recoverData != nil { - lockData = currentData.recoverData.Data + lockData = currentData.recoverData.GetData() } currentData.recoverLock = nil currentData.recoverData = nil @@ -2264,7 +2264,11 @@ func (self *LockDB) DoAckLock(lock *Lock, succed bool) { if lockManager.currentData != nil { currentData := lockManager.currentData if currentData.recoverLock == lock { - lockManager.currentData = currentData.recoverData + if currentData.recoverData == nil { + lockManager.currentData = NewLockDataUnsetData() + } else { + lockManager.currentData = currentData.recoverData + } currentData.recoverLock = nil currentData.recoverData = nil } diff --git a/server/lock.go b/server/lock.go index 3821f6c..1038663 100755 --- a/server/lock.go +++ b/server/lock.go @@ -328,7 +328,7 @@ func (self *LockManager) GetOrNewLock(serverProtocol ServerProtocol, command *pr func (self *LockManager) GetLockData() []byte { if self.currentData != nil { - return self.currentData.Data + return self.currentData.GetData() } return nil } @@ -336,7 +336,7 @@ func (self *LockManager) GetLockData() []byte { func (self *LockManager) AofLockData(commandType uint8) []byte { if self.currentData != nil && (commandType == protocol.COMMAND_LOCK || !self.currentData.isAof) { self.currentData.isAof = true - return self.currentData.Data + return self.currentData.data } return nil } @@ -349,6 +349,8 @@ func (self *LockManager) ProcessLockData(command *protocol.LockCommand) { switch lockCommandData.CommandType { case protocol.LOCK_DATA_COMMAND_TYPE_SET: self.currentData = NewLockData(lockCommandData.Data) + case protocol.LOCK_DATA_COMMAND_TYPE_UNSET: + self.currentData = NewLockDataUnsetData() } command.Data = nil } @@ -385,14 +387,27 @@ func (self *Lock) GetDB() *LockDB { } type LockData struct { - Data []byte - recoverData *LockData + data []byte recoverLock *Lock + recoverData *LockData + commandType uint8 isAof bool } func NewLockData(data []byte) *LockData { - return &LockData{data, nil, nil, false} + return &LockData{data, nil, nil, data[4], false} +} + +func NewLockDataUnsetData() *LockData { + return &LockData{[]byte{2, 0, 0, 0, protocol.LOCK_DATA_COMMAND_TYPE_UNSET, 0}, nil, nil, + protocol.LOCK_DATA_COMMAND_TYPE_UNSET, false} +} + +func (self *LockData) GetData() []byte { + if self.data != nil && self.commandType != protocol.LOCK_DATA_COMMAND_TYPE_UNSET { + return self.data + } + return nil } type PriorityMutex struct {