UpdateChunkStatus updates the bitmap based on byte range
(offset, length int64, status ChunkStatus)
| 432 | |
| 433 | // UpdateChunkStatus updates the bitmap based on byte range |
| 434 | func (ps *ProgressState) UpdateChunkStatus(offset, length int64, status ChunkStatus) { |
| 435 | ps.mu.Lock() |
| 436 | |
| 437 | if ps.ActualChunkSize == 0 || len(ps.ChunkBitmap) == 0 { |
| 438 | utils.Debug("UpdateChunkStatus skipped: ActualChunkSize=%d, BitmapLen=%d", ps.ActualChunkSize, len(ps.ChunkBitmap)) |
| 439 | ps.mu.Unlock() |
| 440 | return |
| 441 | } |
| 442 | |
| 443 | if len(ps.ChunkProgress) != ps.BitmapWidth { |
| 444 | utils.Debug("UpdateChunkStatus: Initializing ChunkProgress array (width=%d)", ps.BitmapWidth) |
| 445 | ps.ChunkProgress = make([]int64, ps.BitmapWidth) |
| 446 | } |
| 447 | |
| 448 | startIdx := int(offset / ps.ActualChunkSize) |
| 449 | endIdx := int((offset + length - 1) / ps.ActualChunkSize) |
| 450 | |
| 451 | if startIdx < 0 { |
| 452 | startIdx = 0 |
| 453 | } |
| 454 | if endIdx >= ps.BitmapWidth { |
| 455 | endIdx = ps.BitmapWidth - 1 |
| 456 | } |
| 457 | |
| 458 | var totalIncrement int64 |
| 459 | |
| 460 | for i := startIdx; i <= endIdx; i++ { |
| 461 | // Calculate precise overlap with this chunk |
| 462 | chunkStart := int64(i) * ps.ActualChunkSize |
| 463 | chunkEnd := chunkStart + ps.ActualChunkSize |
| 464 | if chunkEnd > ps.TotalSize { |
| 465 | chunkEnd = ps.TotalSize |
| 466 | } |
| 467 | |
| 468 | updateStart := offset |
| 469 | if updateStart < chunkStart { |
| 470 | updateStart = chunkStart |
| 471 | } |
| 472 | |
| 473 | updateEnd := offset + length |
| 474 | if updateEnd > chunkEnd { |
| 475 | updateEnd = chunkEnd |
| 476 | } |
| 477 | |
| 478 | overlap := updateEnd - updateStart |
| 479 | if overlap < 0 { |
| 480 | overlap = 0 |
| 481 | } |
| 482 | |
| 483 | switch status { |
| 484 | case ChunkCompleted: |
| 485 | increment := overlap |
| 486 | remainingSpace := (chunkEnd - chunkStart) - ps.ChunkProgress[i] |
| 487 | |
| 488 | if increment > remainingSpace { |
| 489 | increment = remainingSpace |
| 490 | } |
| 491 |