Search in sources :

Example 21 with TimeRange

use of org.apache.hadoop.hbase.io.TimeRange in project hbase by apache.

the class ProtobufUtil method toIncrement.

/**
 * Convert a protocol buffer Mutate to an Increment
 *
 * @param proto the protocol buffer Mutate to convert
 * @return the converted client Increment
 * @throws IOException
 */
public static Increment toIncrement(final MutationProto proto, final CellScanner cellScanner) throws IOException {
    MutationType type = proto.getMutateType();
    assert type == MutationType.INCREMENT : type.name();
    Increment increment = toDelta((Bytes row) -> new Increment(row.get(), row.getOffset(), row.getLength()), Increment::add, proto, cellScanner);
    if (proto.hasTimeRange()) {
        TimeRange timeRange = toTimeRange(proto.getTimeRange());
        increment.setTimeRange(timeRange.getMin(), timeRange.getMax());
    }
    return increment;
}
Also used : Bytes(org.apache.hadoop.hbase.util.Bytes) TimeRange(org.apache.hadoop.hbase.io.TimeRange) MutationType(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType) Increment(org.apache.hadoop.hbase.client.Increment)

Example 22 with TimeRange

use of org.apache.hadoop.hbase.io.TimeRange in project hbase by apache.

the class MultiRowMutationEndpoint method matches.

private boolean matches(Region region, ClientProtos.Condition condition) throws IOException {
    byte[] row = condition.getRow().toByteArray();
    Filter filter = null;
    byte[] family = null;
    byte[] qualifier = null;
    CompareOperator op = null;
    ByteArrayComparable comparator = null;
    if (condition.hasFilter()) {
        filter = ProtobufUtil.toFilter(condition.getFilter());
    } else {
        family = condition.getFamily().toByteArray();
        qualifier = condition.getQualifier().toByteArray();
        op = CompareOperator.valueOf(condition.getCompareType().name());
        comparator = ProtobufUtil.toComparator(condition.getComparator());
    }
    TimeRange timeRange = condition.hasTimeRange() ? ProtobufUtil.toTimeRange(condition.getTimeRange()) : TimeRange.allTime();
    Get get = new Get(row);
    if (family != null) {
        checkFamily(region, family);
        get.addColumn(family, qualifier);
    }
    if (filter != null) {
        get.setFilter(filter);
    }
    if (timeRange != null) {
        get.setTimeRange(timeRange.getMin(), timeRange.getMax());
    }
    boolean matches = false;
    try (RegionScanner scanner = region.getScanner(new Scan(get))) {
        // NOTE: Please don't use HRegion.get() instead,
        // because it will copy cells to heap. See HBASE-26036
        List<Cell> result = new ArrayList<>();
        scanner.next(result);
        if (filter != null) {
            if (!result.isEmpty()) {
                matches = true;
            }
        } else {
            boolean valueIsNull = comparator.getValue() == null || comparator.getValue().length == 0;
            if (result.isEmpty() && valueIsNull) {
                matches = true;
            } else if (result.size() > 0 && result.get(0).getValueLength() == 0 && valueIsNull) {
                matches = true;
            } else if (result.size() == 1 && !valueIsNull) {
                Cell kv = result.get(0);
                int compareResult = PrivateCellUtil.compareValue(kv, comparator);
                matches = matches(op, compareResult);
            }
        }
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) CompareOperator(org.apache.hadoop.hbase.CompareOperator) TimeRange(org.apache.hadoop.hbase.io.TimeRange) ByteArrayComparable(org.apache.hadoop.hbase.filter.ByteArrayComparable) RegionScanner(org.apache.hadoop.hbase.regionserver.RegionScanner) Filter(org.apache.hadoop.hbase.filter.Filter) Get(org.apache.hadoop.hbase.client.Get) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell)

Example 23 with TimeRange

use of org.apache.hadoop.hbase.io.TimeRange in project hbase by apache.

the class TestSerialization method testGet.

