org.apache.hadoop.hbase.io.TimeRange Java Examples

The following examples show how to use org.apache.hadoop.hbase.io.TimeRange. 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: ScanRanges.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static TimeRange getRowTimestampColumnRange(List<List<KeyRange>> ranges, RowKeySchema schema, int rowTimestampColPos) {
    try {
        if (rowTimestampColPos != -1) {
            if (ranges != null && ranges.size() > rowTimestampColPos) {
                List<KeyRange> rowTimestampColRange = ranges.get(rowTimestampColPos);
                List<KeyRange> sortedRange = new ArrayList<>(rowTimestampColRange);
                Field f = schema.getField(rowTimestampColPos);
                Collections.sort(sortedRange, f.getSortOrder() == SortOrder.ASC ? KeyRange.COMPARATOR : KeyRange.DESC_COMPARATOR);
                SortOrder order = f.getSortOrder();
                KeyRange lowestRange = sortedRange.get(0);
                KeyRange highestRange = sortedRange.get(rowTimestampColRange.size() - 1);
                if (order == SortOrder.DESC) {
                    return getDescTimeRange(lowestRange, highestRange, f);
                }
                return getAscTimeRange( lowestRange, highestRange, f);
            }
        }
    } catch (IOException e) {
        Throwables.propagate(e);
    }
    return null;
}
 
Example #2
Source File: UserScanQueryMatcher.java    From hbase with Apache License 2.0 6 votes vote down vote up
protected UserScanQueryMatcher(Scan scan, ScanInfo scanInfo, ColumnTracker columns,
    boolean hasNullColumn, long oldestUnexpiredTS, long now) {
  super(createStartKey(scan, scanInfo), scanInfo, columns, oldestUnexpiredTS, now);
  this.hasNullColumn = hasNullColumn;
  this.filter = scan.getFilter();
  if (this.filter != null) {
    this.versionsAfterFilter =
        scan.isRaw() ? scan.getMaxVersions() : Math.min(scan.getMaxVersions(),
          scanInfo.getMaxVersions());
  } else {
    this.versionsAfterFilter = 0;
  }
  this.stopRow = scan.getStopRow();
  TimeRange timeRange = scan.getColumnFamilyTimeRange().get(scanInfo.getFamily());
  if (timeRange == null) {
    this.tr = scan.getTimeRange();
  } else {
    this.tr = timeRange;
  }
}
 
Example #3
Source File: RequestConverter.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Create a protocol buffer Condition
 *
 * @return a Condition
 * @throws IOException
 */
public static Condition buildCondition(final byte[] row, final byte[] family,
  final byte[] qualifier, final CompareOperator op, final byte[] value, final Filter filter,
  final TimeRange timeRange) throws IOException {

  Condition.Builder builder = Condition.newBuilder().setRow(UnsafeByteOperations.unsafeWrap(row));

  if (filter != null) {
    builder.setFilter(ProtobufUtil.toFilter(filter));
  } else {
    builder.setFamily(UnsafeByteOperations.unsafeWrap(family))
      .setQualifier(UnsafeByteOperations.unsafeWrap(
        qualifier == null ? HConstants.EMPTY_BYTE_ARRAY : qualifier))
      .setComparator(ProtobufUtil.toComparator(new BinaryComparator(value)))
      .setCompareType(CompareType.valueOf(op.name()));
  }

  return builder.setTimeRange(ProtobufUtil.toTimeRange(timeRange)).build();
}
 
Example #4
Source File: TestRegionCoprocessorHost.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testPreStoreScannerOpen() throws IOException {

  RegionCoprocessorHost host = new RegionCoprocessorHost(region, rsServices, conf);
  Scan scan = new Scan();
  scan.setTimeRange(TimeRange.INITIAL_MIN_TIMESTAMP, TimeRange.INITIAL_MAX_TIMESTAMP);
  assertTrue("Scan is not for all time", scan.getTimeRange().isAllTime());
  //SimpleRegionObserver is set to update the ScanInfo parameters if the passed-in scan
  //is for all time. this lets us exercise both that the Scan is wired up properly in the coproc
  //and that we can customize the metadata

  ScanInfo oldScanInfo = getScanInfo();

  HStore store = mock(HStore.class);
  when(store.getScanInfo()).thenReturn(oldScanInfo);
  ScanInfo newScanInfo = host.preStoreScannerOpen(store, scan);

  verifyScanInfo(newScanInfo);
}
 
