Search in sources :

Example 76 with NavigableMap

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

the class IncrementHandler method preDelete.

@Override
public void preDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit, Durability durability) throws IOException {
    boolean transactional = state.containsTransactionalFamily(delete.getFamilyCellMap().keySet());
    if (!transactional) {
        long tsToAssign = state.getUniqueTimestamp();
        delete.setTimestamp(tsToAssign);
        // new key values
        NavigableMap<byte[], List<Cell>> newFamilyMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);
        for (Map.Entry<byte[], List<Cell>> entry : delete.getFamilyCellMap().entrySet()) {
            List<Cell> newCells = new ArrayList<>(entry.getValue().size());
            for (Cell kv : entry.getValue()) {
                // replace the timestamp
                newCells.add(CellUtil.createCell(CellUtil.cloneRow(kv), CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv), tsToAssign, kv.getTypeByte(), CellUtil.cloneValue(kv)));
            }
            newFamilyMap.put(entry.getKey(), newCells);
        }
        delete.setFamilyCellMap(newFamilyMap);
    }
}
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 77 with NavigableMap

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

the class IncrementHandler method preDelete.

@Override
public void preDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit, Durability durability) throws IOException {
    boolean transactional = state.containsTransactionalFamily(delete.getFamilyCellMap().keySet());
    if (!transactional) {
        long tsToAssign = state.getUniqueTimestamp();
        delete.setTimestamp(tsToAssign);
        // new key values
        NavigableMap<byte[], List<Cell>> newFamilyMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);
        for (Map.Entry<byte[], List<Cell>> entry : delete.getFamilyCellMap().entrySet()) {
            List<Cell> newCells = new ArrayList<>(entry.getValue().size());
            for (Cell kv : entry.getValue()) {
                // replace the timestamp
                newCells.add(CellUtil.createCell(CellUtil.cloneRow(kv), CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv), tsToAssign, kv.getTypeByte(), CellUtil.cloneValue(kv)));
            }
            newFamilyMap.put(entry.getKey(), newCells);
        }
        delete.setFamilyCellMap(newFamilyMap);
    }
}
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 78 with NavigableMap

use of java.util.NavigableMap 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)

Example 79 with NavigableMap

use of java.util.NavigableMap 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 80 with NavigableMap

use of java.util.NavigableMap 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;
    }
    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(), true);
                        increment.add(columnFamily, column.getKey(), tx.getWritePointer(), ((IncrementValue) val).getValue());
                    } else {
                        incrementPut = getPutForIncrement(incrementPut, row.getKey());
                        incrementPut.add(columnFamily, column.getKey(), tx.getWritePointer(), Bytes.toBytes(((IncrementValue) val).getValue()));
                    }
                } else if (val instanceof PutValue) {
                    put = getPut(put, row.getKey());
                    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());
                    incrementPut.add(columnFamily, column.getKey(), Bytes.toBytes(((IncrementValue) val).getValue()));
                } else if (val instanceof PutValue) {
                    put = getPut(put, row.getKey());
                    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 : 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) NavigableMap(java.util.NavigableMap) IncrementBuilder(co.cask.cdap.data2.util.hbase.IncrementBuilder) ArrayList(java.util.ArrayList) Mutation(org.apache.hadoop.hbase.client.Mutation) Update(co.cask.cdap.data2.dataset2.lib.table.Update) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) NavigableMap(java.util.NavigableMap)

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