mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 16:57:06 +08:00
optimize locks for sinterstore/sunionstore/sdiffstore
This commit is contained in:
@@ -126,3 +126,41 @@ func (locks *Locks) RUnLocks(keys ...string) {
|
||||
mu.RUnlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (locks *Locks) RWLocks(writeKeys []string, readKeys []string) {
|
||||
keys := append(writeKeys, readKeys...)
|
||||
indices := locks.toLockIndices(keys, false)
|
||||
writeIndices := locks.toLockIndices(writeKeys, false)
|
||||
writeIndexSet := make(map[uint32]struct{})
|
||||
for _, idx := range writeIndices {
|
||||
writeIndexSet[idx] = struct{}{}
|
||||
}
|
||||
for _, index := range indices {
|
||||
_, w := writeIndexSet[index]
|
||||
mu := locks.table[index]
|
||||
if w {
|
||||
mu.Lock()
|
||||
} else {
|
||||
mu.RLock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (locks *Locks) RWUnLocks(writeKeys []string, readKeys []string) {
|
||||
keys := append(writeKeys, readKeys...)
|
||||
indices := locks.toLockIndices(keys, true)
|
||||
writeIndices := locks.toLockIndices(writeKeys, true)
|
||||
writeIndexSet := make(map[uint32]struct{})
|
||||
for _, idx := range writeIndices {
|
||||
writeIndexSet[idx] = struct{}{}
|
||||
}
|
||||
for _, index := range indices {
|
||||
_, w := writeIndexSet[index]
|
||||
mu := locks.table[index]
|
||||
if w {
|
||||
mu.Unlock()
|
||||
} else {
|
||||
mu.RUnlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
10
db.go
10
db.go
@@ -220,6 +220,16 @@ func (db *DB) RUnLocks(keys ...string) {
|
||||
db.locker.RUnLocks(keys...)
|
||||
}
|
||||
|
||||
// RWLocks lock keys for writing and reading
|
||||
func (db *DB) RWLocks(writeKeys []string, readKeys []string) {
|
||||
db.locker.RWLocks(writeKeys, readKeys)
|
||||
}
|
||||
|
||||
// RWUnLocks unlock keys for writing and reading
|
||||
func (db *DB) RWUnLocks(writeKeys []string, readKeys []string) {
|
||||
db.locker.RWUnLocks(writeKeys, readKeys)
|
||||
}
|
||||
|
||||
/* ---- TTL Functions ---- */
|
||||
|
||||
func genExpireTask(key string) string {
|
||||
|
21
set.go
21
set.go
@@ -208,11 +208,8 @@ func execSInterStore(db *DB, args [][]byte) redis.Reply {
|
||||
}
|
||||
|
||||
// lock
|
||||
lockedKeySet := HashSet.Make(keys...)
|
||||
lockedKeySet.Add(dest)
|
||||
lockedKeys := lockedKeySet.ToSlice()
|
||||
db.Locks(lockedKeys...)
|
||||
defer db.UnLocks(lockedKeys...)
|
||||
db.RWLocks([]string{dest}, keys)
|
||||
defer db.RWUnLocks([]string{dest}, keys)
|
||||
|
||||
var result *HashSet.Set
|
||||
for _, key := range keys {
|
||||
@@ -299,11 +296,8 @@ func execSUnionStore(db *DB, args [][]byte) redis.Reply {
|
||||
}
|
||||
|
||||
// lock
|
||||
lockedKeySet := HashSet.Make(keys...)
|
||||
lockedKeySet.Add(dest)
|
||||
lockedKeys := lockedKeySet.ToSlice()
|
||||
db.Locks(lockedKeys...)
|
||||
defer db.UnLocks(lockedKeys...)
|
||||
db.RWLocks([]string{dest}, keys)
|
||||
defer db.RWUnLocks([]string{dest}, keys)
|
||||
|
||||
var result *HashSet.Set
|
||||
for _, key := range keys {
|
||||
@@ -397,11 +391,8 @@ func execSDiffStore(db *DB, args [][]byte) redis.Reply {
|
||||
}
|
||||
|
||||
// lock
|
||||
lockedKeySet := HashSet.Make(keys...)
|
||||
lockedKeySet.Add(dest)
|
||||
lockedKeys := lockedKeySet.ToSlice()
|
||||
db.Locks(lockedKeys...)
|
||||
defer db.UnLocks(lockedKeys...)
|
||||
db.RWLocks([]string{dest}, keys)
|
||||
defer db.RWUnLocks([]string{dest}, keys)
|
||||
|
||||
var result *HashSet.Set
|
||||
for i, key := range keys {
|
||||
|
Reference in New Issue
Block a user