Search in sources :

Example 1 with IncrementValue

use of co.cask.cdap.data2.dataset2.lib.table.IncrementValue in project cdap by caskdata.

the class HBaseTable method persist.

@Override
protected void persist(NavigableMap<byte[], NavigableMap<byte[], Update>> updates) throws Exception {
    if (updates.isEmpty()) {
        return;
    }
    byte[] txId = tx == null ? null : Bytes.toBytes(tx.getTransactionId());
    byte[] txWritePointer = tx == null ? null : Bytes.toBytes(tx.getWritePointer());
    List<Mutation> mutations = new ArrayList<>();
    for (Map.Entry<byte[], NavigableMap<byte[], Update>> row : updates.entrySet()) {
        // create these only when they are needed
        PutBuilder put = null;
        PutBuilder incrementPut = null;
        IncrementBuilder increment = null;
        for (Map.Entry<byte[], Update> column : row.getValue().entrySet()) {
            // we want support tx and non-tx modes
            if (tx != null) {
                // TODO: hijacking timestamp... bad
                Update val = column.getValue();
                if (val instanceof IncrementValue) {
                    if (safeReadlessIncrements) {
                        increment = getIncrement(increment, row.getKey(), txId, txWritePointer);
                        increment.add(columnFamily, column.getKey(), tx.getWritePointer(), ((IncrementValue) val).getValue());
                    } else {
                        incrementPut = getPutForIncrement(incrementPut, row.getKey(), txId);
                        incrementPut.add(columnFamily, column.getKey(), tx.getWritePointer(), Bytes.toBytes(((IncrementValue) val).getValue()));
                    }
                } else if (val instanceof PutValue) {
                    put = getPut(put, row.getKey(), txId);
                    put.add(columnFamily, column.getKey(), tx.getWritePointer(), wrapDeleteIfNeeded(((PutValue) val).getValue()));
                }
            } else {
                Update val = column.getValue();
                if (val instanceof IncrementValue) {
                    incrementPut = getPutForIncrement(incrementPut, row.getKey(), txId);
                    incrementPut.add(columnFamily, column.getKey(), Bytes.toBytes(((IncrementValue) val).getValue()));
                } else if (val instanceof PutValue) {
                    put = getPut(put, row.getKey(), txId);
                    put.add(columnFamily, column.getKey(), ((PutValue) val).getValue());
                }
            }
        }
        if (incrementPut != null) {
            mutations.add(incrementPut.build());
        }
        if (increment != null) {
            mutations.add(increment.build());
        }
        if (put != null) {
            mutations.add(put.build());
        }
    }
    if (!hbaseFlush(mutations)) {
        LOG.info("No writes to persist!");
    }
}
Also used : NavigableMap(java.util.NavigableMap) ArrayList(java.util.ArrayList) Update(co.cask.cdap.data2.dataset2.lib.table.Update) IncrementValue(co.cask.cdap.data2.dataset2.lib.table.IncrementValue) PutValue(co.cask.cdap.data2.dataset2.lib.table.PutValue) PutBuilder(co.cask.cdap.data2.util.hbase.PutBuilder) IncrementBuilder(co.cask.cdap.data2.util.hbase.IncrementBuilder) Mutation(org.apache.hadoop.hbase.client.Mutation) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) NavigableMap(java.util.NavigableMap)

Example 2 with IncrementValue

use of co.cask.cdap.data2.dataset2.lib.table.IncrementValue 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 3 with IncrementValue

use of co.cask.cdap.data2.dataset2.lib.table.IncrementValue in project cdap by caskdata.

the class InMemoryTableService method increment.

// todo: remove it from here: only used by "system" metrics table, which should be revised
@Deprecated
public static synchronized Map<byte[], Long> increment(String tableName, byte[] row, Map<byte[], Long> increments) {
    Map<byte[], Long> resultMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    ConcurrentNavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, Update>>> table = tables.get(tableName);
    // get the correct row from the table, create it if it doesn't exist
    NavigableMap<byte[], NavigableMap<Long, Update>> rowMap = table.get(row);
    if (rowMap == null) {
        rowMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
        table.put(row, rowMap);
    }
    // now increment each column, one by one
    long versionForWrite = System.currentTimeMillis();
    for (Map.Entry<byte[], Long> inc : increments.entrySet()) {
        IncrementValue increment = new IncrementValue(inc.getValue());
        // create the column in the row if it does not exist
        NavigableMap<Long, Update> colMap = rowMap.get(inc.getKey());
        Update last = null;
        if (colMap == null) {
            colMap = Maps.newTreeMap();
            rowMap.put(inc.getKey(), colMap);
        } else {
            last = colMap.lastEntry().getValue();
        }
        Update merged = Updates.mergeUpdates(last, increment);
        // put into the column with given version
        long newValue = Bytes.toLong(merged.getBytes());
        resultMap.put(inc.getKey(), newValue);
        colMap.put(versionForWrite, merged);
    }
    return resultMap;
}
Also used : IncrementValue(co.cask.cdap.data2.dataset2.lib.table.IncrementValue) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) Update(co.cask.cdap.data2.dataset2.lib.table.Update) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Aggregations

IncrementValue (co.cask.cdap.data2.dataset2.lib.table.IncrementValue)3 Update (co.cask.cdap.data2.dataset2.lib.table.Update)3 Map (java.util.Map)3 NavigableMap (java.util.NavigableMap)3 PutValue (co.cask.cdap.data2.dataset2.lib.table.PutValue)2 IncrementBuilder (co.cask.cdap.data2.util.hbase.IncrementBuilder)1 PutBuilder (co.cask.cdap.data2.util.hbase.PutBuilder)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 SortedMap (java.util.SortedMap)1 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1