use of com.github.ambry.messageformat.MessageFormatInputStream in project ambry by linkedin.
the class ReplicationTest method getPutMessage.
/**
* Constructs an entire message with header, blob properties, user metadata and blob content.
* @param id id for which the message has to be constructed.
* @param accountId accountId of the blob
* @param containerId containerId of the blob
* @param enableEncryption {@code true} if encryption needs to be enabled. {@code false} otherwise
* @return a {@link Pair} of {@link ByteBuffer} and {@link MessageInfo} representing the entire message and the
* associated {@link MessageInfo}
* @throws MessageFormatException
* @throws IOException
*/
private Pair<ByteBuffer, MessageInfo> getPutMessage(StoreKey id, short accountId, short containerId, boolean enableEncryption) throws MessageFormatException, IOException {
int blobSize = TestUtils.RANDOM.nextInt(500) + 501;
int userMetadataSize = TestUtils.RANDOM.nextInt(blobSize / 2);
int encryptionKeySize = TestUtils.RANDOM.nextInt(blobSize / 4);
byte[] blob = new byte[blobSize];
byte[] usermetadata = new byte[userMetadataSize];
byte[] encryptionKey = enableEncryption ? new byte[encryptionKeySize] : null;
TestUtils.RANDOM.nextBytes(blob);
TestUtils.RANDOM.nextBytes(usermetadata);
BlobProperties blobProperties = new BlobProperties(blobSize, "test", accountId, containerId, encryptionKey != null);
MessageFormatInputStream stream = new PutMessageFormatInputStream(id, encryptionKey == null ? null : ByteBuffer.wrap(encryptionKey), blobProperties, ByteBuffer.wrap(usermetadata), new ByteBufferInputStream(ByteBuffer.wrap(blob)), blobSize);
byte[] message = Utils.readBytesFromStream(stream, (int) stream.getSize());
return new Pair<>(ByteBuffer.wrap(message), new MessageInfo(id, message.length, Utils.Infinite_Time, accountId, containerId, blobProperties.getCreationTimeInMs()));
}
use of com.github.ambry.messageformat.MessageFormatInputStream in project ambry by linkedin.
the class ReplicationTest method getDeleteMessage.
/**
* Returns a delete message for the given {@code id}
* @param id the id for which a delete message must be constructed.
* @return {@link ByteBuffer} representing the entire message.
* @throws MessageFormatException
* @throws IOException
*/
private ByteBuffer getDeleteMessage(StoreKey id, short accountId, short containerId, long deletionTimeMs) throws MessageFormatException, IOException {
MessageFormatInputStream stream = new DeleteMessageFormatInputStream(id, accountId, containerId, deletionTimeMs);
byte[] message = Utils.readBytesFromStream(stream, (int) stream.getSize());
return ByteBuffer.wrap(message);
}
use of com.github.ambry.messageformat.MessageFormatInputStream in project ambry by linkedin.
the class InMemoryStore method delete.
@Override
public void delete(List<MessageInfo> infos) throws StoreException {
List<MessageInfo> infosToDelete = new ArrayList<>(infos.size());
List<InputStream> inputStreams = new ArrayList<>();
try {
for (MessageInfo info : infos) {
short lifeVersion = info.getLifeVersion();
MessageInfo latestInfo = getMergedMessageInfo(info.getStoreKey(), messageInfos);
if (latestInfo == null) {
throw new StoreException("Cannot delete id " + info.getStoreKey() + " since it is not present in the index.", StoreErrorCodes.ID_Not_Found);
}
if (lifeVersion == MessageInfo.LIFE_VERSION_FROM_FRONTEND) {
if (latestInfo.isDeleted()) {
throw new StoreException("Cannot delete id " + info.getStoreKey() + " since it is already deleted in the index.", StoreErrorCodes.ID_Deleted);
}
lifeVersion = latestInfo.getLifeVersion();
} else {
if ((latestInfo.isDeleted() && latestInfo.getLifeVersion() >= info.getLifeVersion()) || (latestInfo.getLifeVersion() > info.getLifeVersion())) {
throw new StoreException("Cannot delete id " + info.getStoreKey() + " since it is already deleted in the index.", StoreErrorCodes.Life_Version_Conflict);
}
lifeVersion = info.getLifeVersion();
}
MessageFormatInputStream stream = new DeleteMessageFormatInputStream(info.getStoreKey(), info.getAccountId(), info.getContainerId(), info.getOperationTimeMs(), lifeVersion);
infosToDelete.add(new MessageInfo(info.getStoreKey(), stream.getSize(), true, info.isTtlUpdated(), false, info.getExpirationTimeInMs(), null, info.getAccountId(), info.getContainerId(), info.getOperationTimeMs(), lifeVersion));
inputStreams.add(stream);
}
MessageFormatWriteSet writeSet = new MessageFormatWriteSet(new SequenceInputStream(Collections.enumeration(inputStreams)), infosToDelete, false);
writeSet.writeTo(log);
messageInfos.addAll(infosToDelete);
} catch (Exception e) {
throw (e instanceof StoreException ? (StoreException) e : new StoreException(e, StoreErrorCodes.Unknown_Error));
}
}
use of com.github.ambry.messageformat.MessageFormatInputStream in project ambry by linkedin.
the class InMemoryStore method updateTtl.
@Override
public void updateTtl(List<MessageInfo> infos) throws StoreException {
List<MessageInfo> infosToUpdate = new ArrayList<>(infos.size());
List<InputStream> inputStreams = new ArrayList<>();
try {
for (MessageInfo info : infos) {
if (info.getExpirationTimeInMs() != Utils.Infinite_Time) {
throw new StoreException("BlobStore only supports removing the expiration time", StoreErrorCodes.Update_Not_Allowed);
}
MessageInfo latestInfo = getMergedMessageInfo(info.getStoreKey(), messageInfos);
if (latestInfo == null) {
throw new StoreException("Cannot update TTL of " + info.getStoreKey() + " since it's not in the index", StoreErrorCodes.ID_Not_Found);
} else if (latestInfo.isDeleted()) {
throw new StoreException("Cannot update TTL of " + info.getStoreKey() + " since it is already deleted in the index.", StoreErrorCodes.ID_Deleted);
} else if (latestInfo.isTtlUpdated()) {
throw new StoreException("TTL of " + info.getStoreKey() + " is already updated in the index.", StoreErrorCodes.Already_Updated);
}
short lifeVersion = latestInfo.getLifeVersion();
MessageFormatInputStream stream = new TtlUpdateMessageFormatInputStream(info.getStoreKey(), info.getAccountId(), info.getContainerId(), info.getExpirationTimeInMs(), info.getOperationTimeMs(), lifeVersion);
infosToUpdate.add(new MessageInfo(info.getStoreKey(), stream.getSize(), false, true, false, info.getExpirationTimeInMs(), null, info.getAccountId(), info.getContainerId(), info.getOperationTimeMs(), lifeVersion));
inputStreams.add(stream);
}
MessageFormatWriteSet writeSet = new MessageFormatWriteSet(new SequenceInputStream(Collections.enumeration(inputStreams)), infosToUpdate, false);
writeSet.writeTo(log);
messageInfos.addAll(infosToUpdate);
} catch (Exception e) {
throw (e instanceof StoreException ? (StoreException) e : new StoreException(e, StoreErrorCodes.Unknown_Error));
}
}
use of com.github.ambry.messageformat.MessageFormatInputStream in project ambry by linkedin.
the class ReplicationTestHelper method getTtlUpdateMessage.
public static ByteBuffer getTtlUpdateMessage(StoreKey id, short accountId, short containerId, long expiresAtMs, long updateTimeMs, short lifeVersion) throws MessageFormatException, IOException {
MessageFormatInputStream stream = new TtlUpdateMessageFormatInputStream(id, accountId, containerId, expiresAtMs, updateTimeMs, lifeVersion);
byte[] message = Utils.readBytesFromStream(stream, (int) stream.getSize());
return ByteBuffer.wrap(message);
}
Aggregations