-
Notifications
You must be signed in to change notification settings - Fork 338
/
finder.go
223 lines (205 loc) · 5.57 KB
/
finder.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
// Copyright 2021 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 epochs
import (
"context"
"errors"
"github.com/ethersphere/bee/pkg/feeds"
storage "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
)
var _ feeds.Lookup = (*finder)(nil)
var _ feeds.Lookup = (*asyncFinder)(nil)
// finder encapsulates a chunk store getter and a feed and provides
// non-concurrent lookup methods
type finder struct {
getter *feeds.Getter
}
// NewFinder constructs an AsyncFinder
func NewFinder(getter storage.Getter, feed *feeds.Feed) feeds.Lookup {
return &finder{feeds.NewGetter(getter, feed)}
}
// At looks up the version valid at time `at`
// after is a unix time hint of the latest known update
func (f *finder) At(ctx context.Context, at int64, after uint64) (swarm.Chunk, feeds.Index, feeds.Index, error) {
e, ch, err := f.common(ctx, at, after)
if err != nil {
return nil, nil, nil, err
}
ch, err = f.at(ctx, uint64(at), e, ch)
return ch, nil, nil, err
}
// common returns the lowest common ancestor for which a feed update chunk is found in the chunk store
func (f *finder) common(ctx context.Context, at int64, after uint64) (*epoch, swarm.Chunk, error) {
for e := lca(uint64(at), after); ; e = e.parent() {
ch, err := f.getter.Get(ctx, e)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
if e.level == maxLevel {
return e, nil, nil
}
continue
}
return e, nil, err
}
ts, err := feeds.UpdatedAt(ch)
if err != nil {
return e, nil, err
}
if ts <= uint64(at) {
return e, ch, nil
}
}
}
// at is a non-concurrent recursive Finder function to find the version update chunk at time `at`
func (f *finder) at(ctx context.Context, at uint64, e *epoch, ch swarm.Chunk) (swarm.Chunk, error) {
uch, err := f.getter.Get(ctx, e)
if err != nil {
// error retrieving
if !errors.Is(err, storage.ErrNotFound) {
return nil, err
}
// epoch not found on branch
if e.isLeft() { // no lower resolution
return ch, nil
}
// traverse earlier branch
return f.at(ctx, e.start-1, e.left(), ch)
}
// epoch found
// check if timestamp is later then target
ts, err := feeds.UpdatedAt(uch)
if err != nil {
return nil, err
}
if ts > at {
if e.isLeft() {
return ch, nil
}
return f.at(ctx, e.start-1, e.left(), ch)
}
if e.level == 0 { // matching update time or finest resolution
return uch, nil
}
// continue traversing based on at
return f.at(ctx, at, e.childAt(at), uch)
}
type result struct {
path *path
chunk swarm.Chunk
*epoch
}
// asyncFinder encapsulates a chunk store getter and a feed and provides
// non-concurrent lookup methods
type asyncFinder struct {
getter *feeds.Getter
}
type path struct {
at int64
top *result
bottom *result
cancel chan struct{}
}
func newPath(at int64) *path {
return &path{at, nil, nil, make(chan struct{})}
}
// NewAsyncFinder constructs an AsyncFinder
func NewAsyncFinder(getter storage.Getter, feed *feeds.Feed) feeds.Lookup {
return &asyncFinder{feeds.NewGetter(getter, feed)}
}
func (f *asyncFinder) get(ctx context.Context, at int64, e *epoch) (swarm.Chunk, error) {
u, err := f.getter.Get(ctx, e)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return nil, err
}
return nil, nil
}
ts, err := feeds.UpdatedAt(u)
if err != nil {
return nil, err
}
diff := at - int64(ts)
if diff < 0 {
return nil, nil
}
return u, nil
}
// at attempts to retrieve all epoch chunks on the path for `at` concurrently
func (f *asyncFinder) at(ctx context.Context, at int64, p *path, e *epoch, c chan<- *result) {
for ; ; e = e.childAt(uint64(at)) {
select {
case <-p.cancel:
return
default:
}
go func(e *epoch) {
uch, err := f.get(ctx, at, e)
if err != nil {
return
}
select {
case c <- &result{p, uch, e}:
case <-p.cancel:
}
}(e)
if e.level == 0 {
return
}
}
}
func (f *asyncFinder) At(ctx context.Context, at int64, after uint64) (swarm.Chunk, feeds.Index, feeds.Index, error) {
// TODO: current and next index return values need to be implemented
ch, err := f.asyncAt(ctx, at, after)
return ch, nil, nil, err
}
// At looks up the version valid at time `at`
// after is a unix time hint of the latest known update
func (f *asyncFinder) asyncAt(ctx context.Context, at int64, _ uint64) (swarm.Chunk, error) {
c := make(chan *result)
go f.at(ctx, at, newPath(at), &epoch{0, maxLevel}, c)
LOOP:
for r := range c {
p := r.path
// ignore result from paths already cancelled
select {
case <-p.cancel:
continue LOOP
default:
}
if r.chunk != nil { // update chunk for epoch found
if r.level == 0 { // return if deepest level epoch
return r.chunk, nil
}
// ignore if higher level than the deepest epoch found
if p.top != nil && p.top.level < r.level {
continue LOOP
}
p.top = r
} else { // update chunk for epoch not found
// if top level than return with no update found
if r.level == 32 {
close(p.cancel)
return nil, nil
}
// if topmost epoch not found, then set bottom
if p.bottom == nil || p.bottom.level < r.level {
p.bottom = r
}
}
// found - not found for two consecutive epochs
if p.top != nil && p.bottom != nil && p.top.level == p.bottom.level+1 {
// cancel path
close(p.cancel)
if p.bottom.isLeft() {
return p.top.chunk, nil
}
// recursive call on new path through left sister
np := newPath(at)
np.top = &result{np, p.top.chunk, p.top.epoch}
go f.at(ctx, int64(p.bottom.start-1), np, p.bottom.left(), c)
}
}
return nil, nil
}