/
cmdTable.go
182 lines (173 loc) · 4.2 KB
/
cmdTable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package M2N
import (
"github.com/ChenXingyuChina/asynchronousIO"
"os"
"sync"
)
const (
Load uint8 = 1
ToSave uint8 = 1 << iota
DeleteItem
saving
)
type CmdTableForUnChangeableDataSource struct {
table map[int64]*item
}
type item struct {
bean asynchronousIO.Bean
state uint8
waits []chan asynchronousIO.Bean
}
func (t *CmdTableForUnChangeableDataSource) Save(key int64, bean asynchronousIO.Bean, notify bool, cs *sync.Pool) (c chan asynchronousIO.Bean, start bool) {
i, ok := t.table[key]
if ok {
switch i.state {
case saving:
i.state = ToSave
i.bean = bean
if notify {
c = cs.Get().(chan asynchronousIO.Bean)
i.waits = append(i.waits, c)
}
case ToSave: // todo test three save
if len(i.waits) > 0 {
i.waits[0] <- asynchronousIO.Finish{}
i.waits = i.waits[:0]
}
i.bean = bean
if notify {
c = cs.Get().(chan asynchronousIO.Bean)
i.waits = append(i.waits, c)
}
case Load:
i.state = ToSave
i.bean = bean
for _, v := range i.waits {
v <- bean
}
if notify {
c = cs.Get().(chan asynchronousIO.Bean)
i.waits[0] = c
}
case DeleteItem:
return nil, false
}
} else {
t.table[key] = &item{bean, saving, make([]chan asynchronousIO.Bean, 0, 3)}
if notify {
c = cs.Get().(chan asynchronousIO.Bean)
}
return c, true
}
return
}
func (t *CmdTableForUnChangeableDataSource) Load(k int64, cs *sync.Pool) (c chan asynchronousIO.Bean, bean asynchronousIO.Bean) {
i, ok := t.table[k]
if ok {
switch i.state {
case saving:
return nil, i.bean
case ToSave:
return nil, i.bean
case Load:
c = cs.Get().(chan asynchronousIO.Bean)
i.waits = append(i.waits, c)
return c, nil
case DeleteItem:
return nil, asynchronousIO.AsynchronousIOError{E: &os.PathError{Path: string(k)}}
}
} else {
t.table[k] = &item{bean, Load, make([]chan asynchronousIO.Bean, 0, 3)}
c = cs.Get().(chan asynchronousIO.Bean)
bean = asynchronousIO.Start{}
}
return
}
func (t *CmdTableForUnChangeableDataSource) Delete(k int64, cs *sync.Pool) (c chan asynchronousIO.Bean, start bool) {
i, ok := t.table[k]
if ok {
switch i.state {
case saving:
i.state = DeleteItem
if len(i.waits) != 0 {
i.waits[0] <- asynchronousIO.Finish{}
i.waits = i.waits[0:0]
}
c = cs.Get().(chan asynchronousIO.Bean)
i.waits = append(i.waits, c)
return c, false
case ToSave: // test two save and delete
i.state = DeleteItem
if len(i.waits) != 0 {
i.waits[0] <- asynchronousIO.Finish{}
i.waits = i.waits[0:0]
}
c = cs.Get().(chan asynchronousIO.Bean)
i.waits = append(i.waits, c)
return c, false
case Load:
i.state = DeleteItem
for _, v := range i.waits {
v <- asynchronousIO.AsynchronousIOError{E: &os.PathError{}}
i.waits = i.waits[0:0]
}
c = cs.Get().(chan asynchronousIO.Bean)
i.waits = append(i.waits, c)
return c, false
case DeleteItem:
return nil, false
}
} else {
t.table[k] = &item{nil, DeleteItem, nil}
c = cs.Get().(chan asynchronousIO.Bean)
start = true
}
return
}
func (t *CmdTableForUnChangeableDataSource) Get(k int64, lastCmd uint8, lastBean asynchronousIO.Bean) (cmd uint8, bean asynchronousIO.Bean, c chan asynchronousIO.Bean, restart bool) {
i := t.table[k]
//fmt.Println(t.table)
switch lastCmd {
case Load:
if i.state == Load {
for _, v := range i.waits {
v <- lastBean
}
delete(t.table, k)
return Load, nil, nil, false
} else if i.state == ToSave {
i.state = saving
if len(i.waits) > 0 {
w := i.waits[0]
i.waits = i.waits[:0]
return ToSave, i.bean, w, true
}
return ToSave, i.bean, nil, true
} else if i.state == DeleteItem {
w := i.waits[0]
i.waits = i.waits[:0]
return DeleteItem, i.bean, w, true
}
case ToSave:
if i.state == ToSave {
i.state = saving
if len(i.waits) > 0 {
w := i.waits[0]
i.waits = i.waits[:0]
return ToSave, i.bean, w, true
}
return ToSave, i.bean, nil, true
} else if i.state == DeleteItem {
w := i.waits[0]
i.waits = i.waits[:0]
return DeleteItem, i.bean, w, true
} else if i.state == saving {
delete(t.table, k)
return saving, nil, nil, false
}
case DeleteItem:
delete(t.table, k)
return DeleteItem, nil, nil, false
}
return lastCmd, nil, nil, false
}