2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
|
|
|
|
|
//
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package gmap
|
|
|
|
|
|
|
2019-01-12 23:36:22 +08:00
|
|
|
|
import "gitee.com/johng/gf/g/internal/rwmutex"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
|
|
|
|
|
type StringStringMap struct {
|
2018-09-05 18:34:41 +08:00
|
|
|
|
mu *rwmutex.RWMutex
|
2018-01-16 15:38:53 +08:00
|
|
|
|
m map[string]string
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func NewStringStringMap(unsafe...bool) *StringStringMap {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return &StringStringMap{
|
2018-09-05 18:34:41 +08:00
|
|
|
|
m : make(map[string]string),
|
2019-01-12 23:36:22 +08:00
|
|
|
|
mu : rwmutex.New(unsafe...),
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-15 22:02:06 +08:00
|
|
|
|
// 给定回调函数对原始内容进行遍历,回调函数返回true表示继续遍历,否则停止遍历
|
2018-04-11 16:06:45 +08:00
|
|
|
|
func (this *StringStringMap) Iterator(f func (k string, v string) bool) {
|
2018-02-07 17:55:52 +08:00
|
|
|
|
this.mu.RLock()
|
2018-09-06 19:16:08 +08:00
|
|
|
|
defer this.mu.RUnlock()
|
2018-02-07 17:55:52 +08:00
|
|
|
|
for k, v := range this.m {
|
2018-04-11 16:06:45 +08:00
|
|
|
|
if !f(k, v) {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
2018-02-07 17:55:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 哈希表克隆
|
2018-09-04 22:32:43 +08:00
|
|
|
|
func (this *StringStringMap) Clone() map[string]string {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
m := make(map[string]string)
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
for k, v := range this.m {
|
|
|
|
|
|
m[k] = v
|
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2018-09-04 22:32:43 +08:00
|
|
|
|
return m
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置键值对
|
|
|
|
|
|
func (this *StringStringMap) Set(key string, val string) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
this.m[key] = val
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量设置键值对
|
|
|
|
|
|
func (this *StringStringMap) BatchSet(m map[string]string) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
for k, v := range m {
|
|
|
|
|
|
this.m[k] = v
|
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取键值
|
|
|
|
|
|
func (this *StringStringMap) Get(key string) string {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
val, _ := this.m[key]
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return val
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 09:28:45 +08:00
|
|
|
|
// 设置kv缓存键值对,内部会对键名的存在性使用写锁进行二次检索确认,如果存在则不再写入;返回键名对应的键值。
|
|
|
|
|
|
// 在高并发下有用,防止数据写入的并发逻辑错误。
|
|
|
|
|
|
func (this *StringStringMap) doSetWithLockCheck(key string, value string) string {
|
2018-03-13 17:57:41 +08:00
|
|
|
|
this.mu.Lock()
|
2018-09-20 09:28:45 +08:00
|
|
|
|
if v, ok := this.m[key]; ok {
|
|
|
|
|
|
this.mu.Unlock()
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
this.m[key] = value
|
|
|
|
|
|
this.mu.Unlock()
|
|
|
|
|
|
return value
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 当键名存在时返回其键值,否则写入指定的键值
|
|
|
|
|
|
func (this *StringStringMap) GetOrSet(key string, value string) string {
|
|
|
|
|
|
this.mu.RLock()
|
|
|
|
|
|
v, ok := this.m[key]
|
|
|
|
|
|
this.mu.RUnlock()
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return this.doSetWithLockCheck(key, value)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 当键名存在时返回其键值,否则写入指定的键值,键值由指定的函数生成
|
|
|
|
|
|
func (this *StringStringMap) GetOrSetFunc(key string, f func() string) string {
|
|
|
|
|
|
this.mu.RLock()
|
|
|
|
|
|
v, ok := this.m[key]
|
|
|
|
|
|
this.mu.RUnlock()
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return this.doSetWithLockCheck(key, f())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 与GetOrSetFunc不同的是,f是在写锁机制内执行
|
|
|
|
|
|
func (this *StringStringMap) GetOrSetFuncLock(key string, f func() string) string {
|
|
|
|
|
|
this.mu.RLock()
|
2018-03-13 17:57:41 +08:00
|
|
|
|
val, ok := this.m[key]
|
2018-09-20 09:28:45 +08:00
|
|
|
|
this.mu.RUnlock()
|
2018-03-13 17:57:41 +08:00
|
|
|
|
if !ok {
|
2018-09-20 09:28:45 +08:00
|
|
|
|
this.mu.Lock()
|
|
|
|
|
|
defer this.mu.Unlock()
|
|
|
|
|
|
if v, ok := this.m[key]; ok {
|
|
|
|
|
|
this.mu.Unlock()
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
val = f()
|
|
|
|
|
|
this.m[key] = val
|
|
|
|
|
|
return val
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return val
|
2018-03-13 17:57:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-20 09:28:45 +08:00
|
|
|
|
// 当键名不存在时写入,并返回true;否则返回false。
|
|
|
|
|
|
func (this *StringStringMap) SetIfNotExist(key string, value string) bool {
|
|
|
|
|
|
if !this.Contains(key) {
|
|
|
|
|
|
this.doSetWithLockCheck(key, value)
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量删除键值对
|
|
|
|
|
|
func (this *StringStringMap) BatchRemove(keys []string) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
for _, key := range keys {
|
|
|
|
|
|
delete(this.m, key)
|
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返回对应的键值,并删除该键值
|
2018-09-20 09:28:45 +08:00
|
|
|
|
func (this *StringStringMap) Remove(key string) string {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
val, exists := this.m[key]
|
|
|
|
|
|
if exists {
|
|
|
|
|
|
delete(this.m, key)
|
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return val
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返回键列表
|
|
|
|
|
|
func (this *StringStringMap) Keys() []string {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
keys := make([]string, 0)
|
|
|
|
|
|
for key, _ := range this.m {
|
|
|
|
|
|
keys = append(keys, key)
|
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return keys
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返回值列表(注意是随机排序)
|
|
|
|
|
|
func (this *StringStringMap) Values() []string {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
vals := make([]string, 0)
|
|
|
|
|
|
for _, val := range this.m {
|
|
|
|
|
|
vals = append(vals, val)
|
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return vals
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 是否存在某个键
|
|
|
|
|
|
func (this *StringStringMap) Contains(key string) bool {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
_, exists := this.m[key]
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return exists
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 哈希表大小
|
|
|
|
|
|
func (this *StringStringMap) Size() int {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2018-03-29 13:46:05 +08:00
|
|
|
|
length := len(this.m)
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2018-03-29 13:46:05 +08:00
|
|
|
|
return length
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 哈希表是否为空
|
|
|
|
|
|
func (this *StringStringMap) IsEmpty() bool {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2018-08-23 21:55:27 +08:00
|
|
|
|
empty := len(this.m) == 0
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return empty
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空哈希表
|
|
|
|
|
|
func (this *StringStringMap) Clear() {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
this.m = make(map[string]string)
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-04-15 22:02:06 +08:00
|
|
|
|
|
2018-09-06 14:48:55 +08:00
|
|
|
|
// 并发安全写锁操作,使用自定义方法执行加锁修改操作
|
2018-04-15 22:02:06 +08:00
|
|
|
|
func (this *StringStringMap) LockFunc(f func(m map[string]string)) {
|
2018-09-06 14:48:55 +08:00
|
|
|
|
this.mu.Lock(true)
|
|
|
|
|
|
defer this.mu.Unlock(true)
|
2018-04-15 22:02:06 +08:00
|
|
|
|
f(this.m)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-06 14:48:55 +08:00
|
|
|
|
// 并发安全读锁操作,使用自定义方法执行加锁读取操作
|
2018-04-15 22:02:06 +08:00
|
|
|
|
func (this *StringStringMap) RLockFunc(f func(m map[string]string)) {
|
2018-09-06 14:48:55 +08:00
|
|
|
|
this.mu.RLock(true)
|
|
|
|
|
|
defer this.mu.RUnlock(true)
|
2018-04-15 22:02:06 +08:00
|
|
|
|
f(this.m)
|
|
|
|
|
|
}
|