Example usage for com.google.common.collect Maps newTreeMap

List of usage examples for com.google.common.collect Maps newTreeMap

Introduction

In this page you can find the example usage for com.google.common.collect Maps newTreeMap.

Prototype

public static <C, K extends C, V> TreeMap<K, V> newTreeMap(@Nullable Comparator<C> comparator) 

Source Link

Document

Creates a mutable, empty TreeMap instance using the given comparator.

Usage

From source file:org.apache.crunch.io.hbase.RegionLocationTable.java

/**
 * Deserialize a table which was serialized to with the {@link #serialize(DataOutput)} method.
 *
 * @param dataInput input containing a serialized instance of this class
 * @return the deserialized table/*from w  ww.jav a2 s. c om*/
 */
public static RegionLocationTable deserialize(DataInput dataInput) throws IOException {
    NavigableMap<byte[], String> regionStartToServerHostName = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    String tableName = dataInput.readUTF();
    int numEntries = dataInput.readInt();
    for (int i = 0; i < numEntries; i++) {
        int rowKeyLength = dataInput.readInt();
        byte[] rowKey = new byte[rowKeyLength];
        dataInput.readFully(rowKey, 0, rowKeyLength);
        String hostName = dataInput.readUTF();
        regionStartToServerHostName.put(rowKey, hostName);
    }
    return new RegionLocationTable(tableName, regionStartToServerHostName);
}

From source file:co.cask.cdap.data2.dataset2.lib.table.inmemory.InMemoryMetricsTable.java

private NavigableMap<byte[], NavigableMap<byte[], byte[]>> getLatest(
        NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> versionedRows) {
    NavigableMap<byte[], NavigableMap<byte[], byte[]>> rows = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> versionedRow : versionedRows
            .entrySet()) {/*from  ww w  . j a  v a 2s .  c  o  m*/
        NavigableMap<byte[], byte[]> columns = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
        for (Map.Entry<byte[], NavigableMap<Long, byte[]>> versionedColumn : versionedRow.getValue()
                .entrySet()) {
            columns.put(versionedColumn.getKey(), versionedColumn.getValue().firstEntry().getValue());
        }
        rows.put(versionedRow.getKey(), columns);
    }
    return rows;
}

From source file:co.cask.cdap.data2.dataset2.lib.table.leveldb.LevelDBTableCore.java

public synchronized Map<byte[], Long> increment(byte[] row, Map<byte[], Long> increments) throws IOException {
    Map<byte[], Long> result = getResultMap(row, increments);
    Map<byte[], byte[]> replacing = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    for (Map.Entry<byte[], Long> entry : result.entrySet()) {
        replacing.put(entry.getKey(), Bytes.toBytes(entry.getValue()));
    }// w  w w . ja  v  a  2  s. com
    persist(ImmutableMap.of(row, replacing), System.currentTimeMillis());
    return result;
}

From source file:org.cinchapi.concourse.server.storage.db.SecondaryRecord.java

@Override
protected Map<Value, Set<PrimaryKey>> mapType() {
    return Maps.newTreeMap(Value.Sorter.INSTANCE);
}

From source file:org.apache.kylin.query.routing.ModelChooser.java

private static Map<DataModelDesc, Set<IRealization>> makeOrderedModelMap(List<OLAPContext> contexts) {
    // the first context, which is the top most context, contains all columns from all contexts
    OLAPContext first = contexts.get(0);
    KylinConfig kylinConfig = first.olapSchema.getConfig();
    String projectName = first.olapSchema.getProjectName();
    String factTableName = first.firstTableScan.getOlapTable().getTableName();
    Set<IRealization> realizations = ProjectManager.getInstance(kylinConfig).getRealizationsByTable(projectName,
            factTableName);/*from  ww w  . j  a  v  a2 s. c om*/

    final Map<DataModelDesc, Set<IRealization>> models = Maps.newHashMap();
    final Map<DataModelDesc, RealizationCost> costs = Maps.newHashMap();
    for (IRealization real : realizations) {
        if (real.isReady() == false)
            continue;
        if (containsAll(real.getAllColumnDescs(), first.allColumns) == false)
            continue;
        if (RemoveBlackoutRealizationsRule.accept(real) == false)
            continue;

        RealizationCost cost = new RealizationCost(real);
        DataModelDesc m = real.getModel();
        Set<IRealization> set = models.get(m);
        if (set == null) {
            set = Sets.newHashSet();
            set.add(real);
            models.put(m, set);
            costs.put(m, cost);
        } else {
            set.add(real);
            RealizationCost curCost = costs.get(m);
            if (cost.compareTo(curCost) < 0)
                costs.put(m, cost);
        }
    }

    // order model by cheapest realization cost
    TreeMap<DataModelDesc, Set<IRealization>> result = Maps.newTreeMap(new Comparator<DataModelDesc>() {
        @Override
        public int compare(DataModelDesc o1, DataModelDesc o2) {
            return costs.get(o1).compareTo(costs.get(o2));
        }
    });
    result.putAll(models);

    return result;
}

From source file:co.cask.cdap.data2.dataset2.lib.table.leveldb.LevelDBOrderedTableCore.java

