Search in sources :

Example 1 with TestMemoryConsumer

use of io.mycat.memory.unsafe.memory.TestMemoryConsumer in project Mycat-Server by MyCATApache.

the class UnsafeInMemorySorterSuite method testSortingOnlyByIntegerPrefix.

@Test
public void testSortingOnlyByIntegerPrefix() throws Exception {
    final String[] dataToSort = new String[] { "Boba", "Pearls", "Tapioca", "Taho", "Condensed Milk", "Jasmine", "Milk Tea", "Lychee", "Mango" };
    final DataNodeMemoryManager memoryManager = new DataNodeMemoryManager(new TestMemoryManager(new MycatPropertyConf().set("mycat.memory.offHeap.enabled", "false")), 0);
    final TestMemoryConsumer consumer = new TestMemoryConsumer(memoryManager);
    final MemoryBlock dataPage = memoryManager.allocatePage(2048, null);
    final Object baseObject = dataPage.getBaseObject();
    // Write the records into the data page:
    long position = dataPage.getBaseOffset();
    for (String str : dataToSort) {
        final byte[] strBytes = str.getBytes(StandardCharsets.UTF_8);
        Platform.putInt(baseObject, position, strBytes.length);
        position += 4;
        Platform.copyMemory(strBytes, Platform.BYTE_ARRAY_OFFSET, baseObject, position, strBytes.length);
        position += strBytes.length;
    }
    // Since the key fits within the 8-byte prefix, we don't need to do any record comparison, so
    // use a dummy comparator
    final RecordComparator recordComparator = new RecordComparator() {

        @Override
        public int compare(Object leftBaseObject, long leftBaseOffset, Object rightBaseObject, long rightBaseOffset) {
            return 0;
        }
    };
    // Compute key prefixes based on the records' partition ids
    final HashPartitioner hashPartitioner = new HashPartitioner(4);
    // Use integer comparison for comparing prefixes (which are partition ids, in this case)
    final PrefixComparator prefixComparator = PrefixComparators.LONG;
    UnsafeInMemorySorter sorter = new UnsafeInMemorySorter(consumer, memoryManager, recordComparator, prefixComparator, dataToSort.length, shouldUseRadixSort(), true);
    // Given a page of records, insert those records into the sorter one-by-one:
    position = dataPage.getBaseOffset();
    System.out.println("(0)address = " + position);
    for (int i = 0; i < dataToSort.length; i++) {
        if (!sorter.hasSpaceForAnotherRecord()) {
            sorter.expandPointerArray(consumer.allocateLongArray(sorter.getMemoryUsage() / 8 * 2));
        }
        // position now points to the start of a record (which holds its length).
        final int recordLength = Platform.getInt(baseObject, position);
        final long address = memoryManager.encodePageNumberAndOffset(dataPage, position);
        final String str = getStringFromDataPage(baseObject, position + 4, recordLength);
        final int partitionId = hashPartitioner.getPartition(str);
        System.out.println("(" + partitionId + "," + str + ")");
        sorter.insertRecord(address, partitionId);
        position += 4 + recordLength;
    }
    final UnsafeSorterIterator iter = sorter.getSortedIterator();
    int iterLength = 0;
    long prevPrefix = -1;
    Arrays.sort(dataToSort);
    while (iter.hasNext()) {
        iter.loadNext();
        final String str = getStringFromDataPage(iter.getBaseObject(), iter.getBaseOffset(), iter.getRecordLength());
        final long keyPrefix = iter.getKeyPrefix();
        assertThat(str, isIn(Arrays.asList(dataToSort)));
        assertThat(keyPrefix, greaterThanOrEqualTo(prevPrefix));
        prevPrefix = keyPrefix;
        iterLength++;
    }
    assertEquals(dataToSort.length, iterLength);
}
Also used : TestMemoryManager(io.mycat.memory.unsafe.memory.TestMemoryManager) DataNodeMemoryManager(io.mycat.memory.unsafe.memory.mm.DataNodeMemoryManager) MycatPropertyConf(io.mycat.memory.unsafe.utils.MycatPropertyConf) TestMemoryConsumer(io.mycat.memory.unsafe.memory.TestMemoryConsumer) MemoryBlock(io.mycat.memory.unsafe.memory.MemoryBlock) Test(org.junit.Test)

Example 2 with TestMemoryConsumer

use of io.mycat.memory.unsafe.memory.TestMemoryConsumer in project Mycat-Server by MyCATApache.

the class UnsafeInMemorySorterSuite method testSortingEmptyInput.

@Test
public void testSortingEmptyInput() {
    final DataNodeMemoryManager memoryManager = new DataNodeMemoryManager(new TestMemoryManager(new MycatPropertyConf().set("mycat.memory.offHeap.enabled", "false")), 0);
    final TestMemoryConsumer consumer = new TestMemoryConsumer(memoryManager);
    final UnsafeInMemorySorter sorter = new UnsafeInMemorySorter(consumer, memoryManager, mock(RecordComparator.class), mock(PrefixComparator.class), 100, shouldUseRadixSort(), true);
    final UnsafeSorterIterator iter = sorter.getSortedIterator();
    Assert.assertFalse(iter.hasNext());
}
Also used : TestMemoryManager(io.mycat.memory.unsafe.memory.TestMemoryManager) TestMemoryConsumer(io.mycat.memory.unsafe.memory.TestMemoryConsumer) DataNodeMemoryManager(io.mycat.memory.unsafe.memory.mm.DataNodeMemoryManager) MycatPropertyConf(io.mycat.memory.unsafe.utils.MycatPropertyConf) Test(org.junit.Test)

Aggregations

TestMemoryConsumer (io.mycat.memory.unsafe.memory.TestMemoryConsumer)2 TestMemoryManager (io.mycat.memory.unsafe.memory.TestMemoryManager)2 DataNodeMemoryManager (io.mycat.memory.unsafe.memory.mm.DataNodeMemoryManager)2 MycatPropertyConf (io.mycat.memory.unsafe.utils.MycatPropertyConf)2 Test (org.junit.Test)2 MemoryBlock (io.mycat.memory.unsafe.memory.MemoryBlock)1