Java Code Examples for org.apache.hadoop.hbase.util.Bytes#compareTo()

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#compareTo() . 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: BaseScannerRegionObserver.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static void throwIfScanOutOfRegion(Scan scan, HRegion region) throws DoNotRetryIOException {
    boolean isLocalIndex = ScanUtil.isLocalIndex(scan);
    byte[] lowerInclusiveScanKey = scan.getStartRow();
    byte[] upperExclusiveScanKey = scan.getStopRow();
    byte[] lowerInclusiveRegionKey = region.getStartKey();
    byte[] upperExclusiveRegionKey = region.getEndKey();
    boolean isStaleRegionBoundaries;
    if (isLocalIndex) {
        byte[] expectedUpperRegionKey = scan.getAttribute(EXPECTED_UPPER_REGION_KEY);
        isStaleRegionBoundaries = expectedUpperRegionKey != null &&
                Bytes.compareTo(upperExclusiveRegionKey, expectedUpperRegionKey) != 0;
    } else {
        isStaleRegionBoundaries = Bytes.compareTo(lowerInclusiveScanKey, lowerInclusiveRegionKey) < 0 ||
                ( Bytes.compareTo(upperExclusiveScanKey, upperExclusiveRegionKey) > 0 && upperExclusiveRegionKey.length != 0);
    }
    if (isStaleRegionBoundaries) {
        Exception cause = new StaleRegionBoundaryCacheException(region.getRegionInfo().getTable().getNameAsString());
        throw new DoNotRetryIOException(cause.getMessage(), cause);
    }
}
 
Example 2
Source File: HDFSBucketRegionQueue.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public HDFSGatewayEventImpl getFromQueueOrBuffer(KeyToSeqNumObject key) {
  KeyToSeqNumObject result = events.ceilingKey(key);
  if (result != null && Bytes.compareTo(key.getRegionkey(), result.getRegionkey()) == 0) {
    
    // first try to fetch the buffered event to make it fast. 
    HDFSGatewayEventImpl evt = events.get(result);
    if (evt != NULL) {
      return evt;
    }
    // now try to fetch the event from the queue region
    evt = (HDFSGatewayEventImpl) getNoLRU(result.getSeqNum(), true, false, false);
    if (evt != null) {
      return evt;
    }
    
    // try to fetch again from the buffered events to avoid a race between 
    // item deletion and the above two statements. 
    evt = events.get(result);
    if (evt != NULL) {
      return evt;
    }
    
  }
  return null;
}
 
Example 3
Source File: PBinary.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public int compareTo(Object lhs, Object rhs, PDataType rhsType) {
  if (lhs == null && rhs == null) {
    return 0;
  } else if (lhs == null) {
    return -1;
  } else if (rhs == null) {
    return 1;
  }
  if (equalsAny(rhsType, PVarbinary.INSTANCE, PBinary.INSTANCE)) {
    return Bytes.compareTo((byte[]) lhs, (byte[]) rhs);
  } else {
    byte[] rhsBytes = rhsType.toBytes(rhs);
    return Bytes.compareTo((byte[]) lhs, rhsBytes);
  }
}
 
Example 4
Source File: TestUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static void dumpIndexStatus(Connection conn, String indexName) throws IOException, SQLException {
    try (Table table = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES)) { 
        System.out.println("************ dumping index status for " + indexName + " **************");
        Scan s = new Scan();
        s.setRaw(true);
        s.setMaxVersions();
        byte[] startRow = SchemaUtil.getTableKeyFromFullName(indexName);
        s.setStartRow(startRow);
        s.setStopRow(ByteUtil.nextKey(ByteUtil.concat(startRow, QueryConstants.SEPARATOR_BYTE_ARRAY)));
        try (ResultScanner scanner = table.getScanner(s)) {
            Result result = null;
            while ((result = scanner.next()) != null) {
                CellScanner cellScanner = result.cellScanner();
                Cell current = null;
                while (cellScanner.advance()) {
                    current = cellScanner.current();
                    if (Bytes.compareTo(current.getQualifierArray(), current.getQualifierOffset(), current.getQualifierLength(), PhoenixDatabaseMetaData.INDEX_STATE_BYTES, 0, PhoenixDatabaseMetaData.INDEX_STATE_BYTES.length) == 0) {
                        System.out.println(current.getTimestamp() + "/INDEX_STATE=" + PIndexState.fromSerializedValue(current.getValueArray()[current.getValueOffset()]));
                    }
                }
            }
        }
        System.out.println("-----------------------------------------------");
}
}
 