@Test
public void testGet() throws Exception {
    byte[] row = Bytes.toBytes("row");
    byte[] fam = Bytes.toBytes("fam");
    byte[] qf1 = Bytes.toBytes("qf1");
    long ts = EnvironmentEdgeManager.currentTime();
    int maxVersions = 2;
    Get get = new Get(row);
    get.addColumn(fam, qf1);
    get.setTimeRange(ts, ts + 1);
    get.readVersions(maxVersions);
    ClientProtos.Get getProto = ProtobufUtil.toGet(get);
    Get desGet = ProtobufUtil.toGet(getProto);
    assertTrue(Bytes.equals(get.getRow(), desGet.getRow()));
    Set<byte[]> set = null;
    Set<byte[]> desSet = null;
    for (Map.Entry<byte[], NavigableSet<byte[]>> entry : get.getFamilyMap().entrySet()) {
        assertTrue(desGet.getFamilyMap().containsKey(entry.getKey()));
        set = entry.getValue();
        desSet = desGet.getFamilyMap().get(entry.getKey());
        for (byte[] qualifier : set) {
            assertTrue(desSet.contains(qualifier));
        }
    }
    assertEquals(get.getMaxVersions(), desGet.getMaxVersions());
    TimeRange tr = get.getTimeRange();
    TimeRange desTr = desGet.getTimeRange();
    assertEquals(tr.getMax(), desTr.getMax());
    assertEquals(tr.getMin(), desTr.getMin());
}
Also used : TimeRange(org.apache.hadoop.hbase.io.TimeRange) NavigableSet(java.util.NavigableSet) Get(org.apache.hadoop.hbase.client.Get) ClientProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos) Map(java.util.Map) Test(org.junit.Test)

Example 24 with TimeRange

use of org.apache.hadoop.hbase.io.TimeRange in project hbase by apache.

the class ThriftUtilities method scanFromHBase.

public static TScan scanFromHBase(Scan in) throws IOException {
    TScan out = new TScan();
    out.setStartRow(in.getStartRow());
    out.setStopRow(in.getStopRow());
    out.setCaching(in.getCaching());
    out.setMaxVersions(in.getMaxVersions());
    for (Map.Entry<byte[], NavigableSet<byte[]>> family : in.getFamilyMap().entrySet()) {
        if (family.getValue() != null && !family.getValue().isEmpty()) {
            for (byte[] qualifier : family.getValue()) {
                TColumn column = new TColumn();
                column.setFamily(family.getKey());
                column.setQualifier(qualifier);
                out.addToColumns(column);
            }
        } else {
            TColumn column = new TColumn();
            column.setFamily(family.getKey());
            out.addToColumns(column);
        }
    }
    TTimeRange tTimeRange = new TTimeRange();
    tTimeRange.setMinStamp(in.getTimeRange().getMin()).setMaxStamp(in.getTimeRange().getMax());
    out.setTimeRange(tTimeRange);
    out.setBatchSize(in.getBatch());
    for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
        out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())), ByteBuffer.wrap(attribute.getValue()));
    }
    try {
        Authorizations authorizations = in.getAuthorizations();
        if (authorizations != null) {
            TAuthorization tAuthorization = new TAuthorization();
            tAuthorization.setLabels(authorizations.getLabels());
            out.setAuthorizations(tAuthorization);
        }
    } catch (DeserializationException e) {
        throw new RuntimeException(e);
    }
    out.setReversed(in.isReversed());
    out.setCacheBlocks(in.getCacheBlocks());
    out.setReadType(readTypeFromHBase(in.getReadType()));
    out.setLimit(in.getLimit());
    out.setConsistency(consistencyFromHBase(in.getConsistency()));
    out.setTargetReplicaId(in.getReplicaId());
    for (Map.Entry<byte[], TimeRange> entry : in.getColumnFamilyTimeRange().entrySet()) {
        if (entry.getValue() != null) {
            TTimeRange timeRange = new TTimeRange();
            timeRange.setMinStamp(entry.getValue().getMin()).setMaxStamp(entry.getValue().getMax());
            out.putToColFamTimeRangeMap(ByteBuffer.wrap(entry.getKey()), timeRange);
        }
    }
    if (in.getFilter() != null) {
        try {
            out.setFilterBytes(filterFromHBase(in.getFilter()));
        } catch (IOException ioE) {
            throw new RuntimeException(ioE);
        }
    }
    return out;
}
Also used : NavigableSet(java.util.NavigableSet) Authorizations(org.apache.hadoop.hbase.security.visibility.Authorizations) TColumn(org.apache.hadoop.hbase.thrift2.generated.TColumn) TTimeRange(org.apache.hadoop.hbase.thrift2.generated.TTimeRange) TAuthorization(org.apache.hadoop.hbase.thrift2.generated.TAuthorization) IOException(java.io.IOException) DeserializationException(org.apache.hadoop.hbase.exceptions.DeserializationException) TTimeRange(org.apache.hadoop.hbase.thrift2.generated.TTimeRange) TimeRange(org.apache.hadoop.hbase.io.TimeRange) TScan(org.apache.hadoop.hbase.thrift2.generated.TScan) Map(java.util.Map)

