Search in sources :

Example 11 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class TiKVScanAnalyzer method buildTableScanKeyRangeWithIds.

private Map<Long, List<KeyRange>> buildTableScanKeyRangeWithIds(List<Long> ids, List<IndexRange> indexRanges) {
    Map<Long, List<KeyRange>> idRanges = new HashMap<>(ids.size());
    for (Long id : ids) {
        List<KeyRange> ranges = new ArrayList<>(indexRanges.size());
        indexRanges.forEach((ir) -> {
            Pair<Key, Key> pairKey = buildTableScanKeyRangePerId(id, ir);
            Key startKey = pairKey.first;
            Key endKey = pairKey.second;
            // This range only possible when < MIN or > MAX
            if (!startKey.equals(endKey)) {
                ranges.add(makeCoprocRange(startKey.toByteString(), endKey.toByteString()));
            }
        });
        idRanges.put(id, ranges);
    }
    return idRanges;
}
Also used : HashMap(java.util.HashMap) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Key(com.pingcap.tikv.key.Key) RowKey(com.pingcap.tikv.key.RowKey) TypedKey(com.pingcap.tikv.key.TypedKey)

Example 12 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class ScanIterator method cacheLoadFails.

// return true if current cache is not loaded or empty
boolean cacheLoadFails() {
    if (endOfScan || processingLastBatch) {
        return true;
    }
    if (startKey == null) {
        return true;
    }
    try {
        TiRegion region = loadCurrentRegionToCache();
        ByteString curRegionEndKey = region.getEndKey();
        // See https://github.com/pingcap/tispark/issues/393 for details
        if (currentCache == null) {
            return true;
        }
        index = 0;
        Key lastKey;
        // of a transaction. Otherwise below code might lose data
        if (currentCache.size() < limit) {
            startKey = curRegionEndKey;
            lastKey = Key.toRawKey(curRegionEndKey);
        } else if (currentCache.size() > limit) {
            throw new IndexOutOfBoundsException("current cache size = " + currentCache.size() + ", larger than " + limit);
        } else {
            // Start new scan from exact next key in current region
            lastKey = Key.toRawKey(currentCache.get(currentCache.size() - 1).getKey());
            startKey = lastKey.next().toByteString();
        }
        // if startKey is empty, it indicates +∞
        if (hasEndKey && lastKey.compareTo(endKey) >= 0 || startKey.isEmpty()) {
            processingLastBatch = true;
            startKey = null;
        }
    } catch (Exception e) {
        throw new TiClientInternalException("Error scanning data from region.", e);
    }
    return false;
}
Also used : TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException) ByteString(com.google.protobuf.ByteString) TiRegion(com.pingcap.tikv.region.TiRegion) Key(com.pingcap.tikv.key.Key) GrpcException(com.pingcap.tikv.exception.GrpcException) TiClientInternalException(com.pingcap.tikv.exception.TiClientInternalException)

Example 13 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class KVMockServer method coprocessor.

@Override
public void coprocessor(org.tikv.kvproto.Coprocessor.Request requestWrap, io.grpc.stub.StreamObserver<org.tikv.kvproto.Coprocessor.Response> responseObserver) {
    try {
        verifyContext(requestWrap.getContext());
        DAGRequest request = DAGRequest.parseFrom(requestWrap.getData());
        List<Coprocessor.KeyRange> keyRanges = requestWrap.getRangesList();
        Coprocessor.Response.Builder builderWrap = Coprocessor.Response.newBuilder();
        SelectResponse.Builder builder = SelectResponse.newBuilder();
        org.tikv.kvproto.Errorpb.Error.Builder errBuilder = org.tikv.kvproto.Errorpb.Error.newBuilder();
        for (Coprocessor.KeyRange keyRange : keyRanges) {
            Integer errorCode = errorMap.remove(keyRange.getStart());
            if (errorCode != null) {
                if (STALE_EPOCH == errorCode) {
                    errBuilder.setEpochNotMatch(Errorpb.EpochNotMatch.getDefaultInstance());
                } else if (NOT_LEADER == errorCode) {
                    errBuilder.setNotLeader(NotLeader.getDefaultInstance());
                } else {
                    errBuilder.setServerIsBusy(ServerIsBusy.getDefaultInstance());
                }
                builderWrap.setRegionError(errBuilder.build());
                break;
            } else {
                ByteString startKey = keyRange.getStart();
                SortedMap<Key, ByteString> kvs = dataMap.tailMap(toRawKey(startKey));
                builder.addAllChunks(kvs.entrySet().stream().filter(Objects::nonNull).filter(kv -> kv.getKey().compareTo(toRawKey(keyRange.getEnd())) <= 0).map(kv -> Chunk.newBuilder().setRowsData(kv.getValue()).build()).collect(Collectors.toList()));
            }
        }
        responseObserver.onNext(builderWrap.setData(builder.build().toByteString()).build());
        responseObserver.onCompleted();
    } catch (Exception e) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : NotLeader(org.tikv.kvproto.Errorpb.NotLeader) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Errorpb(org.tikv.kvproto.Errorpb) ServerIsBusy(org.tikv.kvproto.Errorpb.ServerIsBusy) HashMap(java.util.HashMap) Error(org.tikv.kvproto.Errorpb.Error) Context(org.tikv.kvproto.Kvrpcpb.Context) Key(com.pingcap.tikv.key.Key) ServerSocket(java.net.ServerSocket) ImmutableList(com.google.common.collect.ImmutableList) ServerBuilder(io.grpc.ServerBuilder) Map(java.util.Map) Status(io.grpc.Status) Server(io.grpc.Server) Coprocessor(org.tikv.kvproto.Coprocessor) Key.toRawKey(com.pingcap.tikv.key.Key.toRawKey) TikvGrpc(org.tikv.kvproto.TikvGrpc) Chunk(com.pingcap.tidb.tipb.Chunk) IOException(java.io.IOException) Kvrpcpb(org.tikv.kvproto.Kvrpcpb) TiRegion(com.pingcap.tikv.region.TiRegion) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) DAGRequest(com.pingcap.tidb.tipb.DAGRequest) Objects(java.util.Objects) List(java.util.List) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) ByteString(com.google.protobuf.ByteString) Error(org.tikv.kvproto.Errorpb.Error) IOException(java.io.IOException) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Coprocessor(org.tikv.kvproto.Coprocessor) DAGRequest(com.pingcap.tidb.tipb.DAGRequest) Objects(java.util.Objects) SelectResponse(com.pingcap.tidb.tipb.SelectResponse) Key(com.pingcap.tikv.key.Key) Key.toRawKey(com.pingcap.tikv.key.Key.toRawKey)

