add ToByteBuffer binding function

This commit is contained in:
Jonathan Shook 2020-11-16 16:32:21 -06:00
parent 93e10e6a70
commit 55edec5389
2 changed files with 55 additions and 2 deletions

View File

@ -20,6 +20,7 @@ package io.nosqlbench.virtdata.library.basics.shared.conversions.from_long;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.nio.ByteBuffer;
@ -32,10 +33,27 @@ import java.util.function.LongFunction;
@Categories({Category.conversion})
public class ToByteBuffer implements LongFunction<ByteBuffer> {
private final int allocSize;
private final int bufSize;
public ToByteBuffer() {
this.allocSize = Long.BYTES;
this.bufSize = Long.BYTES;
}
@Example({"ToByteBuffer(13)", "Repeat the input long value to make a 13byte buffer"})
public ToByteBuffer(int size) {
this.bufSize = size;
this.allocSize = ((size + Long.BYTES - 1) / Long.BYTES) * Long.BYTES;
}
@Override
public ByteBuffer apply(long input) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(input);
ByteBuffer buffer = ByteBuffer.allocate(allocSize);
while (buffer.remaining() >= Long.BYTES) {
buffer.putLong(input);
}
buffer.position(this.bufSize);
buffer.flip();
return buffer;
}

View File

@ -0,0 +1,35 @@
package io.nosqlbench.virtdata.library.basics.shared.conversions.from_long;
import org.junit.Test;
import java.nio.ByteBuffer;
import static org.assertj.core.api.Assertions.assertThat;
public class ToByteBufferTest {
@Test
public void toByteBuffer7() {
ToByteBuffer f = new ToByteBuffer(7);
ByteBuffer byteBuffer = f.apply(33);
ByteBuffer expected = ByteBuffer.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0});
assertThat(byteBuffer).isEqualByComparingTo(expected);
}
@Test
public void toByteBuffer8() {
ToByteBuffer f = new ToByteBuffer(8);
ByteBuffer byteBuffer = f.apply(33);
ByteBuffer expected = ByteBuffer.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 33});
assertThat(byteBuffer).isEqualByComparingTo(expected);
}
@Test
public void toByteBuffer9() {
ToByteBuffer f = new ToByteBuffer(9);
ByteBuffer byteBuffer = f.apply(33);
ByteBuffer expected = ByteBuffer.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 33, 0});
assertThat(byteBuffer).isEqualByComparingTo(expected);
}
}