Search in sources :

Example 81 with NavigableMap

use of java.util.NavigableMap in project cdap by caskdata.

the class LevelDBTable method persist.

@Override
protected void persist(NavigableMap<byte[], NavigableMap<byte[], Update>> changes) throws Exception {
    persistedVersion = tx == null ? System.currentTimeMillis() : tx.getWritePointer();
    NavigableMap<byte[], NavigableMap<byte[], byte[]>> puts = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    NavigableMap<byte[], NavigableMap<byte[], Long>> increments = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    for (Map.Entry<byte[], NavigableMap<byte[], Update>> rowEntry : changes.entrySet()) {
        for (Map.Entry<byte[], Update> colEntry : rowEntry.getValue().entrySet()) {
            Update val = colEntry.getValue();
            if (val instanceof IncrementValue) {
                NavigableMap<byte[], Long> incrCols = increments.get(rowEntry.getKey());
                if (incrCols == null) {
                    incrCols = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
                    increments.put(rowEntry.getKey(), incrCols);
                }
                incrCols.put(colEntry.getKey(), ((IncrementValue) val).getValue());
            } else if (val instanceof PutValue) {
                NavigableMap<byte[], byte[]> putCols = puts.get(rowEntry.getKey());
                if (putCols == null) {
                    putCols = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
                    puts.put(rowEntry.getKey(), putCols);
                }
                putCols.put(colEntry.getKey(), ((PutValue) val).getValue());
            }
        }
    }
    if (!increments.isEmpty() || !puts.isEmpty()) {
        persist(increments, puts);
    }
}
Also used : IncrementValue(co.cask.cdap.data2.dataset2.lib.table.IncrementValue) PutValue(co.cask.cdap.data2.dataset2.lib.table.PutValue) NavigableMap(java.util.NavigableMap) Update(co.cask.cdap.data2.dataset2.lib.table.Update) NavigableMap(java.util.NavigableMap) Map(java.util.Map)

Example 82 with NavigableMap

use of java.util.NavigableMap in project cdap by caskdata.

the class FactTable method add.

public void add(List<Fact> facts) {
    // Simply collecting all rows/cols/values that need to be put to the underlying table.
    NavigableMap<byte[], NavigableMap<byte[], byte[]>> gaugesTable = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    NavigableMap<byte[], NavigableMap<byte[], byte[]>> incrementsTable = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    for (Fact fact : facts) {
        for (Measurement measurement : fact.getMeasurements()) {
            byte[] rowKey = codec.createRowKey(fact.getDimensionValues(), measurement.getName(), fact.getTimestamp());
            byte[] column = codec.createColumn(fact.getTimestamp());
            if (MeasureType.COUNTER == measurement.getType()) {
                inc(incrementsTable, rowKey, column, measurement.getValue());
            } else {
                set(gaugesTable, rowKey, column, Bytes.toBytes(measurement.getValue()));
            }
        }
    }
    NavigableMap<byte[], NavigableMap<byte[], Long>> convertedIncrementsTable = Maps.transformValues(incrementsTable, TRANSFORM_MAP_BYTE_ARRAY_TO_LONG);
    NavigableMap<byte[], NavigableMap<byte[], Long>> convertedGaugesTable = Maps.transformValues(gaugesTable, TRANSFORM_MAP_BYTE_ARRAY_TO_LONG);
    // todo: replace with single call, to be able to optimize rpcs in underlying table
    timeSeriesTable.put(convertedGaugesTable);
    timeSeriesTable.increment(convertedIncrementsTable);
    if (metrics != null) {
        metrics.increment(putCountMetric, convertedGaugesTable.size());
        metrics.increment(incrementCountMetric, convertedIncrementsTable.size());
    }
}
Also used : Measurement(co.cask.cdap.api.dataset.lib.cube.Measurement) NavigableMap(java.util.NavigableMap)

Example 83 with NavigableMap

use of java.util.NavigableMap in project cdap by caskdata.

the class IncrementHandler method preIncrementAfterRowLock.

@Override
public Result preIncrementAfterRowLock(ObserverContext<RegionCoprocessorEnvironment> e, Increment increment) throws IOException {
    // this should only trigger for a transactional readless increment
    boolean isIncrement = increment.getAttribute(HBaseTable.DELTA_WRITE) != null;
    boolean transactional = state.containsTransactionalFamily(increment.getFamilyCellMap().keySet());
    if (!isIncrement || !transactional) {
        return null;
    }
    byte[] txBytes = increment.getAttribute(TxConstants.TX_OPERATION_ATTRIBUTE_KEY);
    if (txBytes == null) {
        throw new IllegalArgumentException("Attribute " + TxConstants.TX_OPERATION_ATTRIBUTE_KEY + " must be set for transactional readless increments");
    }
    byte[] wpBytes = increment.getAttribute(HBaseTable.WRITE_POINTER);
    if (wpBytes == null) {
        throw new IllegalArgumentException("Attribute " + HBaseTable.WRITE_POINTER + " must be set for transactional readless increments");
    }
    long writeVersion = Bytes.toLong(wpBytes);
    Get get = new Get(increment.getRow());
    get.setAttribute(TxConstants.TX_OPERATION_ATTRIBUTE_KEY, txBytes);
    for (Map.Entry<byte[], NavigableMap<byte[], Long>> entry : increment.getFamilyMapOfLongs().entrySet()) {
        byte[] family = entry.getKey();
        for (byte[] column : entry.getValue().keySet()) {
            get.addColumn(family, column);
        }
    }
    Result result = region.get(get);
    Put put = new Put(increment.getRow());
    for (Map.Entry<byte[], NavigableMap<byte[], Long>> entry : increment.getFamilyMapOfLongs().entrySet()) {
        byte[] family = entry.getKey();
        for (Map.Entry<byte[], Long> colEntry : entry.getValue().entrySet()) {
            byte[] column = colEntry.getKey();
            long value = colEntry.getValue();
            byte[] existingValue = result.getValue(family, column);
            if (existingValue != null) {
                long delta = Bytes.toLong(existingValue);
                value += delta;
            }
            put.add(new KeyValue(increment.getRow(), family, column, writeVersion, Bytes.toBytes(value)));
        }
    }
    if (!put.isEmpty()) {
        region.put(put);
    }
    e.bypass();
    return new Result();
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) NavigableMap(java.util.NavigableMap) Put(org.apache.hadoop.hbase.client.Put) Result(org.apache.hadoop.hbase.client.Result) Get(org.apache.hadoop.hbase.client.Get) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap)

