sijie commented on a change in pull request #1298: newOpenLedgerOp and 
ReadHandle implementation for MockBookKeeper
URL: https://github.com/apache/bookkeeper/pull/1298#discussion_r177516314
 
 

 ##########
 File path: 
bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockReadHandle.java
 ##########
 @@ -0,0 +1,141 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.client;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.client.api.LastConfirmedAndEntry;
+import org.apache.bookkeeper.client.api.LedgerEntries;
+import org.apache.bookkeeper.client.api.LedgerEntry;
+import org.apache.bookkeeper.client.api.LedgerMetadata;
+import org.apache.bookkeeper.client.api.ReadHandle;
+import org.apache.bookkeeper.client.impl.LedgerEntriesImpl;
+import org.apache.bookkeeper.client.impl.LedgerEntryImpl;
+
+
+/**
+ * Mock implementation of ReadHandle.
+ */
+@Slf4j
+public class MockReadHandle implements ReadHandle {
+    private final MockBookKeeper bk;
+    private final long ledgerId;
+    private final LedgerMetadata metadata;
+    private final List<LedgerEntryImpl> entries;
+
+    MockReadHandle(MockBookKeeper bk, long ledgerId, LedgerMetadata metadata, 
List<LedgerEntryImpl> entries) {
+        this.bk = bk;
+        this.ledgerId = ledgerId;
+        this.metadata = metadata;
+        this.entries = entries;
+    }
+
+    @Override
+    public CompletableFuture<LedgerEntries> readAsync(long firstEntry, long 
lastEntry) {
+        CompletableFuture<LedgerEntries> promise = new CompletableFuture<>();
+        if (bk.isStopped()) {
+            promise.completeExceptionally(BKException.create(-1));
+            return promise;
+        }
+
+        bk.executor.execute(() -> {
+                if (bk.getProgrammedFailStatus()) {
+                    
promise.completeExceptionally(BKException.create(bk.failReturnCode));
+                    return;
+                } else if (bk.isStopped()) {
+                    promise.completeExceptionally(BKException.create(-1));
+                    return;
+                }
+
+                log.debug("readEntries: first={} last={} total={}", 
firstEntry, lastEntry, entries.size());
+                List<LedgerEntry> seq = new ArrayList<>();
+                long entryId = firstEntry;
+                while (entryId <= lastEntry && entryId < entries.size()) {
+                    seq.add(entries.get((int) entryId++).duplicate());
+                }
+                log.debug("Entries read: {}", seq);
+                promise.complete(LedgerEntriesImpl.create(seq));
+            });
+        return promise;
+
+    }
+
+    @Override
+    public CompletableFuture<LedgerEntries> readUnconfirmedAsync(long 
firstEntry, long lastEntry) {
+        return readAsync(firstEntry, lastEntry);
+    }
+
+    @Override
+    public CompletableFuture<Long> readLastAddConfirmedAsync() {
+        return CompletableFuture.completedFuture((long) (entries.size() - 1));
+    }
+
+    @Override
+    public CompletableFuture<Long> tryReadLastAddConfirmedAsync() {
+        return readLastAddConfirmedAsync();
+    }
+
+    @Override
+    public long getLastAddConfirmed() {
+        return entries.size() - 1;
+    }
+
+    @Override
+    public long getLength() {
+        long length = 0;
+        for (LedgerEntryImpl entry : entries) {
+            length += entry.getLength();
+        }
+
+        return length;
+    }
+
+    @Override
+    public boolean isClosed() {
+        return metadata.isClosed();
+    }
+
+    @Override
+    public CompletableFuture<LastConfirmedAndEntry> 
readLastAddConfirmedAndEntryAsync(long entryId,
+                                                                               
       long timeOutInMillis,
+                                                                               
       boolean parallel) {
+        CompletableFuture<LastConfirmedAndEntry> promise = new 
CompletableFuture<>();
+        // it doesn't make sense to implement long poll until WriteHandle is 
also implemented
 
 Review comment:
   the comment is misleading. long poll doesn't require write handle to 
implement. it is irrelative whether you use old write api or new write handle. 
please improve the comment or remove it.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to