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