Example 5
Source File: MetaDataEndpointImpl.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static MetaDataMutationResult checkTableKeyInRegion(byte[] key, HRegion region) {
    byte[] startKey = region.getStartKey();
    byte[] endKey = region.getEndKey();
    if (Bytes.compareTo(startKey, key) <= 0
            && (Bytes.compareTo(HConstants.LAST_ROW, endKey) == 0 || Bytes.compareTo(key,
                endKey) < 0)) {
        return null; // normal case;
    }
    return new MetaDataMutationResult(MutationCode.TABLE_NOT_IN_REGION,
            EnvironmentEdgeManager.currentTimeMillis(), null);
}
 
Example 6
Source File: CellComparatorImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
static int compareRows(final Cell left, int leftRowLength, final Cell right, int rightRowLength) {
  // left and right can be exactly the same at the beginning of a row
  if (left == right) {
    return 0;
  }
  if (left instanceof ByteBufferExtendedCell && right instanceof ByteBufferExtendedCell) {
    return ByteBufferUtils.compareTo(((ByteBufferExtendedCell) left).getRowByteBuffer(),
        ((ByteBufferExtendedCell) left).getRowPosition(), leftRowLength,
        ((ByteBufferExtendedCell) right).getRowByteBuffer(),
        ((ByteBufferExtendedCell) right).getRowPosition(), rightRowLength);
  }
  if (left instanceof ByteBufferExtendedCell) {
    return ByteBufferUtils.compareTo(((ByteBufferExtendedCell) left).getRowByteBuffer(),
        ((ByteBufferExtendedCell) left).getRowPosition(), leftRowLength,
        right.getRowArray(), right.getRowOffset(), rightRowLength);
  }
  if (right instanceof ByteBufferExtendedCell) {
    // Notice how we flip the order of the compare here. We used to negate the return value but
    // see what FindBugs says
    // http://findbugs.sourceforge.net/bugDescriptions.html#RV_NEGATING_RESULT_OF_COMPARETO
    // It suggest flipping the order to get same effect and 'safer'.
    return ByteBufferUtils.compareTo(left.getRowArray(), left.getRowOffset(), leftRowLength,
        ((ByteBufferExtendedCell)right).getRowByteBuffer(),
        ((ByteBufferExtendedCell)right).getRowPosition(), rightRowLength);
  }
  return Bytes.compareTo(left.getRowArray(), left.getRowOffset(), left.getRowLength(),
      right.getRowArray(), right.getRowOffset(), right.getRowLength());
}
 
Example 7
Source File: MockHTable.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
private boolean check(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value) {
    NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> rowData = data.get(row);
    if (value == null)
        return rowData == null ||
                !rowData.containsKey(family) ||
                !rowData.get(family).containsKey(qualifier);
    else if (rowData != null &&
            rowData.containsKey(family) &&
            rowData.get(family).containsKey(qualifier) &&
            !rowData.get(family).get(qualifier).isEmpty()) {

        byte[] oldValue = rowData.get(family).get(qualifier).lastEntry().getValue();
        int compareResult = Bytes.compareTo(value, oldValue);
        switch (compareOp) {
            case LESS:
                return compareResult < 0;
            case LESS_OR_EQUAL:
                return compareResult <= 0;
            case EQUAL:
                return compareResult == 0;
            case NOT_EQUAL:
                return compareResult != 0;
            case GREATER_OR_EQUAL:
                return compareResult >= 0;
            case GREATER:
                return compareResult > 0;
            default:
                throw new RuntimeException("Unknown Compare op " + compareOp.name());
        }
    } else {
        return false;
    }
}
 
Example 8
Source File: RunMatchFilter.java    From hraven with Apache License 2.0 5 votes vote down vote up
@Override
public boolean filterRowKey(byte[] buffer, int offset, int length) {
  // TODO: don't copy the byte[]
  byte[] rowkey = new byte[length];
  System.arraycopy(buffer, offset, rowkey, 0, length);
  List<ByteUtil.Range> splits = ByteUtil.splitRanges(rowkey, Constants.SEP_BYTES);
  if (splits.size() < 4) {
    // invalid row key
    return true;
  }
  ByteUtil.Range appRange = splits.get(1);
  int appCompare = Bytes.compareTo(appId, 0, appId.length,
      rowkey, appRange.start(), appRange.length());
  if (appCompare != 0) {
    return false;
  }
  ByteUtil.Range runRange = splits.get(2);
  int runLength = runRange.length();
  if (lastRunId == null ||
      Bytes.compareTo(lastRunId, 0, lastRunId.length,
          rowkey, runRange.start(), runLength) != 0) {
    lastRunId = new byte[runLength];
    System.arraycopy(rowkey, runRange.start(), lastRunId, 0, runLength);
    seenCount++;
  }

  return seenCount > maxCount;
}
 
