|
35 | 35 | public class UnsafeInMemorySorterSuite { |
36 | 36 |
|
37 | 37 | private static String getStringFromDataPage(Object baseObject, long baseOffset) { |
38 | | - final int strLength = (int) PlatformDependent.UNSAFE.getLong(baseObject, baseOffset); |
| 38 | + final int strLength = PlatformDependent.UNSAFE.getInt(baseObject, baseOffset); |
39 | 39 | final byte[] strBytes = new byte[strLength]; |
40 | 40 | PlatformDependent.copyMemory( |
41 | 41 | baseObject, |
42 | | - baseOffset + 8, |
| 42 | + baseOffset + 4, |
43 | 43 | strBytes, |
44 | 44 | PlatformDependent.BYTE_ARRAY_OFFSET, strLength); |
45 | 45 | return new String(strBytes); |
@@ -77,8 +77,8 @@ public void testSortingOnlyByIntegerPrefix() throws Exception { |
77 | 77 | long position = dataPage.getBaseOffset(); |
78 | 78 | for (String str : dataToSort) { |
79 | 79 | final byte[] strBytes = str.getBytes("utf-8"); |
80 | | - PlatformDependent.UNSAFE.putLong(baseObject, position, strBytes.length); |
81 | | - position += 8; |
| 80 | + PlatformDependent.UNSAFE.putInt(baseObject, position, strBytes.length); |
| 81 | + position += 4; |
82 | 82 | PlatformDependent.copyMemory( |
83 | 83 | strBytes, |
84 | 84 | PlatformDependent.BYTE_ARRAY_OFFSET, |
@@ -114,22 +114,24 @@ public int compare(long prefix1, long prefix2) { |
114 | 114 | position = dataPage.getBaseOffset(); |
115 | 115 | for (int i = 0; i < dataToSort.length; i++) { |
116 | 116 | // position now points to the start of a record (which holds its length). |
117 | | - final long recordLength = PlatformDependent.UNSAFE.getLong(baseObject, position); |
| 117 | + final int recordLength = PlatformDependent.UNSAFE.getInt(baseObject, position); |
118 | 118 | final long address = memoryManager.encodePageNumberAndOffset(dataPage, position); |
119 | 119 | final String str = getStringFromDataPage(baseObject, position); |
120 | 120 | final int partitionId = hashPartitioner.getPartition(str); |
121 | 121 | sorter.insertRecord(address, partitionId); |
122 | | - position += 8 + recordLength; |
| 122 | + position += 4 + recordLength; |
123 | 123 | } |
124 | 124 | final UnsafeSorterIterator iter = sorter.getSortedIterator(); |
125 | 125 | int iterLength = 0; |
126 | 126 | long prevPrefix = -1; |
127 | 127 | Arrays.sort(dataToSort); |
128 | 128 | while (iter.hasNext()) { |
129 | 129 | iter.loadNext(); |
130 | | - final String str = getStringFromDataPage(iter.getBaseObject(), iter.getBaseOffset()); |
| 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); |
131 | 133 | final long keyPrefix = iter.getKeyPrefix(); |
132 | | - assertTrue(Arrays.asList(dataToSort).contains(str)); |
| 134 | + assertThat(str, isIn(Arrays.asList(dataToSort))); |
133 | 135 | assertThat(keyPrefix, greaterThanOrEqualTo(prevPrefix)); |
134 | 136 | prevPrefix = keyPrefix; |
135 | 137 | iterLength++; |
|
0 commit comments