static SegmentInfo newSegment(String name, Collection<AttributeUpdate> attributeUpdates) { val infoBuilder = StreamSegmentInformation .builder() .name(name); if (attributeUpdates != null) { infoBuilder.attributes(attributeUpdates.stream().collect(Collectors.toMap(AttributeUpdate::getAttributeId, AttributeUpdate::getValue))); } return builder() .segmentId(ContainerMetadata.NO_STREAM_SEGMENT_ID) .properties(infoBuilder.build()) .build(); }
@Override public synchronized SegmentProperties getSnapshot() { return StreamSegmentInformation.from(this).attributes(new HashMap<>(getAttributes())).build(); }
private SegmentProperties createSegmentProperty(String streamSegmentName, UUID txnId) { Map<UUID, Long> attributes = new HashMap<>(); attributes.put(Attributes.EVENT_COUNT, 10L); attributes.put(Attributes.CREATION_TIME, System.currentTimeMillis()); return StreamSegmentInformation.builder() .name(txnId == null ? streamSegmentName + "#." : streamSegmentName + "#transaction." + txnId) .sealed(true) .deleted(false) .lastModified(null) .startOffset(0) .length(100) .attributes(attributes) .build(); }
.length(storageLength) .sealed(true) .attributes(createAttributes()) .build()); updateMap.setStreamSegmentId(mapOp.getStreamSegmentId()); .length(storageLength) .sealed(true) .attributes(createAttributes()) .build()); pinnedMap.markPinned();
.startOffset(0L) .length(1L) .attributes(toAttributes(createAttributeUpdates(ATTRIBUTE_COUNT))) .build(); context.getMetadataStore().updateSegmentInfo(toMetadata(segmentId, initialSegmentInfo), TIMEOUT).join();
.length(123) .sealed(true) .attributes(toAttributes(createAttributeUpdates(ATTRIBUTE_COUNT))) .build();
.startOffset(getSegmentStartOffset.apply(segmentName)) .sealed(i % 2 == 0) .attributes(toAttributes(createAttributeUpdates(ATTRIBUTE_COUNT))) .build();
/** * Tests the getStreamSegmentInfo() method. */ @Test public void testGetStreamSegmentInfo() { @Cleanup val context = new TestContext(); context.container.startAsync().awaitRunning(); // Non-existent segment. AssertExtensions.assertSuppliedFutureThrows( "Unexpected exception when the segment does not exist.", () -> context.container.getStreamSegmentInfo(SEGMENT_NAME, TIMEOUT), ex -> ex instanceof StreamSegmentNotExistsException); // Create a segment, add some data, set some attributes, "truncate" it and then seal it. val storageInfo = context.storage.create(SEGMENT_NAME, TIMEOUT) .thenCompose(handle -> context.storage.write(handle, 0, new ByteArrayInputStream(new byte[10]), 10, TIMEOUT)) .thenCompose(v -> context.storage.getStreamSegmentInfo(SEGMENT_NAME, TIMEOUT)).join(); val expectedInfo = StreamSegmentInformation.from(storageInfo) .startOffset(storageInfo.getLength() / 2) .attributes(ImmutableMap.of(UUID.randomUUID(), 100L, Attributes.EVENT_COUNT, 1L)) .build(); // Fetch the SegmentInfo from the ReadOnlyContainer and verify it is as expected. val actual = context.container.getStreamSegmentInfo(SEGMENT_NAME, TIMEOUT).join(); Assert.assertEquals("Unexpected Name.", expectedInfo.getName(), actual.getName()); Assert.assertEquals("Unexpected Length.", expectedInfo.getLength(), actual.getLength()); Assert.assertEquals("Unexpected Sealed status.", expectedInfo.isSealed(), actual.isSealed()); }
/** * Test the {@link IndexReader#getLastIndexedOffset(SegmentProperties)} method. */ @Test public void testTableAttributes() { val ir = newReader(); Assert.assertEquals("Unexpected value for TABLE_INDEX_OFFSET when attribute is not present.", 0, ir.getLastIndexedOffset(StreamSegmentInformation.builder().name("s").build())); Assert.assertEquals("Unexpected value for TABLE_ENTRY_COUNT when attribute is not present.", 0, ir.getEntryCount(StreamSegmentInformation.builder().name("s").build())); val si = StreamSegmentInformation.builder().name("s") .attributes(ImmutableMap.<UUID, Long>builder() .put(Attributes.TABLE_INDEX_OFFSET, 123456L) .put(Attributes.TABLE_ENTRY_COUNT, 2345L) .put(Attributes.TABLE_BUCKET_COUNT, 3456L) .build()) .build(); Assert.assertEquals("Unexpected value for TABLE_INDEX_OFFSET when attribute present.", 123456, ir.getLastIndexedOffset(si)); Assert.assertEquals("Unexpected value for TABLE_ENTRY_COUNT when attribute present.", 2345, ir.getEntryCount(si)); Assert.assertEquals("Unexpected value for TABLE_BUCKET_COUNT when attribute present.", 3456, ir.getBucketCount(si)); }
/** * Creates a new {@link StreamSegmentInformationBuilder} with information already populated from the given SegmentProperties. * * @param base The SegmentProperties to use as a base. * @return The Builder. */ public static StreamSegmentInformationBuilder from(SegmentProperties base) { return StreamSegmentInformation.builder() .name(base.getName()) .startOffset(base.getStartOffset()) .length(base.getLength()) .sealed(base.isSealed()) .deleted(base.isDeleted()) .lastModified(base.getLastModified()) .attributes(base.getAttributes()); }
@Override protected StreamSegmentMapOperation createOperation(Random random) { long length = MathHelpers.abs(random.nextLong()); val op = new StreamSegmentMapOperation(StreamSegmentInformation .builder() .name(super.getStreamSegmentName(random.nextLong())) .startOffset(length / 2) .length(length) .sealed(random.nextBoolean()) .deleted(random.nextBoolean()) .attributes(createAttributes(10)) .build()); op.markPinned(); return op; }
private long mapTransaction(long parentSegmentId, OperationMetadataUpdater updater, UpdateableContainerMetadata referenceMetadata) throws Exception { String segmentName = "Transaction_" + updater.nextOperationSequenceNumber(); val mapOp = new StreamSegmentMapOperation(StreamSegmentInformation .builder() .name(segmentName) .attributes(Collections.singletonMap(PARENT_ID, parentSegmentId)) .build()); process(mapOp, updater); if (referenceMetadata != null) { val rsm = referenceMetadata.mapStreamSegmentId(segmentName, mapOp.getStreamSegmentId()); rsm.setLength(0); rsm.setStorageLength(0); rsm.updateAttributes(mapOp.getAttributes()); } return mapOp.getStreamSegmentId(); }
private StreamSegmentMapOperation createMap(String name) { return new StreamSegmentMapOperation(StreamSegmentInformation.builder() .name(name) .length(SEGMENT_LENGTH) .startOffset(SEGMENT_LENGTH / 2) .sealed(true) .attributes(createAttributes()) .build()); }
private void read00(RevisionDataInput input, SegmentInfo.SegmentInfoBuilder builder) throws IOException { builder.segmentId(input.readLong()); val infoBuilder = StreamSegmentInformation .builder() .name(input.readUTF()) .length(input.readLong()) .startOffset(input.readLong()) .sealed(input.readBoolean()); infoBuilder.attributes(input.readMap(RevisionDataInput::readUUID, RevisionDataInput::readLong)); builder.properties(infoBuilder.build()); } }
private StreamSegmentMapOperation createTransactionMap(String name) { return new StreamSegmentMapOperation( StreamSegmentInformation.builder() .name(name) .length(SEALED_SOURCE_LENGTH) .sealed(true) .attributes(createAttributes()) .build()); }