refactor: fix variable names & remove unnecessary type conversion

- the name of the variable "shard" has been defined, and change variable "shard" `s name in the function to avoid the case of "Variable Shadowing"
- remove unnecessary type conversion "uint32(hashCode)"
This commit is contained in:
Tianyi Zheng
2022-11-11 13:44:51 +08:00
committed by finley
parent 67a9eae62f
commit 672f0571ca

View File

@@ -69,7 +69,7 @@ func (dict *ConcurrentDict) spread(hashCode uint32) uint32 {
panic("dict is nil") panic("dict is nil")
} }
tableSize := uint32(len(dict.table)) tableSize := uint32(len(dict.table))
return (tableSize - 1) & uint32(hashCode) return (tableSize - 1) & hashCode
} }
func (dict *ConcurrentDict) getShard(index uint32) *shard { func (dict *ConcurrentDict) getShard(index uint32) *shard {
@@ -86,10 +86,10 @@ func (dict *ConcurrentDict) Get(key string) (val interface{}, exists bool) {
} }
hashCode := fnv32(key) hashCode := fnv32(key)
index := dict.spread(hashCode) index := dict.spread(hashCode)
shard := dict.getShard(index) s := dict.getShard(index)
shard.mutex.RLock() s.mutex.RLock()
defer shard.mutex.RUnlock() defer s.mutex.RUnlock()
val, exists = shard.m[key] val, exists = s.m[key]
return return
} }
@@ -108,16 +108,16 @@ func (dict *ConcurrentDict) Put(key string, val interface{}) (result int) {
} }
hashCode := fnv32(key) hashCode := fnv32(key)
index := dict.spread(hashCode) index := dict.spread(hashCode)
shard := dict.getShard(index) s := dict.getShard(index)
dict.addCount() dict.addCount()
shard.mutex.Lock() s.mutex.Lock()
defer shard.mutex.Unlock() defer s.mutex.Unlock()
if _, ok := shard.m[key]; ok { if _, ok := s.m[key]; ok {
shard.m[key] = val s.m[key] = val
return 0 return 0
} }
shard.m[key] = val s.m[key] = val
return 1 return 1
} }
@@ -128,14 +128,14 @@ func (dict *ConcurrentDict) PutIfAbsent(key string, val interface{}) (result int
} }
hashCode := fnv32(key) hashCode := fnv32(key)
index := dict.spread(hashCode) index := dict.spread(hashCode)
shard := dict.getShard(index) s := dict.getShard(index)
shard.mutex.Lock() s.mutex.Lock()
defer shard.mutex.Unlock() defer s.mutex.Unlock()
if _, ok := shard.m[key]; ok { if _, ok := s.m[key]; ok {
return 0 return 0
} }
shard.m[key] = val s.m[key] = val
dict.addCount() dict.addCount()
return 1 return 1
} }
@@ -147,12 +147,12 @@ func (dict *ConcurrentDict) PutIfExists(key string, val interface{}) (result int
} }
hashCode := fnv32(key) hashCode := fnv32(key)
index := dict.spread(hashCode) index := dict.spread(hashCode)
shard := dict.getShard(index) s := dict.getShard(index)
shard.mutex.Lock() s.mutex.Lock()
defer shard.mutex.Unlock() defer s.mutex.Unlock()
if _, ok := shard.m[key]; ok { if _, ok := s.m[key]; ok {
shard.m[key] = val s.m[key] = val
return 1 return 1
} }
return 0 return 0
@@ -165,12 +165,12 @@ func (dict *ConcurrentDict) Remove(key string) (result int) {
} }
hashCode := fnv32(key) hashCode := fnv32(key)
index := dict.spread(hashCode) index := dict.spread(hashCode)
shard := dict.getShard(index) s := dict.getShard(index)
shard.mutex.Lock() s.mutex.Lock()
defer shard.mutex.Unlock() defer s.mutex.Unlock()
if _, ok := shard.m[key]; ok { if _, ok := s.m[key]; ok {
delete(shard.m, key) delete(s.m, key)
dict.decreaseCount() dict.decreaseCount()
return 1 return 1
} }
@@ -192,11 +192,11 @@ func (dict *ConcurrentDict) ForEach(consumer Consumer) {
panic("dict is nil") panic("dict is nil")
} }
for _, shard := range dict.table { for _, s := range dict.table {
shard.mutex.RLock() s.mutex.RLock()
func() { func() {
defer shard.mutex.RUnlock() defer s.mutex.RUnlock()
for key, value := range shard.m { for key, value := range s.m {
continues := consumer(key, value) continues := consumer(key, value)
if !continues { if !continues {
return return
@@ -247,11 +247,11 @@ func (dict *ConcurrentDict) RandomKeys(limit int) []string {
result := make([]string, limit) result := make([]string, limit)
nR := rand.New(rand.NewSource(time.Now().UnixNano())) nR := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < limit; { for i := 0; i < limit; {
shard := dict.getShard(uint32(nR.Intn(shardCount))) s := dict.getShard(uint32(nR.Intn(shardCount)))
if shard == nil { if s == nil {
continue continue
} }
key := shard.RandomKey() key := s.RandomKey()
if key != "" { if key != "" {
result[i] = key result[i] = key
i++ i++
@@ -272,11 +272,11 @@ func (dict *ConcurrentDict) RandomDistinctKeys(limit int) []string {
nR := rand.New(rand.NewSource(time.Now().UnixNano())) nR := rand.New(rand.NewSource(time.Now().UnixNano()))
for len(result) < limit { for len(result) < limit {
shardIndex := uint32(nR.Intn(shardCount)) shardIndex := uint32(nR.Intn(shardCount))
shard := dict.getShard(shardIndex) s := dict.getShard(shardIndex)
if shard == nil { if s == nil {
continue continue
} }
key := shard.RandomKey() key := s.RandomKey()
if key != "" { if key != "" {
if _, exists := result[key]; !exists { if _, exists := result[key]; !exists {
result[key] = struct{}{} result[key] = struct{}{}