Example usage for java.util Arrays binarySearch

List of usage examples for java.util Arrays binarySearch

Introduction

In this page you can find the example usage for java.util Arrays binarySearch.

Prototype

public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) 

Source Link

Document

Searches the specified array for the specified object using the binary search algorithm.

Usage

From source file:org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles.java

/**
 * Attempt to assign the given load queue item into its target region group.
 * If the hfile boundary no longer fits into a region, physically splits
 * the hfile such that the new bottom half will fit and returns the list of
 * LQI's corresponding to the resultant hfiles.
 *
 * protected for testing//from  w ww  .  j av a2  s .  c o m
 * @throws IOException
 */
protected List<LoadQueueItem> groupOrSplit(Multimap<ByteBuffer, LoadQueueItem> regionGroups,
        final LoadQueueItem item, final HTable table, final Pair<byte[][], byte[][]> startEndKeys)
        throws IOException {
    final Path hfilePath = item.hfilePath;
    HFile.Reader hfr = HFile.createReader(fs, hfilePath, new CacheConfig(getConf()), getConf());
    final byte[] first, last;
    try {
        hfr.loadFileInfo();
        first = hfr.getFirstRowKey();
        last = hfr.getLastRowKey();
    } finally {
        hfr.close();
    }

    LOG.info("Trying to load hfile=" + hfilePath + " first=" + Bytes.toStringBinary(first) + " last="
            + Bytes.toStringBinary(last));
    if (first == null || last == null) {
        assert first == null && last == null;
        // TODO what if this is due to a bad HFile?
        LOG.info("hfile " + hfilePath + " has no entries, skipping");
        return null;
    }
    if (Bytes.compareTo(first, last) > 0) {
        throw new IllegalArgumentException(
                "Invalid range: " + Bytes.toStringBinary(first) + " > " + Bytes.toStringBinary(last));
    }
    int idx = Arrays.binarySearch(startEndKeys.getFirst(), first, Bytes.BYTES_COMPARATOR);
    if (idx < 0) {
        // not on boundary, returns -(insertion index).  Calculate region it
        // would be in.
        idx = -(idx + 1) - 1;
    }
    final int indexForCallable = idx;

    /**
     * we can consider there is a region hole in following conditions. 1) if idx < 0,then first
     * region info is lost. 2) if the endkey of a region is not equal to the startkey of the next
     * region. 3) if the endkey of the last region is not empty.
     */
    if (indexForCallable < 0) {
        throw new IOException("The first region info for table " + Bytes.toString(table.getTableName())
                + " cann't be found in hbase:meta.Please use hbck tool to fix it first.");
    } else if ((indexForCallable == startEndKeys.getFirst().length - 1)
            && !Bytes.equals(startEndKeys.getSecond()[indexForCallable], HConstants.EMPTY_BYTE_ARRAY)) {
        throw new IOException("The last region info for table " + Bytes.toString(table.getTableName())
                + " cann't be found in hbase:meta.Please use hbck tool to fix it first.");
    } else if (indexForCallable + 1 < startEndKeys.getFirst().length
            && !(Bytes.compareTo(startEndKeys.getSecond()[indexForCallable],
                    startEndKeys.getFirst()[indexForCallable + 1]) == 0)) {
        throw new IOException("The endkey of one region for table " + Bytes.toString(table.getTableName())
                + " is not equal to the startkey of the next region in hbase:meta."
                + "Please use hbck tool to fix it first.");
    }

    boolean lastKeyInRange = Bytes.compareTo(last, startEndKeys.getSecond()[idx]) < 0
            || Bytes.equals(startEndKeys.getSecond()[idx], HConstants.EMPTY_BYTE_ARRAY);
    if (!lastKeyInRange) {
        List<LoadQueueItem> lqis = splitStoreFile(item, table, startEndKeys.getFirst()[indexForCallable],
                startEndKeys.getSecond()[indexForCallable]);
        return lqis;
    }

    // group regions.
    regionGroups.put(ByteBuffer.wrap(startEndKeys.getFirst()[idx]), item);
    return null;
}

From source file:org.apache.hadoop.hbase.regionserver.StripeStoreFileManager.java

/**
 * Finds the stripe index by end row.//from   w  w w  . j a va 2s .c  o  m
 */
