@Override
    public void reset() {
        // reset all offsets
        this.numRecords = 0;
        this.currentSortIndexOffset = 0;
        this.currentDataBufferOffset = 0;
        this.sortIndexBytes = 0;

        // return all memory
        this.freeMemory.addAll(this.sortIndex);
        this.freeMemory.addAll(this.recordBufferSegments);
        this.sortIndex.clear();
        this.recordBufferSegments.clear();

        // grab first buffers
        this.currentSortIndexSegment = nextMemorySegment();
        this.sortIndex.add(this.currentSortIndexSegment);
        this.recordCollector.reset();
    }
@Test
    void testReset() throws Exception {
        final int numSegments = MEMORY_SIZE / MEMORY_PAGE_SIZE;
        final List<MemorySegment> memory =
                this.memoryManager.allocatePages(new DummyInvokable(), numSegments);

        NormalizedKeySorter<Tuple2<Integer, String>> sorter = newSortBuffer(memory);
        TestData.TupleGenerator generator =
                new TestData.TupleGenerator(
                        SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.FIX_LENGTH);

        // write the buffer full with the first set of records
        Tuple2<Integer, String> record = new Tuple2<>();
        int num = -1;
        do {
            generator.next(record);
            num++;
        } while (sorter.write(record));

        sorter.reset();

        // write a second sequence of records. since the values are of fixed length, we must be able
        // to write an equal number
        generator =
                new TestData.TupleGenerator(
                        SEED2, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.FIX_LENGTH);

        // write the buffer full with the first set of records
        int num2 = -1;
        do {
            generator.next(record);
            num2++;
        } while (sorter.write(record));

        assertThat(num2)
                .withFailMessage(
                        "The number of records written after the reset was not the same as before.")
                .isEqualTo(num);

        // re-read the records
        generator.reset();
        Tuple2<Integer, String> readTarget = new Tuple2<>();

        int i = 0;
        while (i < num) {
            generator.next(record);
            readTarget = sorter.getRecord(readTarget, i++);

            int rk = readTarget.f0;
            int gk = record.f0;

            String rv = readTarget.f1;
            String gv = record.f1;

            assertThat(rk).withFailMessage("The re-read key is wrong").isEqualTo(gk);
            assertThat(rv).withFailMessage("The re-read value is wrong").isEqualTo(gv);
        }

        // release the memory occupied by the buffers
        sorter.dispose();
        this.memoryManager.release(memory);
    }