Search in sources :

Example 1 with KeyRange

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;
}
Also used : HashMap(java.util.HashMap) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) ArrayList(java.util.ArrayList) IndexScanKeyRangeBuilder(org.tikv.common.key.IndexScanKeyRangeBuilder) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with KeyRange

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();
}
Also used : TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) ImmutableList(com.google.common.collect.ImmutableList) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) TiStore(org.tikv.common.region.TiStore) TiRegion(org.tikv.common.region.TiRegion) TLongArrayList(gnu.trove.list.array.TLongArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 3 with KeyRange

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();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) Key(org.tikv.common.key.Key) Key.toRawKey(org.tikv.common.key.Key.toRawKey)

Example 4 with KeyRange

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));
}
Also used : KeyRange(org.tikv.kvproto.Coprocessor.KeyRange) ArrayList(java.util.ArrayList) Handle(com.pingcap.tikv.key.Handle)

Example 5 with KeyRange

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();
}
Also used : RowKey(org.tikv.common.key.RowKey) ImmutableList(org.apache.flink.shaded.guava18.com.google.common.collect.ImmutableList) KeyRange(org.tikv.kvproto.Coprocessor.KeyRange)

Aggregations

KeyRange (org.tikv.kvproto.Coprocessor.KeyRange)15 ImmutableList (com.google.common.collect.ImmutableList)10 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)5 List (java.util.List)5 Key (org.tikv.common.key.Key)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Key (com.pingcap.tikv.key.Key)2 TiDAGRequest (com.pingcap.tikv.meta.TiDAGRequest)2 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 Key.toRawKey (org.tikv.common.key.Key.toRawKey)2 RowKey (org.tikv.common.key.RowKey)2 Metapb (org.tikv.kvproto.Metapb)2 ByteString (com.google.protobuf.ByteString)1 DAGRequest (com.pingcap.tidb.tipb.DAGRequest)1 SelectResponse (com.pingcap.tidb.tipb.SelectResponse)1 MockServerTest (com.pingcap.tikv.MockServerTest)1 CodecDataOutput (com.pingcap.tikv.codec.CodecDataOutput)1 Handle (com.pingcap.tikv.key.Handle)1