Example #5
Source File: ScanRanges.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private ScanRanges (RowKeySchema schema, int[] slotSpan, List<List<KeyRange>> ranges, KeyRange scanRange, boolean useSkipScanFilter, boolean isPointLookup, Integer bucketNum, TimeRange rowTimestampRange) {
    this.isPointLookup = isPointLookup;
    this.isSalted = bucketNum != null;
    this.useSkipScanFilter = useSkipScanFilter;
    this.scanRange = scanRange;
    this.rowTimestampRange = rowTimestampRange;

    if (isSalted && !isPointLookup) {
        ranges.set(0, SaltingUtil.generateAllSaltingRanges(bucketNum));
    }
    this.ranges = ImmutableList.copyOf(ranges);
    this.slotSpan = slotSpan;
    this.schema = schema;
    if (schema != null && !ranges.isEmpty()) {
        if (!this.useSkipScanFilter) {
            int boundSlotCount = this.getBoundSlotCount();
            ranges = ranges.subList(0, boundSlotCount);
            slotSpan = Arrays.copyOf(slotSpan, boundSlotCount);
        }
        this.filter = new SkipScanFilter(ranges, slotSpan, this.schema);
    }
}
 
Example #6
Source File: ScanRanges.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static TimeRange getAscTimeRange(KeyRange lowestRange, KeyRange highestRange, Field f)
        throws IOException {
    long low;
    long high;
    PDataCodec codec = PLong.INSTANCE.getCodec();
    if (lowestRange.lowerUnbound()) {
        low = 0;
    } else {
        long lowerRange = codec.decodeLong(lowestRange.getLowerRange(), 0, SortOrder.ASC);
        low = lowestRange.isLowerInclusive() ? lowerRange : safelyIncrement(lowerRange);
    }
    if (highestRange.upperUnbound()) {
        high = HConstants.LATEST_TIMESTAMP;
    } else {
        long upperRange = codec.decodeLong(highestRange.getUpperRange(), 0, SortOrder.ASC);
        if (highestRange.isUpperInclusive()) {
            high = safelyIncrement(upperRange);
        } else {
            high = upperRange;
        }
    }
    return new TimeRange(low, high);
}
 
Example #7
Source File: RequestConverter.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Create a protocol buffer MutateRequest for a conditioned put/delete
 *
 * @return a mutate request
 * @throws IOException
 */
public static MutateRequest buildMutateRequest(final byte[] regionName, final byte[] row,
  final byte[] family, final byte[] qualifier, final CompareOperator op, final byte[] value,
  final Filter filter, final TimeRange timeRange, final Mutation mutation) throws IOException {
  MutationType type;
  if (mutation instanceof Put) {
    type = MutationType.PUT;
  } else {
    type = MutationType.DELETE;
  }
  return MutateRequest.newBuilder()
    .setRegion(buildRegionSpecifier(RegionSpecifierType.REGION_NAME, regionName))
    .setMutation(ProtobufUtil.toMutation(type, mutation))
    .setCondition(buildCondition(row, family, qualifier, op, value, filter, timeRange))
    .build();
}
 
Example #8
Source File: BaseScannerRegionObserver.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public void preScannerOpen(org.apache.hadoop.hbase.coprocessor.ObserverContext<RegionCoprocessorEnvironment> c,
        Scan scan) throws IOException {
    byte[] txnScn = scan.getAttribute(TX_SCN);
    if (txnScn!=null) {
        TimeRange timeRange = scan.getTimeRange();
        scan.setTimeRange(timeRange.getMin(), Bytes.toLong(txnScn));
    }
    if (isRegionObserverFor(scan)) {
        // For local indexes, we need to throw if out of region as we'll get inconsistent
        // results otherwise while in other cases, it may just mean out client-side data
        // on region boundaries is out of date and can safely be ignored.
        if (!skipRegionBoundaryCheck(scan) || ScanUtil.isLocalIndex(scan)) {
            throwIfScanOutOfRegion(scan, c.getEnvironment().getRegion());
        }
        // Muck with the start/stop row of the scan and set as reversed at the
        // last possible moment. You need to swap the start/stop and make the
        // start exclusive and the stop inclusive.
        ScanUtil.setupReverseScan(scan);
    }
}
 