public synchronized Map<byte[], Long> increment(byte[] row, Map<byte[], Long> increments) throws Exception {
    NavigableMap<byte[], byte[]> existing = getRow(row,
            increments.keySet().toArray(new byte[increments.size()][]), null, null, -1, null);
    Map<byte[], Long> result = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    Map<byte[], byte[]> replacing = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    for (Map.Entry<byte[], Long> increment : increments.entrySet()) {
        long existingValue = 0L;
        byte[] existingBytes = existing.get(increment.getKey());
        if (existingBytes != null) {
            if (existingBytes.length != Bytes.SIZEOF_LONG) {
                throw new NumberFormatException(
                        "Attempted to increment a value that is not convertible to long," + " row: "
                                + Bytes.toStringBinary(row) + " column: "
                                + Bytes.toStringBinary(increment.getKey()));
            }/*  ww w.  ja  v  a2  s  .c o m*/
            existingValue = Bytes.toLong(existingBytes);
        }
        long newValue = existingValue + increment.getValue();
        result.put(increment.getKey(), newValue);
        replacing.put(increment.getKey(), Bytes.toBytes(newValue));
    }
    persist(ImmutableMap.of(row, replacing), System.currentTimeMillis());
    return result;
}

From source file:co.cask.cdap.data2.transaction.queue.leveldb.LevelDBQueueConsumer.java

@Override
protected void updateState(Set<byte[]> rowKeys, byte[] stateColumnName, byte[] stateContent)
        throws IOException {
    if (rowKeys.isEmpty()) {
        return;/*from  ww w  . jav  a2  s  . c om*/
    }
    NavigableMap<byte[], NavigableMap<byte[], byte[]>> changes = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    for (byte[] rowKey : rowKeys) {
        NavigableMap<byte[], byte[]> row = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
        row.put(stateColumnName, stateContent);
        changes.put(rowKey, row);
    }
    core.persist(changes, KeyValue.LATEST_TIMESTAMP);
}

From source file:co.cask.tigon.data.transaction.queue.hbase.coprocessor.ConsumerConfigCache.java

/**
 * This forces an immediate update of the config cache. It should only be called from the refresh thread or from
 * tests, to avoid having to add a sleep for the duration of the refresh interval.
 *
 * This method is synchronized to protect from race conditions if called directly from a test. Otherwise this is
 * only called from the refresh thread, and there will not be concurrent invocations.
 *//*from www. jav  a 2 s  .  c om*/
public synchronized void updateCache() {
    Map<byte[], QueueConsumerConfig> newCache = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    long now = System.currentTimeMillis();
    HTable table = null;
    try {
        table = new HTable(hConf, configTableName);
        Scan scan = new Scan();
        scan.addFamily(QueueEntryRow.COLUMN_FAMILY);
        ResultScanner scanner = table.getScanner(scan);
        int configCnt = 0;
        for (Result result : scanner) {
            if (!result.isEmpty()) {
                NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(QueueEntryRow.COLUMN_FAMILY);
                if (familyMap != null) {
                    configCnt++;
                    Map<ConsumerInstance, byte[]> consumerInstances = new HashMap<ConsumerInstance, byte[]>();
                    // Gather the startRow of all instances across all consumer groups.
                    int numGroups = 0;
                    Long groupId = null;
                    for (Map.Entry<byte[], byte[]> entry : familyMap.entrySet()) {
                        long gid = Bytes.toLong(entry.getKey());
                        int instanceId = Bytes.toInt(entry.getKey(), LONG_BYTES);
                        consumerInstances.put(new ConsumerInstance(gid, instanceId), entry.getValue());

                        // Columns are sorted by groupId, hence if it change, then numGroups would get +1
                        if (groupId == null || groupId != gid) {
                            numGroups++;
                            groupId = gid;
                        }
                    }
                    byte[] queueName = result.getRow();
                    newCache.put(queueName, new QueueConsumerConfig(consumerInstances, numGroups));
                }
            }
        }
        long elapsed = System.currentTimeMillis() - now;
        this.configCache = newCache;
        this.lastUpdated = now;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated consumer config cache with {} entries, took {} msec", configCnt, elapsed);
        }
    } catch (IOException ioe) {
        LOG.warn("Error updating queue consumer config cache: {}", ioe.getMessage());
    } finally {
        if (table != null) {
            try {
                table.close();
            } catch (IOException ioe) {
                LOG.error("Error closing table {}", Bytes.toString(configTableName), ioe);
            }
        }
    }

}

From source file:org.cinchapi.concourse.util.Transformers.java

/**
 * Transform the keys in {@code original} with the {@code keys} function
 * and each of the values with the {@code values} function and return the
 * map result that is sorted according to the {@code sorter}.
 * <p>/*from  www .j  a  v a 2 s. com*/
 * <strong>WARNING:</strong> There is the potential for data loss in the
 * event that {@code function} returns duplicate transformed results for
 * items in {@code original}.
 * </p>
 * 
 * @param original
 * @param keys
 * @param values
 * @param sorter
 * @return the transformed TreeMap
 */
public static <K, K2, V, V2> Map<K2, Set<V2>> transformTreeMapSet(Map<K, Set<V>> original,
        Function<? super K, ? extends K2> keys, Function<? super V, ? extends V2> values,
        final Comparator<K2> sorter) {
    Map<K2, Set<V2>> transformed = Maps.newTreeMap(sorter);
    for (Map.Entry<K, Set<V>> entry : original.entrySet()) {
        transformed.put(keys.apply(entry.getKey()), transformSet(entry.getValue(), values));
    }
    return transformed;
}

From source file:co.cask.cdap.data2.dataset2.lib.table.inmemory.InMemoryOrderedTable.java

private NavigableMap<byte[], byte[]> filterByColumns(NavigableMap<byte[], byte[]> rowMap,
        @Nullable byte[][] columns) {
    if (columns == null) {
        return rowMap;
    }//w ww  .j  a  va 2 s  . c  o  m
    NavigableMap<byte[], byte[]> result = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    for (byte[] column : columns) {
        byte[] val = rowMap.get(column);
        if (val != null) {
            result.put(column, val);
        }
    }
    return result;

}