Search in sources :

Example 6 with StructType

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

the class UnsafeFixedWidthAggregationMapSuite method testAggregateMap.

@Test
public void testAggregateMap() throws NoSuchFieldException, IllegalAccessException, IOException {
    /**
     * 创造上文环境
     */
    MyCatMemory myCatMemory = new MyCatMemory();
    MemoryManager memoryManager = myCatMemory.getResultMergeMemoryManager();
    DataNodeMemoryManager dataNodeMemoryManager = new DataNodeMemoryManager(memoryManager, Thread.currentThread().getId());
    /**
       * 构造数据字段group key
       */
    int fieldCount = 2;
    ColMeta colMeta = null;
    Map<String, ColMeta> colMetaMap = new HashMap<String, ColMeta>(fieldCount);
    colMeta = new ColMeta(0, ColMeta.COL_TYPE_STRING);
    colMetaMap.put("id", colMeta);
    colMeta = new ColMeta(1, ColMeta.COL_TYPE_STRING);
    colMetaMap.put("name", colMeta);
    OrderCol[] orderCols = new OrderCol[1];
    OrderCol orderCol = new OrderCol(colMetaMap.get("id"), OrderCol.COL_ORDER_TYPE_DESC);
    orderCols[0] = orderCol;
    groupKeySchema = new StructType(colMetaMap, fieldCount);
    groupKeySchema.setOrderCols(orderCols);
    /**
   * 构造数据字段value key
   */
    fieldCount = 4;
    colMeta = null;
    colMetaMap = new HashMap<String, ColMeta>(fieldCount);
    colMeta = new ColMeta(0, ColMeta.COL_TYPE_STRING);
    colMetaMap.put("id", colMeta);
    colMeta = new ColMeta(1, ColMeta.COL_TYPE_STRING);
    colMetaMap.put("name", colMeta);
    colMeta = new ColMeta(2, ColMeta.COL_TYPE_INT);
    colMetaMap.put("age", colMeta);
    colMeta = new ColMeta(3, ColMeta.COL_TYPE_LONGLONG);
    colMetaMap.put("score", colMeta);
    orderCols = new OrderCol[1];
    orderCol = new OrderCol(colMetaMap.get("id"), OrderCol.COL_ORDER_TYPE_DESC);
    orderCols[0] = orderCol;
    aggBufferSchema = new StructType(colMetaMap, fieldCount);
    aggBufferSchema.setOrderCols(orderCols);
    /**
     *emtpy Row value
     */
    BufferHolder bufferHolder;
    emptyAggregationBuffer = new UnsafeRow(4);
    bufferHolder = new BufferHolder(emptyAggregationBuffer, 0);
    UnsafeRowWriter unsafeRowWriter = new UnsafeRowWriter(bufferHolder, 4);
    bufferHolder.reset();
    String value = "o";
    unsafeRowWriter.write(0, value.getBytes());
    unsafeRowWriter.write(1, value.getBytes());
    emptyAggregationBuffer.setInt(2, 0);
    emptyAggregationBuffer.setLong(3, 0);
    emptyAggregationBuffer.setTotalSize(bufferHolder.totalSize());
    UnsafeFixedWidthAggregationMap map = new UnsafeFixedWidthAggregationMap(emptyAggregationBuffer, aggBufferSchema, groupKeySchema, dataNodeMemoryManager, 2 * 1024, PAGE_SIZE_BYTES, true);
    /**
       * 造数据
       */
    int i;
    List<UnsafeRow> rows = new ArrayList<UnsafeRow>();
    for (i = 0; i < 100000; i++) {
        /**
       * key
       */
        UnsafeRow groupKey = new UnsafeRow(2);
        bufferHolder = new BufferHolder(groupKey, 0);
        unsafeRowWriter = new UnsafeRowWriter(bufferHolder, 2);
        bufferHolder.reset();
        unsafeRowWriter.write(0, BytesTools.toBytes(rand.nextInt(10000000)));
        unsafeRowWriter.write(1, BytesTools.toBytes(rand.nextInt(10000000)));
        groupKey.setTotalSize(bufferHolder.totalSize());
        UnsafeRow valueKey = new UnsafeRow(4);
        bufferHolder = new BufferHolder(valueKey, 0);
        unsafeRowWriter = new UnsafeRowWriter(bufferHolder, 4);
        bufferHolder.reset();
        unsafeRowWriter.write(0, BytesTools.toBytes(rand.nextInt(10)));
        unsafeRowWriter.write(1, BytesTools.toBytes(rand.nextInt(10)));
        valueKey.setInt(2, i);
        valueKey.setLong(3, 1);
        valueKey.setTotalSize(bufferHolder.totalSize());
        if (map.find(groupKey)) {
            UnsafeRow rs = map.getAggregationBuffer(groupKey);
            rs.setLong(3, i + valueKey.getLong(3));
            rs.setInt(2, 100 + valueKey.getInt(2));
        } else {
            map.put(groupKey, valueKey);
        }
        rows.add(valueKey);
    }
    KVIterator<UnsafeRow, UnsafeRow> iter = map.iterator();
    int j = 0;
    while (iter.next()) {
        Assert.assertEquals(j, iter.getValue().getInt(2));
        j++;
        iter.getValue().setInt(2, 5000000);
        iter.getValue().setLong(3, 600000);
    }
    Assert.assertEquals(rows.size(), j);
    int k = 0;
    KVIterator<UnsafeRow, UnsafeRow> iter1 = map.iterator();
    while (iter1.next()) {
        k++;
        // LOGGER.error("(" + BytesTools.toInt(iter1.getKey().getBinary(0)) + "," +
        //       iter1.getValue().getInt(2) +"," +iter1.getValue().getLong(3)+")");
        Assert.assertEquals(5000000, iter1.getValue().getInt(2));
        Assert.assertEquals(600000, iter1.getValue().getLong(3));
    }
    Assert.assertEquals(j, k);
    map.free();
}
Also used : ColMeta(io.mycat.sqlengine.mpp.ColMeta) StructType(io.mycat.memory.unsafe.row.StructType) DataNodeMemoryManager(io.mycat.memory.unsafe.memory.mm.DataNodeMemoryManager) UnsafeRowWriter(io.mycat.memory.unsafe.row.UnsafeRowWriter) DataNodeMemoryManager(io.mycat.memory.unsafe.memory.mm.DataNodeMemoryManager) MemoryManager(io.mycat.memory.unsafe.memory.mm.MemoryManager) UnsafeRow(io.mycat.memory.unsafe.row.UnsafeRow) BufferHolder(io.mycat.memory.unsafe.row.BufferHolder) OrderCol(io.mycat.sqlengine.mpp.OrderCol) MyCatMemory(io.mycat.memory.MyCatMemory) Test(org.junit.Test)

