use of org.apache.bookkeeper.stream.proto.NamespaceMetadata in project bookkeeper by apache.
the class RootRangeStoreImpl method executeCreateNamespaceTxn.
private CompletableFuture<CreateNamespaceResponse> executeCreateNamespaceTxn(long currentNsId, long currentNsIdRev, CreateNamespaceRequest request) {
long namespaceId = currentNsId + 1;
String nsName = request.getName();
NamespaceMetadata metadata = NamespaceMetadata.newBuilder().setProps(NamespaceProperties.newBuilder().setNamespaceId(namespaceId).setNamespaceName(nsName).setDefaultStreamConf(request.getColConf().getDefaultStreamConf())).build();
byte[] nsNameKey = getNamespaceNameKey(nsName);
byte[] nsNameVal = Bytes.toBytes(namespaceId);
byte[] nsIdKey = getNamespaceIdKey(namespaceId);
byte[] nsIdVal = metadata.toByteArray();
TxnOp<byte[], byte[]> txn = store.newTxn().If(store.newCompareValue(CompareResult.EQUAL, nsNameKey, null), currentNsIdRev < 0 ? store.newCompareValue(CompareResult.EQUAL, NS_ID_KEY, null) : store.newCompareModRevision(CompareResult.EQUAL, NS_ID_KEY, currentNsIdRev)).Then(store.newPut(nsNameKey, nsNameVal), store.newPut(nsIdKey, nsIdVal), store.newPut(NS_ID_KEY, Bytes.toBytes(namespaceId))).build();
return store.txn(txn).thenApply(txnResult -> {
try {
CreateNamespaceResponse.Builder respBuilder = CreateNamespaceResponse.newBuilder();
if (txnResult.isSuccess()) {
respBuilder.setCode(StatusCode.SUCCESS);
respBuilder.setColProps(metadata.getProps());
} else {
// TODO: differentiate the error code
respBuilder.setCode(StatusCode.INTERNAL_SERVER_ERROR);
}
return respBuilder.build();
} finally {
txnResult.close();
}
}).whenComplete((resp, cause) -> txn.close());
}
Aggregations