Example 9
Source File: BufferedDataBlockEncoder.java    From hbase with Apache License 2.0 5 votes vote down vote up
/********************* common prefixes *************************/
// Having this as static is fine but if META is having DBE then we should
// change this.
public static int compareCommonRowPrefix(Cell left, Cell right, int rowCommonPrefix) {
  return Bytes.compareTo(left.getRowArray(), left.getRowOffset() + rowCommonPrefix,
      left.getRowLength() - rowCommonPrefix, right.getRowArray(), right.getRowOffset()
          + rowCommonPrefix, right.getRowLength() - rowCommonPrefix);
}
 
Example 10
Source File: BaseScannerRegionObserver.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void throwIfScanOutOfRegion(Scan scan, Region region) throws DoNotRetryIOException {
    boolean isLocalIndex = ScanUtil.isLocalIndex(scan);
    byte[] lowerInclusiveScanKey = scan.getStartRow();
    byte[] upperExclusiveScanKey = scan.getStopRow();
    byte[] lowerInclusiveRegionKey = region.getRegionInfo().getStartKey();
    byte[] upperExclusiveRegionKey = region.getRegionInfo().getEndKey();
    boolean isStaleRegionBoundaries;
    if (isLocalIndex) {
        // For local indexes we have to abort any scan that was open during a split.
        // We detect that condition as follows:
        // 1. The scanner's stop row has to always match the region's end key.
        // 2. Phoenix sets the SCAN_ACTUAL_START_ROW attribute to the scan's original start row
        //    We cannot directly compare that with the region's start key, but can enforce that
        //    the original start row still falls within the new region.
        byte[] expectedUpperRegionKey =
                scan.getAttribute(EXPECTED_UPPER_REGION_KEY) == null ? scan.getStopRow() : scan
                        .getAttribute(EXPECTED_UPPER_REGION_KEY);

        byte[] actualStartRow = scan.getAttribute(SCAN_ACTUAL_START_ROW);
        isStaleRegionBoundaries = (expectedUpperRegionKey != null &&
                Bytes.compareTo(upperExclusiveRegionKey, expectedUpperRegionKey) != 0) || 
                (actualStartRow != null && Bytes.compareTo(actualStartRow, lowerInclusiveRegionKey) < 0);
    } else {
        isStaleRegionBoundaries = Bytes.compareTo(lowerInclusiveScanKey, lowerInclusiveRegionKey) < 0 ||
                ( Bytes.compareTo(upperExclusiveScanKey, upperExclusiveRegionKey) > 0 && upperExclusiveRegionKey.length != 0) ||
                (upperExclusiveRegionKey.length != 0 && upperExclusiveScanKey.length == 0);
    }
    if (isStaleRegionBoundaries) {
        Exception cause = new StaleRegionBoundaryCacheException(region.getRegionInfo().getTable().getNameAsString());
        throw new DoNotRetryIOException(cause.getMessage(), cause);
    }
    if(isLocalIndex) {
        ScanUtil.setupLocalIndexScan(scan);
    }
}
 
Example 11
Source File: NonTxIndexBuilderTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void assertContains(Collection<Pair<Mutation, byte[]>> indexUpdates,
        final long mutationTs, final byte[] row, final Type cellType, final byte[] fam,
        final byte[] qual, final long cellTs) {
    Predicate<Pair<Mutation, byte[]>> hasCellPredicate =
            new Predicate<Pair<Mutation, byte[]>>() {
                @Override
                public boolean apply(Pair<Mutation, byte[]> input) {
                    assertEquals(TEST_TABLE_INDEX_STRING, Bytes.toString(input.getSecond()));
                    Mutation mutation = input.getFirst();
                    if (mutationTs == mutation.getTimeStamp()) {
                        NavigableMap<byte[], List<Cell>> familyCellMap =
                                mutation.getFamilyCellMap();
                        Cell updateCell = familyCellMap.get(fam).get(0);
                        if (cellType == KeyValue.Type.codeToType(updateCell.getTypeByte())
                                && Bytes.compareTo(fam, CellUtil.cloneFamily(updateCell)) == 0
                                && Bytes.compareTo(qual,
                                    CellUtil.cloneQualifier(updateCell)) == 0
                                && cellTs == updateCell.getTimestamp()) {
                            return true;
                        }
                    }
                    return false;
                }
            };
    Optional<Pair<Mutation, byte[]>> tryFind =
            Iterables.tryFind(indexUpdates, hasCellPredicate);
    assertTrue(tryFind.isPresent());
}
 
