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);
}
}
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());
}
}
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();
}
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
}
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;
}
}
Aggregations