Example #9
Source File: MockHTable.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
private static List<Cell> toKeyValue(byte[] row, NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> rowdata, TimeRange timeRange, int maxVersions) {
    List<Cell> ret = new ArrayList<>();
    for (byte[] family : rowdata.keySet()) {
        for (byte[] qualifier : rowdata.get(family).keySet()) {
            int versionsAdded = 0;
            for (Map.Entry<Long, byte[]> tsToVal : rowdata.get(family).get(qualifier).descendingMap().entrySet()) {
                if (versionsAdded == maxVersions)
                    break;
                Long timestamp = tsToVal.getKey();
                if (!timeRange.withinTimeRange(timestamp))
                    continue;
                byte[] value = tsToVal.getValue();
                ret.add(new KeyValue(row, family, qualifier, timestamp, value));
                versionsAdded++;
            }
        }
    }
    return ret;
}
 
Example #10
Source File: CheckAndMutate.java    From hbase with Apache License 2.0 5 votes vote down vote up
private CheckAndMutate(byte[] row, Filter filter, TimeRange timeRange, Row action) {
  super(row, HConstants.LATEST_TIMESTAMP, Collections.emptyNavigableMap());
  this.family = null;
  this.qualifier = null;
  this.op = null;
  this.value = null;
  this.filter = filter;
  this.timeRange = timeRange;
  this.action = action;
}
 
Example #11
Source File: ScanRanges.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static TimeRange getDescTimeRange(KeyRange lowestKeyRange, KeyRange highestKeyRange, Field f) throws IOException {
    boolean lowerUnbound = lowestKeyRange.lowerUnbound();
    boolean lowerInclusive = lowestKeyRange.isLowerInclusive();
    boolean upperUnbound = highestKeyRange.upperUnbound();
    boolean upperInclusive = highestKeyRange.isUpperInclusive();
    PDataCodec codec = PLong.INSTANCE.getCodec();
    long low = lowerUnbound ? -1 : codec.decodeLong(lowestKeyRange.getLowerRange(), 0, SortOrder.DESC);
    long high = upperUnbound ? -1 : codec.decodeLong(highestKeyRange.getUpperRange(), 0, SortOrder.DESC);
    long newHigh;
    long newLow;
    if (!lowerUnbound && !upperUnbound) {
        newHigh = lowerInclusive ? safelyIncrement(low) : low;
        newLow = upperInclusive ? high : safelyIncrement(high);
        return new TimeRange(newLow, newHigh);
    } else if (!lowerUnbound && upperUnbound) {
        newHigh = lowerInclusive ? safelyIncrement(low) : low;
        newLow = 0;
        return new TimeRange(newLow, newHigh);
    } else if (lowerUnbound && !upperUnbound) {
        newLow = upperInclusive ? high : safelyIncrement(high);
        newHigh = HConstants.LATEST_TIMESTAMP;
        return new TimeRange(newLow, newHigh);
    } else {
        newLow = 0;
        newHigh = HConstants.LATEST_TIMESTAMP;
        return new TimeRange(newLow, newHigh);
    }
}
 
Example #12
Source File: TestProtobufUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Test basic Scan conversions.
 *
 * @throws IOException if the conversion to a {@link org.apache.hadoop.hbase.client.Scan} fails
 */
