Search in sources :

Example 1 with CodecDataInput

use of com.pingcap.tikv.codec.CodecDataInput in project tispark by pingcap.

the class RowKey method decode.

public static RowKey decode(byte[] value) {
    CodecDataInput cdi = new CodecDataInput(value);
    cdi.readByte();
    long tableId = IntegerCodec.readLong(cdi);
    cdi.readByte();
    cdi.readByte();
    Handle handle;
    if (value.length == INT_HANDLE_SIZE) {
        handle = new IntHandle(IntegerCodec.readLong(cdi));
    } else {
        byte[] buffer = new byte[value.length - HANDLE_PREFIX_SIZE];
        cdi.readFully(buffer);
        handle = new CommonHandle(buffer);
    }
    return toRowKey(tableId, handle);
}
Also used : CodecDataInput(com.pingcap.tikv.codec.CodecDataInput)

Example 2 with CodecDataInput

use of com.pingcap.tikv.codec.CodecDataInput in project tispark by pingcap.

the class TypedKey method toString.

@Override
public String toString() {
    try {
        CodecDataInput cdi = new CodecDataInput(value);
        Object val = type.decode(cdi);
        if (val instanceof byte[]) {
            return LogDesensitization.hide(KeyUtils.formatBytes(value));
        }
        return val.toString();
    } catch (Exception e) {
        return "raw value:" + LogDesensitization.hide(KeyUtils.formatBytesUTF8(value));
    }
}
Also used : CodecDataInput(com.pingcap.tikv.codec.CodecDataInput) TypeException(com.pingcap.tikv.exception.TypeException)

Example 3 with CodecDataInput

use of com.pingcap.tikv.codec.CodecDataInput in project tispark by pingcap.

the class CoprocessorIterator method getTiChunkIterator.

/**
 * Build a DAGIterator from TiDAGRequest and region tasks to get rows
 *
 * <p>When we are preforming a scan request using coveringIndex, {@link
 * com.pingcap.tidb.tipb.IndexScan} should be used to read index rows. In other circumstances,
 * {@link com.pingcap.tidb.tipb.TableScan} is used to scan table rows.
 *
 * @param req TiDAGRequest built
 * @param regionTasks a list or RegionTask each contains a task on a single region
 * @param session TiSession
 * @return a DAGIterator to be processed
 */
public static CoprocessorIterator<TiChunk> getTiChunkIterator(TiDAGRequest req, List<RegionTask> regionTasks, TiSession session, int numOfRows) {
    TiDAGRequest dagRequest = req.copy();
    return new DAGIterator<TiChunk>(dagRequest.buildTableScan(), regionTasks, session, SchemaInfer.create(dagRequest), dagRequest.getPushDownType(), dagRequest.getStoreType(), dagRequest.getStartTs().getVersion()) {

        @Override
        public TiChunk next() {
            DataType[] dataTypes = this.schemaInfer.getTypes().toArray(new DataType[0]);
            // TODO tiColumnarBatch is meant to be reused in the entire data loading process.
            if (this.encodeType == EncodeType.TypeDefault) {
                Row[] rows = new Row[numOfRows];
                int count = 0;
                for (int i = 0; i < rows.length && hasNext(); i++) {
                    rows[i] = rowReader.readRow(dataTypes);
                    count += 1;
                }
                TiRowColumnVector[] columnarVectors = new TiRowColumnVector[dataTypes.length];
                for (int i = 0; i < dataTypes.length; i++) {
                    columnarVectors[i] = new TiRowColumnVector(dataTypes[i], i, rows, count);
                }
                return new TiChunk(columnarVectors);
            } else if (this.encodeType == EncodeType.TypeChunk) {
                TiColumnVector[] columnarVectors = new TiColumnVector[dataTypes.length];
                List<List<TiChunkColumnVector>> childColumnVectors = new ArrayList<>();
                for (int i = 0; i < dataTypes.length; i++) {
                    childColumnVectors.add(new ArrayList<>());
                }
                int count = 0;
                // TODO(Zhexuan Yang) we need control memory limit in case of out of memory error
                while (count < numOfRows && hasNext()) {
                    for (int i = 0; i < dataTypes.length; i++) {
                        childColumnVectors.get(i).add(dataTypes[i].decodeChunkColumn(dataInput));
                    }
                    int size = childColumnVectors.get(0).size();
                    count += childColumnVectors.get(0).get(size - 1).numOfRows();
                    // left data should be trashed.
                    dataInput = new CodecDataInput(new byte[0]);
                }
                for (int i = 0; i < dataTypes.length; i++) {
                    columnarVectors[i] = new BatchedTiChunkColumnVector(childColumnVectors.get(i), count);
                }
                return new TiChunk(columnarVectors);
            } else {
                // reading column count
                long colCount = IntegerCodec.readUVarLong(dataInput);
                long numOfRows = IntegerCodec.readUVarLong(dataInput);
                TiColumnVector[] columnVectors = new TiColumnVector[(int) colCount];
                for (int columnIdx = 0; columnIdx < colCount; columnIdx++) {
                    // reading column name
                    long length = IntegerCodec.readUVarLong(dataInput);
                    for (int i = 0; i < length; i++) {
                        dataInput.readByte();
                    }
                    // reading type name
                    length = IntegerCodec.readUVarLong(dataInput);
                    byte[] utf8Bytes = new byte[(int) length];
                    for (int i = 0; i < length; i++) {
                        utf8Bytes[i] = dataInput.readByte();
                    }
                    String typeName = new String(utf8Bytes, StandardCharsets.UTF_8);
                    CHType type = CHTypeMapping.parseType(typeName);
                    columnVectors[columnIdx] = type.decode(dataInput, (int) numOfRows);
                // TODO this is workaround to bybass nullable type
                }
                dataInput = new CodecDataInput(new byte[0]);
                return new TiChunk(columnVectors);
            }
        }
    };
}
Also used : TiChunk(com.pingcap.tikv.columnar.TiChunk) BatchedTiChunkColumnVector(com.pingcap.tikv.columnar.BatchedTiChunkColumnVector) ArrayList(java.util.ArrayList) CHType(com.pingcap.tikv.columnar.datatypes.CHType) TiDAGRequest(com.pingcap.tikv.meta.TiDAGRequest) TiChunkColumnVector(com.pingcap.tikv.columnar.TiChunkColumnVector) BatchedTiChunkColumnVector(com.pingcap.tikv.columnar.BatchedTiChunkColumnVector) CodecDataInput(com.pingcap.tikv.codec.CodecDataInput) DataType(com.pingcap.tikv.types.DataType) ArrayList(java.util.ArrayList) List(java.util.List) Row(com.pingcap.tikv.row.Row) TiRowColumnVector(com.pingcap.tikv.columnar.TiRowColumnVector)