private final int findStripeIndexByEndRow(byte[] endRow) {
    assert !isInvalid(endRow);
    if (isOpen(endRow))
        return state.stripeEndRows.length;
    return Arrays.binarySearch(state.stripeEndRows, endRow, Bytes.BYTES_COMPARATOR);
}

From source file:org.apache.hadoop.hbase.regionserver.StripeStoreFileManager.java

/**
 * Finds the stripe index for the stripe containing a row provided externally for get/scan.
 *///from   w w  w  .j  av a2s . co  m
private final int findStripeForRow(byte[] row, boolean isStart) {
    if (isStart && row == HConstants.EMPTY_START_ROW)
        return 0;
    if (!isStart && row == HConstants.EMPTY_END_ROW)
        return state.stripeFiles.size() - 1;
    // If there's an exact match below, a stripe ends at "row". Stripe right boundary is
    // exclusive, so that means the row is in the next stripe; thus, we need to add one to index.
    // If there's no match, the return value of binarySearch is (-(insertion point) - 1), where
    // insertion point is the index of the next greater element, or list size if none. The
    // insertion point happens to be exactly what we need, so we need to add one to the result.
    return Math.abs(Arrays.binarySearch(state.stripeEndRows, row, Bytes.BYTES_COMPARATOR) + 1);
}

From source file:org.apache.hadoop.hbase.tool.LoadIncrementalHFiles.java

/**
 * Attempt to assign the given load queue item into its target region group. If the hfile boundary
 * no longer fits into a region, physically splits the hfile such that the new bottom half will
 * fit and returns the list of LQI's corresponding to the resultant hfiles.
 * <p>/*from w w  w  .  ja  v a  2s  .  co  m*/
 * protected for testing
 * @throws IOException if an IO failure is encountered
 */
@VisibleForTesting
protected Pair<List<LoadQueueItem>, String> groupOrSplit(Multimap<ByteBuffer, LoadQueueItem> regionGroups,
        final LoadQueueItem item, final Table table, final Pair<byte[][], byte[][]> startEndKeys)
        throws IOException {
    Path hfilePath = item.getFilePath();
    Optional<byte[]> first, last;
    try (HFile.Reader hfr = HFile.createReader(hfilePath.getFileSystem(getConf()), hfilePath,
            CacheConfig.DISABLED, true, getConf())) {
        hfr.loadFileInfo();
        first = hfr.getFirstRowKey();
        last = hfr.getLastRowKey();
    } catch (FileNotFoundException fnfe) {
        LOG.debug("encountered", fnfe);
        return new Pair<>(null, hfilePath.getName());
    }

    LOG.info("Trying to load hfile=" + hfilePath + " first=" + first.map(Bytes::toStringBinary) + " last="
            + last.map(Bytes::toStringBinary));
    if (!first.isPresent() || !last.isPresent()) {
        assert !first.isPresent() && !last.isPresent();
        // TODO what if this is due to a bad HFile?
        LOG.info("hfile " + hfilePath + " has no entries, skipping");
        return null;
    }
    if (Bytes.compareTo(first.get(), last.get()) > 0) {
        throw new IllegalArgumentException("Invalid range: " + Bytes.toStringBinary(first.get()) + " > "
                + Bytes.toStringBinary(last.get()));
    }
    int idx = Arrays.binarySearch(startEndKeys.getFirst(), first.get(), Bytes.BYTES_COMPARATOR);
    if (idx < 0) {
        // not on boundary, returns -(insertion index). Calculate region it
        // would be in.
        idx = -(idx + 1) - 1;
    }
    int indexForCallable = idx;

    /**
     * we can consider there is a region hole in following conditions. 1) if idx < 0,then first
     * region info is lost. 2) if the endkey of a region is not equal to the startkey of the next
     * region. 3) if the endkey of the last region is not empty.
     */
    if (indexForCallable < 0) {
        throw new IOException("The first region info for table " + table.getName()
                + " can't be found in hbase:meta.Please use hbck tool to fix it first.");
    } else if ((indexForCallable == startEndKeys.getFirst().length - 1)
            && !Bytes.equals(startEndKeys.getSecond()[indexForCallable], HConstants.EMPTY_BYTE_ARRAY)) {
        throw new IOException("The last region info for table " + table.getName()
                + " can't be found in hbase:meta.Please use hbck tool to fix it first.");
    } else if (indexForCallable + 1 < startEndKeys.getFirst().length
            && !(Bytes.compareTo(startEndKeys.getSecond()[indexForCallable],
                    startEndKeys.getFirst()[indexForCallable + 1]) == 0)) {
        throw new IOException("The endkey of one region for table " + table.getName()
                + " is not equal to the startkey of the next region in hbase:meta."
                + "Please use hbck tool to fix it first.");
    }

    boolean lastKeyInRange = Bytes.compareTo(last.get(), startEndKeys.getSecond()[idx]) < 0
            || Bytes.equals(startEndKeys.getSecond()[idx], HConstants.EMPTY_BYTE_ARRAY);
    if (!lastKeyInRange) {
        List<LoadQueueItem> lqis = splitStoreFile(item, table, startEndKeys.getFirst()[indexForCallable],
                startEndKeys.getSecond()[indexForCallable]);
        return new Pair<>(lqis, null);
    }

    // group regions.
    regionGroups.put(ByteBuffer.wrap(startEndKeys.getFirst()[idx]), item);
    return null;
}