@Test
public void testScan() throws IOException {
  ClientProtos.Scan.Builder scanBuilder = ClientProtos.Scan.newBuilder();
  scanBuilder.setStartRow(ByteString.copyFromUtf8("row1"));
  scanBuilder.setStopRow(ByteString.copyFromUtf8("row2"));
  Column.Builder columnBuilder = Column.newBuilder();
  columnBuilder.setFamily(ByteString.copyFromUtf8("f1"));
  columnBuilder.addQualifier(ByteString.copyFromUtf8("c1"));
  columnBuilder.addQualifier(ByteString.copyFromUtf8("c2"));
  scanBuilder.addColumn(columnBuilder.build());

  columnBuilder.clear();
  columnBuilder.setFamily(ByteString.copyFromUtf8("f2"));
  scanBuilder.addColumn(columnBuilder.build());

  ClientProtos.Scan proto = scanBuilder.build();

  // Verify default values
  assertEquals(1, proto.getMaxVersions());
  assertEquals(true, proto.getCacheBlocks());

  // Verify fields survive ClientProtos.Scan -> Scan -> ClientProtos.Scan
  // conversion
  scanBuilder = ClientProtos.Scan.newBuilder(proto);
  scanBuilder.setMaxVersions(2);
  scanBuilder.setCacheBlocks(false);
  scanBuilder.setCaching(1024);
  scanBuilder.setTimeRange(ProtobufUtil.toTimeRange(TimeRange.allTime()));
  scanBuilder.setIncludeStopRow(false);
  ClientProtos.Scan expectedProto = scanBuilder.build();

  ClientProtos.Scan actualProto = ProtobufUtil.toScan(
      ProtobufUtil.toScan(expectedProto));
  assertEquals(expectedProto, actualProto);
}
 
Example #13
Source File: RequestConverter.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Create a protocol buffer MutateRequest for conditioned row mutations
 *
 * @return a mutate request
 * @throws IOException
 */
public static ClientProtos.MultiRequest buildMutateRequest(final byte[] regionName,
  final byte[] row, final byte[] family, final byte[] qualifier,
  final CompareOperator op, final byte[] value, final Filter filter, final TimeRange timeRange,
  final RowMutations rowMutations) throws IOException {
  RegionAction.Builder builder =
      getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName);
  builder.setAtomic(true);
  ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
  MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
  for (Mutation mutation: rowMutations.getMutations()) {
    MutationType mutateType;
    if (mutation instanceof Put) {
      mutateType = MutationType.PUT;
    } else if (mutation instanceof Delete) {
      mutateType = MutationType.DELETE;
    } else {
      throw new DoNotRetryIOException("RowMutations supports only put and delete, not " +
          mutation.getClass().getName());
    }
    mutationBuilder.clear();
    MutationProto mp = ProtobufUtil.toMutation(mutateType, mutation, mutationBuilder);
    actionBuilder.clear();
    actionBuilder.setMutation(mp);
    builder.addAction(actionBuilder.build());
  }
  return ClientProtos.MultiRequest.newBuilder().addRegionAction(builder.setCondition(
    buildCondition(row, family, qualifier, op, value, filter, timeRange)).build()).build();
}
 
Example #14
Source File: TestAsyncTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
@Deprecated
public void testCheckAndMutateWithTimeRangeForOldApi() throws Exception {
  AsyncTable<?> table = getTable.get();
  final long ts = System.currentTimeMillis() / 2;
  Put put = new Put(row);
  put.addColumn(FAMILY, QUALIFIER, ts, VALUE);

  boolean ok =
    table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).ifNotExists().thenPut(put).get();
  assertTrue(ok);

  ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts + 10000))
    .ifEquals(VALUE).thenPut(put).get();
  assertFalse(ok);

  ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts))
    .ifEquals(VALUE).thenPut(put).get();
  assertTrue(ok);

  RowMutations rm = new RowMutations(row).add((Mutation) put);

  ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts + 10000))
    .ifEquals(VALUE).thenMutate(rm).get();
  assertFalse(ok);

  ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts))
    .ifEquals(VALUE).thenMutate(rm).get();
  assertTrue(ok);

  Delete delete = new Delete(row).addColumn(FAMILY, QUALIFIER);

  ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts + 10000))
    .ifEquals(VALUE).thenDelete(delete).get();
  assertFalse(ok);

  ok = table.checkAndMutate(row, FAMILY).qualifier(QUALIFIER).timeRange(TimeRange.at(ts))
    .ifEquals(VALUE).thenDelete(delete).get();
  assertTrue(ok);
}
 
