-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- new package to simplify usage of atomic.Value & sync map by using generic struct/interface with cast - add generic function to cast model
- Loading branch information
Showing
6 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package atomic | ||
|
||
import "reflect" | ||
|
||
func Cast[M any](src any) (model M, casted bool) { | ||
if reflect.DeepEqual(src, model) { | ||
return model, false | ||
} else if v, k := src.(M); !k { | ||
return model, false | ||
} else { | ||
return v, true | ||
} | ||
} | ||
|
||
func IsEmpty[M any](src any) bool { | ||
if _, k := Cast[M](src); !k { | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package atomic | ||
|
||
type defaultValue[T any] interface { | ||
GetDefault() T | ||
} | ||
|
||
type defVal[T any] struct { | ||
v T | ||
} | ||
|
||
func (d *defVal[T]) GetDefault() T { | ||
return d.v | ||
} | ||
|
||
func newDefault[T any](def T) *defVal[T] { | ||
return &defVal[T]{ | ||
v: def, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package atomic | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
type Value[T any] interface { | ||
SetDefaultLoad(def T) | ||
SetDefaultStore(def T) | ||
|
||
Load() (val T) | ||
Store(val T) | ||
Swap(new T) (old T) | ||
CompareAndSwap(old, new T) (swapped bool) | ||
} | ||
|
||
type Map[K comparable] interface { | ||
Load(key K) (value any, ok bool) | ||
Store(key K, value any) | ||
|
||
LoadOrStore(key K, value any) (actual any, loaded bool) | ||
LoadAndDelete(key K) (value any, loaded bool) | ||
|
||
Delete(key K) | ||
Swap(key K, value any) (previous any, loaded bool) | ||
|
||
CompareAndSwap(key K, old, new any) bool | ||
CompareAndDelete(key K, old any) (deleted bool) | ||
|
||
Range(f func(key K, value any) bool) | ||
} | ||
|
||
type MapTyped[K comparable, V any] interface { | ||
Load(key K) (value V, ok bool) | ||
Store(key K, value V) | ||
|
||
LoadOrStore(key K, value V) (actual V, loaded bool) | ||
LoadAndDelete(key K) (value V, loaded bool) | ||
|
||
Delete(key K) | ||
Swap(key K, value V) (previous V, loaded bool) | ||
|
||
CompareAndSwap(key K, old, new V) bool | ||
CompareAndDelete(key K, old V) (deleted bool) | ||
|
||
Range(f func(key K, value V) bool) | ||
} | ||
|
||
func NewValue[T any]() Value[T] { | ||
var ( | ||
tmp1 T | ||
tmp2 T | ||
) | ||
|
||
return NewValueDefault[T](tmp1, tmp2) | ||
} | ||
|
||
func NewValueDefault[T any](load, store T) Value[T] { | ||
o := &val[T]{ | ||
av: new(atomic.Value), | ||
dl: new(atomic.Value), | ||
ds: new(atomic.Value), | ||
} | ||
|
||
o.SetDefaultLoad(load) | ||
o.SetDefaultStore(store) | ||
|
||
return o | ||
} | ||
|
||
func NewMapAny[K comparable]() Map[K] { | ||
return &ma[K]{ | ||
m: sync.Map{}, | ||
} | ||
} | ||
|
||
func NewMapTyped[K comparable, V any]() MapTyped[K, V] { | ||
return &mt[K, V]{ | ||
m: NewMapAny[K](), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package atomic | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type ma[K comparable] struct { | ||
m sync.Map | ||
} | ||
|
||
func (o *ma[K]) Load(key K) (value any, ok bool) { | ||
return o.m.Load(key) | ||
} | ||
|
||
func (o *ma[K]) Store(key K, value any) { | ||
o.m.Store(key, value) | ||
} | ||
|
||
func (o *ma[K]) LoadOrStore(key K, value any) (actual any, loaded bool) { | ||
return o.m.LoadOrStore(key, value) | ||
} | ||
|
||
func (o *ma[K]) LoadAndDelete(key K) (value any, loaded bool) { | ||
return o.m.LoadAndDelete(key) | ||
} | ||
|
||
func (o *ma[K]) Delete(key K) { | ||
o.m.Delete(key) | ||
} | ||
|
||
func (o *ma[K]) Swap(key K, value any) (previous any, loaded bool) { | ||
return o.m.Swap(key, value) | ||
} | ||
|
||
func (o *ma[K]) CompareAndSwap(key K, old, new any) bool { | ||
return o.m.CompareAndSwap(key, old, new) | ||
} | ||
|
||
func (o *ma[K]) CompareAndDelete(key K, old any) (deleted bool) { | ||
return o.m.CompareAndDelete(key, old) | ||
} | ||
|
||
func (o *ma[K]) Range(f func(key K, value any) bool) { | ||
o.m.Range(func(key, value any) bool { | ||
var ( | ||
l bool | ||
k K | ||
) | ||
|
||
if k, l = Cast[K](key); !l { | ||
o.m.Delete(key) | ||
return true | ||
} | ||
|
||
return f(k, value) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package atomic | ||
|
||
type mt[K comparable, V any] struct { | ||
m Map[K] | ||
} | ||
|
||
func (o *mt[K, V]) cast(in any) (value V, ok bool) { | ||
return Cast[V](in) | ||
} | ||
|
||
func (o *mt[K, V]) castBool(in any, chk bool) (value V, ok bool) { | ||
if chk { | ||
return Cast[V](in) | ||
} else { | ||
return value, false | ||
} | ||
} | ||
|
||
func (o *mt[K, V]) Load(key K) (value V, ok bool) { | ||
return o.castBool(o.m.Load(key)) | ||
} | ||
|
||
func (o *mt[K, V]) Store(key K, value V) { | ||
o.m.Store(key, value) | ||
} | ||
|
||
func (o *mt[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { | ||
return o.castBool(o.m.LoadOrStore(key, value)) | ||
} | ||
|
||
func (o *mt[K, V]) LoadAndDelete(key K) (value V, loaded bool) { | ||
return o.castBool(o.m.LoadAndDelete(key)) | ||
} | ||
|
||
func (o *mt[K, V]) Delete(key K) { | ||
o.m.Delete(key) | ||
} | ||
|
||
func (o *mt[K, V]) Swap(key K, value V) (previous V, loaded bool) { | ||
return o.castBool(o.m.Swap(key, value)) | ||
} | ||
|
||
func (o *mt[K, V]) CompareAndSwap(key K, old, new V) bool { | ||
return o.m.CompareAndSwap(key, old, new) | ||
} | ||
|
||
func (o *mt[K, V]) CompareAndDelete(key K, old V) (deleted bool) { | ||
return o.m.CompareAndDelete(key, old) | ||
} | ||
|
||
func (o *mt[K, V]) Range(f func(key K, value V) bool) { | ||
o.m.Range(func(key K, value any) bool { | ||
var ( | ||
l bool | ||
v V | ||
) | ||
|
||
if v, l = Cast[V](value); !l { | ||
o.m.Delete(key) | ||
return true | ||
} | ||
|
||
return f(key, v) | ||
}) | ||
} |
Oops, something went wrong.