Search in sources :

Example 1 with AttributeUpdateType

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;
}
Also used : AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) ArrayList(java.util.ArrayList)

Example 2 with AttributeUpdateType

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);
    }
}
Also used : AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) TooManyAttributesException(io.pravega.segmentstore.contracts.TooManyAttributesException) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) BadAttributeUpdateException(io.pravega.segmentstore.contracts.BadAttributeUpdateException)

Example 3 with AttributeUpdateType

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;
}
Also used : lombok.val(lombok.val) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) ArrayList(java.util.ArrayList)

Aggregations

AttributeUpdate (io.pravega.segmentstore.contracts.AttributeUpdate)3 AttributeUpdateType (io.pravega.segmentstore.contracts.AttributeUpdateType)3 ArrayList (java.util.ArrayList)2 BadAttributeUpdateException (io.pravega.segmentstore.contracts.BadAttributeUpdateException)1 TooManyAttributesException (io.pravega.segmentstore.contracts.TooManyAttributesException)1 lombok.val (lombok.val)1