use of org.apache.bookkeeper.stream.proto.StreamName in project bookkeeper by apache.
the class RootRangeStoreImpl method executeCreateStreamTxn.
private CompletableFuture<CreateStreamResponse> executeCreateStreamTxn(long nsId, String streamName, StreamConfiguration streamConf, long currentStreamId, long currentStreamIdRev) {
long streamId;
if (currentStreamId < 0) {
streamId = MIN_DATA_STREAM_ID;
} else {
streamId = currentStreamId + 1;
}
long scId = placementPolicy.placeStreamRange(streamId, 0L);
StreamConfiguration newStreamConf = streamConf;
// no backend service url is provided, use the default service url
if (isBlank(streamConf.getBackendServiceUrl())) {
newStreamConf = StreamConfiguration.newBuilder(streamConf).setBackendServiceUrl(defaultServiceUri.toString()).build();
}
StreamProperties streamProps = StreamProperties.newBuilder().setStreamId(streamId).setStreamName(streamName).setStorageContainerId(scId).setStreamConf(newStreamConf).build();
byte[] nsIdKey = getNamespaceIdKey(nsId);
byte[] streamNameKey = getStreamNameKey(nsId, streamName);
byte[] streamNameVal = Bytes.toBytes(streamId);
byte[] streamIdKey = getStreamIdKey(nsId, streamId);
byte[] streamIdVal = streamProps.toByteArray();
TxnOp<byte[], byte[]> txn = store.newTxn().If(store.newCompareValue(CompareResult.NOT_EQUAL, nsIdKey, null), currentStreamIdRev < 0 ? store.newCompareValue(CompareResult.EQUAL, STREAM_ID_KEY, null) : store.newCompareModRevision(CompareResult.EQUAL, STREAM_ID_KEY, currentStreamIdRev), store.newCompareValue(CompareResult.EQUAL, streamNameKey, null)).Then(store.newPut(streamNameKey, streamNameVal), store.newPut(streamIdKey, streamIdVal), store.newPut(STREAM_ID_KEY, Bytes.toBytes(streamId))).build();
return store.txn(txn).thenApply(txnResult -> {
try {
CreateStreamResponse.Builder respBuilder = CreateStreamResponse.newBuilder();
if (txnResult.isSuccess()) {
respBuilder.setCode(StatusCode.SUCCESS);
respBuilder.setStreamProps(streamProps);
} else {
// TODO: differentiate the error codes
respBuilder.setCode(StatusCode.INTERNAL_SERVER_ERROR);
}
return respBuilder.build();
} finally {
txnResult.close();
txn.close();
}
}).exceptionally(cause -> {
txn.close();
return CreateStreamResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
});
}
use of org.apache.bookkeeper.stream.proto.StreamName in project bookkeeper by apache.
the class RootRangeStoreImpl method getStream.
@Override
public CompletableFuture<GetStreamResponse> getStream(GetStreamRequest request) {
StreamName streamName = request.getStreamName();
StatusCode code = verifyStreamRequest(streamName.getColName(), streamName.getStreamName());
if (StatusCode.SUCCESS != code) {
return FutureUtils.value(GetStreamResponse.newBuilder().setCode(code).build());
}
byte[] nsNameKey = getNamespaceNameKey(streamName.getColName());
GetStreamResponse.Builder respBuilder = GetStreamResponse.newBuilder();
return store.get(nsNameKey).thenCompose(nsIdBytes -> {
if (null == nsIdBytes) {
return FutureUtils.value(respBuilder.setCode(StatusCode.NAMESPACE_NOT_FOUND).build());
}
long nsId = Bytes.toLong(nsIdBytes, 0);
return getStreamProps(nsId, streamName.getStreamName()).thenCompose(streamProps -> {
if (null == streamProps) {
return FutureUtils.value(respBuilder.setCode(StatusCode.STREAM_NOT_FOUND).build());
} else {
return FutureUtils.value(respBuilder.setCode(StatusCode.SUCCESS).setStreamProps(streamProps).build());
}
}).exceptionally(cause -> respBuilder.setCode(StatusCode.INTERNAL_SERVER_ERROR).build());
});
}
Aggregations