Java Code Examples for gnu.trove.list.array.TLongArrayList#get()

The following examples show how to use gnu.trove.list.array.TLongArrayList#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RangeSplitter.java    From tikv-client-lib-java with Apache License 2.0 5 votes vote down vote up
private void createTask(
    int startPos,
    int endPos,
    long tableId,
    TLongArrayList handles,
    Pair<TiRegion, Metapb.Store> regionStorePair,
    ImmutableList.Builder<RegionTask> regionTasks) {
  TiRegion region = regionStorePair.first;
  Store store = regionStorePair.second;
  List<KeyRange> newKeyRanges = new ArrayList<>(endPos - startPos + 1);
  long startHandle = handles.get(startPos);
  long endHandle = startHandle;
  for (int i = startPos + 1; i < endPos; i++) {
    long curHandle = handles.get(i);
    if (endHandle + 1 == curHandle) {
      endHandle = curHandle;
    } else {
      newKeyRanges.add(KeyRangeUtils.makeCoprocRangeWithHandle(
          tableId,
          startHandle,
          endHandle + 1));
      startHandle = curHandle;
      endHandle = startHandle;
    }
  }
  newKeyRanges.add(KeyRangeUtils.makeCoprocRangeWithHandle(tableId, startHandle, endHandle + 1));
  regionTasks.add(new RegionTask(region, store, newKeyRanges));
}
 
Example 2
Source File: RangeSplitter.java    From tikv-client-lib-java with Apache License 2.0 4 votes vote down vote up
public List<RegionTask> splitHandlesByRegion(long tableId, TLongArrayList handles) {
  // Max value for current index handle range
  ImmutableList.Builder<RegionTask> regionTasks = ImmutableList.builder();
  handles.sort();

  int startPos = 0;
  TableCodec.DecodeResult decodeResult = new TableCodec.DecodeResult();
  while (startPos < handles.size()) {
    long curHandle = handles.get(startPos);
    byte[] key = TableCodec.encodeRowKeyWithHandleBytes(tableId, curHandle);
    Pair<TiRegion, Metapb.Store> regionStorePair = regionManager.getRegionStorePairByKey(ByteString.copyFrom(key));
    byte[] endKey = regionStorePair.first.getEndKey().toByteArray();
    TableCodec.tryDecodeRowKey(tableId, endKey, decodeResult);
    if (decodeResult.status == Status.MIN) {
      throw new TiClientInternalException("EndKey is less than current rowKey");
    } else if (decodeResult.status == Status.MAX || decodeResult.status == Status.UNKNOWN_INF) {
      createTask(startPos, handles.size(), tableId, handles, regionStorePair, regionTasks);
      break;
    }

    // Region range is a close-open range
    // If region end key match exactly or slightly less than a handle,
    // that handle should be excluded from current region
    // If region end key is greater than the handle, that handle should be included
    long regionEndHandle = decodeResult.handle;
    int pos = handles.binarySearch(regionEndHandle, startPos, handles.size());

    if (pos < 0) {
      // not found in handles, pos is the next greater pos
      // [startPos, pos) all included
      pos = -(pos + 1);
    } else if (decodeResult.status == Status.GREATER) {
      // found handle and then further consider decode status
      // End key decode to a value v: regionEndHandle < v < regionEndHandle + 1
      // handle at pos included
      pos ++;
    }
    createTask(startPos, pos, tableId, handles, regionStorePair, regionTasks);
    // pos equals to start leads to an dead loop
    // startPos and its handle is used for searching region in PD.
    // The returning close-open range should at least include startPos's handle
    // so only if PD error and startPos is not included in current region then startPos == pos
    if (startPos >= pos) {
      throw new TiClientInternalException("searchKey is not included in region returned by PD");
    }
    startPos = pos;
  }
  return regionTasks.build();
}