| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
// FileSeed is used to populate a contiguous seed during extraction in order |
| 9 |
// to copy/clone ranges that were written to the output file earlier. This is |
| 10 |
// to potentially dedup/reflink duplicate chunks or ranges of chunks within the |
| 11 |
// same file. |
| 12 |
type selfSeed struct { |
| 13 |
file string |
| 14 |
index Index |
| 15 |
pos map[ChunkID][]int |
| 16 |
canReflink bool |
| 17 |
written int |
| 18 |
mu sync.RWMutex |
| 19 |
cache map[int]int |
| 20 |
} |
| 21 |
|
| 22 |
// newSelfSeed initializes a new seed based on the file being extracted |
| 23 |
func newSelfSeed(file string, index Index) (*selfSeed, error) { |
| 24 |
s := selfSeed{ |
| 25 |
file: file, |
| 26 |
pos: make(map[ChunkID][]int), |
| 27 |
index: index, |
| 28 |
canReflink: CanClone(file, file), |
| 29 |
cache: make(map[int]int), |
| 30 |
} |
| 31 |
return &s, nil |
| 32 |
} |
| 33 |
|
| 34 |
// add records a new segment that's been written to the file. Since only contiguous |
| 35 |
// ranges of chunks are considered and writing happens concurrently, the segment |
| 36 |
// written here will not be usable until all earlier chunks have been written as |
| 37 |
// well. |
| 38 |
func (s *selfSeed) add(segment IndexSegment) { |
| 39 |
s.mu.Lock() |
| 40 |
defer s.mu.Unlock() |
| 41 |
|
| 42 |
// Make a record of this segment in the cache since those could come in |
| 43 |
// out-of-order |
| 44 |
s.cache[segment.first] = segment.last + 1 |
| 45 |
|
| 46 |
// Advance pos until we find a chunk we don't yet have recorded while recording |
| 47 |
// the chunk positions we do have in the position map used to find seed matches. |
| 48 |
// Since it's guaranteed that the numbers are only increasing, we drop old numbers |
| 49 |
// from the cache map to keep its size to a minimum and only store out-of-sequence |
| 50 |
// numbers |
| 51 |
for { |
| 52 |
// See if we can advance the write pointer in the self-seed which requires |
| 53 |
// consecutive chunks. If we don't have the next segment yet, just keep it |
| 54 |
// in the cache until we do. |
| 55 |
next, ok := s.cache[s.written] |
| 56 |
if !ok { |
| 57 |
break |
| 58 |
} |
| 59 |
// Record all chunks in this segment as written by adding them to the position map |
| 60 |
for i := s.written; i < next; i++ { |
| 61 |
chunk := s.index.Chunks[i] |
| 62 |
s.pos[chunk.ID] = append(s.pos[chunk.ID], i) |
| 63 |
} |
| 64 |
delete(s.cache, s.written) |
| 65 |
s.written = next |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// getChunk returns a segment with the requested chunk ID. If selfSeed doesn't |
| 70 |
// have the requested chunk, nil will be returned. |
| 71 |
func (s *selfSeed) getChunk(id ChunkID) SeedSegment { |
| 72 |
s.mu.RLock() |
| 73 |
pos, ok := s.pos[id] |
| 74 |
s.mu.RUnlock() |
| 75 |
if !ok { |
| 76 |
return nil |
| 77 |
} |
| 78 |
first := pos[0] |
| 79 |
return newFileSeedSegment(s.file, s.index.Chunks[first:first+1], s.canReflink) |
| 80 |
} |
| 81 |
|
| 82 |
func (s *selfSeed) RegenerateIndex(ctx context.Context, n int, attempt int, seedNumber int) error { |
| 83 |
panic("A selfSeed can't be regenerated") |
| 84 |
} |
| 85 |
|
| 86 |
func (s *selfSeed) SetInvalid(value bool) { |
| 87 |
panic("A selfSeed is never expected to be invalid") |
| 88 |
} |
| 89 |
|
| 90 |
func (s *selfSeed) IsInvalid() bool { |
| 91 |
// A selfSeed is never expected to be invalid |
| 92 |
return false |
| 93 |
} |