Skip to content

Commit f322320

Browse files
committed
Add NewMap Method
1 parent 7d814db commit f322320

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

session_storage.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,46 +60,41 @@ func (c *smap) Range(f func(key string, value any) bool) {
6060

6161
type (
6262
ConcurrentMap[K comparable, V any] struct {
63-
hasher maphash.Hasher[K]
64-
sharding uint64
65-
buckets []*Map[K, V]
63+
hasher maphash.Hasher[K]
64+
num uint64
65+
shardings []*Map[K, V]
6666
}
6767
)
6868

6969
// NewConcurrentMap create a new concurrency-safe map
7070
// arg0 represents the number of shardings; arg1 represents the initialized capacity of a sharding.
7171
func NewConcurrentMap[K comparable, V any](size ...uint64) *ConcurrentMap[K, V] {
72-
sharding, capacity := uint64(0), uint64(0)
73-
if len(size) >= 1 {
74-
sharding = size[0]
75-
}
76-
if len(size) >= 2 {
77-
capacity = size[1]
78-
}
79-
sharding = internal.SelectValue(sharding <= 0, 16, sharding)
80-
sharding = internal.ToBinaryNumber(sharding)
72+
size = append(size, 0, 0)
73+
num, capacity := size[0], size[1]
74+
num = internal.ToBinaryNumber(internal.SelectValue(num <= 0, 16, num))
8175
var cm = &ConcurrentMap[K, V]{
82-
hasher: maphash.NewHasher[K](),
83-
sharding: sharding,
84-
buckets: make([]*Map[K, V], sharding),
76+
hasher: maphash.NewHasher[K](),
77+
num: num,
78+
shardings: make([]*Map[K, V], num),
8579
}
86-
for i, _ := range cm.buckets {
87-
cm.buckets[i] = &Map[K, V]{m: make(map[K]V, capacity)}
80+
for i, _ := range cm.shardings {
81+
cm.shardings[i] = NewMap[K, V](int(capacity))
8882
}
8983
return cm
9084
}
9185

9286
// GetSharding returns a map sharding for a key
87+
// the operations inside the sharding is lockless, and need to be locked manually.
9388
func (c *ConcurrentMap[K, V]) GetSharding(key K) *Map[K, V] {
9489
var hashCode = c.hasher.Hash(key)
95-
var index = hashCode & (c.sharding - 1)
96-
return c.buckets[index]
90+
var index = hashCode & (c.num - 1)
91+
return c.shardings[index]
9792
}
9893

9994
// Len returns the number of elements in the map
10095
func (c *ConcurrentMap[K, V]) Len() int {
10196
var length = 0
102-
for _, b := range c.buckets {
97+
for _, b := range c.shardings {
10398
b.Lock()
10499
length += b.Len()
105100
b.Unlock()
@@ -142,8 +137,8 @@ func (c *ConcurrentMap[K, V]) Range(f func(key K, value V) bool) {
142137
next = f(k, v)
143138
return next
144139
}
145-
for i := uint64(0); i < c.sharding && next; i++ {
146-
var b = c.buckets[i]
140+
for i := uint64(0); i < c.num && next; i++ {
141+
var b = c.shardings[i]
147142
b.Lock()
148143
b.Range(cb)
149144
b.Unlock()
@@ -155,6 +150,16 @@ type Map[K comparable, V any] struct {
155150
m map[K]V
156151
}
157152

153+
func NewMap[K comparable, V any](size ...int) *Map[K, V] {
154+
var capacity = 0
155+
if len(size) > 0 {
156+
capacity = size[0]
157+
}
158+
c := new(Map[K, V])
159+
c.m = make(map[K]V, capacity)
160+
return c
161+
}
162+
158163
func (c *Map[K, V]) Len() int { return len(c.m) }
159164

160165
func (c *Map[K, V]) Load(key K) (value V, ok bool) {

session_storage_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestConcurrentMap(t *testing.T) {
100100
var as = assert.New(t)
101101
var m1 = make(map[string]any)
102102
var m2 = NewConcurrentMap[string, uint32]()
103-
as.Equal(m2.sharding, uint64(16))
103+
as.Equal(m2.num, uint64(16))
104104
var count = internal.AlphabetNumeric.Intn(1000)
105105
for i := 0; i < count; i++ {
106106
var key = string(internal.AlphabetNumeric.Generate(10))
@@ -128,7 +128,7 @@ func TestConcurrentMap(t *testing.T) {
128128
t.Run("", func(t *testing.T) {
129129
var sum = 0
130130
var cm = NewConcurrentMap[string, int](8, 8)
131-
for _, item := range cm.buckets {
131+
for _, item := range cm.shardings {
132132
sum += len(item.m)
133133
}
134134
assert.Equal(t, sum, 0)

0 commit comments

Comments
 (0)