eolivelli commented on a change in pull request #510: Issue-605 BP-15 New
CreateLedger API
URL: https://github.com/apache/bookkeeper/pull/510#discussion_r140976187
##########
File path:
bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerCreateOp.java
##########
@@ -200,4 +209,220 @@ private void createComplete(int rc, LedgerHandle lh) {
cb.createComplete(rc, lh, ctx);
}
+ private static final byte[] EMPTY_PASSWORD = new byte[0];
+
+ public static class CreateBuilderImpl implements CreateBuilder {
+
+ private final BookKeeper bk;
+ private int builderEnsembleSize = 3;
+ private int builderAckQuorumSize = 2;
+ private int builderWriteQuorumSize = 2;
+ private byte[] builderPassword = EMPTY_PASSWORD;
+ private org.apache.bookkeeper.client.api.DigestType builderDigestType
+ = org.apache.bookkeeper.client.api.DigestType.CRC32;
+ private Map<String, byte[]> builderCustomMetadata =
Collections.emptyMap();
+ private CreateAdvBuilder advBuilder;
+
+ public CreateBuilderImpl(BookKeeper bk) {
+ this.bk = bk;
+ }
+
+ @Override
+ public CreateBuilder withEnsembleSize(int ensembleSize) {
+ this.builderEnsembleSize = ensembleSize;
+ return this;
+ }
+
+ @Override
+ public CreateBuilder withWriteQuorumSize(int writeQuorumSize) {
+ this.builderWriteQuorumSize = writeQuorumSize;
+ return this;
+ }
+
+ @Override
+ public CreateBuilder withAckQuorumSize(int ackQuorumSize) {
+ this.builderAckQuorumSize = ackQuorumSize;
+ return this;
+ }
+
+ @Override
+ public CreateBuilder withPassword(byte[] password) {
+ this.builderPassword = password;
+ return this;
+ }
+
+ @Override
+ public CreateBuilder withCustomMetadata(Map<String, byte[]>
customMetadata) {
+ this.builderCustomMetadata = customMetadata;
+ return this;
+ }
+
+ @Override
+ public CreateBuilder
withDigestType(org.apache.bookkeeper.client.api.DigestType digestType) {
+ this.builderDigestType = digestType;
+ return this;
+ }
+
+ @Override
+ public CreateAdvBuilder makeAdv() {
+ if(advBuilder == null) {
+ advBuilder = new CreateAdvBuilderImpl(this);
+ }
+ return advBuilder;
+ }
+
+ private boolean validate(CreateCallback cb, Object ctx) {
+ if (builderWriteQuorumSize > builderEnsembleSize) {
+
cb.createComplete(BKException.Code.IncorrectParameterException, null, ctx);
+ return false;
+ }
+
+ if (builderAckQuorumSize > builderWriteQuorumSize) {
+
cb.createComplete(BKException.Code.IncorrectParameterException, null, ctx);
+ return false;
+ }
+
+ if (builderAckQuorumSize <= 0) {
+
cb.createComplete(BKException.Code.IncorrectParameterException, null, ctx);
+ return false;
+ }
+
+ if (builderPassword == null) {
+
cb.createComplete(BKException.Code.IncorrectParameterException, null, ctx);
+ return false;
+ }
+
+ if (builderDigestType == null) {
+
cb.createComplete(BKException.Code.IncorrectParameterException, null, ctx);
+ return false;
+ }
+
+ if (builderCustomMetadata == null) {
+
cb.createComplete(BKException.Code.IncorrectParameterException, null, ctx);
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public CompletableFuture<WriteHandle> execute() {
+ CompletableFuture<WriteHandle> counter = new CompletableFuture<>();
+ create(new SyncCreateCallback(), counter);
+ return counter;
+ }
+
+ private void create(CreateCallback cb, Object ctx) {
+ if (!validate(cb, ctx)) {
+ return;
+ }
+ LedgerCreateOp op = new LedgerCreateOp(bk, builderEnsembleSize,
+ builderWriteQuorumSize, builderAckQuorumSize,
DigestType.fromApiDigestType(builderDigestType),
+ builderPassword, cb, ctx, builderCustomMetadata);
+ ReentrantReadWriteLock closeLock = bk.getCloseLock();
+ closeLock.readLock().lock();
+ try {
+ if (bk.isClosed()) {
+ cb.createComplete(BKException.Code.ClientClosedException,
null, ctx);
+ return;
+ }
+ op.initiate();
+ } finally {
+ closeLock.readLock().unlock();
+ }
+ }
+ }
+
+ private static class CreateAdvBuilderImpl implements CreateAdvBuilder {
+
+ private Long builderLedgerId;
+ private final int builderEnsembleSize;
+ private final int builderAckQuorumSize;
+ private final int builderWriteQuorumSize;
+ private final byte[] builderPassword;
+ private final Map<String, byte[]> builderCustomMetadata;
+ private final org.apache.bookkeeper.client.api.DigestType
builderDigestType;
+ private final BookKeeper bk;
+
+ private CreateAdvBuilderImpl(CreateBuilderImpl other) {
Review comment:
withLedgerId is options.
The API is following a fluent style and it looks very good to me, we already
discussed it in the BP-15.
try (BookKeeper bk = BookKeeper.newBuilder(config).build();
WriteAdvHandle lh = result(bk
.newCreateLedgerOp()
.withEnsembleSize(3)
.with(xxx)
.makeAdv()
.execute())) {
lh.write(1,data);
}
or
try (BookKeeper bk = BookKeeper.newBuilder(config).build();
WriteHandle lh = result(bk
.newCreateLedgerOp()
.withEnsembleSize(3)
.with(xxx)
.execute())) {
lh.append(data);
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services