| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package iotMap
- import (
- iotStruct "IotAdmin/iot/struct"
- "sync"
- )
- type DtuDeviceMap struct {
- Map *sync.Map
- }
- func NewDMapDtuDev() *DtuDeviceMap {
- cm := &DtuDeviceMap{
- Map: &sync.Map{},
- }
- return cm
- }
- func (cm *DtuDeviceMap) Add(key string, val *iotStruct.DtuDevice) {
- cm.Map.Store(key, val)
- }
- func (cm *DtuDeviceMap) Remove(key string) {
- cm.Map.Delete(key)
- }
- func (cm *DtuDeviceMap) Get(key string) (*iotStruct.DtuDevice, bool) {
- res := &iotStruct.DtuDevice{}
- x, ok := cm.Map.Load(key)
- if ok {
- res = x.(*iotStruct.DtuDevice)
- return res, true
- }
- return res, false
- }
- // AlterKey 修改key
- func (cm *DtuDeviceMap) AlterKey(before, after string) bool {
- if before == after {
- return true
- }
- if v, ok := cm.Map.Load(before); !ok {
- return false
- } else {
- if _, ok = cm.Map.Load(after); ok {
- return false
- }
- val := v.(*iotStruct.DtuDevice)
- cm.Map.Store(after, val)
- cm.Map.Delete(before)
- return true
- }
- }
- func (cm *DtuDeviceMap) Len() int {
- c := 0
- cm.Map.Range(func(key, value interface{}) bool {
- c++
- return true
- })
- return c
- }
- func (cm *DtuDeviceMap) Clean() {
- cm.Map = new(sync.Map)
- }
|