Skip to content

Commit

Permalink
Properly handle reads in FileLog.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Jan 10, 2015
1 parent 7261dfc commit 5be5f99
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions core/src/main/java/net/kuujo/copycat/log/FileLogSegment.java
Expand Up @@ -155,7 +155,8 @@ private void storePosition(long index, long position) {
try {
indexBuffer.clear();
indexBuffer.putLong(index).putLong(position);
indexFileChannel.write(indexBuffer, (index - firstIndex) * 8);
indexBuffer.flip();
indexFileChannel.write(indexBuffer, (index - firstIndex) * 16);
} catch (IOException e) {
throw new LogException(e);
}
Expand All @@ -166,9 +167,13 @@ private void storePosition(long index, long position) {
*/
private long findPosition(long index) {
try {
indexFileChannel.read(indexBuffer, (index - firstIndex) * 8);
indexBuffer.position(4);
return indexBuffer.getLong();
indexBuffer.rewind();
if (indexFileChannel.read(indexBuffer, (index - firstIndex) * 16) == 16) {
indexBuffer.flip();
indexBuffer.position(8);
return indexBuffer.getLong();
}
return logFileChannel.size();
} catch (IOException e) {
throw new LogException(e);
}
Expand Down Expand Up @@ -196,9 +201,12 @@ public boolean containsIndex(long index) {
public ByteBuffer getEntry(long index) {
assertIsOpen();
try {
logFileChannel.read(entryBuffer, findPosition(index));
long startPosition = findPosition(index);
long endPosition = findPosition(index + 1);
logFileChannel.read(entryBuffer, startPosition);
entryBuffer.position((int) (endPosition - startPosition));
entryBuffer.flip();
ByteBuffer buffer = ByteBuffer.allocate(entryBuffer.capacity());
ByteBuffer buffer = ByteBuffer.allocate(entryBuffer.limit());
buffer.put(entryBuffer);
entryBuffer.clear();
buffer.flip();
Expand Down

0 comments on commit 5be5f99

Please sign in to comment.