Example 14 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class IndexStatistics method getRowCount.

public double getRowCount(List<IndexRange> indexRanges) {
    double rowCount = 0.0;
    for (IndexRange ir : indexRanges) {
        StatisticsKeyRangeBuilder builder = new StatisticsKeyRangeBuilder(ir);
        Pair<Key, Key> range = builder.compute();
        // TODO: Implement CMSketch point query
        // if (cmSketch != null) {
        // rowCount += cmSketch.queryBytes(convertedKey.getBytes());
        // } else {
        // rowCount += histogram.betweenRowCount(convertedKey, convertedNext);
        // }
        rowCount += histogram.betweenRowCount(range.first, range.second);
    }
    if (rowCount > histogram.totalRowCount()) {
        rowCount = histogram.totalRowCount();
    } else if (rowCount < 0) {
        rowCount = 0;
    }
    return rowCount;
}
Also used : IndexRange(com.pingcap.tikv.predicates.IndexRange) StatisticsKeyRangeBuilder(com.pingcap.tikv.key.StatisticsKeyRangeBuilder) Key(com.pingcap.tikv.key.Key)

Example 15 with Key

use of com.pingcap.tikv.key.Key in project tispark by pingcap.

the class KeyRangeUtils method mergeRanges.

/**
 * Merge potential discrete ranges into one large range.
 *
 * @param ranges the range list to merge
 * @return the minimal range which encloses all ranges in this range list.
 */
public static List<KeyRange> mergeRanges(List<KeyRange> ranges) {
    if (ranges == null || ranges.isEmpty() || ranges.size() == 1) {
        return ranges;
    }
    KeyRange first = ranges.get(0);
    Key lowMin = toRawKey(first.getStart(), true);
    Key upperMax = toRawKey(first.getEnd(), false);
    for (int i = 1; i < ranges.size(); i++) {
        KeyRange keyRange = ranges.get(i);
        Key start = toRawKey(keyRange.getStart(), true);
        Key end = toRawKey(keyRange.getEnd(), false);
        if (start.compareTo(lowMin) < 0) {
            lowMin = start;
        }
        if (end.compareTo(upperMax) > 0) {
            upperMax = end;
        }
    }
    ImmutableList.Builder<KeyRange> rangeBuilder = ImmutableList.builder();
    rangeBuilder.add(makeCoprocRange(lowMin.toByteString(), upperMax.toByteString()));
    return rangeBuilder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) Key.toRawKey(com.pingcap.tikv.key.Key.toRawKey) Key(com.pingcap.tikv.key.Key)

Aggregations

Key (com.pingcap.tikv.key.Key)15 TypedKey (com.pingcap.tikv.key.TypedKey)6 ImmutableList (com.google.common.collect.ImmutableList)5 TiRegion (com.pingcap.tikv.region.TiRegion)5 ArrayList (java.util.ArrayList)5 Range (com.google.common.collect.Range)4 ByteString (com.google.protobuf.ByteString)4 CompoundKey (com.pingcap.tikv.key.CompoundKey)4 RowKey (com.pingcap.tikv.key.RowKey)4 Key.toRawKey (com.pingcap.tikv.key.Key.toRawKey)3 HashMap (java.util.HashMap)3 List (java.util.List)3 KeyRange (org.tikv.kvproto.Coprocessor.KeyRange)3 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)2 Expression (com.pingcap.tikv.expression.Expression)2 IndexRangeSetBuilder (com.pingcap.tikv.expression.visitor.IndexRangeSetBuilder)2 IndexKey (com.pingcap.tikv.key.IndexKey)2 Pair (com.pingcap.tikv.util.Pair)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2