@@ -454,62 +454,46 @@ func (u *uploadPutter) Put(ctx context.Context, st transaction.Store, chunk swar
454454// with a swarm reference. This can be useful while keeping track of uploads through
455455// the tags. It will update the tag. This will be filled with the Split and Seen count
456456// by the Putter.
457- func (u * uploadPutter ) Close (st transaction. Storage , addr swarm.Address ) error {
457+ func (u * uploadPutter ) Close (s storage. IndexStore , addr swarm.Address ) error {
458458 if u .closed {
459459 return nil
460460 }
461- defer func () {
462- u .closed = true
463- }()
464461
465- if err := u .Cleanup (st ); err != nil {
466- return fmt .Errorf ("cleanup failed on close, root_ref %s: %w" , addr , err )
467- }
468-
469- return st .Run (context .Background (), func (s transaction.Store ) error {
462+ u .closed = true
470463
471- ti := & TagItem {TagID : u .tagID }
472- err := s .IndexStore ().Get (ti )
473- if err != nil {
474- if errors .Is (err , storage .ErrNotFound ) {
475- return nil
476- }
477- return fmt .Errorf ("failed reading tag while closing: %w" , err )
464+ ti := & TagItem {TagID : u .tagID }
465+ err := s .Get (ti )
466+ if err != nil {
467+ if errors .Is (err , storage .ErrNotFound ) {
468+ return s .Delete (& dirtyTagItem {TagID : u .tagID })
478469 }
470+ return fmt .Errorf ("failed reading tag while closing: %w" , err )
471+ }
479472
480- ti .Split += u .split
481- ti .Seen += u .seen
473+ ti .Split += u .split
474+ ti .Seen += u .seen
482475
483- if ! addr .IsZero () {
484- ti .Address = addr .Clone ()
485- }
476+ if ! addr .IsZero () {
477+ ti .Address = addr .Clone ()
478+ }
486479
487- return s .IndexStore ().Put (ti )
488- })
480+ return errors .Join (
481+ s .Put (ti ),
482+ s .Delete (& dirtyTagItem {TagID : u .tagID }),
483+ )
489484}
490485
491- func (u * uploadPutter ) Cleanup (st transaction.Storage ) error {
492- if u .closed {
493- return errPutterAlreadyClosed
494- }
486+ func Cleanup (st transaction.Storage , tag uint64 ) error {
495487
496488 itemsToDelete := make ([]* pushItem , 0 )
497489
498- di := & dirtyTagItem {TagID : u .tagID }
499- err := st .IndexStore ().Get (di )
500- if err != nil {
501- return fmt .Errorf ("failed reading dirty tag while cleaning up: %w" , err )
502- }
503-
504- err = st .IndexStore ().Iterate (
490+ err := st .IndexStore ().Iterate (
505491 storage.Query {
506- Factory : func () storage.Item { return & pushItem {} },
507- PrefixAtStart : true ,
508- Prefix : fmt .Sprintf ("%d" , di .Started ),
492+ Factory : func () storage.Item { return & pushItem {} },
509493 },
510494 func (res storage.Result ) (bool , error ) {
511495 pi := res .Entry .(* pushItem )
512- if pi .TagID == u . tagID {
496+ if pi .TagID == tag {
513497 itemsToDelete = append (itemsToDelete , pi )
514498 }
515499 return false , nil
@@ -540,7 +524,7 @@ func (u *uploadPutter) Cleanup(st transaction.Storage) error {
540524 return errors .Join (
541525 eg .Wait (),
542526 st .Run (context .Background (), func (s transaction.Store ) error {
543- return s .IndexStore ().Delete (di )
527+ return s .IndexStore ().Delete (& dirtyTagItem { TagID : tag } )
544528 }),
545529 )
546530}
@@ -564,33 +548,33 @@ func CleanupDirty(st transaction.Storage) error {
564548 }
565549
566550 for _ , di := range dirtyTags {
567- err = errors .Join (err , ( & uploadPutter { tagID : di .TagID }). Cleanup ( st ))
551+ err = errors .Join (err , Cleanup ( st , di .TagID ))
568552 }
569553
570554 return err
571555}
572556
573557// Report is the implementation of the PushReporter interface.
574558// Must be mutex locked.
575- func Report (st transaction. Store , tag uint64 , update * TagUpdate ) error {
559+ func Report (st storage. IndexStore , tag uint64 , update * TagUpdate ) ( bool , error ) {
576560
577561 ti := & TagItem {TagID : tag }
578- err := st .IndexStore (). Get (ti )
562+ err := st .Get (ti )
579563 if err != nil {
580564 if ! errors .Is (err , storage .ErrNotFound ) {
581- return fmt .Errorf ("failed getting tag: %w" , err )
565+ return false , fmt .Errorf ("failed getting tag: %w" , err )
582566 }
583567
584568 // tag is missing, no need update it
585- return nil
569+ return false , nil
586570 }
587571
588572 // update the tag
589573 ti .Sent += update .Sent
590574 ti .Stored += update .Stored
591575 ti .Synced += update .Synced
592576
593- return st . IndexStore () .Put (ti )
577+ return ti . Synced >= ti . Split , st .Put (ti )
594578}
595579
596580var (
@@ -683,9 +667,11 @@ func ListAllTags(st storage.Reader) ([]TagItem, error) {
683667 return tags , nil
684668}
685669
686- func IteratePending (ctx context.Context , s transaction.ReadOnlyStore , consumerFn func (chunk swarm.Chunk ) (bool , error )) error {
670+ func IteratePending (ctx context.Context , s transaction.ReadOnlyStore , start int64 , consumerFn func (chunk swarm.Chunk , ts int64 ) (bool , error )) error {
687671 return s .IndexStore ().Iterate (storage.Query {
688- Factory : func () storage.Item { return & pushItem {} },
672+ Factory : func () storage.Item { return & pushItem {} },
673+ PrefixAtStart : true ,
674+ Prefix : fmt .Sprintf ("%d" , start ),
689675 }, func (r storage.Result ) (bool , error ) {
690676 pi := r .Entry .(* pushItem )
691677 has , err := s .IndexStore ().Has (& dirtyTagItem {TagID : pi .TagID })
@@ -709,7 +695,7 @@ func IteratePending(ctx context.Context, s transaction.ReadOnlyStore, consumerFn
709695 WithStamp (stamp ).
710696 WithTagID (pi .TagID )
711697
712- return consumerFn (chunk )
698+ return consumerFn (chunk , pi . Timestamp )
713699 })
714700}
715701
0 commit comments