Search in sources :

Example 1 with RowKey

use of com.pingcap.tikv.key.RowKey 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;
}
Also used : RowKey(com.pingcap.tikv.key.RowKey) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) RowKey(com.pingcap.tikv.key.RowKey) IndexKey(com.pingcap.tikv.key.IndexKey) Key(com.pingcap.tikv.key.Key)

Example 2 with RowKey

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

the class RangeSplitter method groupByAndSortHandlesByRegionId.

/**
 * Group by a list of handles by the handles' region, handles will be sorted.
 *
 * @param tableId Table id used for the handle
 * @param handles Handle list
 * @return <Region, HandleList> map
 */
public Map<Pair<TiRegion, Metapb.Store>, List<Handle>> groupByAndSortHandlesByRegionId(long tableId, List<Handle> handles) {
    TLongObjectHashMap<List<Handle>> regionHandles = new TLongObjectHashMap<>();
    TLongObjectHashMap<Pair<TiRegion, Metapb.Store>> idToRegionStorePair = new TLongObjectHashMap<>();
    Map<Pair<TiRegion, Metapb.Store>, List<Handle>> result = new HashMap<>();
    handles.sort(Handle::compare);
    byte[] endKey = null;
    TiRegion curRegion = null;
    List<Handle> handlesInCurRegion = new ArrayList<>();
    for (Handle curHandle : handles) {
        RowKey key = RowKey.toRowKey(tableId, curHandle);
        if (endKey == null || (endKey.length != 0 && FastByteComparisons.compareTo(key.getBytes(), endKey) >= 0)) {
            if (curRegion != null) {
                regionHandles.put(curRegion.getId(), handlesInCurRegion);
                handlesInCurRegion = new ArrayList<>();
            }
            Pair<TiRegion, Metapb.Store> regionStorePair = regionManager.getRegionStorePairByKey(ByteString.copyFrom(key.getBytes()));
            curRegion = regionStorePair.first;
            idToRegionStorePair.put(curRegion.getId(), regionStorePair);
            endKey = curRegion.getEndKey().toByteArray();
        }
        handlesInCurRegion.add(curHandle);
    }
    if (!handlesInCurRegion.isEmpty()) {
        regionHandles.put(curRegion.getId(), handlesInCurRegion);
    }
    regionHandles.forEachEntry((k, v) -> {
        Pair<TiRegion, Metapb.Store> regionStorePair = idToRegionStorePair.get(k);
        result.put(regionStorePair, v);
        return true;
    });
    return result;
}
Also used : HashMap(java.util.HashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) RowKey(com.pingcap.tikv.key.RowKey) ArrayList(java.util.ArrayList) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) Handle(com.pingcap.tikv.key.Handle) TiRegion(com.pingcap.tikv.region.TiRegion) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Metapb(org.tikv.kvproto.Metapb)

Aggregations

RowKey (com.pingcap.tikv.key.RowKey)2 TiRegion (com.pingcap.tikv.region.TiRegion)2 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 Handle (com.pingcap.tikv.key.Handle)1 IndexKey (com.pingcap.tikv.key.IndexKey)1 Key (com.pingcap.tikv.key.Key)1 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Metapb (org.tikv.kvproto.Metapb)1