Example 84 with NavigableMap

use of java.util.NavigableMap in project cdap by caskdata.

the class IncrementHandler method prePut.

@Override
public void prePut(ObserverContext<RegionCoprocessorEnvironment> ctx, Put put, WALEdit edit, Durability durability) throws IOException {
    // we assume that if any of the column families written to are transactional, the entire write is transactional
    boolean transactional = state.containsTransactionalFamily(put.getFamilyCellMap().keySet());
    boolean isIncrement = put.getAttribute(HBaseTable.DELTA_WRITE) != null;
    if (isIncrement || !transactional) {
        // incremental write
        NavigableMap<byte[], List<Cell>> newFamilyMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);
        long tsToAssign = 0;
        if (!transactional) {
            tsToAssign = state.getUniqueTimestamp();
        }
        for (Map.Entry<byte[], List<Cell>> entry : put.getFamilyCellMap().entrySet()) {
            List<Cell> newCells = new ArrayList<>(entry.getValue().size());
            for (Cell cell : entry.getValue()) {
                // rewrite the cell value with a special prefix to identify it as a delta
                // for 0.98 we can update this to use cell tags
                byte[] newValue = isIncrement ? Bytes.add(IncrementHandlerState.DELTA_MAGIC_PREFIX, CellUtil.cloneValue(cell)) : CellUtil.cloneValue(cell);
                newCells.add(CellUtil.createCell(CellUtil.cloneRow(cell), CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), transactional ? cell.getTimestamp() : tsToAssign, cell.getTypeByte(), newValue));
            }
            newFamilyMap.put(entry.getKey(), newCells);
        }
        put.setFamilyCellMap(newFamilyMap);
    }
// put completes normally with value prefix marker
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) Cell(org.apache.hadoop.hbase.Cell)

Example 85 with NavigableMap

use of java.util.NavigableMap in project opennms by OpenNMS.

the class RRDv3IT method testSamplesSingleRRA.

/**
 * Test samples for a single RRA
 *
 * @throws Exception the exception
 */
@Test
public void testSamplesSingleRRA() throws Exception {
    File source = new File("src/test/resources/sample-counter.xml");
    RRDv3 rrd = JaxbUtils.unmarshal(RRDv3.class, source);
    Assert.assertNotNull(rrd);
    NavigableMap<Long, List<Double>> samples = rrd.generateSamples(rrd.getRras().get(0));
    Assert.assertFalse(samples.isEmpty());
    long ts = 1441748400L;
    Double v1 = 600.0;
    Double v2 = 2.0;
    Assert.assertEquals(rrd.getRras().get(0).getRows().size(), samples.size());
    for (Map.Entry<Long, List<Double>> s : samples.entrySet()) {
        System.out.println(s);
        Assert.assertEquals(2, s.getValue().size());
        Assert.assertEquals(ts, (long) s.getKey());
        Assert.assertEquals(v1, s.getValue().get(0));
        Assert.assertEquals(v2, s.getValue().get(1));
        ts += 300L;
        v1 += 300.0 * v2;
        v2 += 1.0;
    }
}
Also used : RRDv3(org.opennms.netmgt.rrd.model.v3.RRDv3) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) NavigableMap(java.util.NavigableMap) Map(java.util.Map) SortedMap(java.util.SortedMap) Test(org.junit.Test)

Aggregations

NavigableMap (java.util.NavigableMap)173 Map (java.util.Map)85 TreeMap (java.util.TreeMap)62 SortedMap (java.util.SortedMap)35 ArrayList (java.util.ArrayList)34 List (java.util.List)27 HashMap (java.util.HashMap)21 Iterator (java.util.Iterator)21 Cell (org.apache.hadoop.hbase.Cell)20 Result (org.apache.hadoop.hbase.client.Result)19 Set (java.util.Set)14 Get (org.apache.hadoop.hbase.client.Get)14 IOException (java.io.IOException)12 KeyValue (org.apache.hadoop.hbase.KeyValue)11 Test (org.junit.Test)11 Put (org.apache.hadoop.hbase.client.Put)10 Entry (java.util.Map.Entry)9 Update (co.cask.cdap.data2.dataset2.lib.table.Update)7 ImmutableMap (com.google.common.collect.ImmutableMap)7 TestSuite (junit.framework.TestSuite)7