Example usage for java.util.concurrent ConcurrentMap remove

List of usage examples for java.util.concurrent ConcurrentMap remove

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentMap remove.

Prototype

boolean remove(Object key, Object value);

Source Link

Document

Removes the entry for a key only if currently mapped to a given value.

Usage

From source file:org.apache.hadoop.hbase.client.MetaCache.java

/**
 * Delete all cached entries of a server.
 *//*  w  ww  .j  a va  2 s.  c  om*/
public void clearCache(final ServerName serverName) {
    if (!this.cachedServers.contains(serverName)) {
        return;
    }

    boolean deletedSomething = false;
    synchronized (this.cachedServers) {
        // We block here, because if there is an error on a server, it's likely that multiple
        //  threads will get the error  simultaneously. If there are hundreds of thousand of
        //  region location to check, it's better to do this only once. A better pattern would
        //  be to check if the server is dead when we get the region location.
        if (!this.cachedServers.contains(serverName)) {
            return;
        }
        for (ConcurrentMap<byte[], RegionLocations> tableLocations : cachedRegionLocations.values()) {
            for (Entry<byte[], RegionLocations> e : tableLocations.entrySet()) {
                RegionLocations regionLocations = e.getValue();
                if (regionLocations != null) {
                    RegionLocations updatedLocations = regionLocations.removeByServer(serverName);
                    if (updatedLocations != regionLocations) {
                        if (updatedLocations.isEmpty()) {
                            deletedSomething |= tableLocations.remove(e.getKey(), regionLocations);
                        } else {
                            deletedSomething |= tableLocations.replace(e.getKey(), regionLocations,
                                    updatedLocations);
                        }
                    }
                }
            }
        }
        this.cachedServers.remove(serverName);
    }
    if (deletedSomething && LOG.isTraceEnabled()) {
        LOG.trace("Removed all cached region locations that map to " + serverName);
    }
}

From source file:org.apache.hadoop.hbase.client.MetaCache.java

/**
 * Deletes the cached location of the region if necessary, based on some error from source.
 * @param hri The region in question.// ww  w.  ja  va 2s  .  c  o m
 */
public void clearCache(HRegionInfo hri) {
    ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(hri.getTable());
    RegionLocations regionLocations = tableLocations.get(hri.getStartKey());
    if (regionLocations != null) {
        HRegionLocation oldLocation = regionLocations.getRegionLocation(hri.getReplicaId());
        if (oldLocation == null)
            return;
        RegionLocations updatedLocations = regionLocations.remove(oldLocation);
        boolean removed = false;
        if (updatedLocations != regionLocations) {
            if (updatedLocations.isEmpty()) {
                removed = tableLocations.remove(hri.getStartKey(), regionLocations);
            } else {
                removed = tableLocations.replace(hri.getStartKey(), regionLocations, updatedLocations);
            }
            if (removed && LOG.isTraceEnabled()) {
                LOG.trace("Removed " + oldLocation + " from cache");
            }
        }
    }
}

From source file:org.apache.hadoop.hbase.client.MetaCache.java

/**
 * Delete a cached location, no matter what it is. Called when we were told to not use cache.
 * @param tableName tableName// w ww.  j  a  va  2  s  .  com
 * @param row
 */
public void clearCache(final TableName tableName, final byte[] row, int replicaId) {
    ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName);

    boolean removed = false;
    RegionLocations regionLocations = getCachedLocation(tableName, row);
    if (regionLocations != null) {
        HRegionLocation toBeRemoved = regionLocations.getRegionLocation(replicaId);
        RegionLocations updatedLocations = regionLocations.remove(replicaId);
        if (updatedLocations != regionLocations) {
            byte[] startKey = regionLocations.getRegionLocation().getRegionInfo().getStartKey();
            if (updatedLocations.isEmpty()) {
                removed = tableLocations.remove(startKey, regionLocations);
            } else {
                removed = tableLocations.replace(startKey, regionLocations, updatedLocations);
            }
        }

        if (removed && LOG.isTraceEnabled() && toBeRemoved != null) {
            LOG.trace("Removed " + toBeRemoved + " from cache");
        }
    }
}