Example 12
Source File: PTableImpl.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void removeIfPresent(Mutation m, byte[] family, byte[] qualifier) {
    Map<byte[],List<Cell>> familyMap = m.getFamilyCellMap();
    List<Cell> kvs = familyMap.get(family);
    if (kvs != null) {
        Iterator<Cell> iterator = kvs.iterator();
        while (iterator.hasNext()) {
            Cell kv = iterator.next();
            if (Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(),
                  qualifier, 0, qualifier.length) == 0) {
                iterator.remove();
                break;
            }
        }
    }
}
 
Example 13
Source File: ConnectionUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
static boolean noMoreResultsForScan(Scan scan, RegionInfo info) {
  if (isEmptyStopRow(info.getEndKey())) {
    return true;
  }
  if (isEmptyStopRow(scan.getStopRow())) {
    return false;
  }
  int c = Bytes.compareTo(info.getEndKey(), scan.getStopRow());
  // 1. if our stop row is less than the endKey of the region
  // 2. if our stop row is equal to the endKey of the region and we do not include the stop row
  // for scan.
  return c > 0 || (c == 0 && !scan.includeStopRow());
}
 
Example 14
Source File: ImmutableBytesPtr.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    ImmutableBytesPtr that = (ImmutableBytesPtr)obj;
    if (this.hashCode != that.hashCode) return false;
    if (Bytes.compareTo(this.get(), this.getOffset(), this.getLength(), that.get(), that.getOffset(), that.getLength()) != 0) return false;
    return true;
}
 
Example 15
Source File: MetaDataUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static Cell getCell(List<Cell> cells, byte[] cq) {
    for (Cell cell : cells) {
        if (Bytes.compareTo(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength(), cq, 0, cq.length) == 0) {
            return cell;
        }
    }
    return null;
}
 
Example 16
Source File: FirstLastValueServerAggregator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private boolean removeLastElement(byte[] currentOrder, byte[] lowestKey, int sortOrderInt) {
    if (Bytes.compareTo(currentOrder, lowestKey) * sortOrderInt >= 0) {
        if (topValues.get(lowestKey).size() == 1) {
            topValues.remove(lowestKey);
        } else {
            topValues.get(lowestKey).pollFirst();
        }
        return true;
    }
    return false;
}
 
Example 17
Source File: KeyValue.java    From hbase with Apache License 2.0 4 votes vote down vote up
protected int compareFamilies(final byte[] left, final int loffset, final int lfamilylength,
    final byte[] right, final int roffset, final int rfamilylength) {
  int diff = Bytes.compareTo(left, loffset, lfamilylength, right, roffset, rfamilylength);
  return diff;
}
 
Example 18
Source File: MultiRowRangeFilter.java    From hbase with Apache License 2.0 4 votes vote down vote up
public boolean isValid() {
  return Bytes.equals(startRow, HConstants.EMPTY_BYTE_ARRAY)
      || Bytes.equals(stopRow, HConstants.EMPTY_BYTE_ARRAY)
      || Bytes.compareTo(startRow, stopRow) < 0
      || (Bytes.compareTo(startRow, stopRow) == 0 && stopRowInclusive == true);
}
 
