org.apache.kafka.raft.internals.MemoryBatchReader Maven / Gradle / Ivy
The newest version!
/*
* 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.kafka.raft.internals;
import org.apache.kafka.raft.Batch;
import org.apache.kafka.raft.BatchReader;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.OptionalLong;
public class MemoryBatchReader implements BatchReader {
private final CloseListener> closeListener;
private final Iterator> iterator;
private final long baseOffset;
private final long lastOffset;
private MemoryBatchReader(
long baseOffset,
long lastOffset,
Iterator> iterator,
CloseListener> closeListener
) {
this.baseOffset = baseOffset;
this.lastOffset = lastOffset;
this.iterator = iterator;
this.closeListener = closeListener;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Batch next() {
return iterator.next();
}
@Override
public long baseOffset() {
return baseOffset;
}
@Override
public OptionalLong lastOffset() {
return OptionalLong.of(lastOffset);
}
@Override
public void close() {
closeListener.onClose(this);
}
public static MemoryBatchReader empty(
long baseOffset,
long lastOffset,
CloseListener> closeListener
) {
return new MemoryBatchReader<>(
baseOffset,
lastOffset,
Collections.emptyIterator(),
closeListener
);
}
public static MemoryBatchReader of(
List> batches,
CloseListener> closeListener
) {
if (batches.isEmpty()) {
throw new IllegalArgumentException("MemoryBatchReader requires at least " +
"one batch to iterate, but an empty list was provided");
}
return new MemoryBatchReader<>(
batches.get(0).baseOffset(),
batches.get(batches.size() - 1).lastOffset(),
batches.iterator(),
closeListener
);
}
}