/** * Creates a new instance of the SegmentChunk class. * * @param segmentName The name of the owning Segment (not the name of this SegmentChunk). * @param startOffset The offset within the owning Segment where this SegmentChunk starts at. * @return A new SegmentChunk. */ static SegmentChunk forSegment(String segmentName, long startOffset) { return new SegmentChunk(StreamSegmentNameUtils.getSegmentChunkName(segmentName, startOffset), startOffset); }
RollingSegmentHandle(SegmentHandle segmentHandle) { this.headerHandle = null; this.readOnly = segmentHandle.isReadOnly(); this.segmentName = Exceptions.checkNotNullOrEmpty(segmentHandle.getSegmentName(), "headerHandle.getSegmentName()"); this.rollingPolicy = SegmentRollingPolicy.NO_ROLLING; this.segmentChunks = Collections.singletonList(new SegmentChunk(segmentHandle.getSegmentName(), 0)); }
SegmentChunk s = new SegmentChunk(entry.getValue(), offset); Preconditions.checkArgument(lastOffset <= s.getStartOffset(), "SegmentChunk Entry '%s' has out-of-order offset (previous=%s).", s, lastOffset);
/** * Creates a new instance of the SegmentChunk class with the same information as this one, but with a new offset. * * @param newOffset The new offset. * @return A new SegmentChunk. */ SegmentChunk withNewOffset(long newOffset) { SegmentChunk ns = new SegmentChunk(this.name, newOffset); ns.setLength(getLength()); if (isSealed()) { ns.markSealed(); } if (!exists()) { ns.markInexistent(); } return ns; }
/** * Tests the ability of the Handle to refresh based on information from another similar handle. */ @Test public void testRefresh() { val headerHandle = new TestHandle(HEADER_NAME, true); val target = new RollingSegmentHandle(headerHandle, DEFAULT_ROLLING_POLICY, Collections.singletonList(new SegmentChunk("s1", 0L))); val source = new RollingSegmentHandle(headerHandle, DEFAULT_ROLLING_POLICY, Arrays.asList( new SegmentChunk("s1", 0L), new SegmentChunk("s2", 100L))); source.chunks().get(0).setLength(100); source.markSealed(); source.setHeaderLength(1000); source.setActiveChunkHandle(new TestHandle(source.lastChunk().getName(), false)); target.refresh(source); Assert.assertEquals("Unexpected getHeaderLength()", source.getHeaderLength(), target.getHeaderLength()); AssertExtensions.assertListEquals("Unexpected chunks()", source.chunks(), target.chunks(), Object::equals); Assert.assertTrue("Unexpected isSealed.", target.isSealed()); Assert.assertNull("Not expecting any ActiveSegmentHandle to be copied.", target.getActiveChunkHandle()); }
new SegmentChunk("s1", 0L), new SegmentChunk("s2", 10L)); firstBadList.get(0).setLength(9); AssertExtensions.assertThrows( new SegmentChunk("s1", 0L), new SegmentChunk("s2", 10L)); validList.get(0).setLength(10); validList.get(1).setLength(5); new SegmentChunk("s3", h.length() - 1), new SegmentChunk("s4", h.length() + 1)); secondBadList.get(0).setLength(2); AssertExtensions.assertThrows(
private RollingSegmentHandle newHandle(String segmentName, int chunkCount) { val chunks = new ArrayList<SegmentChunk>(); long offset = 0; val rnd = new Random(0); for (int i = 0; i < chunkCount; i++) { val chunk = new SegmentChunk(StreamSegmentNameUtils.getSegmentChunkName(segmentName, offset), offset); chunk.setLength(MathHelpers.abs(rnd.nextInt())); if (i < chunkCount - 1) { chunk.markSealed(); } chunks.add(chunk); offset += chunk.getLength(); } return new RollingSegmentHandle(new TestHandle(StreamSegmentNameUtils.getHeaderSegmentName(segmentName), false), TEST_ROLLING_POLICY, chunks); }
val chunk = new SegmentChunk(StreamSegmentNameUtils.getSegmentChunkName(source.getSegmentName(), source.length()), source.length()); chunk.setLength(i + 1); source.addChunks(Collections.singletonList(chunk));
() -> h.addChunk(new SegmentChunk("s", 0L), null), ex -> ex instanceof NullPointerException); AssertExtensions.assertThrows( "addChunk allowed adding a read-only ActiveSegmentHandle.", () -> h.addChunk(new SegmentChunk("s", 0L), new TestHandle("s", true)), ex -> ex instanceof IllegalArgumentException); AssertExtensions.assertThrows( "addChunk allowed adding an ActiveSegmentHandle with different name..", () -> h.addChunk(new SegmentChunk("s", 0L), new TestHandle("s2", false)), ex -> ex instanceof IllegalArgumentException); Assert.assertEquals("Not expecting any SegmentChunks to be added.", 0, h.chunks().size()); Assert.assertNull("Not expecting the Active SegmentChunk handle to be set.", h.getActiveChunkHandle()); val chunk = new SegmentChunk("s1", 100L); h.addChunk(chunk, new TestHandle("s1", false)); chunk.setLength(123L); () -> h.addChunk(new SegmentChunk("s2", chunk.getLastOffset() + 1), new TestHandle("s2", false)), ex -> ex instanceof IllegalArgumentException); val chunk2 = new SegmentChunk("s2", chunk.getLastOffset()); chunk2.setLength(234L); h.addChunk(chunk2, new TestHandle("s2", false));
h.addChunk(new SegmentChunk(chunkName, 0L), new TestHandle(chunkName, false)); AssertExtensions.assertThrows( "setActiveChunkHandle accepted a handle that does not match the last SegmentChunk's name.",