Example #15
Source File: TestAsyncTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
@Deprecated
public void testCheckAndMutateWithFilterAndTimeRangeForOldApi() throws Throwable {
  AsyncTable<?> table = getTable.get();

  // Put with specifying the timestamp
  table.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")))
    .get();

  // Put with success
  boolean ok = table.checkAndMutate(row, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
      CompareOperator.EQUAL, Bytes.toBytes("a")))
    .timeRange(TimeRange.between(0, 101))
    .thenPut(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")))
    .get();
  assertTrue(ok);

  Result result = table.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B"))).get();
  assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));

  // Put with failure
  ok = table.checkAndMutate(row, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
      CompareOperator.EQUAL, Bytes.toBytes("a")))
    .timeRange(TimeRange.between(0, 100))
    .thenPut(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")))
    .get();
  assertFalse(ok);

  assertFalse(table.exists(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).get());
}
 
Example #16
Source File: CheckAndMutate.java    From hbase with Apache License 2.0 5 votes vote down vote up
private CheckAndMutate(byte[] row, byte[] family, byte[] qualifier,final CompareOperator op,
  byte[] value, TimeRange timeRange, Row action) {
  super(row, HConstants.LATEST_TIMESTAMP, Collections.emptyNavigableMap());
  this.family = family;
  this.qualifier = qualifier;
  this.op = op;
  this.value = value;
  this.filter = null;
  this.timeRange = timeRange;
  this.action = action;
}
 
Example #17
Source File: TimeRangeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @return Make a TimeRange from current state of <code>this</code>.
 */
TimeRange toTimeRange() {
  long min = getMin();
  long max = getMax();
  // Initial TimeRangeTracker timestamps are the opposite of what you want for a TimeRange. Fix!
  if (min == INITIAL_MIN_TIMESTAMP) {
    min = TimeRange.INITIAL_MIN_TIMESTAMP;
  }
  if (max == INITIAL_MAX_TIMESTAMP) {
    max = TimeRange.INITIAL_MAX_TIMESTAMP;
  }
  return new TimeRange(min, max);
}
 
Example #18
Source File: TestAsyncTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckAndMutateWithFilterAndTimeRange() throws Throwable {
  AsyncTable<?> table = getTable.get();

  // Put with specifying the timestamp
  table.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")))
    .get();

  // Put with success
  boolean ok = table.checkAndMutate(CheckAndMutate.newBuilder(row)
    .ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
      CompareOperator.EQUAL, Bytes.toBytes("a")))
    .timeRange(TimeRange.between(0, 101))
    .build(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")))).get();
  assertTrue(ok);

  Result result = table.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B"))).get();
  assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));

  // Put with failure
  ok = table.checkAndMutate(CheckAndMutate.newBuilder(row)
    .ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
      CompareOperator.EQUAL, Bytes.toBytes("a")))
    .timeRange(TimeRange.between(0, 100))
    .build(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))))
    .get();
  assertFalse(ok);

  assertFalse(table.exists(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).get());
}
 
Example #19
Source File: ProtobufUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static HBaseProtos.TimeRange toTimeRange(TimeRange timeRange) {
  if (timeRange == null) {
    timeRange = TimeRange.allTime();
  }
  return HBaseProtos.TimeRange.newBuilder().setFrom(timeRange.getMin())
    .setTo(timeRange.getMax())
    .build();
}
 
Example #20
Source File: ScanUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static void setTimeRange(Scan scan, TimeRange range) {
    try {
        scan.setTimeRange(range.getMin(), range.getMax());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #21
Source File: TestCheckAndMutate.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
@Deprecated
public void testCheckAndMutateWithFilterAndTimeRangeForOldApi() throws Throwable {
  try (Table table = createTable()) {
    // Put with specifying the timestamp
    table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));

    // Put with success
    boolean ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY,
        Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")))
      .timeRange(TimeRange.between(0, 101))
      .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")));
    assertTrue(ok);

    Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));

    // Put with failure
    ok = table.checkAndMutate(ROWKEY, new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
        CompareOperator.EQUAL, Bytes.toBytes("a")))
      .timeRange(TimeRange.between(0, 100))
      .thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
    assertFalse(ok);

    assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
  }
}
 
