@@ -569,10 +569,14 @@ func Report(ctx context.Context, st transaction.Store, chunk swarm.Chunk, state
569569
570570 indexStore := st .IndexStore ()
571571
572+ // Once the chunk is stored/synced/failed to sync, it is deleted from the upload store as
573+ // we no longer need to keep track of this chunk. We also need to cleanup
574+ // the pushItem.
572575 deleteFunc := func () error {
573- // Once the chunk is stored/synced/failed to sync, it is deleted from the upload store as
574- // we no longer need to keep track of this chunk. We also need to cleanup
575- // the pushItem.
576+ if state == storage .ChunkSent {
577+ return nil
578+ }
579+
576580 pi := & pushItem {
577581 Timestamp : ui .Uploaded ,
578582 Address : chunk .Address (),
@@ -598,49 +602,41 @@ func Report(ctx context.Context, st transaction.Store, chunk swarm.Chunk, state
598602 return fmt .Errorf ("failed to read uploadItem %x: %w" , ui .BatchID , err )
599603 }
600604
601- if ui .TagID > 0 {
602- ti := & TagItem {TagID : ui .TagID }
603- err = indexStore .Get (ti )
604- if err != nil {
605- if errors .Is (err , storage .ErrNotFound ) { // tag was deleted by user
606- if state == storage .ChunkSent {
607- ui .TagID = 0
608- err = indexStore .Put (ui )
609- if err != nil {
610- return fmt .Errorf ("failed updating empty tag for chunk: %w" , err )
611- }
612-
613- return nil
614- }
615-
616- // state != sent
617- return deleteFunc ()
618-
619- } else {
620- return fmt .Errorf ("failed getting tag: %w" , err )
621- }
622- }
623- switch state {
624- case storage .ChunkSent :
625- ti .Sent ++
626- case storage .ChunkStored :
627- ti .Stored ++
628- // also mark it as synced
629- fallthrough
630- case storage .ChunkSynced :
631- ti .Synced ++
632- case storage .ChunkCouldNotSync :
633- break
605+ // tag is missing
606+ if ui .TagID == 0 {
607+ return deleteFunc ()
608+ }
609+
610+ ti := & TagItem {TagID : ui .TagID }
611+ err = indexStore .Get (ti )
612+ if err != nil {
613+ if ! errors .Is (err , storage .ErrNotFound ) {
614+ return fmt .Errorf ("failed getting tag: %w" , err )
634615 }
635616
636- err = indexStore .Put (ti )
617+ ui .TagID = 0
618+ err = indexStore .Put (ui )
637619 if err != nil {
638- return fmt .Errorf ("failed updating tag: %w" , err )
620+ return fmt .Errorf ("failed updating empty tag for chunk : %w" , err )
639621 }
622+
623+ return deleteFunc ()
640624 }
641625
642- if state == storage .ChunkSent {
643- return nil
626+ // update the tag
627+ switch state {
628+ case storage .ChunkSent :
629+ ti .Sent ++
630+ case storage .ChunkStored :
631+ ti .Stored ++
632+ ti .Synced ++
633+ case storage .ChunkSynced :
634+ ti .Synced ++
635+ }
636+
637+ err = indexStore .Put (ti )
638+ if err != nil {
639+ return fmt .Errorf ("failed updating tag: %w" , err )
644640 }
645641
646642 return deleteFunc ()
0 commit comments