use of io.pravega.segmentstore.contracts.AttributeUpdateType in project pravega by pravega.
the class StreamSegmentMapperTests method createAttributes.
private Collection<AttributeUpdate> createAttributes(int count) {
Collection<AttributeUpdate> result = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
AttributeUpdateType ut = AttributeUpdateType.values()[i % AttributeUpdateType.values().length];
result.add(new AttributeUpdate(UUID.randomUUID(), ut, i, i));
}
return result;
}
use of io.pravega.segmentstore.contracts.AttributeUpdateType in project pravega by pravega.
the class SegmentMetadataUpdateTransaction method preProcessAttributes.
/**
* Pre-processes a collection of attributes.
* After this method returns, all AttributeUpdates in the given collection will have the actual (and updated) value
* of that attribute in the Segment.
*
* @param attributeUpdates The Updates to process (if any).
* @throws BadAttributeUpdateException If any of the given AttributeUpdates is invalid given the current state of
* the segment.
* @throws TooManyAttributesException If, as a result of applying the given updates, the Segment would exceed the
* maximum allowed number of Attributes.
*/
private void preProcessAttributes(Collection<AttributeUpdate> attributeUpdates) throws BadAttributeUpdateException, TooManyAttributesException {
if (attributeUpdates == null) {
return;
}
int newAttributeCount = this.attributeValues.size();
for (AttributeUpdate u : attributeUpdates) {
AttributeUpdateType updateType = u.getUpdateType();
long previousValue = this.attributeValues.getOrDefault(u.getAttributeId(), SegmentMetadata.NULL_ATTRIBUTE_VALUE);
// Perform validation, and set the AttributeUpdate.value to the updated value, if necessary.
switch(updateType) {
case ReplaceIfGreater:
// Verify value against existing value, if any.
boolean hasValue = previousValue != SegmentMetadata.NULL_ATTRIBUTE_VALUE;
if (hasValue && u.getValue() <= previousValue) {
throw new BadAttributeUpdateException(this.name, u, String.format("Expected greater than '%s'.", previousValue));
}
break;
case ReplaceIfEquals:
// Verify value against existing value, if any.
if (u.getComparisonValue() != previousValue) {
throw new BadAttributeUpdateException(this.name, u, String.format("Expected existing value to be '%s', actual '%s'.", u.getComparisonValue(), previousValue));
}
break;
case None:
// Verify value is not already set.
if (previousValue != SegmentMetadata.NULL_ATTRIBUTE_VALUE) {
throw new BadAttributeUpdateException(this.name, u, String.format("Attribute value already set (%s).", previousValue));
}
break;
case Accumulate:
if (previousValue != SegmentMetadata.NULL_ATTRIBUTE_VALUE) {
u.setValue(previousValue + u.getValue());
}
break;
case Replace:
break;
default:
throw new BadAttributeUpdateException(this.name, u, "Unexpected update type: " + updateType);
}
if (previousValue == SegmentMetadata.NULL_ATTRIBUTE_VALUE && u.getValue() != SegmentMetadata.NULL_ATTRIBUTE_VALUE) {
// This attribute did not exist and is about to be added.
newAttributeCount++;
} else if (previousValue != SegmentMetadata.NULL_ATTRIBUTE_VALUE && u.getValue() == SegmentMetadata.NULL_ATTRIBUTE_VALUE) {
// This attribute existed and is about to be removed.
newAttributeCount--;
}
}
if (newAttributeCount > SegmentMetadata.MAXIMUM_ATTRIBUTE_COUNT && newAttributeCount > this.attributeValues.size()) {
// attributes of existing segments, but not increase their count.
throw new TooManyAttributesException(this.name, SegmentMetadata.MAXIMUM_ATTRIBUTE_COUNT);
}
}
use of io.pravega.segmentstore.contracts.AttributeUpdateType in project pravega by pravega.
the class StreamSegmentAppendOperationTests method createAttributes.
static Collection<AttributeUpdate> createAttributes() {
val result = new ArrayList<AttributeUpdate>();
long currentValue = 0;
for (AttributeUpdateType ut : AttributeUpdateType.values()) {
result.add(new AttributeUpdate(UUID.randomUUID(), ut, ++currentValue, currentValue));
}
return result;
}
Aggregations