From source file:com.liferay.portal.util.PortalUtil.java

private boolean _isSystemGroup(String groupName) {
    if (groupName == null) {
        return false;
    }/*from   w  w  w.  j a v  a 2 s .  c om*/

    groupName = groupName.trim();

    int pos = Arrays.binarySearch(_sortedSystemGroups, groupName, new StringComparator());

    if (pos >= 0) {
        return true;
    } else {
        return false;
    }
}

From source file:com.liferay.portal.util.PortalUtil.java

private boolean _isSystemRole(String roleName) {
    if (roleName == null) {
        return false;
    }/*  w w  w  . j a  v a 2s  . c  om*/

    roleName = roleName.trim();

    int pos = Arrays.binarySearch(_sortedSystemRoles, roleName, new StringComparator());

    if (pos >= 0) {
        return true;
    } else {
        return false;
    }
}

From source file:org.broadleafcommerce.openadmin.server.service.persistence.module.provider.BasicFieldPersistenceProvider.java

@Override
public FieldProviderResponse addSearchMapping(AddSearchMappingRequest addSearchMappingRequest,
        List<FilterMapping> filterMappings) {
    if (!canHandleSearchMapping(addSearchMappingRequest, filterMappings)) {
        return FieldProviderResponse.NOT_HANDLED;
    }//from  www. j  a  v  a2s  . com
    Class clazz;
    try {
        clazz = Class.forName(addSearchMappingRequest.getMergedProperties()
                .get(addSearchMappingRequest.getPropertyName()).getInheritedFromType());
    } catch (ClassNotFoundException e) {
        throw new PersistenceException(e);
    }
    Field field = addSearchMappingRequest.getFieldManager().getField(clazz,
            addSearchMappingRequest.getPropertyName());
    Class<?> targetType = null;
    if (field != null) {
        targetType = field.getType();
    }
    BasicFieldMetadata metadata = (BasicFieldMetadata) addSearchMappingRequest.getMergedProperties()
            .get(addSearchMappingRequest.getPropertyName());

    FilterAndSortCriteria fasc = addSearchMappingRequest.getRequestedCto()
            .get(addSearchMappingRequest.getPropertyName());

    FilterMapping filterMapping = new FilterMapping().withInheritedFromClass(clazz)
            .withFullPropertyName(addSearchMappingRequest.getPropertyName())
            .withFilterValues(fasc.getFilterValues()).withSortDirection(fasc.getSortDirection());
    filterMappings.add(filterMapping);

    if (fasc.hasSpecialFilterValue()) {
        filterMapping.setDirectFilterValues(new EmptyFilterValues());

        // Handle special values on a case by case basis
        List<String> specialValues = fasc.getSpecialFilterValues();
        if (specialValues.contains(FilterAndSortCriteria.IS_NULL_FILTER_VALUE)) {
            filterMapping
                    .setRestriction(new Restriction().withPredicateProvider(new IsNullPredicateProvider()));
        }
        if (specialValues.contains(FilterAndSortCriteria.IS_NOT_NULL_FILTER_VALUE)) {
            filterMapping
                    .setRestriction(new Restriction().withPredicateProvider(new IsNotNullPredicateProvider()));
        }
    } else {
        if (fasc.getRestrictionType() != null) {
            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                    fasc.getRestrictionType().getType(), addSearchMappingRequest.getPropertyName()));
        } else {
            switch (metadata.getFieldType()) {
            case BOOLEAN:
                if (targetType == null || targetType.equals(Boolean.class)
                        || targetType.equals(boolean.class)) {

                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                            RestrictionType.BOOLEAN.getType(), addSearchMappingRequest.getPropertyName()));
                } else {
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                            RestrictionType.CHARACTER.getType(), addSearchMappingRequest.getPropertyName()));
                }
                break;
            case DATE:
                filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                        RestrictionType.DATE.getType(), addSearchMappingRequest.getPropertyName()));
                break;
            case DECIMAL:
            case MONEY:
                filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                        RestrictionType.DECIMAL.getType(), addSearchMappingRequest.getPropertyName()));
                break;
            case INTEGER:
                filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                        RestrictionType.LONG.getType(), addSearchMappingRequest.getPropertyName()));
                break;
            case BROADLEAF_ENUMERATION:
                filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                        RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                break;
            default:
                filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                        RestrictionType.STRING_LIKE.getType(), addSearchMappingRequest.getPropertyName()));
                break;
            case FOREIGN_KEY:
                if (!addSearchMappingRequest.getRequestedCto().get(addSearchMappingRequest.getPropertyName())
                        .getFilterValues().isEmpty()) {
                    ForeignKey foreignKey = (ForeignKey) addSearchMappingRequest.getPersistencePerspective()
                            .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
                    if (metadata.getForeignKeyCollection()) {
                        if (ForeignKeyRestrictionType.COLLECTION_SIZE_EQ.toString()
                                .equals(foreignKey.getRestrictionType().toString())) {
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory()
                                    .getRestriction(RestrictionType.COLLECTION_SIZE_EQUAL.getType(),
                                            addSearchMappingRequest.getPropertyName()));
                            filterMapping.setFieldPath(new FieldPath());
                        } else {
                            filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory()
                                    .getRestriction(RestrictionType.LONG.getType(),
                                            addSearchMappingRequest.getPropertyName()));
                            filterMapping.setFieldPath(
                                    new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName()
                                            + "." + metadata.getForeignKeyProperty()));
                        }
                    } else if (addSearchMappingRequest.getRequestedCto()
                            .get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0) == null
                            || "null".equals(addSearchMappingRequest.getRequestedCto()
                                    .get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0))) {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory()
                                .getRestriction(RestrictionType.IS_NULL_LONG.getType(),
                                        addSearchMappingRequest.getPropertyName()));
                    } else if (metadata.getSecondaryType() == SupportedFieldType.STRING) {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory()
                                .getRestriction(RestrictionType.STRING_EQUAL.getType(),
                                        addSearchMappingRequest.getPropertyName()));
                        filterMapping.setFieldPath(
                                new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName()
                                        + "." + metadata.getForeignKeyProperty()));
                    } else {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory()
                                .getRestriction(RestrictionType.LONG_EQUAL.getType(),
                                        addSearchMappingRequest.getPropertyName()));
                        filterMapping.setFieldPath(
                                new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName()
                                        + "." + metadata.getForeignKeyProperty()));
                    }
                }
                break;
            case ADDITIONAL_FOREIGN_KEY:
                int additionalForeignKeyIndexPosition = Arrays.binarySearch(
                        addSearchMappingRequest.getPersistencePerspective().getAdditionalForeignKeys(),
                        new ForeignKey(addSearchMappingRequest.getPropertyName(), null, null),
                        new Comparator<ForeignKey>() {
                            @Override
                            public int compare(ForeignKey o1, ForeignKey o2) {
                                return o1.getManyToField().compareTo(o2.getManyToField());
                            }
                        });
                ForeignKey foreignKey = null;
                if (additionalForeignKeyIndexPosition >= 0) {
                    foreignKey = addSearchMappingRequest.getPersistencePerspective()
                            .getAdditionalForeignKeys()[additionalForeignKeyIndexPosition];
                }
                // in the case of a to-one lookup, an explicit ForeignKey is not passed in. The system should then
                // default to just using a ForeignKeyRestrictionType.ID_EQ
                if (metadata.getForeignKeyCollection()) {
                    if (ForeignKeyRestrictionType.COLLECTION_SIZE_EQ.toString()
                            .equals(foreignKey.getRestrictionType().toString())) {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory()
                                .getRestriction(RestrictionType.COLLECTION_SIZE_EQUAL.getType(),
                                        addSearchMappingRequest.getPropertyName()));
                        filterMapping.setFieldPath(new FieldPath());
                    } else {
                        filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory()
                                .getRestriction(RestrictionType.LONG.getType(),
                                        addSearchMappingRequest.getPropertyName()));
                        filterMapping.setFieldPath(
                                new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName()
                                        + "." + metadata.getForeignKeyProperty()));
                    }
                } else if (CollectionUtils
                        .isEmpty(addSearchMappingRequest.getRequestedCto()
                                .get(addSearchMappingRequest.getPropertyName()).getFilterValues())
                        || addSearchMappingRequest.getRequestedCto()
                                .get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0) == null
                        || "null".equals(addSearchMappingRequest.getRequestedCto()
                                .get(addSearchMappingRequest.getPropertyName()).getFilterValues().get(0))) {
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                            RestrictionType.IS_NULL_LONG.getType(), addSearchMappingRequest.getPropertyName()));
                } else if (metadata.getSecondaryType() == SupportedFieldType.STRING) {
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                            RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                    filterMapping.setFieldPath(
                            new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "."
                                    + metadata.getForeignKeyProperty()));
                } else {
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                            RestrictionType.LONG_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                    filterMapping.setFieldPath(
                            new FieldPath().withTargetProperty(addSearchMappingRequest.getPropertyName() + "."
                                    + metadata.getForeignKeyProperty()));
                }
                break;
            case ID:
                switch (metadata.getSecondaryType()) {
                case INTEGER:
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                            RestrictionType.LONG_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                    break;
                case STRING:
                    filterMapping.setRestriction(addSearchMappingRequest.getRestrictionFactory().getRestriction(
                            RestrictionType.STRING_EQUAL.getType(), addSearchMappingRequest.getPropertyName()));
                    break;
                }
                break;
            }
        }
    }
    return FieldProviderResponse.HANDLED;
}