Example 4 with CodecDataInput

use of com.pingcap.tikv.codec.CodecDataInput in project tispark by pingcap.

the class ChunkIteratorTest method chunkTest.

@Test
public void chunkTest() {
    ChunkIterator<ByteString> chunkIterator = ChunkIterator.getRawBytesChunkIterator(chunks);
    DataType bytes = StringType.VARCHAR;
    DataType ints = IntegerType.INT;
    Row row = ObjectRowImpl.create(6);
    CodecDataInput cdi = new CodecDataInput(chunkIterator.next());
    setValueToRow(cdi, ints, 0, row);
    setValueToRow(cdi, bytes, 1, row);
    cdi = new CodecDataInput(chunkIterator.next());
    setValueToRow(cdi, ints, 2, row);
    setValueToRow(cdi, bytes, 3, row);
    cdi = new CodecDataInput(chunkIterator.next());
    setValueToRow(cdi, ints, 4, row);
    setValueToRow(cdi, bytes, 5, row);
    assertEquals(row.getLong(0), 1);
    assertEquals(row.getString(1), "a");
    assertEquals(row.getLong(2), 2);
    assertEquals(row.getString(3), "b");
    assertEquals(row.getLong(4), 3);
    assertEquals(row.getString(5), "c");
}
Also used : ByteString(com.google.protobuf.ByteString) CodecDataInput(com.pingcap.tikv.codec.CodecDataInput) DataType(com.pingcap.tikv.types.DataType) Row(com.pingcap.tikv.row.Row) Test(org.junit.Test)

Example 5 with CodecDataInput

use of com.pingcap.tikv.codec.CodecDataInput in project tispark by pingcap.

the class RowIDAllocator method getMetaToUpdate.

private Optional<BytePairWrapper> getMetaToUpdate(ByteString key, byte[] oldVal, Snapshot snapshot) {
    // 1. encode hash meta key
    // 2. load meta via hash meta key from TiKV
    // 3. update meta's filed count and set it back to TiKV
    CodecDataOutput cdo = new CodecDataOutput();
    ByteString metaKey = MetaCodec.encodeHashMetaKey(cdo, key.toByteArray());
    long fieldCount = 0;
    ByteString metaVal = snapshot.get(metaKey);
    // big endian the 8 bytes
    if (!metaVal.isEmpty()) {
        try {
            fieldCount = IntegerCodec.readULong(new CodecDataInput(metaVal.toByteArray()));
        } catch (Exception ignored) {
            LOG.warn("metaDecode failed, field is ignored." + KeyUtils.formatBytesUTF8(metaVal));
        }
    }
    // update meta field count only oldVal is null
    if (oldVal == null || oldVal.length == 0) {
        fieldCount++;
        cdo.reset();
        cdo.writeLong(fieldCount);
        return Optional.of(new BytePairWrapper(metaKey.toByteArray(), cdo.toBytes()));
    }
    return Optional.empty();
}
Also used : BytePairWrapper(com.pingcap.tikv.BytePairWrapper) ByteString(com.google.protobuf.ByteString) CodecDataInput(com.pingcap.tikv.codec.CodecDataInput) CodecDataOutput(com.pingcap.tikv.codec.CodecDataOutput) TiBatchWriteException(com.pingcap.tikv.exception.TiBatchWriteException) AllocateRowIDOverflowException(com.pingcap.tikv.exception.AllocateRowIDOverflowException)

Aggregations

CodecDataInput (com.pingcap.tikv.codec.CodecDataInput)8 ByteString (com.google.protobuf.ByteString)3 Row (com.pingcap.tikv.row.Row)2 DataType (com.pingcap.tikv.types.DataType)2 BytePairWrapper (com.pingcap.tikv.BytePairWrapper)1 CodecDataOutput (com.pingcap.tikv.codec.CodecDataOutput)1 BatchedTiChunkColumnVector (com.pingcap.tikv.columnar.BatchedTiChunkColumnVector)1 TiChunk (com.pingcap.tikv.columnar.TiChunk)1 TiChunkColumnVector (com.pingcap.tikv.columnar.TiChunkColumnVector)1 TiRowColumnVector (com.pingcap.tikv.columnar.TiRowColumnVector)1 CHType (com.pingcap.tikv.columnar.datatypes.CHType)1 AllocateRowIDOverflowException (com.pingcap.tikv.exception.AllocateRowIDOverflowException)1 TiBatchWriteException (com.pingcap.tikv.exception.TiBatchWriteException)1 TypeException (com.pingcap.tikv.exception.TypeException)1 TiDAGRequest (com.pingcap.tikv.meta.TiDAGRequest)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1 Region (org.tikv.kvproto.Metapb.Region)1