// Path: c5-replicator-api/src/main/java/c5db/interfaces/log/SequentialEntry.java
// public class SequentialEntry {
//   protected final long seqNum;
// 
//   public SequentialEntry(long seqNum) {
//     this.seqNum = seqNum;
//   }
// 
//   public long getSeqNum() {
//     return seqNum;
//   }
// }
// 
// Path: c5-replicator-api/src/main/java/c5db/interfaces/log/SequentialEntryIterable.java
// interface SequentialEntryIterator<E extends SequentialEntry> extends Closeable {
//   boolean hasNext() throws IOException;
// 
//   E next() throws IOException;
// }
// 
// Path: c5-replicator-log/src/main/java/c5db/log/LogPersistenceService.java
// interface PersistenceNavigator {
//   /**
//    * Updates the navigator with information about an entry to be written, so that it can
//    * optionally incorporate that information into an internal index.
//    *
//    * @param seqNum      Sequence number of an entry being written.
//    * @param byteAddress Byte address of the start of the entry within the persistence.
//    * @throws IOException
//    */
//   void notifyLogging(long seqNum, long byteAddress) throws IOException;
// 
//   /**
//    * Updates the navigator with information about the location of an entry within the
//    * persistence, guaranteeing it will be added to the internal index.
//    *
//    * @param seqNum      Sequence number the entry.
//    * @param byteAddress Byte address of the start of the entry within the persistence.
//    * @throws IOException
//    */
//   void addToIndex(long seqNum, long byteAddress) throws IOException;
// 
//   /**
//    * Updates the navigator that a truncation is being performed on the log, which may
//    * necessitate updating the navigator's internal index.
//    *
//    * @param seqNum Sequence number of the start of the truncation; i.e., entries greater than
//    *               or equal to this sequence number are being deleted.
//    * @throws IOException
//    */
//   void notifyTruncation(long seqNum) throws IOException;
// 
//   /**
//    * Find the position or address of an entry within the persistence, using its sequence number.
//    *
//    * @param seqNum Sequence number of entry to find.
//    * @return Byte position or address.
//    * @throws IOException, LogEntryNotFound
//    */
//   long getAddressOfEntry(long seqNum) throws IOException, LogEntryNotFound;
// 
//   /**
//    * Return an input stream ready to read from the persistence starting at the beginning of the
//    * entry with the specified sequence number.
//    *
//    * @param fromSeqNum Sequence number to read from.
//    * @return A new input stream; the caller takes responsibility for closing it.
//    * @throws IOException, LogEntryNotFound
//    */
//   InputStream getStreamAtSeqNum(long fromSeqNum) throws IOException, LogEntryNotFound;
// 
//   /**
//    * Return an input stream ready to read from the persistence starting at the beginning of the
//    * first entry in the persistence. If there are no entries, the stream will be positioned at
//    * the end of the persistence.
//    *
//    * @return A new input stream; the caller takes responsibility for closing it.
//    * @throws IOException
//    */
//   InputStream getStreamAtFirstEntry() throws IOException;
// 
//   /**
//    * Return an input stream ready to read from the persistence starting at the beginning of the
//    * last entry in the persistence. Throws an unchecked exception if the log is empty.
//    *
//    * @return A new input stream; the caller takes responsibility for closing it.
//    * @throws IOException
//    */
//   InputStream getStreamAtLastEntry() throws IOException;
// }


import c5db.interfaces.log.SequentialEntry;
import c5db.interfaces.log.SequentialEntryCodec;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import static c5db.interfaces.log.SequentialEntryIterable.SequentialEntryIterator;
import static c5db.log.LogPersistenceService.PersistenceNavigator;
/*
 * Copyright 2014 WANdisco
 *
 *  WANdisco 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 c5db.log;




/**
 * Implementation of SequentialEntryIterator for logs encoded with a SequentialEntryCodec.
 */
class EncodedSequentialEntryIterator<E extends SequentialEntry> implements SequentialEntryIterator<E> {
  private final SequentialEntryCodec<E> codec;
  private final InputStream inputStream;
  private E nextEntry;

  EncodedSequentialEntryIterator(PersistenceNavigator persistenceNavigator, SequentialEntryCodec<E> codec)
      throws IOException {

    this.codec = codec;
    this.inputStream = persistenceNavigator.getStreamAtFirstEntry();
    this.nextEntry = fetchNext();
  }