From source file:org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl.java

protected int findAdditionalForeignKeyIndex(ForeignKey[] additionalForeignFields, String prefix,
        String propertyName) {//from  www  .j  ava 2s .  com
    int additionalForeignKeyIndexPosition = -1;
    if (additionalForeignFields != null) {
        additionalForeignKeyIndexPosition = Arrays.binarySearch(additionalForeignFields,
                new ForeignKey(prefix + propertyName, null, null), new Comparator<ForeignKey>() {
                    @Override
                    public int compare(ForeignKey o1, ForeignKey o2) {
                        return o1.getManyToField().compareTo(o2.getManyToField());
                    }
                });
    }
    return additionalForeignKeyIndexPosition;
}

From source file:org.apache.hadoop.hive.ql.QTestUtil.java

private static List<String> getVersionFilesInternal(String tname) {
    if (cachedQvFileList == null) {
        return new ArrayList<String>();
    }// w  w  w  .j  a v  a 2s  .c om
    int pos = Arrays.binarySearch(cachedQvFileList, tname, String.CASE_INSENSITIVE_ORDER);
    if (pos >= 0) {
        throw new BuildException("Unexpected file list element: " + cachedQvFileList[pos]);
    }
    List<String> result = null;
    for (pos = (-pos - 1); pos < cachedQvFileList.length; ++pos) {
        String candidate = cachedQvFileList[pos];
        if (candidate.length() <= tname.length()
                || !tname.equalsIgnoreCase(candidate.substring(0, tname.length()))
                || !qvSuffix.matcher(candidate.substring(tname.length())).matches()) {
            break;
        }
        if (result == null) {
            result = new ArrayList<String>();
        }
        result.add(candidate);
    }
    return result;
}