Search in sources :

Example 1 with PutValue

use of io.cdap.cdap.data2.dataset2.lib.table.PutValue 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 PutValue

use of io.cdap.cdap.data2.dataset2.lib.table.PutValue 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 PutValue

use of io.cdap.cdap.data2.dataset2.lib.table.PutValue in project cdap by caskdata.

the class InMemoryTableService method swap.

public static synchronized boolean swap(String tableName, byte[] row, byte[] column, byte[] oldValue, byte[] newValue) {
    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);
    Update existingValue = null;
    if (rowMap != null) {
        NavigableMap<Long, Update> columnMap = rowMap.get(column);
        if (columnMap != null) {
            existingValue = columnMap.lastEntry().getValue();
        }
    }
    // verify existing value matches
    if (oldValue == null && existingValue != null) {
        return false;
    }
    if (oldValue != null && (existingValue == null || !Bytes.equals(oldValue, existingValue.getBytes()))) {
        return false;
    }
    // write new value
    if (newValue == null) {
        if (rowMap != null) {
            rowMap.remove(column);
        }
    } else {
        if (rowMap == null) {
            rowMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
            table.put(row, rowMap);
        }
        NavigableMap<Long, Update> columnMap = rowMap.get(column);
        if (columnMap == null) {
            columnMap = Maps.newTreeMap();
            rowMap.put(column, columnMap);
        }
        PutValue newPut = new PutValue(newValue);
        columnMap.put(System.currentTimeMillis(), newPut);
    }
    return true;
}
Also used : PutValue(co.cask.cdap.data2.dataset2.lib.table.PutValue) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) Update(co.cask.cdap.data2.dataset2.lib.table.Update)

Example 4 with PutValue

use of io.cdap.cdap.data2.dataset2.lib.table.PutValue 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<>();
    List<Increment> increments = 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) {
            increments.add(increment.build());
        }
        if (put != null) {
            mutations.add(put.build());
        }
    }
    if (!hbaseFlush(mutations) && increments.isEmpty()) {
        LOG.info("No writes to persist!");
    }
    if (!increments.isEmpty()) {
        table.batch(increments, new Object[increments.size()]);
    }
}
Also used : NavigableMap(java.util.NavigableMap) ArrayList(java.util.ArrayList) Update(io.cdap.cdap.data2.dataset2.lib.table.Update) IncrementValue(io.cdap.cdap.data2.dataset2.lib.table.IncrementValue) PutValue(io.cdap.cdap.data2.dataset2.lib.table.PutValue) PutBuilder(io.cdap.cdap.data2.util.hbase.PutBuilder) IncrementBuilder(io.cdap.cdap.data2.util.hbase.IncrementBuilder) Increment(org.apache.hadoop.hbase.client.Increment) Mutation(org.apache.hadoop.hbase.client.Mutation) Map(java.util.Map) NavigableMap(java.util.NavigableMap)

Example 5 with PutValue

use of io.cdap.cdap.data2.dataset2.lib.table.PutValue in project cdap by caskdata.

the class InMemoryTableService method swap.

public static synchronized boolean swap(String tableName, byte[] row, byte[] column, byte[] oldValue, byte[] newValue) {
    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);
    Update existingValue = null;
    if (rowMap != null) {
        NavigableMap<Long, Update> columnMap = rowMap.get(column);
        if (columnMap != null) {
            existingValue = columnMap.lastEntry().getValue();
        }
    }
    // verify existing value matches
    if (oldValue == null && existingValue != null) {
        return false;
    }
    if (oldValue != null && (existingValue == null || !Bytes.equals(oldValue, existingValue.getBytes()))) {
        return false;
    }
    // write new value
    if (newValue == null) {
        if (rowMap != null) {
            rowMap.remove(column);
        }
    } else {
        if (rowMap == null) {
            rowMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
            table.put(row, rowMap);
        }
        NavigableMap<Long, Update> columnMap = rowMap.get(column);
        if (columnMap == null) {
            columnMap = Maps.newTreeMap();
            rowMap.put(column, columnMap);
        }
        PutValue newPut = new PutValue(newValue);
        columnMap.put(System.currentTimeMillis(), newPut);
    }
    return true;
}
Also used : PutValue(io.cdap.cdap.data2.dataset2.lib.table.PutValue) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) Update(io.cdap.cdap.data2.dataset2.lib.table.Update)

Aggregations

NavigableMap (java.util.NavigableMap)8 PutValue (co.cask.cdap.data2.dataset2.lib.table.PutValue)4 Update (co.cask.cdap.data2.dataset2.lib.table.Update)4 PutValue (io.cdap.cdap.data2.dataset2.lib.table.PutValue)4 Update (io.cdap.cdap.data2.dataset2.lib.table.Update)4 Map (java.util.Map)4 IncrementValue (co.cask.cdap.data2.dataset2.lib.table.IncrementValue)2 IncrementValue (io.cdap.cdap.data2.dataset2.lib.table.IncrementValue)2 ArrayList (java.util.ArrayList)2 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)2 Mutation (org.apache.hadoop.hbase.client.Mutation)2 Transaction (org.apache.tephra.Transaction)2 Test (org.junit.Test)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 IncrementBuilder (io.cdap.cdap.data2.util.hbase.IncrementBuilder)1 PutBuilder (io.cdap.cdap.data2.util.hbase.PutBuilder)1 Increment (org.apache.hadoop.hbase.client.Increment)1