Skip to content

Commit

Permalink
(feat) Adds sliceData and getReadOnlyData methods to LogEntry, #755 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 authored Feb 15, 2022
1 parent 65a34ad commit 75dad4b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,35 @@ public void setOldPeers(final List<PeerId> oldPeers) {
this.oldPeers = oldPeers;
}

/**
* Returns the log data, it's not read-only, you SHOULD take care it's modification and
* thread-safety by yourself.
*
* @return the log data
*/
public ByteBuffer getData() {
return this.data;
}

/**
* Creates a new byte buffer whose content is a shared subsequence of this log entry's data
* buffer's content.
*
* @return The new byte buffer
*/
public ByteBuffer sliceData() {
return this.data != null ? this.data.slice() : null;
}

/**
* Creates a new, read-only byte buffer that shares this log entry's data buffer's content.
*
* @return the new, read-only byte buffer
*/
public ByteBuffer getReadOnlyData() {
return this.data != null ? this.data.asReadOnlyBuffer() : null;
}

public void setData(final ByteBuffer data) {
this.data = data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.jraft.entity;

import java.nio.ByteBuffer;
import java.nio.ReadOnlyBufferException;
import java.util.Arrays;

import org.junit.Assert;
Expand All @@ -28,9 +29,11 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class LogEntryTest {

Expand Down Expand Up @@ -124,4 +127,37 @@ public void testChecksum() {
assertNotEquals(c, entry.checksum());
assertTrue(entry.isCorrupted());
}

@Test
public void testSliceReadOnlyData() {
ByteBuffer buf = ByteBuffer.wrap("hello".getBytes());
LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
entry.setData(buf);
assertSame(buf, entry.getData());
final ByteBuffer slice = entry.sliceData();
assertNotSame(buf, slice);
assertEquals(5, slice.remaining());
assertEquals("hello", new String(slice.array()));
slice.position(4);
assertEquals(4, slice.position());
assertEquals(0, entry.getData().position());
slice.put((byte) 'a');
assertEquals(97, slice.get(4));
assertEquals("hella", new String(entry.getData().array()));

final ByteBuffer readOnly = entry.getReadOnlyData();
assertNotSame(buf, readOnly);
assertEquals(5, readOnly.remaining());
byte[] bs = new byte[5];
readOnly.get(bs);
assertEquals("hella", new String(bs));

try {
readOnly.position(4);
readOnly.put((byte) 1);
fail();
} catch (ReadOnlyBufferException e) {

}
}
}

0 comments on commit 75dad4b

Please sign in to comment.