refactor: standardized code

master
710leo 5 years ago
parent 7f1a947226
commit 9c129acdb4

File diff suppressed because it is too large Load Diff

@ -36,49 +36,49 @@ func NewIndexCacheBase(max int) *IndexCacheBase {
return &IndexCacheBase{maxSize: max, data: make(map[interface{}]*dataobj.TsdbItem)} return &IndexCacheBase{maxSize: max, data: make(map[interface{}]*dataobj.TsdbItem)}
} }
func (this *IndexCacheBase) Put(key interface{}, item *dataobj.TsdbItem) { func (i *IndexCacheBase) Put(key interface{}, item *dataobj.TsdbItem) {
this.Lock() i.Lock()
defer this.Unlock() defer i.Unlock()
this.data[key] = item i.data[key] = item
} }
func (this *IndexCacheBase) Get(key interface{}) *dataobj.TsdbItem { func (i *IndexCacheBase) Get(key interface{}) *dataobj.TsdbItem {
this.RLock() i.RLock()
defer this.RUnlock() defer i.RUnlock()
return this.data[key] return i.data[key]
} }
func (this *IndexCacheBase) ContainsKey(key interface{}) bool { func (i *IndexCacheBase) ContainsKey(key interface{}) bool {
this.RLock() i.RLock()
defer this.RUnlock() defer i.RUnlock()
return this.data[key] != nil return i.data[key] != nil
} }
func (this *IndexCacheBase) Size() int { func (i *IndexCacheBase) Size() int {
this.RLock() i.RLock()
defer this.RUnlock() defer i.RUnlock()
return len(this.data) return len(i.data)
} }
func (this *IndexCacheBase) Keys() []interface{} { func (i *IndexCacheBase) Keys() []interface{} {
this.RLock() i.RLock()
defer this.RUnlock() defer i.RUnlock()
count := len(this.data) count := len(i.data)
if count == 0 { if count == 0 {
return []interface{}{} return []interface{}{}
} }
keys := make([]interface{}, 0, count) keys := make([]interface{}, 0, count)
for key := range this.data { for key := range i.data {
keys = append(keys, key) keys = append(keys, key)
} }
return keys return keys
} }
func (this *IndexCacheBase) Remove(key interface{}) { func (i *IndexCacheBase) Remove(key interface{}) {
this.Lock() i.Lock()
defer this.Unlock() defer i.Unlock()
delete(this.data, key) delete(i.data, key)
} }

@ -11,25 +11,25 @@ type ConsistentHashRing struct {
ring *consistent.Consistent ring *consistent.Consistent
} }
func (this *ConsistentHashRing) GetNode(pk string) (string, error) { func (c *ConsistentHashRing) GetNode(pk string) (string, error) {
this.RLock() c.RLock()
defer this.RUnlock() defer c.RUnlock()
return this.ring.Get(pk) return c.ring.Get(pk)
} }
func (this *ConsistentHashRing) Set(r *consistent.Consistent) { func (c *ConsistentHashRing) Set(r *consistent.Consistent) {
this.Lock() c.Lock()
defer this.Unlock() defer c.Unlock()
this.ring = r c.ring = r
return return
} }
func (this *ConsistentHashRing) GetRing() *consistent.Consistent { func (c *ConsistentHashRing) GetRing() *consistent.Consistent {
this.RLock() c.RLock()
defer this.RUnlock() defer c.RUnlock()
return this.ring return c.ring
} }
func NewConsistentHashRing(replicas int32, nodes []string) *ConsistentHashRing { func NewConsistentHashRing(replicas int32, nodes []string) *ConsistentHashRing {

Loading…
Cancel
Save