3434
3535public class UnsafeInMemorySorterSuite {
3636
37- private static String getStringFromDataPage (Object baseObject , long baseOffset ) {
38- final int strLength = PlatformDependent .UNSAFE .getInt (baseObject , baseOffset );
39- final byte [] strBytes = new byte [strLength ];
37+ private static String getStringFromDataPage (Object baseObject , long baseOffset , int length ) {
38+ final byte [] strBytes = new byte [length ];
4039 PlatformDependent .copyMemory (
4140 baseObject ,
42- baseOffset + 4 ,
41+ baseOffset ,
4342 strBytes ,
44- PlatformDependent .BYTE_ARRAY_OFFSET , strLength );
43+ PlatformDependent .BYTE_ARRAY_OFFSET , length );
4544 return new String (strBytes );
4645 }
4746
@@ -116,7 +115,7 @@ public int compare(long prefix1, long prefix2) {
116115 // position now points to the start of a record (which holds its length).
117116 final int recordLength = PlatformDependent .UNSAFE .getInt (baseObject , position );
118117 final long address = memoryManager .encodePageNumberAndOffset (dataPage , position );
119- final String str = getStringFromDataPage (baseObject , position );
118+ final String str = getStringFromDataPage (baseObject , position + 4 , recordLength );
120119 final int partitionId = hashPartitioner .getPartition (str );
121120 sorter .insertRecord (address , partitionId );
122121 position += 4 + recordLength ;
@@ -127,9 +126,8 @@ public int compare(long prefix1, long prefix2) {
127126 Arrays .sort (dataToSort );
128127 while (iter .hasNext ()) {
129128 iter .loadNext ();
130- // TODO: the logic for how we manipulate record length offsets here is confusing; clean
131- // this up and clarify it in comments.
132- final String str = getStringFromDataPage (iter .getBaseObject (), iter .getBaseOffset () - 4 );
129+ final String str =
130+ getStringFromDataPage (iter .getBaseObject (), iter .getBaseOffset (), iter .getRecordLength ());
133131 final long keyPrefix = iter .getKeyPrefix ();
134132 assertThat (str , isIn (Arrays .asList (dataToSort )));
135133 assertThat (keyPrefix , greaterThanOrEqualTo (prevPrefix ));
0 commit comments