-
Notifications
You must be signed in to change notification settings - Fork 462
/
buf_bstore.go
201 lines (168 loc) · 3.95 KB
/
buf_bstore.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
package blockstore
import (
"context"
"os"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"
)
var log = logging.Logger("bufbs")
type BufferedBS struct {
read Blockstore
write Blockstore
readviewer Viewer
writeviewer Viewer
}
func NewBufferedBstore(base Blockstore) *BufferedBS {
var buf Blockstore
if os.Getenv("LOTUS_DISABLE_VM_BUF") == "iknowitsabadidea" {
log.Warn("VM BLOCKSTORE BUFFERING IS DISABLED")
buf = base
} else {
buf = NewTemporary()
}
bs := &BufferedBS{
read: base,
write: buf,
}
if v, ok := base.(Viewer); ok {
bs.readviewer = v
}
if v, ok := buf.(Viewer); ok {
bs.writeviewer = v
}
if (bs.writeviewer == nil) != (bs.readviewer == nil) {
log.Warnf("one of the stores is not viewable; running less efficiently")
}
return bs
}
func NewTieredBstore(r Blockstore, w Blockstore) *BufferedBS {
return &BufferedBS{
read: r,
write: w,
}
}
var (
_ Blockstore = (*BufferedBS)(nil)
_ Viewer = (*BufferedBS)(nil)
)
func (bs *BufferedBS) Flush(ctx context.Context) error { return bs.write.Flush(ctx) }
func (bs *BufferedBS) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
a, err := bs.read.AllKeysChan(ctx)
if err != nil {
return nil, err
}
b, err := bs.write.AllKeysChan(ctx)
if err != nil {
return nil, err
}
out := make(chan cid.Cid)
go func() {
defer close(out)
for a != nil || b != nil {
select {
case val, ok := <-a:
if !ok {
a = nil
} else {
select {
case out <- val:
case <-ctx.Done():
return
}
}
case val, ok := <-b:
if !ok {
b = nil
} else {
select {
case out <- val:
case <-ctx.Done():
return
}
}
}
}
}()
return out, nil
}
func (bs *BufferedBS) DeleteBlock(ctx context.Context, c cid.Cid) error {
if err := bs.read.DeleteBlock(ctx, c); err != nil {
return err
}
return bs.write.DeleteBlock(ctx, c)
}
func (bs *BufferedBS) DeleteMany(ctx context.Context, cids []cid.Cid) error {
if err := bs.read.DeleteMany(ctx, cids); err != nil {
return err
}
return bs.write.DeleteMany(ctx, cids)
}
func (bs *BufferedBS) View(ctx context.Context, c cid.Cid, callback func([]byte) error) error {
if bs.writeviewer == nil || bs.readviewer == nil {
// one of the stores isn't Viewer; fall back to pure Get behaviour.
blk, err := bs.Get(ctx, c)
if err != nil {
return err
}
return callback(blk.RawData())
}
// both stores are viewable.
if err := bs.writeviewer.View(ctx, c, callback); ipld.IsNotFound(err) {
// not found in write blockstore; fall through.
} else {
return err // propagate errors, or nil, i.e. found.
}
return bs.readviewer.View(ctx, c, callback)
}
func (bs *BufferedBS) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
if out, err := bs.write.Get(ctx, c); err != nil {
if !ipld.IsNotFound(err) {
return nil, err
}
} else {
return out, nil
}
return bs.read.Get(ctx, c)
}
func (bs *BufferedBS) GetSize(ctx context.Context, c cid.Cid) (int, error) {
s, err := bs.read.GetSize(ctx, c)
if ipld.IsNotFound(err) || s == 0 {
return bs.write.GetSize(ctx, c)
}
return s, err
}
func (bs *BufferedBS) Put(ctx context.Context, blk blocks.Block) error {
has, err := bs.read.Has(ctx, blk.Cid()) // TODO: consider dropping this check
if err != nil {
return err
}
if has {
return nil
}
return bs.write.Put(ctx, blk)
}
func (bs *BufferedBS) Has(ctx context.Context, c cid.Cid) (bool, error) {
has, err := bs.write.Has(ctx, c)
if err != nil {
return false, err
}
if has {
return true, nil
}
return bs.read.Has(ctx, c)
}
func (bs *BufferedBS) HashOnRead(hor bool) {
bs.read.HashOnRead(hor)
bs.write.HashOnRead(hor)
}
func (bs *BufferedBS) PutMany(ctx context.Context, blks []blocks.Block) error {
return bs.write.PutMany(ctx, blks)
}
func (bs *BufferedBS) Read() Blockstore {
return bs.read
}
func (bs *BufferedBS) Write() Blockstore {
return bs.write
}