Java Code Examples for org.apache.hadoop.hbase.client.Mutation#getFamilyCellMap()

The following examples show how to use org.apache.hadoop.hbase.client.Mutation#getFamilyCellMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MutationState.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static void logMutationSize(HTableInterface htable, List<Mutation> mutations, PhoenixConnection connection) {
    long byteSize = 0;
    int keyValueCount = 0;
    for (Mutation mutation : mutations) {
        if (mutation.getFamilyCellMap() != null) { // Not a Delete of the row
            for (Entry<byte[], List<Cell>> entry : mutation.getFamilyCellMap().entrySet()) {
                if (entry.getValue() != null) {
                    for (Cell kv : entry.getValue()) {
                        byteSize += CellUtil.estimatedSizeOf(kv);
                        keyValueCount++;
                    }
                }
            }
        }
    }
    logger.debug(LogUtil.addCustomAnnotations("Sending " + mutations.size() + " mutations for " + Bytes.toString(htable.getTableName()) + " with " + keyValueCount + " key values of total size " + byteSize + " bytes", connection));
}
 
Example 2
Source File: TestTableSpaceQuotaViolationNotifier.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(T argument) {
  if (!expected.getClass().isAssignableFrom(argument.getClass())) {
    return false;
  }
  Mutation actual = (Mutation) argument;
  if (!Arrays.equals(expected.getRow(), actual.getRow())) {
    return false;
  }
  if (expected.size() != actual.size()) {
    return false;
  }
  NavigableMap<byte[],List<Cell>> expectedCells = expected.getFamilyCellMap();
  NavigableMap<byte[],List<Cell>> actualCells = actual.getFamilyCellMap();
  Entry<byte[],List<Cell>> expectedEntry = expectedCells.entrySet().iterator().next();
  Entry<byte[],List<Cell>> actualEntry = actualCells.entrySet().iterator().next();
  if (!Arrays.equals(expectedEntry.getKey(), actualEntry.getKey())) {
    return false;
  }
  return Objects.equals(expectedEntry.getValue(), actualEntry.getValue());
}
 
Example 3
Source File: IndexedKeyValue.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static Cell adaptFirstCellFromMutation(Mutation m) {
    if (m != null && m.getFamilyCellMap() != null &&
        m.getFamilyCellMap().firstEntry() != null &&
        m.getFamilyCellMap().firstEntry().getValue() != null
        && m.getFamilyCellMap().firstEntry().getValue().get(0) != null) {
        //have to replace the column family with WALEdit.METAFAMILY to make sure
        //that IndexedKeyValues don't get replicated. The superclass KeyValue fields
        //like row, qualifier and value are placeholders to prevent NPEs
        // when using the KeyValue APIs. See PHOENIX-5188 / 5455
        Cell mutationCell = m.getFamilyCellMap().firstEntry().getValue().get(0);
        CellBuilder builder = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);
        return builder.setFamily(WALEdit.METAFAMILY).
            setQualifier(mutationCell.getQualifierArray()).
            setRow(m.getRow()).
            setTimestamp(mutationCell.getTimestamp()).
            setValue(mutationCell.getValueArray()).setType(Cell.Type.Put).build();
    } else {
        throw new IllegalArgumentException("Tried to create an IndexedKeyValue with a " +
            "Mutation with no Cells!");
    }

}
 
Example 4
Source File: VerifySingleIndexRowTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private List <Mutation> getInvalidActualMutations(TestType testType,
        List<Mutation> actualMutations) {
    List <Mutation> newActualMutations = new ArrayList<>();
    newActualMutations.addAll(actualMutations);
    for (Mutation m : actualMutations) {
        newActualMutations.remove(m);
        NavigableMap<byte[], List<Cell>> familyCellMap = m.getFamilyCellMap();
        List<Cell> cellList = familyCellMap.firstEntry().getValue();
        List<Cell> newCellList = new ArrayList<>();
        byte[] fam = CellUtil.cloneFamily(cellList.get(0));
        for (Cell c : cellList) {
            infiltrateCell(c, newCellList, testType);
        }
        familyCellMap.put(fam, newCellList);
        m.setFamilyCellMap(familyCellMap);
        newActualMutations.add(m);
    }
    return newActualMutations;
}
 
