-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindexes.go
More file actions
404 lines (374 loc) · 9.84 KB
/
indexes.go
File metadata and controls
404 lines (374 loc) · 9.84 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package sqlrepo
import (
"context"
"fmt"
"iter"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
"github.com/storacha/go-ucanto/core/invocation"
"github.com/storacha/guppy/pkg/preparation/blobs"
"github.com/storacha/guppy/pkg/preparation/blobs/model"
"github.com/storacha/guppy/pkg/preparation/sqlrepo/util"
"github.com/storacha/guppy/pkg/preparation/types/id"
)
func (r *Repo) ShardsNotInIndexes(ctx context.Context, uploadID id.UploadID) ([]id.ShardID, error) {
shardsNotInIndexesQuery, err := r.prepareStmt(ctx, `
SELECT id
FROM shards
WHERE upload_id = ?
AND state != 'open'
AND NOT EXISTS (
SELECT 1
FROM shards_in_indexes si
WHERE si.shard_id = shards.id
)`)
if err != nil {
return nil, fmt.Errorf("failed to prepare statement: %w", err)
}
rows, err := shardsNotInIndexesQuery.QueryContext(ctx,
util.DbID(&uploadID),
)
if err != nil {
return nil, fmt.Errorf("failed to query shards not in indexes for upload %s: %w", uploadID, err)
}
defer rows.Close()
var shardIDs []id.ShardID
for rows.Next() {
var shardID id.ShardID
if err := rows.Scan(util.DbID(&shardID)); err != nil {
return nil, fmt.Errorf("failed to scan shard ID: %w", err)
}
shardIDs = append(shardIDs, shardID)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating shard rows: %w", err)
}
return shardIDs, nil
}
func (r *Repo) CreateIndex(ctx context.Context, uploadID id.UploadID) (*model.Index, error) {
index, err := model.NewIndex(uploadID)
if err != nil {
return nil, err
}
err = model.WriteIndexToDatabase(index, func(
id id.IndexID,
uploadID id.UploadID,
size uint64,
sliceCount int,
digest multihash.Multihash,
pieceCID cid.Cid,
state model.BlobState,
location invocation.Invocation,
pdpAccept invocation.Invocation,
) error {
stmt, err := r.prepareStmt(ctx, `
INSERT INTO indexes (
id,
upload_id,
size,
slice_count,
digest,
piece_cid,
state,
location_inv,
pdp_accept_inv
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`)
if err != nil {
return fmt.Errorf("failed to prepare statement: %w", err)
}
_, err = stmt.ExecContext(ctx,
id,
uploadID,
size,
sliceCount,
digest,
util.DbCID(&pieceCID),
state,
util.DbInvocation(&location),
util.DbInvocation(&pdpAccept),
)
return err
})
if err != nil {
return nil, fmt.Errorf("failed to write index for upload %s: %w", uploadID, err)
}
return index, nil
}
func (r *Repo) IndexesForUploadByState(ctx context.Context, uploadID id.UploadID, state model.BlobState) ([]*model.Index, error) {
stmt, err := r.prepareStmt(ctx, `
SELECT
id,
upload_id,
size,
slice_count,
digest,
piece_cid,
state,
location_inv,
pdp_accept_inv
FROM indexes
WHERE upload_id = ?
AND state = ?
`)
if err != nil {
return nil, fmt.Errorf("failed to prepare statement: %w", err)
}
rows, err := stmt.QueryContext(ctx, uploadID, state)
if err != nil {
return nil, fmt.Errorf("failed to query indexes for upload %s in state %s: %w", uploadID, state, err)
}
defer rows.Close()
var indexes []*model.Index
for rows.Next() {
index, err := model.ReadIndexFromDatabase(func(
id *id.IndexID,
uploadID *id.UploadID,
size *uint64,
sliceCount *int,
digest *multihash.Multihash,
pieceCID *cid.Cid,
state *model.BlobState,
location *invocation.Invocation,
pdpAccept *invocation.Invocation,
) error {
return rows.Scan(id, uploadID, size, sliceCount, util.DbBytes(digest), util.DbCID(pieceCID), state, util.DbInvocation(location), util.DbInvocation(pdpAccept))
})
if err != nil {
return nil, err
}
if index == nil {
continue
}
indexes = append(indexes, index)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating index rows: %w", err)
}
return indexes, nil
}
func (r *Repo) GetIndexByID(ctx context.Context, indexID id.IndexID) (*model.Index, error) {
stmt, err := r.prepareStmt(ctx, `
SELECT
id,
upload_id,
digest,
piece_cid,
size,
slice_count,
state,
location_inv,
pdp_accept_inv
FROM indexes
WHERE id = ?
`)
if err != nil {
return nil, fmt.Errorf("failed to prepare statement: %w", err)
}
row := stmt.QueryRowContext(ctx, indexID)
index, err := model.ReadIndexFromDatabase(func(
id *id.IndexID,
uploadID *id.UploadID,
size *uint64,
sliceCount *int,
digest *multihash.Multihash,
pieceCID *cid.Cid,
state *model.BlobState,
location *invocation.Invocation,
pdpAccept *invocation.Invocation,
) error {
return row.Scan(id, uploadID, util.DbBytes(digest), util.DbCID(pieceCID), size, sliceCount, state, util.DbInvocation(location), util.DbInvocation(pdpAccept))
})
if err != nil {
return nil, err
}
return index, nil
}
func (r *Repo) UpdateIndex(ctx context.Context, index *model.Index) error {
return model.WriteIndexToDatabase(index, func(
id id.IndexID,
uploadID id.UploadID,
size uint64,
sliceCount int,
digest multihash.Multihash,
pieceCID cid.Cid,
state model.BlobState,
location invocation.Invocation,
pdpAccept invocation.Invocation,
) error {
stmt, err := r.prepareStmt(ctx, `
UPDATE indexes
SET id = ?,
upload_id = ?,
size = ?,
slice_count = ?,
digest = ?,
piece_cid = ?,
state = ?,
location_inv = ?,
pdp_accept_inv = ?
WHERE id = ?
`)
if err != nil {
return fmt.Errorf("failed to prepare statement: %w", err)
}
_, err = stmt.ExecContext(ctx,
id,
uploadID,
size,
sliceCount,
digest,
util.DbCID(&pieceCID),
state,
util.DbInvocation(&location),
util.DbInvocation(&pdpAccept),
id,
)
return err
})
}
func (r *Repo) AddShardToIndex(ctx context.Context, indexID id.IndexID, shardID id.ShardID) error {
// Add the shard to the index
stmt, err := r.prepareStmt(ctx, `
INSERT INTO shards_in_indexes (shard_id, index_id)
VALUES (?, ?)
`)
if err != nil {
return fmt.Errorf("failed to prepare statement: %w", err)
}
_, err = stmt.ExecContext(ctx, shardID, indexID)
if err != nil {
return fmt.Errorf("failed to add shard %s to index %s: %w", shardID, indexID, err)
}
// Update the index's slice count using a subquery to avoid a separate read
stmt, err = r.prepareStmt(ctx, `
UPDATE indexes
SET slice_count = slice_count + (SELECT slice_count FROM shards WHERE id = ?)
WHERE id = ?
`)
if err != nil {
return fmt.Errorf("failed to prepare statement: %w", err)
}
_, err = stmt.ExecContext(ctx, shardID, indexID)
if err != nil {
return fmt.Errorf("failed to update index %s slice count: %w", indexID, err)
}
return nil
}
func (r *Repo) ShardsForIndex(ctx context.Context, indexID id.IndexID) ([]*model.Shard, error) {
stmt, err := r.prepareStmt(ctx, `
SELECT
s.id,
s.upload_id,
s.size,
s.slice_count,
s.digest,
s.piece_cid,
s.digest_state_up_to,
s.digest_state,
s.piece_cid_state,
s.state,
s.location_inv,
s.pdp_accept_inv
FROM shards s
INNER JOIN shards_in_indexes si ON s.id = si.shard_id
WHERE si.index_id = ?
ORDER BY s.id
`)
if err != nil {
return nil, fmt.Errorf("failed to prepare statement: %w", err)
}
rows, err := stmt.QueryContext(ctx, indexID)
if err != nil {
return nil, fmt.Errorf("failed to query shards for index %s: %w", indexID, err)
}
defer rows.Close()
var shards []*model.Shard
for rows.Next() {
shard, err := model.ReadShardFromDatabase(func(
id *id.ShardID,
uploadID *id.UploadID,
size *uint64,
sliceCount *int,
digest *multihash.Multihash,
pieceCID *cid.Cid,
digestStateUpTo *uint64,
digestState *[]byte,
pieceCIDState *[]byte,
state *model.BlobState,
location *invocation.Invocation,
pdpAccept *invocation.Invocation,
) error {
return rows.Scan(
id,
uploadID,
size,
sliceCount,
util.DbBytes(digest),
util.DbCID(pieceCID),
digestStateUpTo,
util.DbBytes(digestState),
util.DbBytes(pieceCIDState),
state,
util.DbInvocation(location),
util.DbInvocation(pdpAccept),
)
})
if err != nil {
return nil, fmt.Errorf("failed to read shard from database: %w", err)
}
shards = append(shards, shard)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating shard rows: %w", err)
}
return shards, nil
}
func (r *Repo) ForEachNodeInIndex(ctx context.Context, indexID id.IndexID) iter.Seq2[blobs.NodeInIndex, error] {
return func(yield func(blobs.NodeInIndex, error) bool) {
stmt, err := r.prepareStmt(ctx, `
SELECT s.id, s.digest, nodes.cid, nodes.size, nu.shard_offset
FROM shards_in_indexes si
JOIN shards s ON s.id = si.shard_id
JOIN node_uploads nu ON nu.shard_id = si.shard_id
JOIN nodes ON nodes.cid = nu.node_cid AND nodes.space_did = nu.space_did
WHERE si.index_id = ?`)
if err != nil {
yield(blobs.NodeInIndex{}, fmt.Errorf("failed to prepare statement: %w", err))
return
}
rows, err := stmt.QueryContext(ctx, indexID)
if err != nil {
yield(blobs.NodeInIndex{}, fmt.Errorf("failed to query nodes in index %s: %w", indexID, err))
return
}
defer rows.Close()
for rows.Next() {
var shardID id.ShardID
var shardDigest multihash.Multihash
var nodeCID cid.Cid
var nodeSize uint64
var shardOffset uint64
if err := rows.Scan(&shardID, util.DbBytes(&shardDigest), util.DbCID(&nodeCID), &nodeSize, &shardOffset); err != nil {
yield(blobs.NodeInIndex{}, fmt.Errorf("failed to scan node row in index %s: %w", indexID, err))
return
}
if len(shardDigest) == 0 {
yield(blobs.NodeInIndex{}, fmt.Errorf("failed to iterate nodes in index %s, because shard with ID %s has no digest set", indexID, shardID))
return
}
if !yield(blobs.NodeInIndex{
NodeCID: nodeCID,
NodeSize: nodeSize,
ShardDigest: shardDigest,
ShardOffset: shardOffset,
}, nil) {
return
}
}
err = rows.Err()
if err != nil {
yield(blobs.NodeInIndex{}, fmt.Errorf("error iterating node rows in index %s: %w", indexID, err))
}
}
}