Example #22
Source File: TestCheckAndMutate.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckAndMutateWithFilterAndTimeRange() throws Throwable {
  try (Table table = createTable()) {
    // Put with specifying the timestamp
    table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));

    // Put with success
    boolean ok = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
      .ifMatches(new SingleColumnValueFilter(FAMILY,
        Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")))
      .timeRange(TimeRange.between(0, 101))
      .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
    assertTrue(ok);

    Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));

    // Put with failure
    ok = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
      .ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"),
        CompareOperator.EQUAL, Bytes.toBytes("a")))
      .timeRange(TimeRange.between(0, 100))
      .build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))));
    assertFalse(ok);

    assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
  }
}
 
Example #23
Source File: StoreFileScanner.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public boolean shouldUseScanner(Scan scan, HStore store, long oldestUnexpiredTS) {
  // if the file has no entries, no need to validate or create a scanner.
  byte[] cf = store.getColumnFamilyDescriptor().getName();
  TimeRange timeRange = scan.getColumnFamilyTimeRange().get(cf);
  if (timeRange == null) {
    timeRange = scan.getTimeRange();
  }
  return reader.passesTimerangeFilter(timeRange, oldestUnexpiredTS) && reader
      .passesKeyRangeFilter(scan) && reader.passesBloomFilter(scan, scan.getFamilyMap().get(cf));
}
 
Example #24
Source File: TestIncrementTimeRange.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void checkHTableInterfaceMethods() throws Exception {
  long time = EnvironmentEdgeManager.currentTime();
  mee.setValue(time);
  hTableInterface.put(new Put(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, Bytes.toBytes(1L)));
  checkRowValue(ROW_A, Bytes.toBytes(1L));

  time = EnvironmentEdgeManager.currentTime();
  mee.setValue(time);
  TimeRange range10 = new TimeRange(1, time+10);
  hTableInterface.increment(new Increment(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, 10L)
      .setTimeRange(range10.getMin(), range10.getMax()));
  checkRowValue(ROW_A, Bytes.toBytes(11L));
  assertEquals(MyObserver.tr10.getMin(), range10.getMin());
  assertEquals(MyObserver.tr10.getMax(), range10.getMax());

  time = EnvironmentEdgeManager.currentTime();
  mee.setValue(time);
  TimeRange range2 = new TimeRange(1, time+20);
  List<Row> actions =
      Arrays.asList(new Row[] { new Increment(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, 2L)
          .setTimeRange(range2.getMin(), range2.getMax()),
          new Increment(ROW_A).addColumn(TEST_FAMILY, qualifierCol1, 2L)
          .setTimeRange(range2.getMin(), range2.getMax()) });
  Object[] results3 = new Object[actions.size()];
  Object[] results1 = results3;
  hTableInterface.batch(actions, results1);
  assertEquals(MyObserver.tr2.getMin(), range2.getMin());
  assertEquals(MyObserver.tr2.getMax(), range2.getMax());
  for (Object r2 : results1) {
    assertTrue(r2 instanceof Result);
  }
  checkRowValue(ROW_A, Bytes.toBytes(15L));

  hTableInterface.close();
}
 
