From 5488b3d45585d55821ef21170911519865c32c2f Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Fri, 19 May 2023 09:34:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/asynchronization/map.go | 3 ++- utils/hash/map.go | 29 ++++++++++++++++++++++++++++ utils/synchronization/map.go | 3 ++- utils/synchronization/map_segment.go | 4 ++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 utils/hash/map.go diff --git a/utils/asynchronization/map.go b/utils/asynchronization/map.go index 1d27089..5082fcc 100644 --- a/utils/asynchronization/map.go +++ b/utils/asynchronization/map.go @@ -2,6 +2,7 @@ package asynchronization import ( "encoding/json" + "github.com/kercylan98/minotaur/utils/hash" ) func NewMap[Key comparable, value any]() *Map[Key, value] { @@ -33,7 +34,7 @@ func (slf *Map[Key, Value]) AtomGetSet(key Key, handle func(value Value, exist b } // Atom 原子操作 -func (slf *Map[Key, Value]) Atom(handle func(m *Map[Key, Value])) { +func (slf *Map[Key, Value]) Atom(handle func(m hash.Map[Key, Value])) { handle(slf) } diff --git a/utils/hash/map.go b/utils/hash/map.go new file mode 100644 index 0000000..acb79c7 --- /dev/null +++ b/utils/hash/map.go @@ -0,0 +1,29 @@ +package hash + +// Map 提供了map集合接口 +type Map[Key comparable, Value any] interface { + Set(key Key, value Value) + Get(key Key) Value + // AtomGetSet 原子方式获取一个值并在之后进行赋值 + AtomGetSet(key Key, handle func(value Value, exist bool) (newValue Value, isSet bool)) + // Atom 原子操作 + Atom(handle func(m Map[Key, Value])) + Exist(key Key) bool + GetExist(key Key) (Value, bool) + Delete(key Key) + DeleteGet(key Key) Value + DeleteGetExist(key Key) (Value, bool) + DeleteExist(key Key) bool + Clear() + ClearHandle(handle func(key Key, value Value)) + Range(handle func(key Key, value Value)) + RangeSkip(handle func(key Key, value Value) bool) + RangeBreakout(handle func(key Key, value Value) bool) + RangeFree(handle func(key Key, value Value, skip func(), breakout func())) + Keys() []Key + Slice() []Value + Map() map[Key]Value + Size() int + // GetOne 获取一个 + GetOne() (value Value) +} diff --git a/utils/synchronization/map.go b/utils/synchronization/map.go index d8816d6..d2ad468 100644 --- a/utils/synchronization/map.go +++ b/utils/synchronization/map.go @@ -2,6 +2,7 @@ package synchronization import ( "encoding/json" + "github.com/kercylan98/minotaur/utils/hash" "sync" ) @@ -47,7 +48,7 @@ func (slf *Map[Key, Value]) AtomGetSet(key Key, handle func(value Value, exist b } // Atom 原子操作 -func (slf *Map[Key, Value]) Atom(handle func(m *Map[Key, Value])) { +func (slf *Map[Key, Value]) Atom(handle func(m hash.Map[Key, Value])) { slf.lock.Lock() slf.atom = true handle(slf) diff --git a/utils/synchronization/map_segment.go b/utils/synchronization/map_segment.go index 6836b15..3182fa2 100644 --- a/utils/synchronization/map_segment.go +++ b/utils/synchronization/map_segment.go @@ -28,6 +28,10 @@ type MapSegment[Key comparable, Value any] struct { lock sync.RWMutex } +func (slf *MapSegment[Key, Value]) Atom(handle func(m hash.Map[Key, Value])) { + panic("this function is currently not supported") +} + func (slf *MapSegment[Key, Value]) Set(key Key, value Value) { slf.lock.RLock() s, exist := slf.cache[key]