use of com.pingcap.tikv.key.Key in project tispark by pingcap.
the class KVMockServer method kvScan.
@Override
public void kvScan(org.tikv.kvproto.Kvrpcpb.ScanRequest request, io.grpc.stub.StreamObserver<org.tikv.kvproto.Kvrpcpb.ScanResponse> responseObserver) {
try {
verifyContext(request.getContext());
if (request.getVersion() == 0) {
throw new Exception();
}
ByteString key = request.getStartKey();
Kvrpcpb.ScanResponse.Builder builder = Kvrpcpb.ScanResponse.newBuilder();
Error.Builder errBuilder = Error.newBuilder();
Integer errorCode = errorMap.remove(key);
if (errorCode != null) {
if (errorCode == ABORT) {
errBuilder.setServerIsBusy(Errorpb.ServerIsBusy.getDefaultInstance());
}
builder.setRegionError(errBuilder.build());
} else {
ByteString startKey = request.getStartKey();
SortedMap<Key, ByteString> kvs = dataMap.tailMap(toRawKey(startKey));
builder.addAllPairs(kvs.entrySet().stream().limit(request.getLimit()).map(kv -> Kvrpcpb.KvPair.newBuilder().setKey(kv.getKey().toByteString()).setValue(kv.getValue()).build()).collect(Collectors.toList()));
}
responseObserver.onNext(builder.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 TiBatchWriteUtils method getRecordRegions.
public static List<TiRegion> getRecordRegions(TiSession session, TiTableInfo table) {
ArrayList<TiRegion> regionList = new ArrayList<>();
Key key = RowKey.createMin(table.getId());
RowKey endRowKey = RowKey.createBeyondMax(table.getId());
while (key.compareTo(endRowKey) < 0) {
TiRegion region = session.getRegionManager().getRegionByKey(key.toByteString());
regionList.add(region);
key = Key.toRawKey(region.getEndKey());
}
return regionList;
}
use of com.pingcap.tikv.key.Key in project tispark by pingcap.
the class TiBatchWriteUtils method getRegionByIndex.
public static List<TiRegion> getRegionByIndex(TiSession session, TiTableInfo table, TiIndexInfo index) {
ArrayList<TiRegion> regionList = new ArrayList<>();
Key min = IndexKey.toIndexKey(table.getId(), index.getId());
Key max = min.nextPrefix();
while (min.compareTo(max) < 0) {
TiRegion region = session.getRegionManager().getRegionByKey(min.toByteString());
regionList.add(region);
min = Key.toRawKey(region.getEndKey());
}
return regionList;
}
use of com.pingcap.tikv.key.Key in project tispark by pingcap.
the class TiSession method splitRegion.
private List<TiRegion> splitRegion(List<ByteString> splitKeys, BackOffer backOffer) {
List<TiRegion> regions = new ArrayList<>();
Map<TiRegion, List<ByteString>> groupKeys = groupKeysByRegion(regionManager, splitKeys, backOffer);
for (Map.Entry<TiRegion, List<ByteString>> entry : groupKeys.entrySet()) {
Pair<TiRegion, Metapb.Store> pair = getRegionManager().getRegionStorePairByKey(entry.getKey().getStartKey());
TiRegion region = pair.first;
Metapb.Store store = pair.second;
List<ByteString> splits = entry.getValue().stream().filter(k -> !k.equals(region.getStartKey()) && !k.equals(region.getEndKey())).collect(Collectors.toList());
if (splits.isEmpty()) {
logger.warn("split key equal to region start key or end key. Region splitting is not needed.");
} else {
logger.info("start to split region id={}, split size={}", region.getId(), splits.size());
List<TiRegion> newRegions;
try {
newRegions = getRegionStoreClientBuilder().build(region, store).splitRegion(splits);
} catch (final TiKVException e) {
// retry
logger.warn("ReSplitting ranges for splitRegion", e);
clientBuilder.getRegionManager().invalidateRegion(region);
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
newRegions = splitRegion(splits, backOffer);
}
logger.info("region id={}, new region size={}", region.getId(), newRegions.size());
regions.addAll(newRegions);
}
}
logger.info("splitRegion: return region size={}", regions.size());
return regions;
}
use of com.pingcap.tikv.key.Key in project tispark by pingcap.
the class PredicateUtils method expressionToIndexRanges.
/**
* Build index ranges from access points and access conditions
*
* @param pointPredicates conditions converting to a single point access
* @param rangePredicate conditions converting to a range
* @return Index Range for scan
*/
public static List<IndexRange> expressionToIndexRanges(List<Expression> pointPredicates, Optional<Expression> rangePredicate, TiTableInfo table, TiIndexInfo index) {
requireNonNull(pointPredicates, "pointPredicates is null");
requireNonNull(rangePredicate, "rangePredicate is null");
ImmutableList.Builder<IndexRange> builder = ImmutableList.builder();
IndexRangeSetBuilder indexRangeBuilder = new IndexRangeSetBuilder(table, index);
if (pointPredicates.size() != 0) {
List<Key> pointKeys = expressionToPoints(pointPredicates, table, index);
for (Key key : pointKeys) {
if (rangePredicate.isPresent()) {
Set<Range<TypedKey>> ranges = indexRangeBuilder.buildRange(rangePredicate.get()).asRanges();
for (Range<TypedKey> range : ranges) {
builder.add(new IndexRange(key, range));
}
} else {
// no predicates with point keys leads to empty range encoding
builder.add(new IndexRange(key, null));
}
}
} else {
if (rangePredicate.isPresent()) {
Set<Range<TypedKey>> ranges = indexRangeBuilder.buildRange(rangePredicate.get()).asRanges();
for (Range<TypedKey> range : ranges) {
builder.add(new IndexRange(null, range));
}
} else {
// no filter at all means full range
builder.add(new IndexRange(null, Range.all()));
}
}
return builder.build();
}
Aggregations