Example 7 with StructType

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

the class UnsafeFixedWidthAggregationMapSuite method testWithMemoryLeakDetection.

@Test
public void testWithMemoryLeakDetection() throws IOException, NoSuchFieldException, IllegalAccessException {
    MyCatMemory myCatMemory = new MyCatMemory();
    MemoryManager memoryManager = myCatMemory.getResultMergeMemoryManager();
    DataNodeMemoryManager dataNodeMemoryManager = new DataNodeMemoryManager(memoryManager, Thread.currentThread().getId());
    int fieldCount = 3;
    ColMeta colMeta = null;
    Map<String, ColMeta> colMetaMap = new HashMap<String, ColMeta>(fieldCount);
    colMeta = new ColMeta(0, ColMeta.COL_TYPE_STRING);
    colMetaMap.put("id", colMeta);
    colMeta = new ColMeta(1, ColMeta.COL_TYPE_STRING);
    colMetaMap.put("name", colMeta);
    colMeta = new ColMeta(2, ColMeta.COL_TYPE_STRING);
    colMetaMap.put("age", colMeta);
    OrderCol[] orderCols = new OrderCol[1];
    OrderCol orderCol = new OrderCol(colMetaMap.get("id"), OrderCol.COL_ORDER_TYPE_DESC);
    orderCols[0] = orderCol;
    groupKeySchema = new StructType(colMetaMap, fieldCount);
    groupKeySchema.setOrderCols(orderCols);
    fieldCount = 3;
    colMeta = null;
    colMetaMap = new HashMap<String, ColMeta>(fieldCount);
    colMeta = new ColMeta(0, ColMeta.COL_TYPE_LONGLONG);
    colMetaMap.put("age", colMeta);
    colMeta = new ColMeta(1, ColMeta.COL_TYPE_LONGLONG);
    colMetaMap.put("age1", colMeta);
    colMeta = new ColMeta(2, ColMeta.COL_TYPE_STRING);
    colMetaMap.put("name", colMeta);
    orderCols = new OrderCol[1];
    orderCol = new OrderCol(colMetaMap.get("id"), OrderCol.COL_ORDER_TYPE_DESC);
    orderCols[0] = orderCol;
    aggBufferSchema = new StructType(colMetaMap, fieldCount);
    aggBufferSchema.setOrderCols(orderCols);
    /**
   * value
   */
    BufferHolder bufferHolder;
    emptyAggregationBuffer = new UnsafeRow(3);
    bufferHolder = new BufferHolder(emptyAggregationBuffer, 0);
    UnsafeRowWriter unsafeRowWriter = new UnsafeRowWriter(bufferHolder, 3);
    bufferHolder.reset();
    String value = "ok,hello";
    emptyAggregationBuffer.setLong(0, 0);
    emptyAggregationBuffer.setLong(1, 0);
    unsafeRowWriter.write(2, value.getBytes());
    emptyAggregationBuffer.setTotalSize(bufferHolder.totalSize());
    UnsafeFixedWidthAggregationMap map = new UnsafeFixedWidthAggregationMap(emptyAggregationBuffer, aggBufferSchema, groupKeySchema, dataNodeMemoryManager, 2 * 1024, PAGE_SIZE_BYTES, false);
    int i;
    List<UnsafeRow> rows = new ArrayList<UnsafeRow>();
    for (i = 0; i < 1000; i++) {
        String line = "testUnsafeRow" + i;
        /**
     * key
     */
        UnsafeRow groupKey = new UnsafeRow(3);
        bufferHolder = new BufferHolder(groupKey, 0);
        unsafeRowWriter = new UnsafeRowWriter(bufferHolder, 3);
        bufferHolder.reset();
        final byte[] key = getRandomByteArray(rand.nextInt(8));
        String age = "5" + i;
        unsafeRowWriter.write(0, key);
        unsafeRowWriter.write(1, line.getBytes());
        unsafeRowWriter.write(2, age.getBytes());
        groupKey.setTotalSize(bufferHolder.totalSize());
        map.getAggregationBuffer(groupKey);
        rows.add(groupKey);
    }
    Assert.assertEquals(i, rows.size());
    UnsafeRow row = rows.get(12);
    UnsafeRow rs = map.getAggregationBuffer(row);
    rs.setLong(0, 12);
    rs = map.getAggregationBuffer(row);
    Assert.assertEquals(12, rs.getLong(0));
    map.free();
}
Also used : ColMeta(io.mycat.sqlengine.mpp.ColMeta) StructType(io.mycat.memory.unsafe.row.StructType) DataNodeMemoryManager(io.mycat.memory.unsafe.memory.mm.DataNodeMemoryManager) UnsafeRowWriter(io.mycat.memory.unsafe.row.UnsafeRowWriter) DataNodeMemoryManager(io.mycat.memory.unsafe.memory.mm.DataNodeMemoryManager) MemoryManager(io.mycat.memory.unsafe.memory.mm.MemoryManager) UnsafeRow(io.mycat.memory.unsafe.row.UnsafeRow) BufferHolder(io.mycat.memory.unsafe.row.BufferHolder) OrderCol(io.mycat.sqlengine.mpp.OrderCol) MyCatMemory(io.mycat.memory.MyCatMemory) Test(org.junit.Test)