Example #25
Source File: TestAppendTimeRange.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testHTableInterfaceMethods() throws Exception {
  try (Table table = util.createTable(TableName.valueOf(name.getMethodName()), TEST_FAMILY)) {
    table.put(new Put(ROW).addColumn(TEST_FAMILY, QUAL, VALUE));
    long time = EnvironmentEdgeManager.currentTime();
    mee.setValue(time);
    table.put(new Put(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("a")));
    checkRowValue(table, ROW, Bytes.toBytes("a"));

    time = EnvironmentEdgeManager.currentTime();
    mee.setValue(time);
    TimeRange range10 = new TimeRange(1, time + 10);
    Result r = table.append(new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("b"))
        .setTimeRange(range10.getMin(), range10.getMax()));
    checkRowValue(table, ROW, Bytes.toBytes("ab"));
    assertEquals(MyObserver.tr10.getMin(), range10.getMin());
    assertEquals(MyObserver.tr10.getMax(), range10.getMax());
    time = EnvironmentEdgeManager.currentTime();
    mee.setValue(time);
    TimeRange range2 = new TimeRange(1, time+20);
    List<Row> actions =
        Arrays.asList(new Row[] {
            new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("c"))
                .setTimeRange(range2.getMin(), range2.getMax()),
            new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("c"))
                .setTimeRange(range2.getMin(), range2.getMax()) });
    Object[] results1 = new Object[actions.size()];
    table.batch(actions, results1);
    assertEquals(MyObserver.tr2.getMin(), range2.getMin());
    assertEquals(MyObserver.tr2.getMax(), range2.getMax());
    for (Object r2 : results1) {
      assertTrue(r2 instanceof Result);
    }
    checkRowValue(table, ROW, Bytes.toBytes("abcc"));
  }
}
 
Example #26
Source File: TestSimpleTimeRangeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtreme() {
  TimeRange tr = TimeRange.allTime();
  assertTrue(tr.includesTimeRange(TimeRange.allTime()));
  TimeRangeTracker trt = getTimeRangeTracker();
  assertFalse(trt.includesTimeRange(TimeRange.allTime()));
  trt.includeTimestamp(1);
  trt.includeTimestamp(10);
  assertTrue(trt.includesTimeRange(TimeRange.allTime()));
}
 
Example #27
Source File: TestSimpleTimeRangeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeRangeTrackerNullIsSameAsTimeRangeNull() throws IOException {
  TimeRangeTracker src = getTimeRangeTracker(1, 2);
  byte[] bytes = TimeRangeTracker.toByteArray(src);
  TimeRange tgt = TimeRangeTracker.parseFrom(bytes).toTimeRange();
  assertEquals(src.getMin(), tgt.getMin());
  assertEquals(src.getMax(), tgt.getMax());
}
 
Example #28
Source File: TestSimpleTimeRangeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleInRange() {
  TimeRangeTracker trr = getTimeRangeTracker();
  trr.includeTimestamp(0);
  trr.includeTimestamp(2);
  assertTrue(trr.includesTimeRange(new TimeRange(1)));
}
 
Example #29
Source File: TestSimpleTimeRangeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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 = new TimeRange(0L);
  assertEquals(0L, oneArgRange.getMin());
  assertEquals(Long.MAX_VALUE, oneArgRange.getMax());
  assertTrue(oneArgRange.isAllTime());

  TimeRange oneArgRange2 = new TimeRange(1);
  assertEquals(1, oneArgRange2.getMin());
  assertEquals(Long.MAX_VALUE, oneArgRange2.getMax());
  assertFalse(oneArgRange2.isAllTime());

  TimeRange twoArgRange = new TimeRange(0L, Long.MAX_VALUE);
  assertEquals(0L, twoArgRange.getMin());
  assertEquals(Long.MAX_VALUE, twoArgRange.getMax());
  assertTrue(twoArgRange.isAllTime());

  TimeRange twoArgRange2 = new TimeRange(0L, Long.MAX_VALUE - 1);
  assertEquals(0L, twoArgRange2.getMin());
  assertEquals(Long.MAX_VALUE - 1, twoArgRange2.getMax());
  assertFalse(twoArgRange2.isAllTime());

  TimeRange twoArgRange3 = new TimeRange(1, Long.MAX_VALUE);
  assertEquals(1, twoArgRange3.getMin());
  assertEquals(Long.MAX_VALUE, twoArgRange3.getMax());
  assertFalse(twoArgRange3.isAllTime());
}
 
Example #30
Source File: TestSerialization.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() throws Exception {
  byte[] row = Bytes.toBytes("row");
  byte[] fam = Bytes.toBytes("fam");
  byte[] qf1 = Bytes.toBytes("qf1");

  long ts = System.currentTimeMillis();
  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());
}