Example 19
Source File: PhoenixIndexBuilder.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public static byte[] combineOnDupKey(byte[] oldOnDupKeyBytes, byte[] newOnDupKeyBytes) {
    // If old ON DUPLICATE KEY is null, then the new value always takes effect
    // If new ON DUPLICATE KEY is null, then reset back to null
    if (oldOnDupKeyBytes == null || newOnDupKeyBytes == null) {
        if (newOnDupKeyBytes == null) {
            return newOnDupKeyBytes;
        }
        return doNotSkipFirstOnDupKey(newOnDupKeyBytes);
    }
    // If the new UPSERT VALUES statement has an ON DUPLICATE KEY IGNORE, and there
    // is an already existing UPSERT VALUES statement with an ON DUPLICATE KEY clause,
    // then we can just keep that one as the new one has no impact.
    if (isDupKeyIgnore(newOnDupKeyBytes)) {
        return oldOnDupKeyBytes;
    }
    boolean isOldDupKeyIgnore = isDupKeyIgnore(oldOnDupKeyBytes);
    try (TrustedByteArrayOutputStream stream = new TrustedByteArrayOutputStream(Math.max(0, oldOnDupKeyBytes.length - ON_DUP_KEY_HEADER_BYTE_SIZE) + newOnDupKeyBytes.length); 
            ByteArrayInputStream oldStream = new ByteArrayInputStream(oldOnDupKeyBytes); 
            ByteArrayInputStream newStream = new ByteArrayInputStream(newOnDupKeyBytes);
            DataOutputStream output = new DataOutputStream(stream);
            DataInputStream oldInput = new DataInputStream(oldStream);
            DataInputStream newInput = new DataInputStream(newStream)) {
        
        boolean execute1 = oldInput.readBoolean();
        newInput.readBoolean(); // ignore
        int repeating2 = newInput.readShort();
        if (isOldDupKeyIgnore) {
            output.writeBoolean(false); // Will force subsequent ON DUPLICATE KEY UPDATE statement to execute
            output.writeShort(repeating2);
            output.write(newOnDupKeyBytes, ON_DUP_KEY_HEADER_BYTE_SIZE, newOnDupKeyBytes.length - ON_DUP_KEY_HEADER_BYTE_SIZE);
        } else {
            int repeating1 = oldInput.readShort();
            if (Bytes.compareTo(
                oldOnDupKeyBytes, ON_DUP_KEY_HEADER_BYTE_SIZE, oldOnDupKeyBytes.length - ON_DUP_KEY_HEADER_BYTE_SIZE, 
                newOnDupKeyBytes, Bytes.SIZEOF_SHORT + Bytes.SIZEOF_BOOLEAN, newOnDupKeyBytes.length - ON_DUP_KEY_HEADER_BYTE_SIZE) == 0) {
            // If both old and new ON DUPLICATE KEY UPDATE clauses match,
            // reduce the size of data we're sending over the wire.
            // TODO: optimization size of RPC more.
            output.writeBoolean(execute1);
            output.writeShort(repeating1 + repeating2);
            output.write(newOnDupKeyBytes, ON_DUP_KEY_HEADER_BYTE_SIZE, newOnDupKeyBytes.length - ON_DUP_KEY_HEADER_BYTE_SIZE);
            } else {
                output.writeBoolean(execute1);
                output.writeShort(repeating1); // retain first ON DUPLICATE KEY UPDATE having repeated
                output.write(oldOnDupKeyBytes, ON_DUP_KEY_HEADER_BYTE_SIZE, oldOnDupKeyBytes.length - ON_DUP_KEY_HEADER_BYTE_SIZE);
                // If the new ON DUPLICATE KEY UPDATE was repeating, we need to write it multiple times as only the first
                // statement is effected by the repeating amount
                for (int i = 0; i < repeating2; i++) {
                    output.write(newOnDupKeyBytes, ON_DUP_KEY_HEADER_BYTE_SIZE, newOnDupKeyBytes.length - ON_DUP_KEY_HEADER_BYTE_SIZE);
                }
            }
        }
        return stream.toByteArray();
    } catch (IOException e) { // Shouldn't be possible with ByteInput/Output streams
        throw new RuntimeException(e);
    }
}
 
Example 20
Source File: CellComparatorImpl.java    From hbase with Apache License 2.0 3 votes vote down vote up
/**
 * Compares the row part of the cell with a simple plain byte[] like the
 * stopRow in Scan. This should be used with context where for hbase:meta
 * cells the {{@link #META_COMPARATOR} should be used
 *
 * @param left
 *          the cell to be compared
 * @param right
 *          the kv serialized byte[] to be compared with
 * @param roffset
 *          the offset in the byte[]
 * @param rlength
 *          the length in the byte[]
 * @return 0 if both cell and the byte[] are equal, 1 if the cell is bigger
 *         than byte[], -1 otherwise
 */
@Override
public int compareRows(Cell left, byte[] right, int roffset, int rlength) {
  if (left instanceof ByteBufferExtendedCell) {
    return ByteBufferUtils.compareTo(((ByteBufferExtendedCell) left).getRowByteBuffer(),
        ((ByteBufferExtendedCell) left).getRowPosition(), left.getRowLength(), right,
        roffset, rlength);
  }
  return Bytes.compareTo(left.getRowArray(), left.getRowOffset(), left.getRowLength(), right,
      roffset, rlength);
}