/
index_pebble.go
269 lines (214 loc) · 6.47 KB
/
index_pebble.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
package rbstor
import (
"context"
"encoding/binary"
"sync"
"github.com/cockroachdb/pebble"
iface "github.com/lotus-web3/ribs"
"github.com/multiformats/go-multihash"
"golang.org/x/xerrors"
)
// PebbleIndex is the top-level index, thread-safe.
type PebbleIndex struct {
db *pebble.DB
/*
Keys:
- 's:[mh bytes]' -> [i32BE size]{[i64BE best groupIdx]}
- 'i:[mh bytes][i64BE groupIdx]' -> {}
*/
// todo: why tf is this all big endian?
// todo limit size somehow
iterPool sync.Pool
// todo sharded lock
dropLk sync.Mutex
}
// NewPebbleIndex creates a new Pebble-backed Index.
func NewPebbleIndex(path string) (*PebbleIndex, error) {
db, err := pebble.Open(path, &pebble.Options{})
if err != nil {
return nil, err
}
return &PebbleIndex{
db: db,
iterPool: sync.Pool{
New: func() interface{} {
return db.NewIter(nil)
},
},
}, nil
}
func (i *PebbleIndex) Sync(ctx context.Context) error {
return i.db.Flush()
}
func (i *PebbleIndex) GetGroups(ctx context.Context, mh []multihash.Multihash, cb func(cidx int, gk iface.GroupKey) (more bool, err error)) error {
for idx, m := range mh {
// try to get from sizes
sizeKey := append([]byte("s:"), m...)
val, closer, err := i.db.Get(sizeKey)
if err == pebble.ErrNotFound {
continue
}
if err != nil {
return xerrors.Errorf("get(s:) get: %w", err)
}
if len(val) > 4 {
//size := binary.BigEndian.Uint32(val[:4])
groupIdx := binary.BigEndian.Uint64(val[4:])
groupKey := iface.GroupKey(groupIdx)
if err := closer.Close(); err != nil {
return xerrors.Errorf("get(s:) close: %w", err)
}
more, err := cb(idx, groupKey)
if err != nil {
return err
}
if !more {
continue
}
}
if err := closer.Close(); err != nil {
return xerrors.Errorf("getsizes close: %w", err)
}
// try to get from iterable list
keyPrefix := append([]byte("i:"), m...)
upperBound := append(append([]byte("i:"), m...), 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff)
iter := i.db.NewIter(nil)
iter.SetBounds(keyPrefix, upperBound)
for iter.SeekGE(keyPrefix); iter.Valid(); iter.Next() {
key := iter.Key()
groupKeyBytes := key[len(key)-8:]
groupKey := binary.BigEndian.Uint64(groupKeyBytes)
more, err := cb(idx, iface.GroupKey(groupKey))
if err != nil {
if err := iter.Close(); err != nil {
return xerrors.Errorf("closing iterator: %w", err)
}
return err
}
if !more {
break
}
}
if err := iter.Error(); err != nil {
i.iterPool.Put(iter)
return xerrors.Errorf("iter error: %w", err)
}
if err := iter.Close(); err != nil {
return xerrors.Errorf("closing iterator: %w", err)
}
}
return nil
}
func (i *PebbleIndex) GetSizes(ctx context.Context, mh []multihash.Multihash, cb func([]int32) error) error {
sizes := make([]int32, len(mh))
for id, m := range mh {
sizeKey := append([]byte("s:"), m...)
val, closer, err := i.db.Get(sizeKey)
if err == pebble.ErrNotFound {
sizes[id] = -1
continue
}
if err != nil {
return xerrors.Errorf("getsizes get: %w", err)
}
sizes[id] = int32(binary.BigEndian.Uint32(val))
if err := closer.Close(); err != nil {
return xerrors.Errorf("getsizes close: %w", err)
}
}
return cb(sizes)
}
func (i *PebbleIndex) AddGroup(ctx context.Context, mh []multihash.Multihash, sizes []int32, group iface.GroupKey) error {
groupBytes := make([]byte, 8)
binary.BigEndian.PutUint64(groupBytes, uint64(group))
sizeBytes := make([]byte, 4+len(groupBytes))
copy(sizeBytes[4:], groupBytes)
batch := i.db.NewBatch()
defer batch.Close()
for i, m := range mh {
{
// group key
key := append(append([]byte("i:"), m...), groupBytes...)
if err := batch.Set(key, nil, pebble.NoSync); err != nil {
return xerrors.Errorf("addgroup set (gk): %w", err)
}
}
{
// size key
binary.BigEndian.PutUint32(sizeBytes, uint32(sizes[i]))
key := append([]byte("s:"), m...)
if err := batch.Set(key, sizeBytes, pebble.NoSync); err != nil {
return xerrors.Errorf("addgroup set (sk): %w", err)
}
}
}
if err := batch.Commit(pebble.NoSync); err != nil {
return xerrors.Errorf("addgroup commit: %w", err)
}
return nil
}
func (i *PebbleIndex) DropGroup(ctx context.Context, mh []multihash.Multihash, group iface.GroupKey) error {
i.dropLk.Lock()
defer i.dropLk.Unlock()
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(group))
batch := i.db.NewBatch()
defer batch.Close()
for _, m := range mh {
key := append(append([]byte("i:"), m...), b...)
if err := batch.Delete(key, pebble.NoSync); err != nil {
return xerrors.Errorf("dropgroup delete: %w", err)
}
// if the size key contains entry for this group, remove the group pointer from the size key
// this way GetGroups is able to return data with a single read from s: keys in the
// common, optimistic case where the size key contains a group entry, and in the case when
// the size key does not contain a group entry, it will still be able to return the correct
// groups by reading from the i: keys
//
// note that we could scan the i: keys to find out if another mh->group mapping exists,
// but that would slow down deletes by a lot. Instead this will be done lazily in a GC pass
sizeKey := append([]byte("s:"), m...)
val, closer, err := i.db.Get(sizeKey)
if err == pebble.ErrNotFound {
continue
}
if err != nil {
return xerrors.Errorf("get(s:) get: %w", err)
}
if len(val) > 4 {
//size := binary.BigEndian.Uint32(val[:4])
groupIdx := binary.BigEndian.Uint64(val[4:])
groupKey := iface.GroupKey(groupIdx)
if groupKey == group {
newSizeVal := make([]byte, 4)
copy(newSizeVal, val[:4])
if err := batch.Set(sizeKey, newSizeVal, pebble.NoSync); err != nil {
return xerrors.Errorf("dropgroup set (sk): %w", err)
}
}
}
if err := closer.Close(); err != nil {
return xerrors.Errorf("delget(s:) close: %w", err)
}
}
if err := batch.Commit(pebble.NoSync); err != nil {
return xerrors.Errorf("dropgroup commit: %w", err)
}
// todo: gc size keys
return nil
}
const averageEntrySize = 35 + 8 // multihash is ~35 bytes, groupkey is 8 bytes
func (i *PebbleIndex) EstimateSize(ctx context.Context) (int64, error) {
lowerBound := []byte("i:")
upperBound := []byte("s;")
estimatedSize, err := i.db.EstimateDiskUsage(lowerBound, upperBound)
if err != nil {
return 0, err
}
estimatedEntries := int64(estimatedSize) / averageEntrySize
return estimatedEntries, nil
}
func (i *PebbleIndex) Close() error {
return i.db.Close()
}
var _ iface.Index = (*PebbleIndex)(nil)