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;
}
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;
}
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());
}
}
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;
}
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();
}
Aggregations