Example usage for java.util NavigableMap containsKey

List of usage examples for java.util NavigableMap containsKey

Introduction

In this page you can find the example usage for java.util NavigableMap containsKey.

Prototype

boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

public void testEntrySet() {
    K[] keys = getSortedKeys();/*  w  w w  . j  a  va  2 s. com*/
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();
    map.put(keys[0], values[0]);
    map.put(keys[1], values[1]);
    map.put(keys[2], values[2]);

    Set<Map.Entry<K, V>> entries = map.entrySet();
    Iterator<Map.Entry<K, V>> entrySetIterator = entries.iterator();
    assertEquals(3, entries.size());
    assertEquals(keys[0] + "=" + values[0], entrySetIterator.next().toString());
    while (entrySetIterator.hasNext()) {
        Map.Entry<K, V> entry = entrySetIterator.next();
        assertTrue(map.get(entry.getKey()) == entry.getValue());
    }

    assertEquals(map.size(), entries.size());
    _assertEquals(entries, map.entrySet());
    map.clear();
    assertEquals(map.size(), entries.size());
    _assertEquals(entries, map.entrySet());
    map.put(keys[0], values[0]);
    assertEquals(map.size(), entries.size());
    _assertEquals(entries, map.entrySet());
    entries.clear();
    assertEquals(map.size(), entries.size());
    _assertEquals(entries, map.entrySet());

    map.put(keys[1], values[1]);
    map.put(keys[2], values[2]);
    Iterator<Entry<K, V>> it = entries.iterator();
    while (it.hasNext()) {
        Map.Entry<K, V> entry = it.next();
        map.containsKey(entry.getKey());
        map.containsValue(entry.getValue());
        it.remove();
    }
    try {
        it.next();
        fail("should throw NoSuchElementException");
    } catch (NoSuchElementException expected) {
    }
    _assertEmpty(map);
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

public void testNavigableKeySet() {
    K[] keys = getSortedKeys();/*from  www  . j  a v a 2  s.c o  m*/
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();
    map.put(keys[0], values[0]);

    Set<K> keySet = map.navigableKeySet();
    _assertEquals(keySet, map.navigableKeySet());

    map.put(keys[1], values[1]);
    map.put(keys[2], values[2]);
    _assertEquals(map.navigableKeySet(), keySet);
    _assertEquals(keySet, keySet);

    try {
        keySet.add(keys[3]);
        fail("should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }
    try {
        keySet.add(null);
        fail("should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }
    try {
        keySet.addAll(null);
        fail("should throw NullPointerException");
    } catch (NullPointerException expected) {
    }
    Collection<K> collection = new ArrayList<K>();
    keySet.addAll(collection);
    try {
        collection.add(keys[3]);
        keySet.addAll(collection);
        fail("should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }

    Iterator<K> iter = keySet.iterator();
    iter.next();
    iter.remove();
    assertFalse(map.containsKey(keys[0]));

    collection = new ArrayList<K>();
    collection.add(keys[2]);
    keySet.retainAll(collection);
    assertEquals(1, map.size());
    assertTrue(keySet.contains(keys[2]));

    keySet.removeAll(collection);
    _assertEmpty(map);

    map.put(keys[0], values[0]);
    assertEquals(1, map.size());
    assertTrue(keySet.contains(keys[0]));

    keySet.clear();
    _assertEmpty(map);
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

public void testHeadMapLjava_lang_ObjectZL() {
    K[] keys = getSortedKeys();// w  ww  .  ja  va2s  . c o m
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();
    for (int i = 0; i < keys.length; i++) {
        map.put(keys[i], values[i]);
    }

    // normal case
    SortedMap<K, V> subMap = map.headMap(keys[2], true);
    assertEquals(3, subMap.size());
    subMap = map.headMap(keys[3], true);
    assertEquals(4, subMap.size());
    for (int i = 0; i < 4; i++) {
        assertEquals(values[i], subMap.get(keys[i]));
    }
    subMap = map.headMap(keys[2], false);
    assertEquals(2, subMap.size());
    assertNull(subMap.get(keys[3]));

    // Exceptions
    assertEquals(0, map.headMap(keys[0], false).size());

    try {
        map.headMap(null, true);
        assertTrue("expected exception", useNullKey());
    } catch (NullPointerException e) {
        assertFalse("unexpected NPE", useNullKey());
    }

    try {
        map.headMap(null, false);
        assertTrue("expected exception", useNullKey());
    } catch (NullPointerException e) {
        assertFalse("unexpected NPE", useNullKey());
    }

    subMap = map.headMap(keys[2]);
    assertEquals(2, subMap.size());
    try {
        subMap.put(keys[2], values[2]);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    assertEquals(keys.length, map.size());
    subMap = map.headMap(keys[2], true);
    assertEquals(3, subMap.size());
    subMap.remove(keys[1]);
    assertFalse(subMap.containsKey(keys[1]));
    assertFalse(subMap.containsValue(values[1]));
    assertFalse(map.containsKey(keys[1]));
    assertFalse(map.containsValue(values[1]));
    assertEquals(2, subMap.size());
    assertEquals(keys.length - 1, map.size());

    subMap.put(keys[1], values[1]);

    try {
        subMap.subMap(keys[1], keys[3]);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        subMap.subMap(keys[3], keys[1]);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }

    if (useNullKey() && useNullValue()) {
        map.put(null, null);

        subMap = map.headMap(null, true);
        assertEquals(1, subMap.size());
        assertTrue(subMap.containsValue(null));
        assertNull(subMap.get(null));

        subMap = map.subMap(null, false, keys[2], true);
        assertEquals(3, subMap.size());

        Set<K> keySet = subMap.keySet();
        assertEquals(3, keySet.size());

        Set<Map.Entry<K, V>> entrySet = subMap.entrySet();
        assertEquals(3, entrySet.size());

        Collection<V> valueCollection = subMap.values();
        assertEquals(3, valueCollection.size());

        map.remove(null);
    }

    // head map of head map
    NavigableMap<K, V> headMap = map.headMap(keys[3], true);
    assertEquals(4, headMap.size());
    headMap = headMap.headMap(keys[3], false);
    assertEquals(3, headMap.size());
    headMap = headMap.headMap(keys[2], false);
    assertEquals(2, headMap.size());
    headMap = headMap.tailMap(keys[0], false);
    assertEquals(1, headMap.size());
    headMap = headMap.tailMap(keys[1], false);
    assertEquals(0, headMap.size());
}

From source file:org.alfresco.repo.imap.AlfrescoImapFolder.java

/**
 * Returns message sequence number in the folder by its UID.
 * /*from   www.j a  va2 s  .co m*/
 * @param uid - message UID.
 * @return message sequence number.
 * @throws FolderException if no message with given UID.
 */
@Override
public int getMsn(long uid) throws FolderException {
    NavigableMap<Long, FileInfo> messages = searchMails();
    if (!messages.containsKey(uid)) {
        throw new FolderException("No such message.");
    }
    return messages.headMap(uid, true).size();
}

From source file:org.apache.hadoop.hbase.master.RegionManager.java

static MetaRegion getMetaRegionPointingTo(NavigableMap<byte[], MetaRegion> metaRegions, HRegionInfo newRegion) {
    if (metaRegions.isEmpty()) {
        return null;
    } else if (metaRegions.size() == 1) {
        return metaRegions.get(metaRegions.firstKey());
    } else {/* ww  w  .  j  a v  a2s . c o m*/
        if (metaRegions.containsKey(newRegion.getRegionName())) {
            return metaRegions.get(newRegion.getRegionName());
        }
        return metaRegions.get(metaRegions.headMap(newRegion.getRegionName()).lastKey());
    }
}

From source file:org.apache.hadoop.hbase.regionserver.tableindexed.IndexedRegion.java

private void updateIndexes(BatchUpdate batchUpdate) throws IOException {
    List<IndexSpecification> indexesToUpdate = new LinkedList<IndexSpecification>();

    // Find the indexes we need to update
    for (IndexSpecification index : getIndexes()) {
        if (possiblyAppliesToIndex(index, batchUpdate)) {
            indexesToUpdate.add(index);//from w ww  . ja va  2 s  .  c om
        }
    }

    if (indexesToUpdate.size() == 0) {
        return;
    }

    NavigableSet<byte[]> neededColumns = getColumnsForIndexes(indexesToUpdate);

    NavigableMap<byte[], byte[]> newColumnValues = getColumnsFromBatchUpdate(batchUpdate);
    Map<byte[], Cell> oldColumnCells = super.getFull(batchUpdate.getRow(), neededColumns,
            HConstants.LATEST_TIMESTAMP, 1, null);

    // Handle delete batch updates. Go back and get the next older values
    for (BatchOperation op : batchUpdate) {
        if (!op.isPut()) {
            Cell current = oldColumnCells.get(op.getColumn());
            if (current != null) {
                // TODO: Fix this profligacy!!! St.Ack
                Cell[] older = Cell.createSingleCellArray(
                        super.get(batchUpdate.getRow(), op.getColumn(), current.getTimestamp(), 1));
                if (older != null && older.length > 0) {
                    newColumnValues.put(op.getColumn(), older[0].getValue());
                }
            }
        }
    }

    // Add the old values to the new if they are not there
    for (Entry<byte[], Cell> oldEntry : oldColumnCells.entrySet()) {
        if (!newColumnValues.containsKey(oldEntry.getKey())) {
            newColumnValues.put(oldEntry.getKey(), oldEntry.getValue().getValue());
        }
    }

    Iterator<IndexSpecification> indexIterator = indexesToUpdate.iterator();
    while (indexIterator.hasNext()) {
        IndexSpecification indexSpec = indexIterator.next();
        if (!doesApplyToIndex(indexSpec, newColumnValues)) {
            indexIterator.remove();
        }
    }

    SortedMap<byte[], byte[]> oldColumnValues = convertToValueMap(oldColumnCells);

    for (IndexSpecification indexSpec : indexesToUpdate) {
        removeOldIndexEntry(indexSpec, batchUpdate.getRow(), oldColumnValues);
        updateIndex(indexSpec, batchUpdate.getRow(), newColumnValues);
    }
}

From source file:org.apache.hadoop.hbase.replication.regionserver.Replication.java

/**
 * Utility method used to set the correct scopes on each log key. Doesn't set a scope on keys
 * from compaction WAL edits and if the scope is local.
 * @param htd Descriptor used to find the scope to use
 * @param logKey Key that may get scoped according to its edits
 * @param logEdit Edits used to lookup the scopes
 *//*  w  ww .  j  a va 2  s  .  co m*/
public static void scopeWALEdits(HTableDescriptor htd, HLogKey logKey, WALEdit logEdit) {
    NavigableMap<byte[], Integer> scopes = new TreeMap<byte[], Integer>(Bytes.BYTES_COMPARATOR);
    byte[] family;
    for (KeyValue kv : logEdit.getKeyValues()) {
        family = kv.getFamily();
        // This is expected and the KV should not be replicated
        if (CellUtil.matchingFamily(kv, WALEdit.METAFAMILY))
            continue;
        // Unexpected, has a tendency to happen in unit tests
        assert htd.getFamily(family) != null;

        int scope = htd.getFamily(family).getScope();
        if (scope != REPLICATION_SCOPE_LOCAL && !scopes.containsKey(family)) {
            scopes.put(family, scope);
        }
    }
    if (!scopes.isEmpty()) {
        logKey.setScopes(scopes);
    }
}

From source file:org.apache.hadoop.hbase.replication.regionserver.ReplicationSource.java

/**
 * We only want KVs that are scoped other than local
 * @param entry The entry to check for replication
 *//*from  w  ww  .j  a  va2 s .c  o m*/
protected void removeNonReplicableEdits(HLog.Entry entry) {
    String tabName = entry.getKey().getTablename().getNameAsString();
    ArrayList<KeyValue> kvs = entry.getEdit().getKeyValues();
    Map<String, List<String>> tableCFs = null;
    try {
        tableCFs = this.replicationPeers.getTableCFs(peerId);
    } catch (IllegalArgumentException e) {
        LOG.error("should not happen: can't get tableCFs for peer " + peerId
                + ", degenerate as if it's not configured by keeping tableCFs==null");
    }
    int size = kvs.size();

    // clear kvs(prevent replicating) if logKey's table isn't in this peer's
    // replicable table list (empty tableCFs means all table are replicable)
    if (tableCFs != null && !tableCFs.containsKey(tabName)) {
        kvs.clear();
    } else {
        NavigableMap<byte[], Integer> scopes = entry.getKey().getScopes();
        List<String> cfs = (tableCFs == null) ? null : tableCFs.get(tabName);
        for (int i = size - 1; i >= 0; i--) {
            KeyValue kv = kvs.get(i);
            // The scope will be null or empty if
            // there's nothing to replicate in that WALEdit
            // ignore(remove) kv if its cf isn't in the replicable cf list
            // (empty cfs means all cfs of this table are replicable)
            if (scopes == null || !scopes.containsKey(kv.getFamily())
                    || (cfs != null && !cfs.contains(Bytes.toString(kv.getFamily())))) {
                kvs.remove(i);
            }
        }
    }

    if (kvs.size() < size / 2) {
        kvs.trimToSize();
    }
}

From source file:org.apache.hadoop.hbase.replication.ScopeWALEntryFilter.java

@Override
public Entry filter(Entry entry) {
    NavigableMap<byte[], Integer> scopes = entry.getKey().getReplicationScopes();
    if (scopes == null || scopes.isEmpty()) {
        return null;
    }//from  w ww. j a v  a  2 s .  co  m
    ArrayList<Cell> cells = entry.getEdit().getCells();
    int size = cells.size();
    byte[] fam;
    for (int i = size - 1; i >= 0; i--) {
        Cell cell = cells.get(i);
        // If a bulk load entry has a scope then that means user has enabled replication for bulk load
        // hfiles.
        // TODO There is a similar logic in TableCfWALEntryFilter but data structures are different so
        // cannot refactor into one now, can revisit and see if any way to unify them.
        if (CellUtil.matchingColumn(cell, WALEdit.METAFAMILY, WALEdit.BULK_LOAD)) {
            Cell filteredBulkLoadEntryCell = filterBulkLoadEntries(scopes, cell);
            if (filteredBulkLoadEntryCell != null) {
                cells.set(i, filteredBulkLoadEntryCell);
            } else {
                cells.remove(i);
            }
        } else {
            // The scope will be null or empty if
            // there's nothing to replicate in that WALEdit
            fam = CellUtil.cloneFamily(cell);
            if (!scopes.containsKey(fam) || scopes.get(fam) == HConstants.REPLICATION_SCOPE_LOCAL) {
                cells.remove(i);
            }
        }
    }
    if (cells.size() < size / 2) {
        cells.trimToSize();
    }
    return entry;
}

From source file:org.apache.hadoop.hbase.replication.ScopeWALEntryFilter.java

private Cell filterBulkLoadEntries(NavigableMap<byte[], Integer> scopes, Cell cell) {
    byte[] fam;/*  ww  w. j  ava 2s .  c  om*/
    BulkLoadDescriptor bld = null;
    try {
        bld = WALEdit.getBulkLoadDescriptor(cell);
    } catch (IOException e) {
        LOG.warn("Failed to get bulk load events information from the WAL file.", e);
        return cell;
    }
    List<StoreDescriptor> storesList = bld.getStoresList();
    // Copy the StoreDescriptor list and update it as storesList is a unmodifiableList
    List<StoreDescriptor> copiedStoresList = new ArrayList<StoreDescriptor>(storesList);
    Iterator<StoreDescriptor> copiedStoresListIterator = copiedStoresList.iterator();
    boolean anyStoreRemoved = false;
    while (copiedStoresListIterator.hasNext()) {
        StoreDescriptor sd = copiedStoresListIterator.next();
        fam = sd.getFamilyName().toByteArray();
        if (!scopes.containsKey(fam) || scopes.get(fam) == HConstants.REPLICATION_SCOPE_LOCAL) {
            copiedStoresListIterator.remove();
            anyStoreRemoved = true;
        }
    }

    if (!anyStoreRemoved) {
        return cell;
    } else if (copiedStoresList.isEmpty()) {
        return null;
    }
    BulkLoadDescriptor.Builder newDesc = BulkLoadDescriptor.newBuilder().setTableName(bld.getTableName())
            .setEncodedRegionName(bld.getEncodedRegionName()).setBulkloadSeqNum(bld.getBulkloadSeqNum());
    newDesc.addAllStores(copiedStoresList);
    BulkLoadDescriptor newBulkLoadDescriptor = newDesc.build();
    return CellUtil.createCell(CellUtil.cloneRow(cell), WALEdit.METAFAMILY, WALEdit.BULK_LOAD,
            cell.getTimestamp(), cell.getTypeByte(), newBulkLoadDescriptor.toByteArray());
}