Search in sources :

Example 16 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndMutateWithCorrectValue.

@Test
public void testCheckAndMutateWithCorrectValue() throws IOException {
    byte[] row1 = Bytes.toBytes("row1");
    byte[] fam1 = Bytes.toBytes("fam1");
    byte[] qf1 = Bytes.toBytes("qualifier");
    byte[] val1 = Bytes.toBytes("value1");
    BigDecimal bd1 = new BigDecimal(Double.MIN_VALUE);
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, fam1);
    // Putting data in key
    long now = EnvironmentEdgeManager.currentTime();
    Put put = new Put(row1);
    put.addColumn(fam1, qf1, now, val1);
    region.put(put);
    // checkAndPut with correct value
    CheckAndMutateResult res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.EQUAL, val1).build(put));
    assertTrue("First", res.isSuccess());
    // checkAndDelete with correct value
    Delete delete = new Delete(row1, now + 1);
    delete.addColumn(fam1, qf1);
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.EQUAL, val1).build(delete));
    assertTrue("Delete", res.isSuccess());
    assertNull(res.getResult());
    // Putting data in key
    put = new Put(row1);
    put.addColumn(fam1, qf1, now + 2, Bytes.toBytes(bd1));
    region.put(put);
    // checkAndPut with correct value
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.EQUAL, Bytes.toBytes(bd1)).build(put));
    assertTrue("Second put", res.isSuccess());
    assertNull(res.getResult());
    // checkAndDelete with correct value
    delete = new Delete(row1, now + 3);
    delete.addColumn(fam1, qf1);
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.EQUAL, Bytes.toBytes(bd1)).build(delete));
    assertTrue("Second delete", res.isSuccess());
    assertNull(res.getResult());
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) BigDecimal(java.math.BigDecimal) Put(org.apache.hadoop.hbase.client.Put) Test(org.junit.Test)

Example 17 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndAppend.

@Test
public void testCheckAndAppend() throws Throwable {
    final byte[] FAMILY = Bytes.toBytes("fam");
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, FAMILY);
    region.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")));
    // CheckAndAppend with correct value
    CheckAndMutateResult res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")).build(new Append(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
    assertTrue(res.isSuccess());
    assertEquals("b", Bytes.toString(res.getResult().getValue(FAMILY, Bytes.toBytes("B"))));
    Result result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
    // CheckAndAppend with wrong value
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("b")).build(new Append(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
    region.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
    // CheckAndAppend with a filter and correct value
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("C"), CompareOperator.EQUAL, Bytes.toBytes("c")))).build(new Append(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("bb"))));
    assertTrue(res.isSuccess());
    assertEquals("bbb", Bytes.toString(res.getResult().getValue(FAMILY, Bytes.toBytes("B"))));
    result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("bbb", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
    // CheckAndAppend with a filter and wrong value
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("b")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("C"), CompareOperator.EQUAL, Bytes.toBytes("d")))).build(new Append(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("bb"))));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("bbb", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
}
Also used : Append(org.apache.hadoop.hbase.client.Append) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Get(org.apache.hadoop.hbase.client.Get) FilterList(org.apache.hadoop.hbase.filter.FilterList) Put(org.apache.hadoop.hbase.client.Put) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Example 18 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndMutateWithNonEqualCompareOp.

@Test
public void testCheckAndMutateWithNonEqualCompareOp() throws IOException {
    byte[] row1 = Bytes.toBytes("row1");
    byte[] fam1 = Bytes.toBytes("fam1");
    byte[] qf1 = Bytes.toBytes("qualifier");
    byte[] val1 = Bytes.toBytes("value1");
    byte[] val2 = Bytes.toBytes("value2");
    byte[] val3 = Bytes.toBytes("value3");
    byte[] val4 = Bytes.toBytes("value4");
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, fam1);
    // Putting val3 in key
    Put put = new Put(row1);
    put.addColumn(fam1, qf1, val3);
    region.put(put);
    // Test CompareOp.LESS: original = val3, compare with val3, fail
    CheckAndMutateResult res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.LESS, val3).build(put));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.LESS: original = val3, compare with val4, fail
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.LESS, val4).build(put));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.LESS: original = val3, compare with val2,
    // succeed (now value = val2)
    put = new Put(row1);
    put.addColumn(fam1, qf1, val2);
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.LESS, val2).build(put));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.LESS_OR_EQUAL: original = val2, compare with val3, fail
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.LESS_OR_EQUAL, val3).build(put));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.LESS_OR_EQUAL: original = val2, compare with val2,
    // succeed (value still = val2)
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.LESS_OR_EQUAL, val2).build(put));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.LESS_OR_EQUAL: original = val2, compare with val1,
    // succeed (now value = val3)
    put = new Put(row1);
    put.addColumn(fam1, qf1, val3);
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.LESS_OR_EQUAL, val1).build(put));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.GREATER: original = val3, compare with val3, fail
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.GREATER, val3).build(put));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.GREATER: original = val3, compare with val2, fail
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.GREATER, val2).build(put));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.GREATER: original = val3, compare with val4,
    // succeed (now value = val2)
    put = new Put(row1);
    put.addColumn(fam1, qf1, val2);
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.GREATER, val4).build(put));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.GREATER_OR_EQUAL: original = val2, compare with val1, fail
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.GREATER_OR_EQUAL, val1).build(put));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.GREATER_OR_EQUAL: original = val2, compare with val2,
    // succeed (value still = val2)
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.GREATER_OR_EQUAL, val2).build(put));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    // Test CompareOp.GREATER_OR_EQUAL: original = val2, compare with val3, succeed
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.GREATER_OR_EQUAL, val3).build(put));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
}
Also used : CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Put(org.apache.hadoop.hbase.client.Put) Test(org.junit.Test)

