public int countEncodedCrossSegments() { int total = 0; for(SplitFileInserterCrossSegmentStorage segment : crossSegments) { if(segment.isFinishedEncoding()) total++; } return total; }
void addDataBlock(SplitFileInserterSegmentStorage seg, int blockNum) { assert(counter < dataBlockCount); assert(blockNum < seg.dataBlockCount); addBlock(seg, blockNum); }
private boolean allSegmentsCompletedOrFailed() { for(SplitFileInserterSegmentStorage segment : segments) { if(!segment.hasCompletedOrFailed()) return false; } if(crossSegments != null) { for(SplitFileInserterCrossSegmentStorage segment : crossSegments) { if(!segment.hasCompletedOrFailed()) return false; } } return true; }
/** Status. Generally depends on the status of the individual segments... * Not persisted: Can be deduced from the state of the segments, except for the last 3 states, * which are only used during completion (we don't keep the storage around once we're * finished). */ enum Status { NOT_STARTED, STARTED, ENCODED_CROSS_SEGMENTS, ENCODED, GENERATING_METADATA, SUCCEEDED, FAILED }
for(int i=0;i<segments;i++) { assertEquals(storage.crossSegments[i].dataBlockCount, fetcherStorage.crossSegments[i].dataBlockCount); assertTrue(Arrays.equals(storage.crossSegments[i].getSegmentNumbers(), fetcherStorage.crossSegments[i].getSegmentNumbers())); assertTrue(Arrays.equals(storage.crossSegments[i].getBlockNumbers(), fetcherStorage.crossSegments[i].getBlockNumbers()));
private void allocateCrossCheckBlock(SplitFileInserterCrossSegmentStorage segment, Random xsRandom) { int x = 0; for (int i = 0; i < 10; i++) { x = xsRandom.nextInt(segments.length); SplitFileInserterSegmentStorage seg = segments[x]; int blockNum = seg.allocateCrossCheckBlock(segment, xsRandom, segment.getAllocatedCrossCheckBlocks()); if (blockNum >= 0) { segment.addCheckBlock(seg, blockNum); return; } } for (int i = 0; i < segments.length; i++) { x++; if (x == segments.length) x = 0; SplitFileInserterSegmentStorage seg = segments[x]; int blockNum = seg.allocateCrossCheckBlock(segment, xsRandom, segment.getAllocatedCrossCheckBlocks()); if (blockNum >= 0) { segment.addCheckBlock(seg, blockNum); return; } } throw new IllegalStateException("Unable to allocate cross data block!"); }
dis = new DataInputStream(is); for(int i=0;i<crossSegments.length;i++) { crossSegments[i] = new SplitFileInserterCrossSegmentStorage(this, dis, i); segment.readStatus();
private void allocateCrossDataBlock(SplitFileInserterCrossSegmentStorage segment, Random xsRandom) { int x = 0; for (int i = 0; i < 10; i++) { x = xsRandom.nextInt(segments.length); SplitFileInserterSegmentStorage seg = segments[x]; int blockNum = seg.allocateCrossDataBlock(segment, xsRandom); if (blockNum >= 0) { segment.addDataBlock(seg, blockNum); return; } } for (int i = 0; i < segments.length; i++) { x++; if (x == segments.length) x = 0; SplitFileInserterSegmentStorage seg = segments[x]; int blockNum = seg.allocateCrossDataBlock(segment, xsRandom); if (blockNum >= 0) { segment.addDataBlock(seg, blockNum); return; } } throw new IllegalStateException("Unable to allocate cross data block!"); }
@Override public boolean run(ClientContext context) { // Tell the segments to cancel. boolean allDone = true; for(SplitFileInserterSegmentStorage segment : segments) { if(!segment.cancel()) allDone = false; } if(crossSegments != null) { for(SplitFileInserterCrossSegmentStorage segment : crossSegments) { if(!segment.cancel()) allDone = false; } } if(allDone) { synchronized(this) { // Could have beaten us to it in callback. if(hasFinished()) return false; status = Status.FAILED; } callback.onFailed(e); return true; } else { // Wait for them to finish encoding. return false; } }
for(int i=0;i<segments;i++) { assertEquals(storage.crossSegments[i].dataBlockCount, fetcherStorage.crossSegments[i].dataBlockCount); assertTrue(Arrays.equals(storage.crossSegments[i].getSegmentNumbers(), fetcherStorage.crossSegments[i].getSegmentNumbers())); assertTrue(Arrays.equals(storage.crossSegments[i].getBlockNumbers(), fetcherStorage.crossSegments[i].getBlockNumbers()));
private boolean allFinishedCrossEncoding() { for (SplitFileInserterCrossSegmentStorage segment : crossSegments) { if (!segment.isFinishedEncoding()) return false; } return true; }
/** Only used during construction */ void addCheckBlock(SplitFileInserterSegmentStorage seg, int blockNum) { assert(counter >= dataBlockCount); assert(blockNum >= seg.dataBlockCount && blockNum < seg.dataBlockCount + seg.crossCheckBlockCount); addBlock(seg, blockNum); }
private int countEncodedCrossSegments(SplitFileInserterStorage storage) { int total = 0; for(SplitFileInserterCrossSegmentStorage segment : storage.crossSegments) { if(segment.isFinishedEncoding()) total++; } return total; }
private void computeStatus() { status = Status.STARTED; if(crossSegments != null) { for(SplitFileInserterCrossSegmentStorage segment : crossSegments) { if(!segment.isFinishedEncoding()) return; } status = Status.ENCODED_CROSS_SEGMENTS; } for(SplitFileInserterSegmentStorage segment : segments) { if(!segment.isFinishedEncoding()) return; } status = Status.ENCODED; // Last 3 statuses are only used during completion. }