  @Override
  public void close() throws IOException {
// Path: c5-replicator-api/src/main/java/c5db/interfaces/log/SequentialEntry.java
// public class SequentialEntry {
//   protected final long seqNum;
// 
//   public SequentialEntry(long seqNum) {
//     this.seqNum = seqNum;
//   }
// 
//   public long getSeqNum() {
//     return seqNum;
//   }
// }
// 
// Path: c5-replicator-api/src/main/java/c5db/interfaces/log/SequentialEntryIterable.java
// interface SequentialEntryIterator<E extends SequentialEntry> extends Closeable {
//   boolean hasNext() throws IOException;
// 
//   E next() throws IOException;
// }
// 
// Path: c5-replicator-log/src/main/java/c5db/log/LogPersistenceService.java
// interface PersistenceNavigator {
//   /**
//    * Updates the navigator with information about an entry to be written, so that it can
//    * optionally incorporate that information into an internal index.
//    *
//    * @param seqNum      Sequence number of an entry being written.
//    * @param byteAddress Byte address of the start of the entry within the persistence.
//    * @throws IOException
//    */
//   void notifyLogging(long seqNum, long byteAddress) throws IOException;
// 
//   /**
//    * Updates the navigator with information about the location of an entry within the
//    * persistence, guaranteeing it will be added to the internal index.
//    *
//    * @param seqNum      Sequence number the entry.
//    * @param byteAddress Byte address of the start of the entry within the persistence.
//    * @throws IOException
//    */
//   void addToIndex(long seqNum, long byteAddress) throws IOException;
// 
//   /**
//    * Updates the navigator that a truncation is being performed on the log, which may
//    * necessitate updating the navigator's internal index.
//    *
//    * @param seqNum Sequence number of the start of the truncation; i.e., entries greater than
//    *               or equal to this sequence number are being deleted.
//    * @throws IOException
//    */
//   void notifyTruncation(long seqNum) throws IOException;
// 
//   /**
//    * Find the position or address of an entry within the persistence, using its sequence number.
//    *
//    * @param seqNum Sequence number of entry to find.
//    * @return Byte position or address.
//    * @throws IOException, LogEntryNotFound
//    */
//   long getAddressOfEntry(long seqNum) throws IOException, LogEntryNotFound;
// 
//   /**
//    * Return an input stream ready to read from the persistence starting at the beginning of the
//    * entry with the specified sequence number.
//    *
//    * @param fromSeqNum Sequence number to read from.
//    * @return A new input stream; the caller takes responsibility for closing it.
//    * @throws IOException, LogEntryNotFound
//    */
//   InputStream getStreamAtSeqNum(long fromSeqNum) throws IOException, LogEntryNotFound;
// 
//   /**
//    * Return an input stream ready to read from the persistence starting at the beginning of the
//    * first entry in the persistence. If there are no entries, the stream will be positioned at
//    * the end of the persistence.
//    *
//    * @return A new input stream; the caller takes responsibility for closing it.
//    * @throws IOException
//    */
//   InputStream getStreamAtFirstEntry() throws IOException;
// 
//   /**
//    * Return an input stream ready to read from the persistence starting at the beginning of the
//    * last entry in the persistence. Throws an unchecked exception if the log is empty.
//    *
//    * @return A new input stream; the caller takes responsibility for closing it.
//    * @throws IOException
//    */
//   InputStream getStreamAtLastEntry() throws IOException;
// }


// Path: c5-replicator-log/src/main/java/c5db/log/EncodedSequentialEntryIterator.java
import c5db.interfaces.log.SequentialEntry;
import c5db.interfaces.log.SequentialEntryCodec;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import static c5db.interfaces.log.SequentialEntryIterable.SequentialEntryIterator;
import static c5db.log.LogPersistenceService.PersistenceNavigator;

/*
 * Copyright 2014 WANdisco
 *
 *  WANdisco 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 c5db.log;




/**
 * Implementation of SequentialEntryIterator for logs encoded with a SequentialEntryCodec.
 */
class EncodedSequentialEntryIterator<E extends SequentialEntry> implements SequentialEntryIterator<E> {
  private final SequentialEntryCodec<E> codec;
  private final InputStream inputStream;
  private E nextEntry;

  EncodedSequentialEntryIterator(PersistenceNavigator persistenceNavigator, SequentialEntryCodec<E> codec)
      throws IOException {

    this.codec = codec;
    this.inputStream = persistenceNavigator.getStreamAtFirstEntry();
    this.nextEntry = fetchNext();
  }

  @Override
  public void close() throws IOException {
    inputStream.close();