Java Code Examples for org.apache.bookkeeper.client.LedgerHandle#asyncReadEntries()

The following examples show how to use org.apache.bookkeeper.client.LedgerHandle#asyncReadEntries() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompactedTopicImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private static CompletableFuture<MessageIdData> readOneMessageId(LedgerHandle lh, long entryId) {
    CompletableFuture<MessageIdData> promise = new CompletableFuture<>();

    lh.asyncReadEntries(entryId, entryId,
                        (rc, _lh, seq, ctx) -> {
                            if (rc != BKException.Code.OK) {
                                promise.completeExceptionally(BKException.create(rc));
                            } else {
                                // Need to release buffers for all entries in the sequence
                                if (seq.hasMoreElements()) {
                                    LedgerEntry entry = seq.nextElement();
                                    try (RawMessage m = RawMessageImpl.deserializeFrom(entry.getEntryBuffer())) {
                                        entry.getEntryBuffer().release();
                                        while (seq.hasMoreElements()) {
                                            seq.nextElement().getEntryBuffer().release();
                                        }
                                        promise.complete(m.getMessageIdData());
                                    }
                                } else {
                                    promise.completeExceptionally(new NoSuchElementException(
                                            String.format("No such entry %d in ledger %d", entryId, lh.getId())));
                                }
                            }
                        }, null);
    return promise;
}
 
Example 2
Source File: CompactedTopicImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static CompletableFuture<List<Entry>> readEntries(LedgerHandle lh, long from, long to) {
    CompletableFuture<Enumeration<LedgerEntry>> promise = new CompletableFuture<>();

    lh.asyncReadEntries(from, to,
                        (rc, _lh, seq, ctx) -> {
                            if (rc != BKException.Code.OK) {
                                promise.completeExceptionally(BKException.create(rc));
                            } else {
                                promise.complete(seq);
                            }
                        }, null);
    return promise.thenApply(
            (seq) -> {
                List<Entry> entries = new ArrayList<Entry>();
                while (seq.hasMoreElements()) {
                    ByteBuf buf = seq.nextElement().getEntryBuffer();
                    try (RawMessage m = RawMessageImpl.deserializeFrom(buf)) {
                        entries.add(EntryImpl.create(m.getMessageIdData().getLedgerId(),
                                                     m.getMessageIdData().getEntryId(),
                                                     m.getHeadersAndPayload()));
                    } finally {
                        buf.release();
                    }
                }
                return entries;
            });
}
 
Example 3
Source File: BookkeeperSchemaStorage.java    From pulsar with Apache License 2.0 5 votes vote down vote up
static CompletableFuture<LedgerEntry> getLedgerEntry(LedgerHandle ledger, long entry) {
    final CompletableFuture<LedgerEntry> future = new CompletableFuture<>();
    ledger.asyncReadEntries(entry, entry,
        (rc, handle, entries, ctx) -> {
            if (rc != BKException.Code.OK) {
                future.completeExceptionally(bkException("Failed to read entry", rc, ledger.getId(), entry));
            } else {
                future.complete(entries.nextElement());
            }
        }, null
    );
    return future;
}