Skip to content
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 @@ -50,11 +50,31 @@ public static VSizeIndexedInts empty()
return fromList(Lists.<Integer>newArrayList(), 0);
}

/**
* provide for performance reason.
*/
public static byte[] getBytesNoPaddingfromList(List<Integer> list, int maxValue)
{
int numBytes = getNumBytesForMax(maxValue);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we refactor to reduce copy-paste with fromList?


final ByteBuffer buffer = ByteBuffer.allocate((list.size() * numBytes));
writeToBuffer(buffer, list, numBytes, maxValue);

return buffer.array();
}

public static VSizeIndexedInts fromList(List<Integer> list, int maxValue)
{
int numBytes = getNumBytesForMax(maxValue);

final ByteBuffer buffer = ByteBuffer.allocate((list.size() * numBytes) + (4 - numBytes));
writeToBuffer(buffer, list, numBytes, maxValue);

return new VSizeIndexedInts(buffer.asReadOnlyBuffer(), numBytes);
}

private static void writeToBuffer(ByteBuffer buffer, List<Integer> list, int numBytes, int maxValue)
{
int i = 0;
for (Integer val : list) {
if (val < 0) {
Expand All @@ -69,8 +89,6 @@ public static VSizeIndexedInts fromList(List<Integer> list, int maxValue)
++i;
}
buffer.position(0);

return new VSizeIndexedInts(buffer.asReadOnlyBuffer(), numBytes);
}

public static byte getNumBytesForMax(int maxValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void open() throws IOException

public void write(List<Integer> ints) throws IOException
{
byte[] bytesToWrite = ints == null ? EMPTY_ARRAY : VSizeIndexedInts.fromList(ints, maxId).getBytesNoPadding();
byte[] bytesToWrite = ints == null ? EMPTY_ARRAY : VSizeIndexedInts.getBytesNoPaddingfromList(ints, maxId);

valuesOut.write(bytesToWrite);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import org.junit.Assert;
import org.junit.Test;

import com.google.common.primitives.Ints;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.util.List;

/**
*/
Expand Down Expand Up @@ -63,4 +66,15 @@ public void testSerialization() throws Exception
}
}

@Test
public void testGetBytesNoPaddingfromList() throws Exception
{
final int[] array = {1, 2, 4, 5, 6, 8, 9, 10};
List<Integer> list = Ints.asList(array);
int maxValue = Ints.max(array);
VSizeIndexedInts ints = VSizeIndexedInts.fromList(list, maxValue);
byte[] bytes1 = ints.getBytesNoPadding();
byte[] bytes2 = VSizeIndexedInts.getBytesNoPaddingfromList(list, maxValue);
Assert.assertArrayEquals(bytes1, bytes2);
}
}