|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.logging; |
| 18 | + |
| 19 | +import com.google.api.gax.rpc.BidiStream; |
| 20 | +import com.google.common.collect.Lists; |
| 21 | +import com.google.logging.v2.TailLogEntriesRequest; |
| 22 | +import com.google.logging.v2.TailLogEntriesResponse; |
| 23 | +import java.util.Iterator; |
| 24 | + |
| 25 | +public class LogEntryServerStream implements Iterable<LogEntry> { |
| 26 | + private final BidiStream<TailLogEntriesRequest, TailLogEntriesResponse> serverStream; |
| 27 | + |
| 28 | + LogEntryServerStream(BidiStream<TailLogEntriesRequest, TailLogEntriesResponse> serverStream) { |
| 29 | + this.serverStream = serverStream; |
| 30 | + } |
| 31 | + |
| 32 | + /** {@inheritDoc} */ |
| 33 | + @Override |
| 34 | + public Iterator<LogEntry> iterator() { |
| 35 | + return new LogEntryIterator(serverStream.iterator()); |
| 36 | + } |
| 37 | + |
| 38 | + public BidiStream<TailLogEntriesRequest, TailLogEntriesResponse> getInternalStream() { |
| 39 | + return serverStream; |
| 40 | + } |
| 41 | + |
| 42 | + public java.util.List<LogEntry> convert(TailLogEntriesResponse resp) { |
| 43 | + return Lists.transform(resp.getEntriesList(), LogEntry.FROM_PB_FUNCTION); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns true if the next call to the iterator's hasNext() or next() is guaranteed to be |
| 48 | + * nonblocking. |
| 49 | + * |
| 50 | + * @return If the call on any of the iterator's methods is guaranteed to be nonblocking. |
| 51 | + */ |
| 52 | + public boolean isReceiveReady() { |
| 53 | + return serverStream.isReceiveReady(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Cleanly cancels a partially consumed stream. The associated iterator will return false for the |
| 58 | + * hasNext() in the next iteration. This maintains the contract that an observed true from |
| 59 | + * hasNext() will yield an item in next(), but afterwards will return false. |
| 60 | + */ |
| 61 | + public void cancel() { |
| 62 | + serverStream.cancel(); |
| 63 | + } |
| 64 | +} |
0 commit comments