Aggregations

StructType (io.mycat.memory.unsafe.row.StructType)7 BufferHolder (io.mycat.memory.unsafe.row.BufferHolder)6 UnsafeRow (io.mycat.memory.unsafe.row.UnsafeRow)6 UnsafeRowWriter (io.mycat.memory.unsafe.row.UnsafeRowWriter)6 DataNodeMemoryManager (io.mycat.memory.unsafe.memory.mm.DataNodeMemoryManager)5 ColMeta (io.mycat.sqlengine.mpp.ColMeta)4 OrderCol (io.mycat.sqlengine.mpp.OrderCol)4 MyCatMemory (io.mycat.memory.MyCatMemory)3 MemoryManager (io.mycat.memory.unsafe.memory.mm.MemoryManager)3 Test (org.junit.Test)3 PrefixComparator (io.mycat.memory.unsafe.utils.sort.PrefixComparator)2 RowPrefixComputer (io.mycat.memory.unsafe.utils.sort.RowPrefixComputer)2 UnsafeExternalRowSorter (io.mycat.memory.unsafe.utils.sort.UnsafeExternalRowSorter)2 BigDecimal (java.math.BigDecimal)2 UnsafeFixedWidthAggregationMap (io.mycat.memory.unsafe.map.UnsafeFixedWidthAggregationMap)1 DataNodeDiskManager (io.mycat.memory.unsafe.storage.DataNodeDiskManager)1 SerializerManager (io.mycat.memory.unsafe.storage.SerializerManager)1 MycatPropertyConf (io.mycat.memory.unsafe.utils.MycatPropertyConf)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1