string_any_map.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Package mhayaMap file from https://github.com/gogf/gf
  2. package mhayaMap
  3. import (
  4. "encoding/json"
  5. "sync"
  6. "unsafe"
  7. cutils "github.com/mhaya/extend/utils"
  8. )
  9. type StringAnyMap struct {
  10. mu *sync.RWMutex
  11. data map[string]interface{}
  12. }
  13. // NewStrAnyMap returns an empty StrAnyMap object.
  14. // The parameter <safe> is used to specify whether using map in concurrent-safety,
  15. // which is false in default.
  16. func NewStrAnyMap() *StringAnyMap {
  17. return &StringAnyMap{
  18. mu: &sync.RWMutex{},
  19. data: make(map[string]interface{}),
  20. }
  21. }
  22. // NewStrAnyMapFrom creates and returns a hash map from given map <data>.
  23. // Note that, the param <data> map will be set as the underlying data map(no deep copy),
  24. // there might be some concurrent-safe issues when changing the map outside.
  25. func NewStrAnyMapFrom(data map[string]interface{}) *StringAnyMap {
  26. return &StringAnyMap{
  27. mu: &sync.RWMutex{},
  28. data: data,
  29. }
  30. }
  31. // Iterator iterates the hash map readonly with custom callback function <f>.
  32. // If <f> returns true, then it continues iterating; or false to stop.
  33. func (m *StringAnyMap) Iterator(f func(k string, v interface{}) bool) {
  34. m.mu.RLock()
  35. defer m.mu.RUnlock()
  36. for k, v := range m.data {
  37. if !f(k, v) {
  38. break
  39. }
  40. }
  41. }
  42. // Clone returns a new hash map with copy of current map data.
  43. func (m *StringAnyMap) Clone() *StringAnyMap {
  44. return NewStrAnyMapFrom(m.MapCopy())
  45. }
  46. // Map returns the underlying data map.
  47. // Note that, if it's in concurrent-safe usage, it returns a copy of underlying data,
  48. // or else a pointer to the underlying data.
  49. func (m *StringAnyMap) Map() map[string]interface{} {
  50. m.mu.RLock()
  51. defer m.mu.RUnlock()
  52. data := make(map[string]interface{}, len(m.data))
  53. for k, v := range m.data {
  54. data[k] = v
  55. }
  56. return data
  57. }
  58. // MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.
  59. func (m *StringAnyMap) MapStrAny() map[string]interface{} {
  60. return m.Map()
  61. }
  62. // MapCopy returns a copy of the underlying data of the hash map.
  63. func (m *StringAnyMap) MapCopy() map[string]interface{} {
  64. m.mu.RLock()
  65. defer m.mu.RUnlock()
  66. data := make(map[string]interface{}, len(m.data))
  67. for k, v := range m.data {
  68. data[k] = v
  69. }
  70. return data
  71. }
  72. // FilterEmpty deletes all key-value pair of which the value is empty.
  73. // Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
  74. func (m *StringAnyMap) FilterEmpty() {
  75. m.mu.Lock()
  76. for k, v := range m.data {
  77. if cutils.IsEmpty(v) {
  78. delete(m.data, k)
  79. }
  80. }
  81. m.mu.Unlock()
  82. }
  83. // FilterNil deletes all key-value pair of which the value is nil.
  84. func (m *StringAnyMap) FilterNil() {
  85. m.mu.Lock()
  86. defer m.mu.Unlock()
  87. for k, v := range m.data {
  88. if cutils.IsNil(v) {
  89. delete(m.data, k)
  90. }
  91. }
  92. }
  93. // Set sets key-value to the hash map.
  94. func (m *StringAnyMap) Set(key string, val interface{}) {
  95. m.mu.Lock()
  96. if m.data == nil {
  97. m.data = make(map[string]interface{})
  98. }
  99. m.data[key] = val
  100. m.mu.Unlock()
  101. }
  102. // Sets batch sets key-values to the hash map.
  103. func (m *StringAnyMap) Sets(data map[string]interface{}) {
  104. m.mu.Lock()
  105. if m.data == nil {
  106. m.data = data
  107. } else {
  108. for k, v := range data {
  109. m.data[k] = v
  110. }
  111. }
  112. m.mu.Unlock()
  113. }
  114. // Search searches the map with given <key>.
  115. // Second return parameter <found> is true if key was found, otherwise false.
  116. func (m *StringAnyMap) Search(key string) (value interface{}, found bool) {
  117. m.mu.RLock()
  118. if m.data != nil {
  119. value, found = m.data[key]
  120. }
  121. m.mu.RUnlock()
  122. return
  123. }
  124. // Get returns the value by given <key>.
  125. func (m *StringAnyMap) Get(key string) (value interface{}) {
  126. m.mu.RLock()
  127. if m.data != nil {
  128. value = m.data[key]
  129. }
  130. m.mu.RUnlock()
  131. return
  132. }
  133. // Pop retrieves and deletes an item from the map.
  134. func (m *StringAnyMap) Pop() (key string, value interface{}) {
  135. m.mu.Lock()
  136. defer m.mu.Unlock()
  137. for key, value = range m.data {
  138. delete(m.data, key)
  139. return
  140. }
  141. return
  142. }
  143. // Pops retrieves and deletes <size> items from the map.
  144. // It returns all items if size == -1.
  145. func (m *StringAnyMap) Pops(size int) map[string]interface{} {
  146. m.mu.Lock()
  147. defer m.mu.Unlock()
  148. if size > len(m.data) || size == -1 {
  149. size = len(m.data)
  150. }
  151. if size == 0 {
  152. return nil
  153. }
  154. var (
  155. index = 0
  156. newMap = make(map[string]interface{}, size)
  157. )
  158. for k, v := range m.data {
  159. delete(m.data, k)
  160. newMap[k] = v
  161. index++
  162. if index == size {
  163. break
  164. }
  165. }
  166. return newMap
  167. }
  168. // doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
  169. // if not exists, set value to the map with given <key>,
  170. // or else just return the existing value.
  171. //
  172. // When setting value, if <value> is type of <func() interface {}>,
  173. // it will be executed with mutex.Lock of the hash map,
  174. // and its return value will be set to the map with <key>.
  175. //
  176. // It returns value with given <key>.
  177. func (m *StringAnyMap) doSetWithLockCheck(key string, value interface{}) interface{} {
  178. m.mu.Lock()
  179. defer m.mu.Unlock()
  180. if m.data == nil {
  181. m.data = make(map[string]interface{})
  182. }
  183. if v, ok := m.data[key]; ok {
  184. return v
  185. }
  186. if f, ok := value.(func() interface{}); ok {
  187. value = f()
  188. }
  189. if value != nil {
  190. m.data[key] = value
  191. }
  192. return value
  193. }
  194. // GetOrSet returns the value by key,
  195. // or sets value with given <value> if it does not exist and then returns this value.
  196. func (m *StringAnyMap) GetOrSet(key string, value interface{}) interface{} {
  197. if v, ok := m.Search(key); !ok {
  198. return m.doSetWithLockCheck(key, value)
  199. } else {
  200. return v
  201. }
  202. }
  203. // GetOrSetFunc returns the value by key,
  204. // or sets value with returned value of callback function <f> if it does not exist
  205. // and then returns this value.
  206. func (m *StringAnyMap) GetOrSetFunc(key string, f func() interface{}) interface{} {
  207. if v, ok := m.Search(key); !ok {
  208. return m.doSetWithLockCheck(key, f())
  209. } else {
  210. return v
  211. }
  212. }
  213. // GetOrSetFuncLock returns the value by key,
  214. // or sets value with returned value of callback function <f> if it does not exist
  215. // and then returns this value.
  216. //
  217. // GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
  218. // with mutex.Lock of the hash map.
  219. func (m *StringAnyMap) GetOrSetFuncLock(key string, f func() interface{}) interface{} {
  220. if v, ok := m.Search(key); !ok {
  221. return m.doSetWithLockCheck(key, f)
  222. } else {
  223. return v
  224. }
  225. }
  226. // GetVar returns a Var with the value by given <key>.
  227. // The returned Var is un-concurrent safe.
  228. func (m *StringAnyMap) GetVar(key string) interface{} {
  229. return m.Get(key)
  230. }
  231. // GetVarOrSet returns a Var with result from GetVarOrSet.
  232. // The returned Var is un-concurrent safe.
  233. func (m *StringAnyMap) GetVarOrSet(key string, value interface{}) interface{} {
  234. return m.GetOrSet(key, value)
  235. }
  236. // GetVarOrSetFunc returns a Var with result from GetOrSetFunc.
  237. // The returned Var is un-concurrent safe.
  238. func (m *StringAnyMap) GetVarOrSetFunc(key string, f func() interface{}) interface{} {
  239. return m.GetOrSetFunc(key, f)
  240. }
  241. // GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock.
  242. // The returned Var is un-concurrent safe.
  243. func (m *StringAnyMap) GetVarOrSetFuncLock(key string, f func() interface{}) interface{} {
  244. return m.GetOrSetFuncLock(key, f)
  245. }
  246. // SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
  247. // It returns false if <key> exists, and <value> would be ignored.
  248. func (m *StringAnyMap) SetIfNotExist(key string, value interface{}) bool {
  249. if !m.Contains(key) {
  250. m.doSetWithLockCheck(key, value)
  251. return true
  252. }
  253. return false
  254. }
  255. // SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
  256. // It returns false if <key> exists, and <value> would be ignored.
  257. func (m *StringAnyMap) SetIfNotExistFunc(key string, f func() interface{}) bool {
  258. if !m.Contains(key) {
  259. m.doSetWithLockCheck(key, f())
  260. return true
  261. }
  262. return false
  263. }
  264. // SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
  265. // It returns false if <key> exists, and <value> would be ignored.
  266. //
  267. // SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
  268. // it executes function <f> with mutex.Lock of the hash map.
  269. func (m *StringAnyMap) SetIfNotExistFuncLock(key string, f func() interface{}) bool {
  270. if !m.Contains(key) {
  271. m.doSetWithLockCheck(key, f)
  272. return true
  273. }
  274. return false
  275. }
  276. // Removes batch deletes values of the map by keys.
  277. func (m *StringAnyMap) Removes(keys []string) {
  278. m.mu.Lock()
  279. if m.data != nil {
  280. for _, key := range keys {
  281. delete(m.data, key)
  282. }
  283. }
  284. m.mu.Unlock()
  285. }
  286. // Remove deletes value from map by given <key>, and return this deleted value.
  287. func (m *StringAnyMap) Remove(key string) (value interface{}) {
  288. m.mu.Lock()
  289. if m.data != nil {
  290. var ok bool
  291. if value, ok = m.data[key]; ok {
  292. delete(m.data, key)
  293. }
  294. }
  295. m.mu.Unlock()
  296. return
  297. }
  298. // Keys returns all keys of the map as a slice.
  299. func (m *StringAnyMap) Keys() []string {
  300. m.mu.RLock()
  301. var (
  302. keys = make([]string, len(m.data))
  303. index = 0
  304. )
  305. for key := range m.data {
  306. keys[index] = key
  307. index++
  308. }
  309. m.mu.RUnlock()
  310. return keys
  311. }
  312. // Values returns all values of the map as a slice.
  313. func (m *StringAnyMap) Values() []interface{} {
  314. m.mu.RLock()
  315. var (
  316. values = make([]interface{}, len(m.data))
  317. index = 0
  318. )
  319. for _, value := range m.data {
  320. values[index] = value
  321. index++
  322. }
  323. m.mu.RUnlock()
  324. return values
  325. }
  326. // Contains checks whether a key exists.
  327. // It returns true if the <key> exists, or else false.
  328. func (m *StringAnyMap) Contains(key string) bool {
  329. var ok bool
  330. m.mu.RLock()
  331. if m.data != nil {
  332. _, ok = m.data[key]
  333. }
  334. m.mu.RUnlock()
  335. return ok
  336. }
  337. // Size returns the size of the map.
  338. func (m *StringAnyMap) Size() int {
  339. m.mu.RLock()
  340. length := len(m.data)
  341. m.mu.RUnlock()
  342. return length
  343. }
  344. // IsEmpty checks whether the map is empty.
  345. // It returns true if map is empty, or else false.
  346. func (m *StringAnyMap) IsEmpty() bool {
  347. return m.Size() == 0
  348. }
  349. // Clear deletes all data of the map, it will remake a new underlying data map.
  350. func (m *StringAnyMap) Clear() {
  351. m.mu.Lock()
  352. m.data = make(map[string]interface{})
  353. m.mu.Unlock()
  354. }
  355. // Replace the data of the map with given <data>.
  356. func (m *StringAnyMap) Replace(data map[string]interface{}) {
  357. m.mu.Lock()
  358. m.data = data
  359. m.mu.Unlock()
  360. }
  361. // LockFunc locks writing with given callback function <f> within RWMutex.Lock.
  362. func (m *StringAnyMap) LockFunc(f func(m map[string]interface{})) {
  363. m.mu.Lock()
  364. defer m.mu.Unlock()
  365. f(m.data)
  366. }
  367. // RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
  368. func (m *StringAnyMap) RLockFunc(f func(m map[string]interface{})) {
  369. m.mu.RLock()
  370. defer m.mu.RUnlock()
  371. f(m.data)
  372. }
  373. // Merge merges two hash maps.
  374. // The <other> map will be merged into the map <m>.
  375. func (m *StringAnyMap) Merge(other *StringAnyMap) {
  376. m.mu.Lock()
  377. defer m.mu.Unlock()
  378. if m.data == nil {
  379. m.data = other.MapCopy()
  380. return
  381. }
  382. if other != m {
  383. other.mu.RLock()
  384. defer other.mu.RUnlock()
  385. }
  386. for k, v := range other.data {
  387. m.data[k] = v
  388. }
  389. }
  390. // String returns the map as a string.
  391. func (m *StringAnyMap) String() string {
  392. b, _ := m.MarshalJSON()
  393. return *(*string)(unsafe.Pointer(&b))
  394. }
  395. // MarshalJSON implements the interface MarshalJSON for json.Marshal.
  396. func (m *StringAnyMap) MarshalJSON() ([]byte, error) {
  397. m.mu.RLock()
  398. defer m.mu.RUnlock()
  399. return json.Marshal(m.data)
  400. }
  401. // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
  402. func (m *StringAnyMap) UnmarshalJSON(b []byte) error {
  403. m.mu.Lock()
  404. defer m.mu.Unlock()
  405. if m.data == nil {
  406. m.data = make(map[string]interface{})
  407. }
  408. if err := json.Unmarshal(b, &m.data); err != nil {
  409. return err
  410. }
  411. return nil
  412. }