Example 25 with TimeRange

use of org.apache.hadoop.hbase.io.TimeRange in project hbase by apache.

the class TestSimpleTimeRangeTracker method testRangeConstruction.

@Test
public void testRangeConstruction() throws IOException {
    TimeRange defaultRange = TimeRange.allTime();
    assertEquals(0L, defaultRange.getMin());
    assertEquals(Long.MAX_VALUE, defaultRange.getMax());
    assertTrue(defaultRange.isAllTime());
    TimeRange oneArgRange = TimeRange.from(0L);
    assertEquals(0L, oneArgRange.getMin());
    assertEquals(Long.MAX_VALUE, oneArgRange.getMax());
    assertTrue(oneArgRange.isAllTime());
    TimeRange oneArgRange2 = TimeRange.from(1);
    assertEquals(1, oneArgRange2.getMin());
    assertEquals(Long.MAX_VALUE, oneArgRange2.getMax());
    assertFalse(oneArgRange2.isAllTime());
    TimeRange twoArgRange = TimeRange.between(0L, Long.MAX_VALUE);
    assertEquals(0L, twoArgRange.getMin());
    assertEquals(Long.MAX_VALUE, twoArgRange.getMax());
    assertTrue(twoArgRange.isAllTime());
    TimeRange twoArgRange2 = TimeRange.between(0L, Long.MAX_VALUE - 1);
    assertEquals(0L, twoArgRange2.getMin());
    assertEquals(Long.MAX_VALUE - 1, twoArgRange2.getMax());
    assertFalse(twoArgRange2.isAllTime());
    TimeRange twoArgRange3 = TimeRange.between(1, Long.MAX_VALUE);
    assertEquals(1, twoArgRange3.getMin());
    assertEquals(Long.MAX_VALUE, twoArgRange3.getMax());
    assertFalse(twoArgRange3.isAllTime());
}
Also used : TimeRange(org.apache.hadoop.hbase.io.TimeRange) Test(org.junit.Test)

Aggregations

TimeRange (org.apache.hadoop.hbase.io.TimeRange)45 Test (org.junit.Test)11 Map (java.util.Map)10 Get (org.apache.hadoop.hbase.client.Get)10 Scan (org.apache.hadoop.hbase.client.Scan)10 Cell (org.apache.hadoop.hbase.Cell)8 NavigableSet (java.util.NavigableSet)7 NameBytesPair (org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameBytesPair)7 HashMap (java.util.HashMap)6 Filter (org.apache.hadoop.hbase.filter.Filter)6 NameBytesPair (org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameBytesPair)6 ByteString (com.google.protobuf.ByteString)5 ArrayList (java.util.ArrayList)5 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)5 Put (org.apache.hadoop.hbase.client.Put)5 List (java.util.List)4 Increment (org.apache.hadoop.hbase.client.Increment)4 Result (org.apache.hadoop.hbase.client.Result)4 Column (org.apache.hadoop.hbase.protobuf.generated.ClientProtos.Column)4 Column (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Column)4