use of org.tikv.kvproto.Coprocessor.KeyRange in project client-java by tikv.
the class TiKVScanAnalyzer method buildIndexScanKeyRangeWithIds.
@VisibleForTesting
private Map<Long, List<KeyRange>> buildIndexScanKeyRangeWithIds(List<Long> ids, TiIndexInfo index, List<IndexRange> indexRanges) {
Map<Long, List<KeyRange>> idRanges = new HashMap<>();
for (long id : ids) {
List<KeyRange> ranges = new ArrayList<>(indexRanges.size());
for (IndexRange ir : indexRanges) {
IndexScanKeyRangeBuilder indexScanKeyRangeBuilder = new IndexScanKeyRangeBuilder(id, index, ir);
ranges.add(indexScanKeyRangeBuilder.compute());
}
idRanges.put(id, ranges);
}
return idRanges;
}
use of org.tikv.kvproto.Coprocessor.KeyRange in project client-java by tikv.
the class RangeSplitter method splitRangeByRegion.
/**
* Split key ranges into corresponding region tasks and group by their region id
*
* @param keyRanges List of key ranges
* @param storeType Store type, null or TiKV for TiKV(leader), otherwise TiFlash(learner)
* @return List of RegionTask, each task corresponds to a different region.
*/
public List<RegionTask> splitRangeByRegion(List<KeyRange> keyRanges, TiStoreType storeType) {
if (keyRanges == null || keyRanges.size() == 0) {
return ImmutableList.of();
}
int i = 0;
KeyRange range = keyRanges.get(i++);
// region id to keyRange list
Map<Long, List<KeyRange>> idToRange = new HashMap<>();
Map<Long, Pair<TiRegion, TiStore>> idToRegion = new HashMap<>();
while (true) {
Pair<TiRegion, TiStore> regionStorePair = regionManager.getRegionStorePairByKey(range.getStart(), storeType);
if (regionStorePair == null) {
throw new NullPointerException("fail to get region/store pair by key " + formatByteString(range.getStart()));
}
TiRegion region = regionStorePair.first;
idToRegion.putIfAbsent(region.getId(), regionStorePair);
// Both keys are at right hand side and then always not -INF
if (toRawKey(range.getEnd()).compareTo(toRawKey(region.getEndKey())) > 0) {
// current region does not cover current end key
KeyRange cutRange = KeyRange.newBuilder().setStart(range.getStart()).setEnd(region.getEndKey()).build();
List<KeyRange> ranges = idToRange.computeIfAbsent(region.getId(), k -> new ArrayList<>());
ranges.add(cutRange);
// cut new remaining for current range
range = KeyRange.newBuilder().setStart(region.getEndKey()).setEnd(range.getEnd()).build();
} else {
// current range covered by region
List<KeyRange> ranges = idToRange.computeIfAbsent(region.getId(), k -> new ArrayList<>());
ranges.add(range);
if (i >= keyRanges.size()) {
break;
}
range = keyRanges.get(i++);
}
}
ImmutableList.Builder<RegionTask> resultBuilder = ImmutableList.builder();
idToRange.forEach((k, v) -> {
Pair<TiRegion, TiStore> regionStorePair = idToRegion.get(k);
resultBuilder.add(new RegionTask(regionStorePair.first, regionStorePair.second, v));
});
return resultBuilder.build();
}
use of org.tikv.kvproto.Coprocessor.KeyRange in project client-java by tikv.
the class KeyRangeUtils method mergeSortedRanges.
/**
* Merge SORTED potential discrete ranges into no more than {@code splitNum} large range.
*
* @param ranges the sorted range list to merge
* @param splitNum upper bound of number of ranges to merge into
* @return the minimal range which encloses all ranges in this range list.
*/
public static List<KeyRange> mergeSortedRanges(List<KeyRange> ranges, int splitNum) {
if (splitNum <= 0) {
throw new RuntimeException("Cannot split ranges by non-positive integer");
}
if (ranges == null || ranges.isEmpty() || ranges.size() <= splitNum) {
return ranges;
}
// use ceil for split step
int step = (ranges.size() + splitNum - 1) / splitNum;
ImmutableList.Builder<KeyRange> rangeBuilder = ImmutableList.builder();
for (int i = 0, nowPos = 0; i < splitNum; i++) {
int nextPos = Math.min(nowPos + step - 1, ranges.size() - 1);
KeyRange first = ranges.get(nowPos);
KeyRange last = ranges.get(nextPos);
Key lowerMin = toRawKey(first.getStart(), true);
Key upperMax = toRawKey(last.getEnd(), false);
rangeBuilder.add(makeCoprocRange(lowerMin.toByteString(), upperMax.toByteString()));
nowPos = nowPos + step;
}
return rangeBuilder.build();
}
use of org.tikv.kvproto.Coprocessor.KeyRange in project tispark by pingcap.
the class RangeSplitter method createTask.
private void createTask(int startPos, int endPos, long tableId, List<Handle> handles, Pair<TiRegion, Metapb.Store> regionStorePair, ImmutableList.Builder<RegionTask> regionTasks) {
List<KeyRange> newKeyRanges = new ArrayList<>(endPos - startPos + 1);
Handle startHandle = handles.get(startPos);
Handle endHandle = startHandle;
for (int i = startPos + 1; i < endPos; i++) {
Handle curHandle = handles.get(i);
if (endHandle.next().equals(curHandle)) {
endHandle = curHandle;
} else {
newKeyRanges.add(makeCoprocRange(RowKey.toRowKey(tableId, startHandle).toByteString(), RowKey.toRowKey(tableId, endHandle.next()).toByteString()));
startHandle = curHandle;
endHandle = startHandle;
}
}
newKeyRanges.add(makeCoprocRange(RowKey.toRowKey(tableId, startHandle).toByteString(), RowKey.toRowKey(tableId, endHandle.next()).toByteString()));
regionTasks.add(new RegionTask(regionStorePair.first, regionStorePair.second, newKeyRanges));
}
use of org.tikv.kvproto.Coprocessor.KeyRange in project flink-cdc-connectors by ververica.
the class TableKeyRangeUtils method getTableKeyRanges.
public static List<KeyRange> getTableKeyRanges(final long tableId, final int num) {
Preconditions.checkArgument(num > 0, "Illegal value of num");
if (num == 1) {
return ImmutableList.of(getTableKeyRange(tableId));
}
final long delta = BigInteger.valueOf(Long.MAX_VALUE).subtract(BigInteger.valueOf(Long.MIN_VALUE + 1)).divide(BigInteger.valueOf(num)).longValueExact();
final ImmutableList.Builder<KeyRange> builder = ImmutableList.builder();
for (int i = 0; i < num; i++) {
final RowKey startKey = (i == 0) ? RowKey.createMin(tableId) : RowKey.toRowKey(tableId, Long.MIN_VALUE + delta * i);
final RowKey endKey = (i == num - 1) ? RowKey.createBeyondMax(tableId) : RowKey.toRowKey(tableId, Long.MIN_VALUE + delta * (i + 1));
builder.add(KeyRangeUtils.makeCoprocRange(startKey.toByteString(), endKey.toByteString()));
}
return builder.build();
}
Aggregations