### Question:
BatchAppendLogEntries implements RaftLogCommand { @Override public void applyTo( RaftLog raftLog, Log log ) throws IOException { long lastIndex = baseIndex + offset; if ( lastIndex <= raftLog.appendIndex() ) { throw new IllegalStateException( "Attempted to append over an existing entry starting at index " + lastIndex ); } raftLog.append( Arrays.copyOfRange( entries, offset, entries.length ) ); } BatchAppendLogEntries( long baseIndex, int offset, RaftLogEntry[] entries ); @Override void dispatch( Handler handler ); @Override void applyTo( RaftLog raftLog, Log log ); @Override void applyTo( InFlightCache inFlightCache, Log log ); @Override boolean equals( Object o ); @Override int hashCode(); @Override String toString(); final long baseIndex; final int offset; final RaftLogEntry[] entries; }### Answer:
@Test public void shouldApplyMultipleEntries() throws Exception { InMemoryRaftLog raftLog = new InMemoryRaftLog(); BatchAppendLogEntries batchAppendLogEntries = new BatchAppendLogEntries( 0, 0, new RaftLogEntry[]{entryA, entryB, entryC} ); batchAppendLogEntries.applyTo( raftLog, log ); assertEquals( entryA, readLogEntry( raftLog, 0 ) ); assertEquals( entryB, readLogEntry( raftLog, 1 ) ); assertEquals( entryC, readLogEntry( raftLog, 2 ) ); assertEquals( 2, raftLog.appendIndex() ); }
@Test public void shouldApplyFromOffsetOnly() throws Exception { InMemoryRaftLog raftLog = new InMemoryRaftLog(); BatchAppendLogEntries batchAppendLogEntries = new BatchAppendLogEntries( 0, 2, new RaftLogEntry[]{entryA, entryB, entryC, entryD} ); batchAppendLogEntries.applyTo( raftLog, log ); assertEquals( entryC, readLogEntry( raftLog, 0 ) ); assertEquals( entryD, readLogEntry( raftLog, 1 ) ); assertEquals( 1, raftLog.appendIndex() ); }
@Test public void applyTo() { long baseIndex = 0; int offset = 1; RaftLogEntry[] entries = new RaftLogEntry[]{new RaftLogEntry( 0L, valueOf( 0 ) ), new RaftLogEntry( 1L, valueOf( 1 ) ), new RaftLogEntry( 2L, valueOf( 2 ) ),}; BatchAppendLogEntries batchAppend = new BatchAppendLogEntries( baseIndex, offset, entries ); InFlightCache cache = new ConsecutiveInFlightCache(); batchAppend.applyTo( cache, log ); assertNull( cache.get( 0L ) ); assertNotNull( cache.get( 1L ) ); assertNotNull( cache.get( 2L ) ); }