Example 5
Source File: PTableImpl.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void removeIfPresent(Mutation m, byte[] family, byte[] qualifier) {
    Map<byte[],List<Cell>> familyMap = m.getFamilyCellMap();
    List<Cell> kvs = familyMap.get(family);
    if (kvs != null) {
        Iterator<Cell> iterator = kvs.iterator();
        while (iterator.hasNext()) {
            Cell kv = iterator.next();
            if (Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(),
                  qualifier, 0, qualifier.length) == 0) {
                iterator.remove();
            }
        }
    }
}
 
Example 6
Source File: PTableImpl.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void removeIfPresent(Mutation m, byte[] family, byte[] qualifier) {
    Map<byte[],List<Cell>> familyMap = m.getFamilyCellMap();
    List<Cell> kvs = familyMap.get(family);
    if (kvs != null) {
        Iterator<Cell> iterator = kvs.iterator();
        while (iterator.hasNext()) {
            Cell kv = iterator.next();
            if (Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(),
                  qualifier, 0, qualifier.length) == 0) {
                iterator.remove();
                break;
            }
        }
    }
}
 
Example 7
Source File: MutableIndexFailureIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void preBatchMutate(ObserverContext<RegionCoprocessorEnvironment> c, MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
    boolean throwException = false;
    if (FAIL_NEXT_WRITE) {
        throwException = true;
        FAIL_NEXT_WRITE = false;
    } else if (c.getEnvironment().getRegionInfo().getTable().getNameAsString().endsWith("A_" + FAIL_INDEX_NAME)
            && FAIL_WRITE) {
        throwException = true;
        if (TOGGLE_FAIL_WRITE_FOR_RETRY) {
            FAIL_WRITE = !FAIL_WRITE;
        }
    } else {
        // When local index updates are atomic with data updates, testing a write failure to a local
        // index won't make sense.
        Mutation operation = miniBatchOp.getOperation(0);
        if (FAIL_WRITE) {
            Map<byte[],List<Cell>>cellMap = operation.getFamilyCellMap();
            for (Map.Entry<byte[],List<Cell>> entry : cellMap.entrySet()) {
                byte[] family = entry.getKey();
                if (Bytes.toString(family).startsWith(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX)) {
                    int regionStartKeyLen = c.getEnvironment().getRegionInfo().getStartKey().length;
                    Cell firstCell = entry.getValue().get(0);
                    long indexId = MetaDataUtil.getViewIndexIdDataType().getCodec().decodeLong(firstCell.getRowArray(), firstCell.getRowOffset() + regionStartKeyLen, SortOrder.getDefault());
                    // Only throw for first local index as the test may have multiple local indexes
                    if (indexId == Short.MIN_VALUE) {
                        throwException = true;
                        break;
                    }
                }
            }
        }
    }
    if (throwException) {
        if (!TOGGLE_FAIL_WRITE_FOR_RETRY) {
            dropIndex(c);
        }
        throw new DoNotRetryIOException();
    }
}
 
Example 8
Source File: NonTxIndexBuilderTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void assertContains(Collection<Pair<Mutation, byte[]>> indexUpdates,
        final long mutationTs, final byte[] row, final Type cellType, final byte[] fam,
        final byte[] qual, final long cellTs) {
    Predicate<Pair<Mutation, byte[]>> hasCellPredicate =
            new Predicate<Pair<Mutation, byte[]>>() {
                @Override
                public boolean apply(Pair<Mutation, byte[]> input) {
                    assertEquals(TEST_TABLE_INDEX_STRING, Bytes.toString(input.getSecond()));
                    Mutation mutation = input.getFirst();
                    if (mutationTs == mutation.getTimeStamp()) {
                        NavigableMap<byte[], List<Cell>> familyCellMap =
                                mutation.getFamilyCellMap();
                        Cell updateCell = familyCellMap.get(fam).get(0);
                        if (cellType == KeyValue.Type.codeToType(updateCell.getTypeByte())
                                && Bytes.compareTo(fam, CellUtil.cloneFamily(updateCell)) == 0
                                && Bytes.compareTo(qual,
                                    CellUtil.cloneQualifier(updateCell)) == 0
                                && cellTs == updateCell.getTimestamp()) {
                            return true;
                        }
                    }
                    return false;
                }
            };
    Optional<Pair<Mutation, byte[]>> tryFind =
            Iterables.tryFind(indexUpdates, hasCellPredicate);
    assertTrue(tryFind.isPresent());
}