Skip to content

Commit 8a70cc5

Browse files
committed
clean up code; fix tests
1 parent 602d4cb commit 8a70cc5

File tree

3 files changed

+27
-35
lines changed

3 files changed

+27
-35
lines changed

map.go

+15-21
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
11
package lrc
22

33
import (
4-
"fmt"
54
"runtime"
6-
"sync"
75
"sync/atomic"
86
)
97

10-
const read_left int32 = -1
11-
const read_right int32 = 1
8+
const ReadOnLeft int32 = -1
9+
const ReadOnRight int32 = 1
1210

1311
type LRMap struct {
14-
readIndicators [2]*ReadIndicator
12+
readIndicators [2]*readIndicator
1513
versionIndex *int32
16-
leftRight *int32
14+
sideToRead *int32
1715

1816
left map[int]int
1917
right map[int]int
20-
21-
wm sync.Mutex
2218
}
2319

2420
func New() *LRMap {
2521

2622
m := &LRMap{
27-
readIndicators: [2]*ReadIndicator{
23+
readIndicators: [2]*readIndicator{
2824
newReadIndicator(),
2925
newReadIndicator(),
3026
},
3127
versionIndex: new(int32),
32-
leftRight: new(int32),
28+
sideToRead: new(int32),
3329

3430
left: make(map[int]int),
3531
right: make(map[int]int),
36-
37-
wm: sync.Mutex{},
3832
}
3933

4034
*m.versionIndex = 0
41-
*m.leftRight = read_left
35+
*m.sideToRead = ReadOnLeft
4236
return m
4337
}
4438

@@ -73,8 +67,8 @@ func (lr *LRMap) Get(k int) (val int, exist bool) {
7367

7468
lvi := lr.arrive()
7569

76-
which := atomic.LoadInt32(lr.leftRight)
77-
if which == read_left {
70+
which := atomic.LoadInt32(lr.sideToRead)
71+
if which == ReadOnLeft {
7872
val, exist = lr.left[k]
7973
} else {
8074
val, exist = lr.right[k]
@@ -86,20 +80,20 @@ func (lr *LRMap) Get(k int) (val int, exist bool) {
8680

8781
func (lr *LRMap) Put(key, val int) {
8882

89-
which := atomic.LoadInt32(lr.leftRight)
90-
if which == read_left {
83+
side := atomic.LoadInt32(lr.sideToRead)
84+
if side == ReadOnLeft {
9185
// write on right
9286
lr.right[key] = val
93-
atomic.StoreInt32(lr.leftRight, read_right)
87+
atomic.StoreInt32(lr.sideToRead, ReadOnRight)
9488
lr.toggleVersionAndWait()
9589
lr.left[key] = val
96-
} else if which == read_right {
90+
} else if side == ReadOnRight {
9791
// write on left
9892
lr.left[key] = val
99-
atomic.StoreInt32(lr.leftRight, read_left)
93+
atomic.StoreInt32(lr.sideToRead, ReadOnLeft)
10094
lr.toggleVersionAndWait()
10195
lr.right[key] = val
10296
} else {
103-
fmt.Println("fuuuu")
97+
panic("illegal state: you can only read on LEFT or RIGHT")
10498
}
10599
}

map_test.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ func TestLRMap(t *testing.T) {
1919
}()
2020
}
2121

22-
for i := 0; i < 100; i++ {
23-
wg.Add(1)
24-
go func() {
25-
k := rand.Intn(10000)
26-
lrmap.Put(k, k)
27-
wg.Done()
28-
}()
29-
}
22+
wg.Add(1)
23+
go func() {
24+
k := rand.Intn(10000)
25+
lrmap.Put(k, k)
26+
wg.Done()
27+
}()
3028

3129
wg.Wait()
3230
}

read_indicator.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ package lrc
22

33
import "sync/atomic"
44

5-
type ReadIndicator struct {
5+
type readIndicator struct {
66
count *int32
77
}
88

9-
func newReadIndicator() *ReadIndicator {
10-
r := &ReadIndicator{
9+
func newReadIndicator() *readIndicator {
10+
r := &readIndicator{
1111
count: new(int32),
1212
}
1313
*r.count = 0
1414
return r
1515
}
1616

17-
func (r *ReadIndicator) arrive() {
17+
func (r *readIndicator) arrive() {
1818
atomic.AddInt32(r.count, 1)
1919
}
2020

21-
func (r *ReadIndicator) depart() {
21+
func (r *readIndicator) depart() {
2222
atomic.AddInt32(r.count, -1)
2323
}
2424

25-
func (r *ReadIndicator) isEmpty() bool {
25+
func (r *readIndicator) isEmpty() bool {
2626
return atomic.LoadInt32(r.count) == 0
2727
}

0 commit comments

Comments
 (0)