Skip to content

Commit d8c38b7

Browse files
authored
Merge pull request #2 from slashdevops/feat-SMapKeyValue
Feat: Add SMapKeyValue container implementing golang sync.Map
2 parents 74031b0 + b86949e commit d8c38b7

File tree

8 files changed

+2473
-50
lines changed

8 files changed

+2473
-50
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"cSpell.words": [
3+
"codecov",
34
"Doakes",
45
"Donato",
6+
"Gosec",
57
"keyval",
68
"Println",
79
"Ramstorag",

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ This is focused on `usability and simplicity` rather than performance, but it do
1616

1717
## Overview
1818

19-
Taking advantage of the [Golang Generics](https://go.dev/blog/intro-generics) and internal golang data structures like [Map](https://go.dev/blog/maps), `RamStorage (r9e)` provides a simple way to store and retrieve data.
19+
Taking advantage of the [Golang Generics](https://go.dev/blog/intro-generics) and internal golang data structures, `RamStorage (r9e)` provides a simple way to store and retrieve data.
2020

21-
The goal is to provide a simple and easy way to use a library to store and retrieve data from memory using a simple API and data structures.
21+
The goal is to provide an easy way to use a library to store and retrieve data from memory using an API and data structures simples and.
22+
23+
This package doesn't have any dependencies, so it's easy to use and maintain, only golang standard library is required.
2224

2325
### Available Containers
2426

25-
* [MapKeyValue[K comparable, T any]](https://pkg.go.dev/github.com/slashdevops/r9e#MapKeyValue) using `sync.RWMutex`
27+
* [MapKeyValue[K comparable, T any]](https://pkg.go.dev/github.com/slashdevops/r9e#MapKeyValue) using [sync.RWMutex](https://pkg.go.dev/sync#RWMutex)
28+
* [SMapKeyValue[K comparable, T any]](https://pkg.go.dev/github.com/slashdevops/r9e#SMapKeyValue) using [sync.Map](https://pkg.go.dev/sync#Map)
2629

2730
### Documentation
2831

example_test.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/slashdevops/r9e"
77
)
88

9-
func Example_basic() {
9+
func ExampleMapKeyValue_basic() {
1010
type MathematicalConstants struct {
1111
Name string
1212
Value float64
@@ -61,3 +61,57 @@ func Example_basic() {
6161
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
6262
})
6363
}
64+
65+
func ExampleSMapKeyValue_basic() {
66+
type MathematicalConstants struct {
67+
Name string
68+
Value float64
69+
}
70+
71+
kv := r9e.NewSMapKeyValue[string, MathematicalConstants]()
72+
73+
kv.Set("pi", MathematicalConstants{"Archimedes' constant", 3.141592})
74+
kv.Set("e", MathematicalConstants{"Euler number, Napier's constant", 2.718281})
75+
kv.Set("γ", MathematicalConstants{"Euler number, Napier's constant", 0.577215})
76+
kv.Set("Φ", MathematicalConstants{"Golden ratio constant", 1.618033})
77+
kv.Set("ρ", MathematicalConstants{"Plastic number ρ (or silver constant)", 2.414213})
78+
79+
kvFilteredValues := kv.FilterValue(func(value MathematicalConstants) bool {
80+
return value.Value > 2.0
81+
})
82+
83+
fmt.Println("Mathematical Constants:")
84+
kvFilteredValues.ForEach(func(key string, value MathematicalConstants) {
85+
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
86+
})
87+
88+
fmt.Printf("\n")
89+
fmt.Printf("The most famous mathematical constant:\n")
90+
fmt.Printf("Name: %v, Value: %v\n", kv.Get("pi").Name, kv.Get("pi").Value)
91+
92+
lst := kv.SortValues(func(value1, value2 MathematicalConstants) bool {
93+
return value1.Value > value2.Value
94+
})
95+
96+
fmt.Printf("\n")
97+
fmt.Printf("The most famous mathematical constant sorted by value:\n")
98+
for i, value := range lst {
99+
fmt.Printf("i: %v, Name: %v, Value: %v\n", i, value.Name, value.Value)
100+
}
101+
102+
kvHigh, kvLow := kv.Partition(func(key string, value MathematicalConstants) bool {
103+
return value.Value > 2.5
104+
})
105+
106+
fmt.Printf("\n")
107+
fmt.Printf("Mathematical constants which value is greater than 2.5:\n")
108+
kvHigh.ForEach(func(key string, value MathematicalConstants) {
109+
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
110+
})
111+
112+
fmt.Printf("\n")
113+
fmt.Printf("Mathematical constants which value is less than 2.5:\n")
114+
kvLow.ForEach(func(key string, value MathematicalConstants) {
115+
fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
116+
})
117+
}

godoc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Package r9e provides a collection of memory store containers.
44
# Available Containers
55
66
* [MapKeyValue[K comparable, T any]](https://pkg.go.dev/github.com/slashdevops/r9e#MapKeyValue) using sync.RWMutex
7+
* [SMapKeyValue[K comparable, T any]](https://pkg.go.dev/github.com/slashdevops/r9e#SMapKeyValue) using sync.Map
78
89
*/
910
package r9e

mapkeyvalue.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ func (r *MapKeyValue[K, T]) Set(key K, value T) {
5454
r.data[key] = value
5555
}
5656

57-
// GetCheck returns the value associated with the key if this exist also a
57+
// GetAndCheck returns the value associated with the key if this exist also a
5858
// boolean value if this exist of not.
59-
func (r *MapKeyValue[K, T]) GetCheck(key K) (T, bool) {
59+
func (r *MapKeyValue[K, T]) GetAndCheck(key K) (T, bool) {
6060
r.mu.RLock()
6161
defer r.mu.RUnlock()
6262

@@ -65,6 +65,7 @@ func (r *MapKeyValue[K, T]) GetCheck(key K) (T, bool) {
6565
}
6666

6767
// Get returns the value associated with the key.
68+
// If the key does not exist, return zero value of the type.
6869
func (r *MapKeyValue[K, T]) Get(key K) T {
6970
r.mu.RLock()
7071
defer r.mu.RUnlock()

0 commit comments

Comments
 (0)