-
Notifications
You must be signed in to change notification settings - Fork 338
/
storer.go
333 lines (293 loc) · 8.09 KB
/
storer.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mock
import (
"context"
"errors"
"sync"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
)
var _ storage.Storer = (*MockStorer)(nil)
type MockStorer struct {
store map[string][]byte
modePut map[string]storage.ModePut
modeSet map[string]storage.ModeSet
pinnedAddress []swarm.Address // Stores the pinned address
pinnedCounter []uint64 // and its respective counter. These are stored as slices to preserve the order.
subpull []storage.Descriptor
partialInterval bool
morePull chan struct{}
mtx sync.Mutex
quit chan struct{}
baseAddress []byte
bins []uint64
}
func WithSubscribePullChunks(chs ...storage.Descriptor) Option {
return optionFunc(func(m *MockStorer) {
m.subpull = make([]storage.Descriptor, len(chs))
for i, v := range chs {
m.subpull[i] = v
}
})
}
func WithBaseAddress(a swarm.Address) Option {
return optionFunc(func(m *MockStorer) {
m.baseAddress = a.Bytes()
})
}
func WithPartialInterval(v bool) Option {
return optionFunc(func(m *MockStorer) {
m.partialInterval = v
})
}
func NewStorer(opts ...Option) *MockStorer {
s := &MockStorer{
store: make(map[string][]byte),
modePut: make(map[string]storage.ModePut),
modeSet: make(map[string]storage.ModeSet),
morePull: make(chan struct{}),
quit: make(chan struct{}),
bins: make([]uint64, swarm.MaxBins),
}
for _, v := range opts {
v.apply(s)
}
return s
}
func (m *MockStorer) Get(_ context.Context, _ storage.ModeGet, addr swarm.Address) (ch swarm.Chunk, err error) {
m.mtx.Lock()
defer m.mtx.Unlock()
v, has := m.store[addr.String()]
if !has {
return nil, storage.ErrNotFound
}
return swarm.NewChunk(addr, v), nil
}
func (m *MockStorer) Put(ctx context.Context, mode storage.ModePut, chs ...swarm.Chunk) (exist []bool, err error) {
m.mtx.Lock()
defer m.mtx.Unlock()
exist = make([]bool, len(chs))
for i, ch := range chs {
exist[i], err = m.has(ctx, ch.Address())
if err != nil {
return exist, err
}
if !exist[i] {
po := swarm.Proximity(ch.Address().Bytes(), m.baseAddress)
m.bins[po]++
}
// this is needed since the chunk feeder shares memory across calls
// to the pipeline. this is in order to avoid multiple allocations.
// this change mimics the behavior of shed and localstore
// and copies the data from the call into the in-memory store
b := make([]byte, len(ch.Data()))
copy(b, ch.Data())
m.store[ch.Address().String()] = b
m.modePut[ch.Address().String()] = mode
// pin chunks if needed
switch mode {
case storage.ModePutUploadPin:
// if mode is set pin, increment the pin counter
var found bool
addr := ch.Address()
for i, ad := range m.pinnedAddress {
if addr.String() == ad.String() {
m.pinnedCounter[i] = m.pinnedCounter[i] + 1
found = true
}
}
if !found {
m.pinnedAddress = append(m.pinnedAddress, addr)
m.pinnedCounter = append(m.pinnedCounter, uint64(1))
}
default:
}
}
return exist, nil
}
func (m *MockStorer) GetMulti(ctx context.Context, mode storage.ModeGet, addrs ...swarm.Address) (ch []swarm.Chunk, err error) {
panic("not implemented") // TODO: Implement
}
func (m *MockStorer) has(ctx context.Context, addr swarm.Address) (yes bool, err error) {
_, has := m.store[addr.String()]
return has, nil
}
func (m *MockStorer) Has(ctx context.Context, addr swarm.Address) (yes bool, err error) {
m.mtx.Lock()
defer m.mtx.Unlock()
return m.has(ctx, addr)
}
func (m *MockStorer) HasMulti(ctx context.Context, addrs ...swarm.Address) (yes []bool, err error) {
panic("not implemented") // TODO: Implement
}
func (m *MockStorer) Set(ctx context.Context, mode storage.ModeSet, addrs ...swarm.Address) (err error) {
m.mtx.Lock()
defer m.mtx.Unlock()
for _, addr := range addrs {
m.modeSet[addr.String()] = mode
switch mode {
case storage.ModeSetPin:
// check if chunk exists
has, err := m.has(ctx, addr)
if err != nil {
return err
}
if !has {
return storage.ErrNotFound
}
// if mode is set pin, increment the pin counter
var found bool
for i, ad := range m.pinnedAddress {
if addr.String() == ad.String() {
m.pinnedCounter[i] = m.pinnedCounter[i] + 1
found = true
}
}
if !found {
m.pinnedAddress = append(m.pinnedAddress, addr)
m.pinnedCounter = append(m.pinnedCounter, uint64(1))
}
case storage.ModeSetUnpin:
// if mode is set unpin, decrement the pin counter and remove the address
// once it reaches zero
for i, ad := range m.pinnedAddress {
if addr.String() == ad.String() {
m.pinnedCounter[i] = m.pinnedCounter[i] - 1
if m.pinnedCounter[i] == 0 {
copy(m.pinnedAddress[i:], m.pinnedAddress[i+1:])
m.pinnedAddress[len(m.pinnedAddress)-1] = swarm.NewAddress([]byte{0})
m.pinnedAddress = m.pinnedAddress[:len(m.pinnedAddress)-1]
copy(m.pinnedCounter[i:], m.pinnedCounter[i+1:])
m.pinnedCounter[len(m.pinnedCounter)-1] = uint64(0)
m.pinnedCounter = m.pinnedCounter[:len(m.pinnedCounter)-1]
}
}
}
case storage.ModeSetRemove:
delete(m.store, addr.String())
default:
}
}
return nil
}
func (m *MockStorer) GetModePut(addr swarm.Address) (mode storage.ModePut) {
m.mtx.Lock()
defer m.mtx.Unlock()
if mode, ok := m.modePut[addr.String()]; ok {
return mode
}
return mode
}
func (m *MockStorer) GetModeSet(addr swarm.Address) (mode storage.ModeSet) {
m.mtx.Lock()
defer m.mtx.Unlock()
if mode, ok := m.modeSet[addr.String()]; ok {
return mode
}
return mode
}
func (m *MockStorer) LastPullSubscriptionBinID(bin uint8) (id uint64, err error) {
return m.bins[bin], nil
}
func (m *MockStorer) SubscribePull(ctx context.Context, bin uint8, since, until uint64) (<-chan storage.Descriptor, <-chan struct{}, func()) {
c := make(chan storage.Descriptor)
done := make(chan struct{})
stop := func() {
close(done)
}
go func() {
defer close(c)
m.mtx.Lock()
for _, ch := range m.subpull {
select {
case c <- ch:
case <-done:
return
case <-ctx.Done():
return
case <-m.quit:
return
}
}
m.mtx.Unlock()
if m.partialInterval {
// block since we're at the top of the bin and waiting for new chunks
select {
case <-done:
return
case <-m.quit:
return
case <-ctx.Done():
return
case <-m.morePull:
}
}
m.mtx.Lock()
defer m.mtx.Unlock()
// iterate on what we have in the iterator
for _, ch := range m.subpull {
select {
case c <- ch:
case <-done:
return
case <-ctx.Done():
return
case <-m.quit:
return
}
}
}()
return c, m.quit, stop
}
func (m *MockStorer) MorePull(d ...storage.Descriptor) {
// clear out what we already have in subpull
m.mtx.Lock()
defer m.mtx.Unlock()
m.subpull = make([]storage.Descriptor, len(d))
for i, v := range d {
m.subpull[i] = v
}
close(m.morePull)
}
func (m *MockStorer) SubscribePush(ctx context.Context) (c <-chan swarm.Chunk, stop func()) {
panic("not implemented") // TODO: Implement
}
func (m *MockStorer) PinnedChunks(ctx context.Context, offset, cursor int) (pinnedChunks []*storage.Pinner, err error) {
m.mtx.Lock()
defer m.mtx.Unlock()
if len(m.pinnedAddress) == 0 {
return pinnedChunks, nil
}
for i, addr := range m.pinnedAddress {
pi := &storage.Pinner{
Address: swarm.NewAddress(addr.Bytes()),
PinCounter: m.pinnedCounter[i],
}
pinnedChunks = append(pinnedChunks, pi)
}
if pinnedChunks == nil {
return pinnedChunks, errors.New("pin chunks: leveldb: not found")
}
return pinnedChunks, nil
}
func (m *MockStorer) PinCounter(address swarm.Address) (uint64, error) {
m.mtx.Lock()
defer m.mtx.Unlock()
for i, addr := range m.pinnedAddress {
if addr.String() == address.String() {
return m.pinnedCounter[i], nil
}
}
return 0, storage.ErrNotFound
}
func (m *MockStorer) Close() error {
close(m.quit)
return nil
}
type Option interface {
apply(*MockStorer)
}
type optionFunc func(*MockStorer)
func (f optionFunc) apply(r *MockStorer) { f(r) }