Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Adds sliceData and getReadOnlyData methods to LogEntry, #755 #762

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

}
}
}