FixedLengthRecordSorter implements InMemorySorter<T> { @Override public void swap(int i, int j) { final int bufferNumI = i / this.recordsPerSegment; final int segmentOffsetI = (i % this.recordsPerSegment) * this.recordSize; final int bufferNumJ = j / this.recordsPerSegment; final int segmentOffsetJ = (j % this.recordsPerSegment) * this.recordSize; final MemorySegment segI = this.sortBuffer.get(bufferNumI); final MemorySegment segJ = this.sortBuffer.get(bufferNumJ); segI.swapBytes(this.swapBuffer, segJ, segmentOffsetI, segmentOffsetJ, this.recordSize); } FixedLengthRecordSorter(TypeSerializer<T> serializer, TypeComparator<T> comparator, 
			List<MemorySegment> memory); @Override void reset(); @Override boolean isEmpty(); @Override void dispose(); @Override long getCapacity(); @Override long getOccupancy(); @Override T getRecord(int logicalPosition); @Override T getRecord(T reuse, int logicalPosition); @Override boolean write(T record); @Override int compare(int i, int j); @Override void swap(int i, int j); @Override int size(); @Override final MutableObjectIterator<T> getIterator(); @Override void writeToOutput(final ChannelWriterOutputView output); @Override void writeToOutput(ChannelWriterOutputView output, LargeRecordHandler<T> largeRecordsOutput); @Override void writeToOutput(final ChannelWriterOutputView output, final int start, int num);  }
@Test public void testSwap() throws Exception { final int numSegments = MEMORY_SIZE / MEMORY_PAGE_SIZE; final List<MemorySegment> memory = this.memoryManager.allocatePages(new DummyInvokable(), numSegments); FixedLengthRecordSorter<IntPair> sorter = newSortBuffer(memory); RandomIntPairGenerator generator = new RandomIntPairGenerator(SEED); IntPair record = new IntPair(); int num = -1; do { generator.next(record); num++; } while (sorter.write(record) && num < 3354624); int start = 0, end = num - 1; while (start < end) { sorter.swap(start++, end--); } generator.reset(); IntPair readTarget = new IntPair(); int i = num - 1; while (i >= 0) { generator.next(record); readTarget = sorter.getRecord(readTarget, i--); int rk = readTarget.getKey(); int gk = record.getKey(); int rv = readTarget.getValue(); int gv = record.getValue(); Assert.assertEquals("The re-read key is wrong", gk, rk); Assert.assertEquals("The re-read value is wrong", gv, rv); } sorter.dispose(); this.memoryManager.release(memory); }