Example 19 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndPutThatPutWasWritten.

@Test
public void testCheckAndPutThatPutWasWritten() throws IOException {
    byte[] row1 = Bytes.toBytes("row1");
    byte[] fam1 = Bytes.toBytes("fam1");
    byte[] fam2 = Bytes.toBytes("fam2");
    byte[] qf1 = Bytes.toBytes("qualifier");
    byte[] val1 = Bytes.toBytes("value1");
    byte[] val2 = Bytes.toBytes("value2");
    byte[][] families = { fam1, fam2 };
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, families);
    // Putting data in the key to check
    Put put = new Put(row1);
    put.addColumn(fam1, qf1, val1);
    region.put(put);
    // Creating put to add
    long ts = EnvironmentEdgeManager.currentTime();
    KeyValue kv = new KeyValue(row1, fam2, qf1, ts, KeyValue.Type.Put, val2);
    put = new Put(row1);
    put.add(kv);
    // checkAndPut with wrong value
    CheckAndMutateResult res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.EQUAL, val1).build(put));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    Get get = new Get(row1);
    get.addColumn(fam2, qf1);
    Cell[] actual = region.get(get).rawCells();
    Cell[] expected = { kv };
    assertEquals(expected.length, actual.length);
    for (int i = 0; i < actual.length; i++) {
        assertEquals(expected[i], actual[i]);
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Get(org.apache.hadoop.hbase.client.Get) Cell(org.apache.hadoop.hbase.Cell) Put(org.apache.hadoop.hbase.client.Put) Test(org.junit.Test)

Example 20 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndIncrementAndAppend.

@Test
public void testCheckAndIncrementAndAppend() throws Throwable {
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, fam1);
    // CheckAndMutate with Increment and Append
    CheckAndMutate checkAndMutate = CheckAndMutate.newBuilder(row).ifNotExists(fam1, qual).build(new RowMutations(row).add((Mutation) new Increment(row).addColumn(fam1, qual1, 1L)).add((Mutation) new Append(row).addColumn(fam1, qual2, Bytes.toBytes("a"))));
    CheckAndMutateResult result = region.checkAndMutate(checkAndMutate);
    assertTrue(result.isSuccess());
    assertEquals(1L, Bytes.toLong(result.getResult().getValue(fam1, qual1)));
    assertEquals("a", Bytes.toString(result.getResult().getValue(fam1, qual2)));
    Result r = region.get(new Get(row));
    assertEquals(1L, Bytes.toLong(r.getValue(fam1, qual1)));
    assertEquals("a", Bytes.toString(r.getValue(fam1, qual2)));
    // Set return results to false
    checkAndMutate = CheckAndMutate.newBuilder(row).ifNotExists(fam1, qual).build(new RowMutations(row).add((Mutation) new Increment(row).addColumn(fam1, qual1, 1L).setReturnResults(false)).add((Mutation) new Append(row).addColumn(fam1, qual2, Bytes.toBytes("a")).setReturnResults(false)));
    result = region.checkAndMutate(checkAndMutate);
    assertTrue(result.isSuccess());
    assertNull(result.getResult().getValue(fam1, qual1));
    assertNull(result.getResult().getValue(fam1, qual2));
    r = region.get(new Get(row));
    assertEquals(2L, Bytes.toLong(r.getValue(fam1, qual1)));
    assertEquals("aa", Bytes.toString(r.getValue(fam1, qual2)));
    checkAndMutate = CheckAndMutate.newBuilder(row).ifNotExists(fam1, qual).build(new RowMutations(row).add((Mutation) new Increment(row).addColumn(fam1, qual1, 1L)).add((Mutation) new Append(row).addColumn(fam1, qual2, Bytes.toBytes("a")).setReturnResults(false)));
    result = region.checkAndMutate(checkAndMutate);
    assertTrue(result.isSuccess());
    assertEquals(3L, Bytes.toLong(result.getResult().getValue(fam1, qual1)));
    assertNull(result.getResult().getValue(fam1, qual2));
    r = region.get(new Get(row));
    assertEquals(3L, Bytes.toLong(r.getValue(fam1, qual1)));
    assertEquals("aaa", Bytes.toString(r.getValue(fam1, qual2)));
}
Also used : Append(org.apache.hadoop.hbase.client.Append) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Increment(org.apache.hadoop.hbase.client.Increment) Get(org.apache.hadoop.hbase.client.Get) CheckAndMutate(org.apache.hadoop.hbase.client.CheckAndMutate) Mutation(org.apache.hadoop.hbase.client.Mutation) RowMutations(org.apache.hadoop.hbase.client.RowMutations) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Aggregations

CheckAndMutateResult (org.apache.hadoop.hbase.client.CheckAndMutateResult)20 Put (org.apache.hadoop.hbase.client.Put)13 Test (org.junit.Test)12 Result (org.apache.hadoop.hbase.client.Result)10 Get (org.apache.hadoop.hbase.client.Get)9 Delete (org.apache.hadoop.hbase.client.Delete)8 Mutation (org.apache.hadoop.hbase.client.Mutation)5 RowMutations (org.apache.hadoop.hbase.client.RowMutations)5 IOException (java.io.IOException)4 Append (org.apache.hadoop.hbase.client.Append)4 CheckAndMutate (org.apache.hadoop.hbase.client.CheckAndMutate)4 Increment (org.apache.hadoop.hbase.client.Increment)4 SingleColumnValueFilter (org.apache.hadoop.hbase.filter.SingleColumnValueFilter)4 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)3 FilterList (org.apache.hadoop.hbase.filter.FilterList)3 MutationType (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType)3 UncheckedIOException (java.io.UncheckedIOException)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Cell (org.apache.hadoop.hbase.Cell)2