-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathuploadstore.go
More file actions
822 lines (706 loc) · 23 KB
/
uploadstore.go
File metadata and controls
822 lines (706 loc) · 23 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// Copyright 2022 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 upload
import (
"context"
"encoding/binary"
"errors"
"fmt"
"runtime"
"strconv"
"time"
"github.com/ethersphere/bee/v2/pkg/encryption"
storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storage/storageutil"
"github.com/ethersphere/bee/v2/pkg/storer/internal"
"github.com/ethersphere/bee/v2/pkg/storer/internal/chunkstamp"
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
"github.com/ethersphere/bee/v2/pkg/swarm"
"golang.org/x/sync/errgroup"
)
// now returns the current time.Time; used in testing.
var now = time.Now
var (
// errPushItemMarshalAddressIsZero is returned when trying
// to marshal a pushItem with an address that is zero.
errPushItemMarshalAddressIsZero = errors.New("marshal pushItem: address is zero")
// errPushItemMarshalBatchInvalid is returned when trying to
// marshal a pushItem with invalid batch
errPushItemMarshalBatchInvalid = errors.New("marshal pushItem: batch is invalid")
// errPushItemUnmarshalInvalidSize is returned when trying
// to unmarshal buffer that is not of size pushItemSize.
errPushItemUnmarshalInvalidSize = errors.New("unmarshal pushItem: invalid size")
)
// pushItemSize is the size of a marshaled pushItem.
const pushItemSize = 8 + 2*swarm.HashSize + 8
const uploadScope = "upload"
var _ storage.Item = (*pushItem)(nil)
// pushItem is an store.Item that represents data relevant to push.
// The key is a combination of Timestamp, Address and postage stamp, where the
// Timestamp provides an order to iterate.
type pushItem struct {
Timestamp int64
Address swarm.Address
BatchID []byte
TagID uint64
}
// ID implements the storage.Item interface.
func (i pushItem) ID() string {
return fmt.Sprintf("%d/%s/%s", i.Timestamp, i.Address.ByteString(), string(i.BatchID))
}
// Namespace implements the storage.Item interface.
func (i pushItem) Namespace() string {
return "pushIndex"
}
// Marshal implements the storage.Item interface.
// If the Address is zero, an error is returned.
func (i pushItem) Marshal() ([]byte, error) {
if i.Address.IsZero() {
return nil, errPushItemMarshalAddressIsZero
}
if len(i.BatchID) != swarm.HashSize {
return nil, errPushItemMarshalBatchInvalid
}
buf := make([]byte, pushItemSize)
binary.LittleEndian.PutUint64(buf, uint64(i.Timestamp))
copy(buf[8:], i.Address.Bytes())
copy(buf[8+swarm.HashSize:8+2*swarm.HashSize], i.BatchID)
binary.LittleEndian.PutUint64(buf[8+2*swarm.HashSize:], i.TagID)
return buf, nil
}
// Unmarshal implements the storage.Item interface.
// If the buffer is not of size pushItemSize, an error is returned.
func (i *pushItem) Unmarshal(bytes []byte) error {
if len(bytes) != pushItemSize {
return errPushItemUnmarshalInvalidSize
}
ni := new(pushItem)
ni.Timestamp = int64(binary.LittleEndian.Uint64(bytes))
ni.Address = swarm.NewAddress(append(make([]byte, 0, swarm.HashSize), bytes[8:8+swarm.HashSize]...))
ni.BatchID = append(make([]byte, 0, swarm.HashSize), bytes[8+swarm.HashSize:8+2*swarm.HashSize]...)
ni.TagID = binary.LittleEndian.Uint64(bytes[8+2*swarm.HashSize:])
*i = *ni
return nil
}
// Clone implements the storage.Item interface.
func (i *pushItem) Clone() storage.Item {
if i == nil {
return nil
}
return &pushItem{
Timestamp: i.Timestamp,
Address: i.Address.Clone(),
BatchID: append([]byte(nil), i.BatchID...),
TagID: i.TagID,
}
}
// String implements the fmt.Stringer interface.
func (i pushItem) String() string {
return storageutil.JoinFields(i.Namespace(), i.ID())
}
var (
// errTagIDAddressItemUnmarshalInvalidSize is returned when trying
// to unmarshal buffer that is not of size tagItemSize.
errTagItemUnmarshalInvalidSize = errors.New("unmarshal TagItem: invalid size")
)
// tagItemSize is the size of a marshaled TagItem.
const tagItemSize = swarm.HashSize + 7*8
var _ storage.Item = (*TagItem)(nil)
// TagItem is an store.Item that stores information about a session of upload.
type TagItem struct {
TagID uint64 // unique identifier for the tag
Split uint64 // total no of chunks processed by the splitter for hashing
Seen uint64 // total no of chunks already seen
Stored uint64 // total no of chunks stored locally on the node
Sent uint64 // total no of chunks sent to the neighbourhood
Synced uint64 // total no of chunks synced with proof
Address swarm.Address // swarm.Address associated with this tag
StartedAt int64 // start timestamp
}
// ID implements the storage.Item interface.
func (i TagItem) ID() string {
return strconv.FormatUint(i.TagID, 10)
}
// Namespace implements the storage.Item interface.
func (i TagItem) Namespace() string {
return "tagItem"
}
// Marshal implements the storage.Item interface.
func (i TagItem) Marshal() ([]byte, error) {
buf := make([]byte, tagItemSize)
binary.LittleEndian.PutUint64(buf, i.TagID)
binary.LittleEndian.PutUint64(buf[8:], i.Split)
binary.LittleEndian.PutUint64(buf[16:], i.Seen)
binary.LittleEndian.PutUint64(buf[24:], i.Stored)
binary.LittleEndian.PutUint64(buf[32:], i.Sent)
binary.LittleEndian.PutUint64(buf[40:], i.Synced)
addrBytes := internal.AddressBytesOrZero(i.Address)
if len(addrBytes) == encryption.ReferenceSize {
// in case of encrypted reference we use the swarm hash as the address and
// avoid storing the encryption key
addrBytes = addrBytes[:swarm.HashSize]
}
copy(buf[48:], addrBytes)
binary.LittleEndian.PutUint64(buf[48+swarm.HashSize:], uint64(i.StartedAt))
return buf, nil
}
// Unmarshal implements the storage.Item interface.
// If the buffer is not of size tagItemSize, an error is returned.
func (i *TagItem) Unmarshal(bytes []byte) error {
if len(bytes) != tagItemSize {
return errTagItemUnmarshalInvalidSize
}
ni := new(TagItem)
ni.TagID = binary.LittleEndian.Uint64(bytes)
ni.Split = binary.LittleEndian.Uint64(bytes[8:])
ni.Seen = binary.LittleEndian.Uint64(bytes[16:])
ni.Stored = binary.LittleEndian.Uint64(bytes[24:])
ni.Sent = binary.LittleEndian.Uint64(bytes[32:])
ni.Synced = binary.LittleEndian.Uint64(bytes[40:])
ni.Address = internal.AddressOrZero(bytes[48 : 48+swarm.HashSize])
ni.StartedAt = int64(binary.LittleEndian.Uint64(bytes[48+swarm.HashSize:]))
*i = *ni
return nil
}
// Clone implements the storage.Item interface.
func (i *TagItem) Clone() storage.Item {
if i == nil {
return nil
}
return &TagItem{
TagID: i.TagID,
Split: i.Split,
Seen: i.Seen,
Stored: i.Stored,
Sent: i.Sent,
Synced: i.Synced,
Address: i.Address.Clone(),
StartedAt: i.StartedAt,
}
}
// String implements the fmt.Stringer interface.
func (i TagItem) String() string {
return storageutil.JoinFields(i.Namespace(), i.ID())
}
var (
// errUploadItemMarshalAddressIsZero is returned when trying
// to marshal a uploadItem with an address that is zero.
errUploadItemMarshalAddressIsZero = errors.New("marshal uploadItem: address is zero")
// errUploadItemMarshalBatchInvalid is returned when trying to
// marshal a uploadItem with invalid batch
errUploadItemMarshalBatchInvalid = errors.New("marshal uploadItem: batch is invalid")
// errTagIDAddressItemUnmarshalInvalidSize is returned when trying
// to unmarshal buffer that is not of size uploadItemSize.
errUploadItemUnmarshalInvalidSize = errors.New("unmarshal uploadItem: invalid size")
)
// uploadItemSize is the size of a marshaled uploadItem.
const uploadItemSize = 3 * 8
var _ storage.Item = (*uploadItem)(nil)
// uploadItem is an store.Item that stores addresses of already seen chunks.
type uploadItem struct {
Address swarm.Address
BatchID []byte
TagID uint64
Uploaded int64
Synced int64
// IdFunc overrides the ID method.
// This used to get the ID from the item where the address and batchID were not marshalled.
IdFunc func() string
}
// ID implements the storage.Item interface.
func (i uploadItem) ID() string {
if i.IdFunc != nil {
return i.IdFunc()
}
return storageutil.JoinFields(i.Address.ByteString(), string(i.BatchID))
}
// Namespace implements the storage.Item interface.
func (i uploadItem) Namespace() string {
return "UploadItem"
}
// Marshal implements the storage.Item interface.
// If the Address is zero, an error is returned.
func (i uploadItem) Marshal() ([]byte, error) {
// Address and BatchID are not part of the marshaled payload. But they are used
// in they key and hence are required. The Marshaling is done when item is to
// be stored, so we return errors for these cases.
if i.Address.IsZero() {
return nil, errUploadItemMarshalAddressIsZero
}
if len(i.BatchID) != swarm.HashSize {
return nil, errUploadItemMarshalBatchInvalid
}
buf := make([]byte, uploadItemSize)
binary.LittleEndian.PutUint64(buf, i.TagID)
binary.LittleEndian.PutUint64(buf[8:], uint64(i.Uploaded))
binary.LittleEndian.PutUint64(buf[16:], uint64(i.Synced))
return buf, nil
}
// Unmarshal implements the storage.Item interface.
// If the buffer is not of size pushItemSize, an error is returned.
func (i *uploadItem) Unmarshal(bytes []byte) error {
if len(bytes) != uploadItemSize {
return errUploadItemUnmarshalInvalidSize
}
// The Address and BatchID are required for the key, so it is assumed that
// they will be filled already. We reuse them during unmarshaling.
i.TagID = binary.LittleEndian.Uint64(bytes[:8])
i.Uploaded = int64(binary.LittleEndian.Uint64(bytes[8:16]))
i.Synced = int64(binary.LittleEndian.Uint64(bytes[16:]))
return nil
}
// Clone implements the storage.Item interface.
func (i *uploadItem) Clone() storage.Item {
if i == nil {
return nil
}
return &uploadItem{
Address: i.Address.Clone(),
BatchID: append([]byte(nil), i.BatchID...),
TagID: i.TagID,
Uploaded: i.Uploaded,
Synced: i.Synced,
}
}
// String implements the fmt.Stringer interface.
func (i uploadItem) String() string {
return storageutil.JoinFields(i.Namespace(), i.ID())
}
// dirtyTagItemUnmarshalInvalidSize is returned when trying
// to unmarshal buffer that is not of size dirtyTagItemSize.
var errDirtyTagItemUnmarshalInvalidSize = errors.New("unmarshal dirtyTagItem: invalid size")
// dirtyTagItemSize is the size of a marshaled dirtyTagItem.
const dirtyTagItemSize = 8 + 8
type dirtyTagItem struct {
TagID uint64
Started int64
}
// ID implements the storage.Item interface.
func (i dirtyTagItem) ID() string {
return strconv.FormatUint(i.TagID, 10)
}
// Namespace implements the storage.Item interface.
func (i dirtyTagItem) Namespace() string {
return "DirtyTagItem"
}
// Marshal implements the storage.Item interface.
func (i dirtyTagItem) Marshal() ([]byte, error) {
buf := make([]byte, dirtyTagItemSize)
binary.LittleEndian.PutUint64(buf, i.TagID)
binary.LittleEndian.PutUint64(buf[8:], uint64(i.Started))
return buf, nil
}
// Unmarshal implements the storage.Item interface.
func (i *dirtyTagItem) Unmarshal(bytes []byte) error {
if len(bytes) != dirtyTagItemSize {
return errDirtyTagItemUnmarshalInvalidSize
}
i.TagID = binary.LittleEndian.Uint64(bytes[:8])
i.Started = int64(binary.LittleEndian.Uint64(bytes[8:]))
return nil
}
// Clone implements the storage.Item interface.
func (i *dirtyTagItem) Clone() storage.Item {
if i == nil {
return nil
}
return &dirtyTagItem{
TagID: i.TagID,
Started: i.Started,
}
}
// String implements the fmt.Stringer interface.
func (i dirtyTagItem) String() string {
return storageutil.JoinFields(i.Namespace(), i.ID())
}
var (
// errPutterAlreadyClosed is returned when trying to Put a new chunk
// after the putter has been closed.
errPutterAlreadyClosed = errors.New("upload store: putter already closed")
// errOverwriteOfImmutableBatch is returned when stamp index already
// exists and the batch is immutable.
errOverwriteOfImmutableBatch = errors.New("upload store: overwrite of existing immutable batch")
// errOverwriteOfNewerBatch is returned if a stamp index already exists
// and the existing chunk with the same stamp index has a newer timestamp.
errOverwriteOfNewerBatch = errors.New("upload store: overwrite of existing batch with newer timestamp")
)
type uploadPutter struct {
tagID uint64
split uint64
seen uint64
closed bool
}
// NewPutter returns a new chunk putter associated with the tagID.
// Calls to the Putter must be mutex locked to prevent concurrent upload data races.
func NewPutter(s storage.IndexStore, tagID uint64) (internal.PutterCloserWithReference, error) {
ti := &TagItem{TagID: tagID}
has, err := s.Has(ti)
if err != nil {
return nil, err
}
if !has {
return nil, fmt.Errorf("upload store: tag %d not found: %w", tagID, storage.ErrNotFound)
}
err = s.Put(&dirtyTagItem{TagID: tagID, Started: now().UnixNano()})
if err != nil {
return nil, err
}
return &uploadPutter{
tagID: ti.TagID,
}, nil
}
// Put operation will do the following:
// 1.If upload store has already seen this chunk, it will update the tag and return
// 2.For a new chunk it will add:
// - uploadItem entry to keep track of this chunk.
// - pushItem entry to make it available for PushSubscriber
// - add chunk to the chunkstore till it is synced
// The user of the putter MUST mutex lock the call to prevent data-races across multiple upload sessions.
func (u *uploadPutter) Put(ctx context.Context, st transaction.Store, chunk swarm.Chunk) error {
if u.closed {
return errPutterAlreadyClosed
}
// Check if upload store has already seen this chunk
ui := &uploadItem{Address: chunk.Address(), BatchID: chunk.Stamp().BatchID()}
switch exists, err := st.IndexStore().Has(ui); {
case err != nil:
return fmt.Errorf("store has item %q call failed: %w", ui, err)
case exists:
u.seen++
u.split++
return nil
}
u.split++
ui.Uploaded = now().UnixNano()
ui.TagID = u.tagID
pi := &pushItem{
Timestamp: ui.Uploaded,
Address: chunk.Address(),
BatchID: chunk.Stamp().BatchID(),
TagID: u.tagID,
}
return errors.Join(
st.IndexStore().Put(ui),
st.IndexStore().Put(pi),
st.ChunkStore().Put(ctx, chunk),
chunkstamp.Store(st.IndexStore(), uploadScope, chunk),
)
}
// Close provides the CloseWithReference interface where the session can be associated
// with a swarm reference. This can be useful while keeping track of uploads through
// the tags. It will update the tag. This will be filled with the Split and Seen count
// by the Putter.
func (u *uploadPutter) Close(s storage.IndexStore, addr swarm.Address) error {
if u.closed {
return nil
}
ti := &TagItem{TagID: u.tagID}
err := s.Get(ti)
if err != nil {
return fmt.Errorf("failed reading tag while closing: %w", err)
}
ti.Split += u.split
ti.Seen += u.seen
if !addr.IsZero() {
ti.Address = addr.Clone()
}
u.closed = true
return errors.Join(
s.Put(ti),
s.Delete(&dirtyTagItem{TagID: u.tagID}),
)
}
func (u *uploadPutter) Cleanup(st transaction.Storage) error {
if u.closed {
return nil
}
itemsToDelete := make([]*pushItem, 0)
di := &dirtyTagItem{TagID: u.tagID}
err := st.IndexStore().Get(di)
if err != nil {
return fmt.Errorf("failed reading dirty tag while cleaning up: %w", err)
}
err = st.IndexStore().Iterate(
storage.Query{
Factory: func() storage.Item { return &pushItem{} },
PrefixAtStart: true,
Prefix: fmt.Sprintf("%d", di.Started),
},
func(res storage.Result) (bool, error) {
pi := res.Entry.(*pushItem)
if pi.TagID == u.tagID {
itemsToDelete = append(itemsToDelete, pi)
}
return false, nil
},
)
if err != nil {
return fmt.Errorf("failed iterating over push items: %w", err)
}
var eg errgroup.Group
eg.SetLimit(runtime.NumCPU())
for _, item := range itemsToDelete {
func(item *pushItem) {
eg.Go(func() error {
return st.Run(context.Background(), func(s transaction.Store) error {
ui := &uploadItem{Address: item.Address, BatchID: item.BatchID}
return errors.Join(
s.IndexStore().Delete(ui),
s.ChunkStore().Delete(context.Background(), item.Address),
chunkstamp.Delete(s.IndexStore(), uploadScope, item.Address, item.BatchID),
s.IndexStore().Delete(item),
)
})
})
}(item)
}
return errors.Join(
eg.Wait(),
st.Run(context.Background(), func(s transaction.Store) error {
return s.IndexStore().Delete(&dirtyTagItem{TagID: u.tagID})
}),
)
}
// CleanupDirty does a best-effort cleanup of dirty tags. This is called on startup.
func CleanupDirty(st transaction.Storage) error {
dirtyTags := make([]*dirtyTagItem, 0)
err := st.IndexStore().Iterate(
storage.Query{
Factory: func() storage.Item { return &dirtyTagItem{} },
},
func(res storage.Result) (bool, error) {
di := res.Entry.(*dirtyTagItem)
dirtyTags = append(dirtyTags, di)
return false, nil
},
)
if err != nil {
return fmt.Errorf("failed iterating dirty tags: %w", err)
}
for _, di := range dirtyTags {
err = errors.Join(err, (&uploadPutter{tagID: di.TagID}).Cleanup(st))
}
return err
}
// Report is the implementation of the PushReporter interface.
func Report(ctx context.Context, st transaction.Store, chunk swarm.Chunk, state storage.ChunkState) error {
ui := &uploadItem{Address: chunk.Address(), BatchID: chunk.Stamp().BatchID()}
indexStore := st.IndexStore()
// Once the chunk is stored/synced/failed to sync, it is deleted from the upload store as
// we no longer need to keep track of this chunk. We also need to cleanup
// the pushItem.
deleteFunc := func() error {
if state == storage.ChunkSent {
return nil
}
pi := &pushItem{
Timestamp: ui.Uploaded,
Address: chunk.Address(),
BatchID: chunk.Stamp().BatchID(),
}
return errors.Join(
indexStore.Delete(pi),
chunkstamp.Delete(indexStore, uploadScope, pi.Address, pi.BatchID),
st.ChunkStore().Delete(ctx, chunk.Address()),
indexStore.Delete(ui),
)
}
err := indexStore.Get(ui)
if err != nil {
// because of the nature of the feed mechanism of the uploadstore/pusher, a chunk that is in inflight may be sent more than once to the pusher.
// this is because the chunks are removed from the queue only when they are synced, not at the start of the upload
if errors.Is(err, storage.ErrNotFound) {
return nil
}
return fmt.Errorf("failed to read uploadItem %x: %w", ui.BatchID, err)
}
// tag is missing
if ui.TagID == 0 {
return deleteFunc()
}
ti := &TagItem{TagID: ui.TagID}
err = indexStore.Get(ti)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("failed getting tag: %w", err)
}
ui.TagID = 0
err = indexStore.Put(ui)
if err != nil {
return fmt.Errorf("failed updating empty tag for chunk: %w", err)
}
return deleteFunc()
}
// update the tag
switch state {
case storage.ChunkSent:
ti.Sent++
case storage.ChunkStored:
ti.Stored++
ti.Synced++
case storage.ChunkSynced:
ti.Synced++
}
err = indexStore.Put(ti)
if err != nil {
return fmt.Errorf("failed updating tag: %w", err)
}
return deleteFunc()
}
var (
errNextTagIDUnmarshalInvalidSize = errors.New("unmarshal nextTagID: invalid size")
)
// nextTagID is a storage.Item which stores a uint64 value in the store.
type nextTagID uint64
func (nextTagID) Namespace() string { return "upload" }
func (nextTagID) ID() string { return "nextTagID" }
func (n nextTagID) Marshal() ([]byte, error) {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(n))
return buf, nil
}
func (n *nextTagID) Unmarshal(buf []byte) error {
if len(buf) != 8 {
return errNextTagIDUnmarshalInvalidSize
}
*n = nextTagID(binary.LittleEndian.Uint64(buf))
return nil
}
func (n *nextTagID) Clone() storage.Item {
if n == nil {
return nil
}
ni := *n
return &ni
}
func (n nextTagID) String() string {
return storageutil.JoinFields(n.Namespace(), n.ID())
}
// NextTag returns the next tag ID to be used. It reads the last used ID and
// increments it by 1. This method needs to be called under lock by user as there
// is no guarantee for parallel updates.
func NextTag(st storage.IndexStore) (TagItem, error) {
var (
tagID nextTagID
tag TagItem
)
err := st.Get(&tagID)
if err != nil && !errors.Is(err, storage.ErrNotFound) {
return tag, err
}
tagID++
err = st.Put(&tagID)
if err != nil {
return tag, err
}
tag.TagID = uint64(tagID)
tag.StartedAt = now().UnixNano()
return tag, st.Put(&tag)
}
// TagInfo returns the TagItem for this particular tagID.
func TagInfo(st storage.Reader, tagID uint64) (TagItem, error) {
ti := TagItem{TagID: tagID}
err := st.Get(&ti)
if err != nil {
return ti, fmt.Errorf("uploadstore: failed getting tag %d: %w", tagID, err)
}
return ti, nil
}
// ListAllTags returns all the TagItems in the store.
func ListAllTags(st storage.Reader) ([]TagItem, error) {
var tags []TagItem
err := st.Iterate(storage.Query{
Factory: func() storage.Item { return new(TagItem) },
}, func(r storage.Result) (bool, error) {
tags = append(tags, *r.Entry.(*TagItem))
return false, nil
})
if err != nil {
return nil, fmt.Errorf("uploadstore: failed to iterate tags: %w", err)
}
return tags, nil
}
func IteratePending(ctx context.Context, s transaction.ReadOnlyStore, consumerFn func(chunk swarm.Chunk) (bool, error)) error {
return s.IndexStore().Iterate(storage.Query{
Factory: func() storage.Item { return &pushItem{} },
}, func(r storage.Result) (bool, error) {
pi := r.Entry.(*pushItem)
has, err := s.IndexStore().Has(&dirtyTagItem{TagID: pi.TagID})
if err != nil {
return true, err
}
if has {
return false, nil
}
chunk, err := s.ChunkStore().Get(ctx, pi.Address)
if err != nil {
return true, err
}
stamp, err := chunkstamp.LoadWithBatchID(s.IndexStore(), uploadScope, chunk.Address(), pi.BatchID)
if err != nil {
return true, err
}
chunk = chunk.
WithStamp(stamp).
WithTagID(uint32(pi.TagID))
return consumerFn(chunk)
})
}
// DeleteTag deletes TagItem associated with the given tagID.
func DeleteTag(st storage.Writer, tagID uint64) error {
if err := st.Delete(&TagItem{TagID: tagID}); err != nil {
return fmt.Errorf("uploadstore: failed to delete tag %d: %w", tagID, err)
}
return nil
}
func IterateAll(st storage.Reader, iterateFn func(item storage.Item) (bool, error)) error {
return st.Iterate(
storage.Query{
Factory: func() storage.Item { return new(uploadItem) },
},
func(r storage.Result) (bool, error) {
ui := r.Entry.(*uploadItem)
ui.IdFunc = func() string {
return r.ID
}
return iterateFn(ui)
},
)
}
func IterateAllTagItems(st storage.Reader, cb func(ti *TagItem) (bool, error)) error {
return st.Iterate(
storage.Query{
Factory: func() storage.Item { return new(TagItem) },
},
func(result storage.Result) (bool, error) {
ti := result.Entry.(*TagItem)
return cb(ti)
},
)
}
// BatchIDForChunk returns the first known batchID for the given chunk address.
func BatchIDForChunk(st storage.Reader, addr swarm.Address) ([]byte, error) {
var batchID []byte
err := st.Iterate(
storage.Query{
Factory: func() storage.Item { return new(uploadItem) },
Prefix: addr.ByteString(),
ItemProperty: storage.QueryItemID,
},
func(r storage.Result) (bool, error) {
if len(r.ID) < 32 {
return false, nil
}
batchID = []byte(r.ID[len(r.ID)-32:])
return false, nil
},
)
if err != nil {
return nil, err
}
if batchID == nil {
return nil, storage.ErrNotFound
}
return batchID, nil
}