diff --git a/.gitignore b/.gitignore index 0fb95ff1..6c7cde0f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ pegasus-client.iml log.txt rolling_log/ .vscode/ - +google-java-format-* diff --git a/scripts/format-all.sh b/scripts/format-all.sh index 6345509d..1bfdcc04 100755 --- a/scripts/format-all.sh +++ b/scripts/format-all.sh @@ -9,7 +9,12 @@ SRC_FILES=(src/main/java/com/xiaomi/infra/pegasus/client/*.java src/main/java/com/xiaomi/infra/pegasus/rpc/*.java src/main/java/com/xiaomi/infra/pegasus/rpc/async/*.java src/main/java/com/xiaomi/infra/pegasus/operator/*.java - src/main/java/com/xiaomi/infra/pegasus/tools/*.java) + src/main/java/com/xiaomi/infra/pegasus/tools/*.java + src/test/java/com/xiaomi/infra/pegasus/client/*.java + src/test/java/com/xiaomi/infra/pegasus/metrics/*.java + src/test/java/com/xiaomi/infra/pegasus/rpc/async/*.java + src/test/java/com/xiaomi/infra/pegasus/tools/*.java + ) if [ ! -f "${PROJECT_DIR}"/google-java-format-1.7-all-deps.jar ]; then wget https://github.com/google/google-java-format/releases/download/google-java-format-1.7/google-java-format-1.7-all-deps.jar diff --git a/scripts/travis.sh b/scripts/travis.sh index 970cc274..b6bc7ef9 100755 --- a/scripts/travis.sh +++ b/scripts/travis.sh @@ -9,7 +9,6 @@ cd "${PROJECT_DIR}" || exit -1 # lint all scripts, abort if there's any warning. function shellcheck_must_pass() { - echo "$1" if [[ $(shellcheck "$1") ]]; then echo "shellcheck $1 failed" shellcheck "$1" @@ -19,6 +18,14 @@ function shellcheck_must_pass() shellcheck_must_pass ./scripts/format-all.sh shellcheck_must_pass ./scripts/travis.sh +# ensure source files are well formatted +./scripts/format-all.sh +if [[ $(git status -s) ]]; then + git status -s + echo "please format the above files before commit" + exit 1 +fi + # start pegasus onebox environment wget https://github.com/XiaoMi/pegasus/releases/download/v1.11.2/pegasus-tools-1.11.2-a186d38-ubuntu-18.04-release.tar.gz tar xvf pegasus-tools-1.11.2-a186d38-ubuntu-18.04-release.tar.gz diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java index d1c2782f..969935dd 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java @@ -3,2144 +3,2251 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; -/** - * @author qinzuoyan - */ - +/** @author qinzuoyan */ import io.netty.util.concurrent.Future; -import org.apache.commons.lang3.tuple.Pair; -import org.junit.Assert; -import org.junit.Test; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; +import org.junit.Test; -/** - * Created by mi on 16-3-22. - */ +/** Created by mi on 16-3-22. */ public class TestBasic { - private static final String basicSetGetDelHashKey = "TestBasic_testSetGetDel_hash_key_1"; - private static final String multiSetGetDelHashKey = "TestBasic_testMultiSetGetDel_hash_key_1"; - private static final String multiGetHashKey = "TestBasic_testMultiGet_hash_key_1"; - private static final String multiGetReverseHashKey = "TestBasic_testMultiGetReverse_hash_key_1"; - - private final static char[] hexArray = "0123456789ABCDEF".toCharArray(); - public static String bytesToHex(byte[] bytes) { - char[] hexChars = new char[bytes.length * 2]; - for ( int j = 0; j < bytes.length; j++ ) { - int v = bytes[j] & 0xFF; - hexChars[j * 2] = hexArray[v >>> 4]; - hexChars[j * 2 + 1] = hexArray[v & 0x0F]; - } - return new String(hexChars); + private static final String basicSetGetDelHashKey = "TestBasic_testSetGetDel_hash_key_1"; + private static final String multiSetGetDelHashKey = "TestBasic_testMultiSetGetDel_hash_key_1"; + private static final String multiGetHashKey = "TestBasic_testMultiGet_hash_key_1"; + private static final String multiGetReverseHashKey = "TestBasic_testMultiGetReverse_hash_key_1"; + + private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); + + public static String bytesToHex(byte[] bytes) { + char[] hexChars = new char[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } - - @Test - public void testGenerateKey() throws PException { - Assert.assertEquals("00010A0BFEFF", - bytesToHex(new byte[]{0x00, 0x01, 0x0A, 0x0B, (byte)0xFE, (byte)0xFF})); - - Assert.assertArrayEquals(new byte[]{0x00, 0x00}, - PegasusClient.generateKey(new byte[]{}, new byte[]{})); - Assert.assertArrayEquals(new byte[]{0x00, 0x01, 0x44}, - PegasusClient.generateKey(new byte[]{0x44}, new byte[]{})); - Assert.assertArrayEquals(new byte[]{0x00, 0x00, 0x55}, - PegasusClient.generateKey(new byte[]{}, new byte[]{0x55})); - Assert.assertArrayEquals(new byte[]{0x00, 0x01, 0x44, 0x55}, - PegasusClient.generateKey(new byte[]{0x44}, new byte[]{0x55})); - try { - byte[] k = PegasusClient.generateKey(new byte[64 * 1024], new byte[]{0x55}); - Assert.assertTrue(false); - } - catch (Exception e) { - } - - Assert.assertArrayEquals(new byte[]{0x00, 0x01}, - PegasusClient.generateNextBytes(new byte[]{})); - Assert.assertArrayEquals(new byte[]{0x00, 0x01, 0x0B}, - PegasusClient.generateNextBytes(new byte[]{0x0A})); - Assert.assertArrayEquals(new byte[]{0x00, 0x01, (byte)0xFF}, - PegasusClient.generateNextBytes(new byte[]{(byte)0xFE})); - Assert.assertArrayEquals(new byte[]{0x00, 0x02}, - PegasusClient.generateNextBytes(new byte[]{(byte)0xFF})); - Assert.assertArrayEquals(new byte[]{0x00, 0x02, 0x0B}, - PegasusClient.generateNextBytes(new byte[]{0x0A, (byte)0xFF})); - Assert.assertArrayEquals(new byte[]{0x00, 0x03}, - PegasusClient.generateNextBytes(new byte[]{(byte)0xFF, (byte)0xFF})); - try { - byte[] k = PegasusClient.generateNextBytes(new byte[64 * 1024]); - Assert.assertTrue(false); - } - catch (Exception e) { - } - - Assert.assertArrayEquals(new byte[]{0x00, 0x01}, - PegasusClient.generateNextBytes(new byte[]{}, new byte[]{})); - Assert.assertArrayEquals(new byte[]{0x00, 0x01, 0x45}, - PegasusClient.generateNextBytes(new byte[]{0x44}, new byte[]{})); - Assert.assertArrayEquals(new byte[]{0x00, 0x00, 0x56}, - PegasusClient.generateNextBytes(new byte[]{}, new byte[]{0x55})); - Assert.assertArrayEquals(new byte[]{0x00, 0x01, 0x44, 0x56}, - PegasusClient.generateNextBytes(new byte[]{0x44}, new byte[]{0x55})); - Assert.assertArrayEquals(new byte[]{0x00, 0x01, 0x45}, - PegasusClient.generateNextBytes(new byte[]{0x44}, new byte[]{(byte)0xFF})); - Assert.assertArrayEquals(new byte[]{0x00, 0x02}, - PegasusClient.generateNextBytes(new byte[]{(byte)0xFF}, new byte[]{})); - Assert.assertArrayEquals(new byte[]{0x00, 0x01, (byte)0xFF, 0x56}, - PegasusClient.generateNextBytes(new byte[]{(byte)0xFF}, new byte[]{0x55})); - Assert.assertArrayEquals(new byte[]{0x00, 0x02}, - PegasusClient.generateNextBytes(new byte[]{(byte)0xFF}, new byte[]{(byte)0xFF})); - Assert.assertArrayEquals(new byte[]{0x00, 0x02}, - PegasusClient.generateNextBytes(new byte[]{(byte)0xFF}, new byte[]{(byte)0xFF, (byte)0xFF})); - Assert.assertArrayEquals(new byte[]{0x00, 0x03}, - PegasusClient.generateNextBytes(new byte[]{(byte)0xFF, (byte)0xFF}, new byte[]{(byte)0xFF})); - try { - byte[] k = PegasusClient.generateNextBytes(new byte[64 * 1024], new byte[0]); - Assert.assertTrue(false); - } - catch (Exception e) { - } + return new String(hexChars); + } + + @Test + public void testGenerateKey() throws PException { + Assert.assertEquals( + "00010A0BFEFF", bytesToHex(new byte[] {0x00, 0x01, 0x0A, 0x0B, (byte) 0xFE, (byte) 0xFF})); + + Assert.assertArrayEquals( + new byte[] {0x00, 0x00}, PegasusClient.generateKey(new byte[] {}, new byte[] {})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01, 0x44}, PegasusClient.generateKey(new byte[] {0x44}, new byte[] {})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x00, 0x55}, PegasusClient.generateKey(new byte[] {}, new byte[] {0x55})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01, 0x44, 0x55}, + PegasusClient.generateKey(new byte[] {0x44}, new byte[] {0x55})); + try { + byte[] k = PegasusClient.generateKey(new byte[64 * 1024], new byte[] {0x55}); + Assert.assertTrue(false); + } catch (Exception e) { } - @Test - public void testGetSingletonClient() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - PegasusClientInterface client1 = PegasusClientFactory.getSingletonClient(); - Assert.assertEquals(client, client1); - try { - PegasusClientInterface client2 = PegasusClientFactory.getSingletonClient("resource:///xxx.properties"); - Assert.assertTrue(false); - } - catch (PException e) { - } - PegasusClientFactory.closeSingletonClient(); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01}, PegasusClient.generateNextBytes(new byte[] {})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01, 0x0B}, PegasusClient.generateNextBytes(new byte[] {0x0A})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01, (byte) 0xFF}, + PegasusClient.generateNextBytes(new byte[] {(byte) 0xFE})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x02}, PegasusClient.generateNextBytes(new byte[] {(byte) 0xFF})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x02, 0x0B}, + PegasusClient.generateNextBytes(new byte[] {0x0A, (byte) 0xFF})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x03}, + PegasusClient.generateNextBytes(new byte[] {(byte) 0xFF, (byte) 0xFF})); + try { + byte[] k = PegasusClient.generateNextBytes(new byte[64 * 1024]); + Assert.assertTrue(false); + } catch (Exception e) { } - @Test - public void testSetGetDel() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = basicSetGetDelHashKey.getBytes(); - - try { - // set - client.set(tableName, hashKey, "basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); - - // check exist - boolean exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertTrue(exist); - - exist = client.exist(tableName, hashKey, "basic_test_sort_key_2".getBytes()); - Assert.assertFalse(exist); - - // check sortkey count - long sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(1, sortKeyCount); - - // get - byte[] value = client.get(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), value); - - value = client.get(tableName, hashKey, "basic_test_sort_key_2".getBytes()); - Assert.assertEquals(null, value); - - // del - client.del(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - - // check exist - exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertFalse(exist); - - // check sortkey count - sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(0, sortKeyCount); - - // check deleted - value = client.get(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertEquals(null, value); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } + Assert.assertArrayEquals( + new byte[] {0x00, 0x01}, PegasusClient.generateNextBytes(new byte[] {}, new byte[] {})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01, 0x45}, + PegasusClient.generateNextBytes(new byte[] {0x44}, new byte[] {})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x00, 0x56}, + PegasusClient.generateNextBytes(new byte[] {}, new byte[] {0x55})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01, 0x44, 0x56}, + PegasusClient.generateNextBytes(new byte[] {0x44}, new byte[] {0x55})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01, 0x45}, + PegasusClient.generateNextBytes(new byte[] {0x44}, new byte[] {(byte) 0xFF})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x02}, + PegasusClient.generateNextBytes(new byte[] {(byte) 0xFF}, new byte[] {})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x01, (byte) 0xFF, 0x56}, + PegasusClient.generateNextBytes(new byte[] {(byte) 0xFF}, new byte[] {0x55})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x02}, + PegasusClient.generateNextBytes(new byte[] {(byte) 0xFF}, new byte[] {(byte) 0xFF})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x02}, + PegasusClient.generateNextBytes( + new byte[] {(byte) 0xFF}, new byte[] {(byte) 0xFF, (byte) 0xFF})); + Assert.assertArrayEquals( + new byte[] {0x00, 0x03}, + PegasusClient.generateNextBytes( + new byte[] {(byte) 0xFF, (byte) 0xFF}, new byte[] {(byte) 0xFF})); + try { + byte[] k = PegasusClient.generateNextBytes(new byte[64 * 1024], new byte[0]); + Assert.assertTrue(false); + } catch (Exception e) { + } + } + + @Test + public void testGetSingletonClient() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + PegasusClientInterface client1 = PegasusClientFactory.getSingletonClient(); + Assert.assertEquals(client, client1); + try { + PegasusClientInterface client2 = + PegasusClientFactory.getSingletonClient("resource:///xxx.properties"); + Assert.assertTrue(false); + } catch (PException e) { + } + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testSetGetDel() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = basicSetGetDelHashKey.getBytes(); + + try { + // set + client.set( + tableName, hashKey, "basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); + + // check exist + boolean exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertTrue(exist); + + exist = client.exist(tableName, hashKey, "basic_test_sort_key_2".getBytes()); + Assert.assertFalse(exist); + + // check sortkey count + long sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(1, sortKeyCount); + + // get + byte[] value = client.get(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertArrayEquals("basic_test_value_1".getBytes(), value); + + value = client.get(tableName, hashKey, "basic_test_sort_key_2".getBytes()); + Assert.assertEquals(null, value); + + // del + client.del(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + + // check exist + exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertFalse(exist); + + // check sortkey count + sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(0, sortKeyCount); + + // check deleted + value = client.get(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertEquals(null, value); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMultiSetGetDel() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = multiSetGetDelHashKey.getBytes(); + + try { + // multi set + List> values = new ArrayList>(); + values.add(Pair.of("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes())); + values.add(Pair.of("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes())); + values.add(Pair.of("basic_test_sort_key_3".getBytes(), "basic_test_value_3".getBytes())); + values.add(Pair.of("basic_test_sort_key_4".getBytes(), "basic_test_value_4".getBytes())); + client.multiSet(tableName, hashKey, values); + + // check exist + boolean exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertTrue(exist); + + exist = client.exist(tableName, hashKey, "basic_test_sort_key_5".getBytes()); + Assert.assertFalse(exist); + + // check sortkey count + long sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(4, sortKeyCount); + + // multi get + List sortKeys = new ArrayList(); + sortKeys.add("basic_test_sort_key_0".getBytes()); + sortKeys.add("basic_test_sort_key_1".getBytes()); + sortKeys.add("basic_test_sort_key_2".getBytes()); + sortKeys.add("basic_test_sort_key_3".getBytes()); + sortKeys.add("basic_test_sort_key_4".getBytes()); + List> newValues = new ArrayList>(); + boolean ret = client.multiGet(tableName, hashKey, sortKeys, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(4, newValues.size()); + Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("basic_test_value_1".getBytes(), newValues.get(0).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("basic_test_value_2".getBytes(), newValues.get(1).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("basic_test_value_3".getBytes(), newValues.get(2).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("basic_test_value_4".getBytes(), newValues.get(3).getValue()); + + // multi get with count limit + newValues.clear(); + ret = client.multiGet(tableName, hashKey, sortKeys, 1, 0, newValues); + Assert.assertFalse(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("basic_test_value_1".getBytes(), newValues.get(0).getValue()); + + // multi get with empty sortKeys + sortKeys.clear(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, sortKeys, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(4, newValues.size()); + Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("basic_test_value_1".getBytes(), newValues.get(0).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("basic_test_value_2".getBytes(), newValues.get(1).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("basic_test_value_3".getBytes(), newValues.get(2).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("basic_test_value_4".getBytes(), newValues.get(3).getValue()); + + // multi get with null sortKeys + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(4, newValues.size()); + Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("basic_test_value_1".getBytes(), newValues.get(0).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("basic_test_value_2".getBytes(), newValues.get(1).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("basic_test_value_3".getBytes(), newValues.get(2).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("basic_test_value_4".getBytes(), newValues.get(3).getValue()); + + // multi get sort keys + sortKeys.clear(); + ret = client.multiGetSortKeys(tableName, hashKey, sortKeys); + Assert.assertTrue(ret); + Assert.assertEquals(4, sortKeys.size()); + Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), sortKeys.get(0)); + Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), sortKeys.get(1)); + Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), sortKeys.get(2)); + Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), sortKeys.get(3)); + + // multi del + sortKeys.clear(); + sortKeys.add("basic_test_sort_key_0".getBytes()); + sortKeys.add("basic_test_sort_key_1".getBytes()); + sortKeys.add("basic_test_sort_key_2".getBytes()); + client.multiDel(tableName, hashKey, sortKeys); + + // check sortkey count + sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(2, sortKeyCount); + + // check deleted + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("basic_test_value_3".getBytes(), newValues.get(0).getValue()); + Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("basic_test_value_4".getBytes(), newValues.get(1).getValue()); + + // multi del all + sortKeys.clear(); + sortKeys.add("basic_test_sort_key_3".getBytes()); + sortKeys.add("basic_test_sort_key_4".getBytes()); + sortKeys.add("basic_test_sort_key_5".getBytes()); + client.multiDel(tableName, hashKey, sortKeys); + + // check sortkey count + sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(0, sortKeyCount); + + // check deleted by multiGet + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // check deleted by multiGetSortKeys + sortKeys.clear(); + ret = client.multiGetSortKeys(tableName, hashKey, sortKeys); + Assert.assertTrue(ret); + Assert.assertEquals(0, sortKeys.size()); + + // multi set many kvs + values.clear(); + for (int i = 1; i <= 200; i++) { + String sortKey = "basic_test_sort_key_" + String.format("%03d", i); + String value = "basic_test_value_" + String.valueOf(i); + values.add(Pair.of(sortKey.getBytes(), value.getBytes())); + } + client.multiSet(tableName, hashKey, values); + + // multi get with no limit + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, 0, 0, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(200, newValues.size()); + for (int i = 1; i <= 200; i++) { + String sortKey = "basic_test_sort_key_" + String.format("%03d", i); + String value = "basic_test_value_" + String.valueOf(i); + Assert.assertArrayEquals(sortKey.getBytes(), newValues.get(i - 1).getKey()); + Assert.assertArrayEquals(value.getBytes(), newValues.get(i - 1).getValue()); + } + + // multi del all + sortKeys.clear(); + for (int i = 1; i <= 200; i++) { + String sortKey = "basic_test_sort_key_" + String.format("%03d", i); + Assert.assertArrayEquals(sortKey.getBytes(), newValues.get(i - 1).getKey()); + sortKeys.add(sortKey.getBytes()); + } + client.multiDel(tableName, hashKey, sortKeys); + + // check sortkey count + sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(0, sortKeyCount); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testMultiSetGetDel() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = multiSetGetDelHashKey.getBytes(); - - try { - // multi set - List> values = new ArrayList>(); - values.add(Pair.of("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes())); - values.add(Pair.of("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes())); - values.add(Pair.of("basic_test_sort_key_3".getBytes(), "basic_test_value_3".getBytes())); - values.add(Pair.of("basic_test_sort_key_4".getBytes(), "basic_test_value_4".getBytes())); - client.multiSet(tableName, hashKey, values); - - // check exist - boolean exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertTrue(exist); - - exist = client.exist(tableName, hashKey, "basic_test_sort_key_5".getBytes()); - Assert.assertFalse(exist); - - // check sortkey count - long sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(4, sortKeyCount); - - // multi get - List sortKeys = new ArrayList(); - sortKeys.add("basic_test_sort_key_0".getBytes()); - sortKeys.add("basic_test_sort_key_1".getBytes()); - sortKeys.add("basic_test_sort_key_2".getBytes()); - sortKeys.add("basic_test_sort_key_3".getBytes()); - sortKeys.add("basic_test_sort_key_4".getBytes()); - List> newValues = new ArrayList>(); - boolean ret = client.multiGet(tableName, hashKey, sortKeys, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(4, newValues.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), newValues.get(0).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), newValues.get(1).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("basic_test_value_3".getBytes(), newValues.get(2).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("basic_test_value_4".getBytes(), newValues.get(3).getValue()); - - // multi get with count limit - newValues.clear(); - ret = client.multiGet(tableName, hashKey, sortKeys, 1, 0, newValues); - Assert.assertFalse(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), newValues.get(0).getValue()); - - // multi get with empty sortKeys - sortKeys.clear(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, sortKeys, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(4, newValues.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), newValues.get(0).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), newValues.get(1).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("basic_test_value_3".getBytes(), newValues.get(2).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("basic_test_value_4".getBytes(), newValues.get(3).getValue()); - - // multi get with null sortKeys - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(4, newValues.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), newValues.get(0).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), newValues.get(1).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("basic_test_value_3".getBytes(), newValues.get(2).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("basic_test_value_4".getBytes(), newValues.get(3).getValue()); - - // multi get sort keys - sortKeys.clear(); - ret = client.multiGetSortKeys(tableName, hashKey, sortKeys); - Assert.assertTrue(ret); - Assert.assertEquals(4, sortKeys.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), sortKeys.get(0)); - Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), sortKeys.get(1)); - Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), sortKeys.get(2)); - Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), sortKeys.get(3)); - - // multi del - sortKeys.clear(); - sortKeys.add("basic_test_sort_key_0".getBytes()); - sortKeys.add("basic_test_sort_key_1".getBytes()); - sortKeys.add("basic_test_sort_key_2".getBytes()); - client.multiDel(tableName, hashKey, sortKeys); - - // check sortkey count - sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(2, sortKeyCount); - - // check deleted - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("basic_test_value_3".getBytes(), newValues.get(0).getValue()); - Assert.assertArrayEquals("basic_test_sort_key_4".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("basic_test_value_4".getBytes(), newValues.get(1).getValue()); - - // multi del all - sortKeys.clear(); - sortKeys.add("basic_test_sort_key_3".getBytes()); - sortKeys.add("basic_test_sort_key_4".getBytes()); - sortKeys.add("basic_test_sort_key_5".getBytes()); - client.multiDel(tableName, hashKey, sortKeys); - - // check sortkey count - sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(0, sortKeyCount); - - // check deleted by multiGet - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // check deleted by multiGetSortKeys - sortKeys.clear(); - ret = client.multiGetSortKeys(tableName, hashKey, sortKeys); - Assert.assertTrue(ret); - Assert.assertEquals(0, sortKeys.size()); - - // multi set many kvs - values.clear(); - for (int i = 1; i <= 200; i++) - { - String sortKey = "basic_test_sort_key_" + String.format("%03d", i); - String value = "basic_test_value_" + String.valueOf(i); - values.add(Pair.of(sortKey.getBytes(), value.getBytes())); - } - client.multiSet(tableName, hashKey, values); - - // multi get with no limit - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, 0, 0, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(200, newValues.size()); - for (int i = 1; i <= 200; i++) - { - String sortKey = "basic_test_sort_key_" + String.format("%03d", i); - String value = "basic_test_value_" + String.valueOf(i); - Assert.assertArrayEquals(sortKey.getBytes(), newValues.get(i - 1).getKey()); - Assert.assertArrayEquals(value.getBytes(), newValues.get(i - 1).getValue()); - } + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMultiGet() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = multiGetHashKey.getBytes(); + + try { + // multi set + List> values = new ArrayList>(); + values.add(Pair.of("".getBytes(), "0".getBytes())); + values.add(Pair.of("1".getBytes(), "1".getBytes())); + values.add(Pair.of("1-abcdefg".getBytes(), "1-abcdefg".getBytes())); + values.add(Pair.of("2".getBytes(), "2".getBytes())); + values.add(Pair.of("2-abcdefg".getBytes(), "2-abcdefg".getBytes())); + values.add(Pair.of("3".getBytes(), "3".getBytes())); + values.add(Pair.of("3-efghijk".getBytes(), "3-efghijk".getBytes())); + values.add(Pair.of("4".getBytes(), "4".getBytes())); + values.add(Pair.of("4-hijklmn".getBytes(), "4-hijklmn".getBytes())); + values.add(Pair.of("5".getBytes(), "5".getBytes())); + values.add(Pair.of("5-hijklmn".getBytes(), "5-hijklmn".getBytes())); + values.add(Pair.of("6".getBytes(), "6".getBytes())); + values.add(Pair.of("7".getBytes(), "7".getBytes())); + client.multiSet(tableName, hashKey, values); + + // check sortkey count + long sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(13, sortKeyCount); + + // [null, null) + MultiGetOptions options = new MultiGetOptions(); + Assert.assertTrue(options.startInclusive); + Assert.assertFalse(options.stopInclusive); + List> newValues = new ArrayList>(); + boolean ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(13, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(4).getKey()); + Assert.assertArrayEquals("3".getBytes(), newValues.get(5).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(6).getKey()); + Assert.assertArrayEquals("4".getBytes(), newValues.get(7).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(8).getKey()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(9).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(10).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(11).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(12).getKey()); + + // [null, null] + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(13, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(4).getKey()); + Assert.assertArrayEquals("3".getBytes(), newValues.get(5).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(6).getKey()); + Assert.assertArrayEquals("4".getBytes(), newValues.get(7).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(8).getKey()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(9).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(10).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(11).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(12).getKey()); + + // (null, null) + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = false; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(12, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("3".getBytes(), newValues.get(4).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(5).getKey()); + Assert.assertArrayEquals("4".getBytes(), newValues.get(6).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(7).getKey()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(8).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(9).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(10).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(11).getKey()); + + // (null, null] + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(12, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("3".getBytes(), newValues.get(4).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(5).getKey()); + Assert.assertArrayEquals("4".getBytes(), newValues.get(6).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(7).getKey()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(8).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(9).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(10).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(11).getKey()); + + // [null, 1] + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); + + // [null, 1) + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = false; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + + // (null, 1] + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + + // (null, 1) + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = false; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // [1, 1] + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + + // [1, 1) + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = false; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // (1, 1] + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // (1, 1) + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = false; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // [2, 1] + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = false; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "2".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-anywhere("-") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; + options.sortKeyFilterPattern = "-".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(5, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(4).getKey()); + + // match-anywhere("1") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; + options.sortKeyFilterPattern = "1".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-anywhere("1-") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; + options.sortKeyFilterPattern = "1-".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + + // match-anywhere("abc") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; + options.sortKeyFilterPattern = "abc".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-prefix("1") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-prefix("1") in [0, 1) + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = true; + options.stopInclusive = false; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "0".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("1") in [0, 1] + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = true; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "0".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + + // match-prefix("1") in [1, 2] + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = true; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "2".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-prefix("1") in (1, 2] + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = false; + options.stopInclusive = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "2".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + + // match-prefix("1") in (1-abcdefg, 2] + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = false; + options.stopInclusive = true; + newValues.clear(); + ret = + client.multiGet( + tableName, hashKey, "1-abcdefg".getBytes(), "2".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("1-") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1-".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + + // match-prefix("1-x") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1-x".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("abc") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "abc".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("efg") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "efg".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("ijk") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "ijk".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("lnm") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "lmn".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-postfix("5-hijklmn") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "5-hijklmn".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(0).getKey()); + + // match-postfix("1") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "1".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + + // match-postfix("1-") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "1-".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-postfix("1-x") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "1-x".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-postfix("abc") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "abc".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-postfix("efg") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "efg".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-postfix("ijk") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "ijk".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(0).getKey()); + + // match-postfix("lmn") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "lmn".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(1).getKey()); + + // match-postfix("5-hijklmn") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "5-hijklmn".getBytes(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(0).getKey()); + + // maxCount = 4 + options = new MultiGetOptions(); + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, 4, -1, newValues); + Assert.assertFalse(ret); + Assert.assertEquals(4, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); + + // maxCount = 1 + options = new MultiGetOptions(); + newValues.clear(); + ret = + client.multiGet( + tableName, hashKey, "5".getBytes(), "6".getBytes(), options, 1, -1, newValues); + Assert.assertFalse(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(0).getKey()); + + // multi del all + List sortKeys = new ArrayList(); + sortKeys.add("".getBytes()); + sortKeys.add("1".getBytes()); + sortKeys.add("1-abcdefg".getBytes()); + sortKeys.add("2".getBytes()); + sortKeys.add("2-abcdefg".getBytes()); + sortKeys.add("3".getBytes()); + sortKeys.add("3-efghijk".getBytes()); + sortKeys.add("4".getBytes()); + sortKeys.add("4-hijklmn".getBytes()); + sortKeys.add("5".getBytes()); + sortKeys.add("5-hijklmn".getBytes()); + sortKeys.add("6".getBytes()); + sortKeys.add("7".getBytes()); + client.multiDel(tableName, hashKey, sortKeys); + + // check sortkey count + sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(0, sortKeyCount); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } - // multi del all - sortKeys.clear(); - for (int i = 1; i <= 200; i++) - { - String sortKey = "basic_test_sort_key_" + String.format("%03d", i); - Assert.assertArrayEquals(sortKey.getBytes(), newValues.get(i - 1).getKey()); - sortKeys.add(sortKey.getBytes()); - } - client.multiDel(tableName, hashKey, sortKeys); - - // check sortkey count - sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(0, sortKeyCount); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMultiGetReverse() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = multiGetReverseHashKey.getBytes(); + + try { + // multi set + List> values = new ArrayList>(); + values.add(Pair.of("".getBytes(), "0".getBytes())); + values.add(Pair.of("1".getBytes(), "1".getBytes())); + values.add(Pair.of("1-abcdefg".getBytes(), "1-abcdefg".getBytes())); + values.add(Pair.of("2".getBytes(), "2".getBytes())); + values.add(Pair.of("2-abcdefg".getBytes(), "2-abcdefg".getBytes())); + values.add(Pair.of("3".getBytes(), "3".getBytes())); + values.add(Pair.of("3-efghijk".getBytes(), "3-efghijk".getBytes())); + values.add(Pair.of("4".getBytes(), "4".getBytes())); + values.add(Pair.of("4-hijklmn".getBytes(), "4-hijklmn".getBytes())); + values.add(Pair.of("5".getBytes(), "5".getBytes())); + values.add(Pair.of("5-hijklmn".getBytes(), "5-hijklmn".getBytes())); + values.add(Pair.of("6".getBytes(), "6".getBytes())); + values.add(Pair.of("7".getBytes(), "7".getBytes())); + client.multiSet(tableName, hashKey, values); + + // check sortkey count + long sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(13, sortKeyCount); + + // [null, null) + MultiGetOptions options = new MultiGetOptions(); + Assert.assertTrue(options.startInclusive); + Assert.assertFalse(options.stopInclusive); + options.reverse = true; + List> newValues = new ArrayList>(); + boolean ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(13, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(4).getKey()); + Assert.assertArrayEquals("3".getBytes(), newValues.get(5).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(6).getKey()); + Assert.assertArrayEquals("4".getBytes(), newValues.get(7).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(8).getKey()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(9).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(10).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(11).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(12).getKey()); + + // [null, null] + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(13, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(4).getKey()); + Assert.assertArrayEquals("3".getBytes(), newValues.get(5).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(6).getKey()); + Assert.assertArrayEquals("4".getBytes(), newValues.get(7).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(8).getKey()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(9).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(10).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(11).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(12).getKey()); + + // (null, null) + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = false; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(12, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("3".getBytes(), newValues.get(4).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(5).getKey()); + Assert.assertArrayEquals("4".getBytes(), newValues.get(6).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(7).getKey()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(8).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(9).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(10).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(11).getKey()); + + // (null, null] + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(12, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("2".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("3".getBytes(), newValues.get(4).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(5).getKey()); + Assert.assertArrayEquals("4".getBytes(), newValues.get(6).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(7).getKey()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(8).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(9).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(10).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(11).getKey()); + + // [null, 1] + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); + + // [null, 1) + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = false; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); + + // (null, 1] + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + + // (null, 1) + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = false; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // [1, 1] + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + + // [1, 1) + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = false; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // (1, 1] + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // (1, 1) + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = false; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // [2, 1] + options = new MultiGetOptions(); + options.startInclusive = false; + options.stopInclusive = false; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "2".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-anywhere("-") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; + options.sortKeyFilterPattern = "-".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(5, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(3).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(4).getKey()); + + // match-anywhere("1") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; + options.sortKeyFilterPattern = "1".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-anywhere("1-") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; + options.sortKeyFilterPattern = "1-".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + + // match-anywhere("abc") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; + options.sortKeyFilterPattern = "abc".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-prefix("1") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-prefix("1") in [0, 1) + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = true; + options.stopInclusive = false; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "0".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("1") in [0, 1] + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = true; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "0".getBytes(), "1".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + + // match-prefix("1") in [1, 2] + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = true; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "2".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-prefix("1") in (1, 2] + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = false; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, "1".getBytes(), "2".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + + // match-prefix("1") in (1-abcdefg, 2] + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.startInclusive = false; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = + client.multiGet( + tableName, hashKey, "1-abcdefg".getBytes(), "2".getBytes(), options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("1-") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1-".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + + // match-prefix("1-x") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "1-x".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("abc") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "abc".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("efg") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "efg".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("ijk") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "ijk".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-prefix("lnm") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "lmn".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-postfix("5-hijklmn") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "5-hijklmn".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(0).getKey()); + + // match-postfix("1") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "1".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); + + // match-postfix("1-") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "1-".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-postfix("1-x") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "1-x".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-postfix("abc") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "abc".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(0, newValues.size()); + + // match-postfix("efg") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "efg".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); + + // match-postfix("ijk") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "ijk".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(0).getKey()); + + // match-postfix("lmn") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "lmn".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(2, newValues.size()); + Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(1).getKey()); + + // match-postfix("5-hijklmn") + options = new MultiGetOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; + options.sortKeyFilterPattern = "5-hijklmn".getBytes(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, newValues); + Assert.assertTrue(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(0).getKey()); + + // maxCount = 4 + options = new MultiGetOptions(); + options.reverse = true; + newValues.clear(); + ret = client.multiGet(tableName, hashKey, null, null, options, 4, -1, newValues); + Assert.assertFalse(ret); + Assert.assertEquals(4, newValues.size()); + Assert.assertArrayEquals("5".getBytes(), newValues.get(0).getKey()); + Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(1).getKey()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(2).getKey()); + Assert.assertArrayEquals("7".getBytes(), newValues.get(3).getKey()); + + // maxCount = 1 + options = new MultiGetOptions(); + options.startInclusive = true; + options.stopInclusive = true; + options.reverse = true; + newValues.clear(); + ret = + client.multiGet( + tableName, hashKey, "5".getBytes(), "6".getBytes(), options, 1, -1, newValues); + Assert.assertFalse(ret); + Assert.assertEquals(1, newValues.size()); + Assert.assertArrayEquals("6".getBytes(), newValues.get(0).getKey()); + + // multi del all + List sortKeys = new ArrayList(); + sortKeys.add("".getBytes()); + sortKeys.add("1".getBytes()); + sortKeys.add("1-abcdefg".getBytes()); + sortKeys.add("2".getBytes()); + sortKeys.add("2-abcdefg".getBytes()); + sortKeys.add("3".getBytes()); + sortKeys.add("3-efghijk".getBytes()); + sortKeys.add("4".getBytes()); + sortKeys.add("4-hijklmn".getBytes()); + sortKeys.add("5".getBytes()); + sortKeys.add("5-hijklmn".getBytes()); + sortKeys.add("6".getBytes()); + sortKeys.add("7".getBytes()); + client.multiDel(tableName, hashKey, sortKeys); + + // check sortkey count + sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(0, sortKeyCount); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testMultiGet() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = multiGetHashKey.getBytes(); - - try { - // multi set - List> values = new ArrayList>(); - values.add(Pair.of("".getBytes(), "0".getBytes())); - values.add(Pair.of("1".getBytes(), "1".getBytes())); - values.add(Pair.of("1-abcdefg".getBytes(), "1-abcdefg".getBytes())); - values.add(Pair.of("2".getBytes(), "2".getBytes())); - values.add(Pair.of("2-abcdefg".getBytes(), "2-abcdefg".getBytes())); - values.add(Pair.of("3".getBytes(), "3".getBytes())); - values.add(Pair.of("3-efghijk".getBytes(), "3-efghijk".getBytes())); - values.add(Pair.of("4".getBytes(), "4".getBytes())); - values.add(Pair.of("4-hijklmn".getBytes(), "4-hijklmn".getBytes())); - values.add(Pair.of("5".getBytes(), "5".getBytes())); - values.add(Pair.of("5-hijklmn".getBytes(), "5-hijklmn".getBytes())); - values.add(Pair.of("6".getBytes(), "6".getBytes())); - values.add(Pair.of("7".getBytes(), "7".getBytes())); - client.multiSet(tableName, hashKey, values); - - // check sortkey count - long sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(13, sortKeyCount); - - // [null, null) - MultiGetOptions options = new MultiGetOptions(); - Assert.assertTrue(options.startInclusive); - Assert.assertFalse(options.stopInclusive); - List> newValues = new ArrayList>(); - boolean ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(13, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(4).getKey()); - Assert.assertArrayEquals("3".getBytes(), newValues.get(5).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(6).getKey()); - Assert.assertArrayEquals("4".getBytes(), newValues.get(7).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(8).getKey()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(9).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(10).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(11).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(12).getKey()); - - // [null, null] - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(13, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(4).getKey()); - Assert.assertArrayEquals("3".getBytes(), newValues.get(5).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(6).getKey()); - Assert.assertArrayEquals("4".getBytes(), newValues.get(7).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(8).getKey()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(9).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(10).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(11).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(12).getKey()); - - // (null, null) - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = false; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(12, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("3".getBytes(), newValues.get(4).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(5).getKey()); - Assert.assertArrayEquals("4".getBytes(), newValues.get(6).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(7).getKey()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(8).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(9).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(10).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(11).getKey()); - - // (null, null] - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(12, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("3".getBytes(), newValues.get(4).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(5).getKey()); - Assert.assertArrayEquals("4".getBytes(), newValues.get(6).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(7).getKey()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(8).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(9).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(10).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(11).getKey()); - - // [null, 1] - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); - - // [null, 1) - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = false; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - - // (null, 1] - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - - // (null, 1) - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = false; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // [1, 1] - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - - // [1, 1) - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = false; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // (1, 1] - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // (1, 1) - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = false; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // [2, 1] - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = false; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "2".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-anywhere("-") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; - options.sortKeyFilterPattern = "-".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(5, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(4).getKey()); - - // match-anywhere("1") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; - options.sortKeyFilterPattern = "1".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-anywhere("1-") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; - options.sortKeyFilterPattern = "1-".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - - // match-anywhere("abc") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; - options.sortKeyFilterPattern = "abc".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-prefix("1") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-prefix("1") in [0, 1) - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = true; - options.stopInclusive = false; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "0".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("1") in [0, 1] - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = true; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "0".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - - // match-prefix("1") in [1, 2] - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = true; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "2".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-prefix("1") in (1, 2] - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = false; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "2".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - - // match-prefix("1") in (1-abcdefg, 2] - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = false; - options.stopInclusive = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1-abcdefg".getBytes(), "2".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("1-") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1-".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - - // match-prefix("1-x") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1-x".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("abc") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "abc".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("efg") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "efg".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("ijk") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "ijk".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("lnm") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "lmn".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-postfix("5-hijklmn") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "5-hijklmn".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(0).getKey()); - - // match-postfix("1") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "1".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - - // match-postfix("1-") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "1-".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-postfix("1-x") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "1-x".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-postfix("abc") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "abc".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-postfix("efg") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "efg".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-postfix("ijk") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "ijk".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(0).getKey()); - - // match-postfix("lmn") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "lmn".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(1).getKey()); - - // match-postfix("5-hijklmn") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "5-hijklmn".getBytes(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(0).getKey()); - - // maxCount = 4 - options = new MultiGetOptions(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, 4, -1, newValues); - Assert.assertFalse(ret); - Assert.assertEquals(4, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); - - // maxCount = 1 - options = new MultiGetOptions(); - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "5".getBytes(), "6".getBytes(), options, 1, -1, newValues); - Assert.assertFalse(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(0).getKey()); - - // multi del all - List sortKeys = new ArrayList(); - sortKeys.add("".getBytes()); - sortKeys.add("1".getBytes()); - sortKeys.add("1-abcdefg".getBytes()); - sortKeys.add("2".getBytes()); - sortKeys.add("2-abcdefg".getBytes()); - sortKeys.add("3".getBytes()); - sortKeys.add("3-efghijk".getBytes()); - sortKeys.add("4".getBytes()); - sortKeys.add("4-hijklmn".getBytes()); - sortKeys.add("5".getBytes()); - sortKeys.add("5-hijklmn".getBytes()); - sortKeys.add("6".getBytes()); - sortKeys.add("7".getBytes()); - client.multiDel(tableName, hashKey, sortKeys); - - // check sortkey count - sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(0, sortKeyCount); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testBatchSetGetDel() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + + try { + // batch set + List items = new ArrayList(); + items.add( + new SetItem( + "TestBasic_testBatchSetGetDel_hash_key_1".getBytes(), + "basic_test_sort_key".getBytes(), + "basic_test_value_1".getBytes())); + items.add( + new SetItem( + "TestBasic_testBatchSetGetDel_hash_key_2".getBytes(), + "basic_test_sort_key".getBytes(), + "basic_test_value_2".getBytes())); + items.add( + new SetItem( + "TestBasic_testBatchSetGetDel_hash_key_3".getBytes(), + "basic_test_sort_key".getBytes(), + "basic_test_value_3".getBytes())); + client.batchSet(tableName, items); + + // check exist + boolean exist = + client.exist( + tableName, + "TestBasic_testBatchSetGetDel_hash_key_1".getBytes(), + "basic_test_sort_key".getBytes()); + Assert.assertTrue(exist); + exist = + client.exist( + tableName, + "TestBasic_testBatchSetGetDel_hash_key_2".getBytes(), + "basic_test_sort_key".getBytes()); + Assert.assertTrue(exist); + exist = + client.exist( + tableName, + "TestBasic_testBatchSetGetDel_hash_key_3".getBytes(), + "basic_test_sort_key".getBytes()); + Assert.assertTrue(exist); + + // batch get + List> keys = new ArrayList>(); + keys.add( + Pair.of( + "TestBasic_testBatchSetGetDel_hash_key_1".getBytes(), + "basic_test_sort_key".getBytes())); + keys.add( + Pair.of( + "TestBasic_testBatchSetGetDel_hash_key_2".getBytes(), + "basic_test_sort_key".getBytes())); + keys.add( + Pair.of( + "TestBasic_testBatchSetGetDel_hash_key_3".getBytes(), + "basic_test_sort_key".getBytes())); + List values = new ArrayList(); + client.batchGet(tableName, keys, values); + Assert.assertEquals(3, values.size()); + Assert.assertArrayEquals("basic_test_value_1".getBytes(), values.get(0)); + Assert.assertArrayEquals("basic_test_value_2".getBytes(), values.get(1)); + Assert.assertArrayEquals("basic_test_value_3".getBytes(), values.get(2)); + + // batch del + client.batchDel(tableName, keys); + + // check deleted + client.batchGet(tableName, keys, values); + Assert.assertEquals(3, values.size()); + Assert.assertNull(values.get(0)); + Assert.assertNull(values.get(1)); + Assert.assertNull(values.get(2)); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testBatchSetGetDel2() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + + try { + // batch set + List items = new ArrayList(); + items.add( + new SetItem( + "TestBasic_testBatchSetGetDel2_hash_key_1".getBytes(), + "basic_test_sort_key".getBytes(), + "basic_test_value_1".getBytes())); + items.add( + new SetItem( + "TestBasic_testBatchSetGetDel2_hash_key_2".getBytes(), + "basic_test_sort_key".getBytes(), + "basic_test_value_2".getBytes())); + items.add( + new SetItem( + "TestBasic_testBatchSetGetDel2_hash_key_3".getBytes(), + "basic_test_sort_key".getBytes(), + "basic_test_value_3".getBytes())); + List resultsBatchSet = new ArrayList(); + int count = client.batchSet2(tableName, items, resultsBatchSet); + Assert.assertEquals(3, count); + Assert.assertEquals(3, resultsBatchSet.size()); + Assert.assertNull(resultsBatchSet.get(0)); + Assert.assertNull(resultsBatchSet.get(1)); + Assert.assertNull(resultsBatchSet.get(2)); + + // check exist + boolean exist = + client.exist( + tableName, + "TestBasic_testBatchSetGetDel2_hash_key_1".getBytes(), + "basic_test_sort_key".getBytes()); + Assert.assertTrue(exist); + exist = + client.exist( + tableName, + "TestBasic_testBatchSetGetDel2_hash_key_2".getBytes(), + "basic_test_sort_key".getBytes()); + Assert.assertTrue(exist); + exist = + client.exist( + tableName, + "TestBasic_testBatchSetGetDel2_hash_key_3".getBytes(), + "basic_test_sort_key".getBytes()); + Assert.assertTrue(exist); + + // batch get + List> keys = new ArrayList>(); + keys.add( + Pair.of( + "TestBasic_testBatchSetGetDel2_hash_key_1".getBytes(), + "basic_test_sort_key".getBytes())); + keys.add( + Pair.of( + "TestBasic_testBatchSetGetDel2_hash_key_2".getBytes(), + "basic_test_sort_key".getBytes())); + keys.add( + Pair.of( + "TestBasic_testBatchSetGetDel2_hash_key_3".getBytes(), + "basic_test_sort_key".getBytes())); + List> resultsBatchGet = new ArrayList>(); + count = client.batchGet2(tableName, keys, resultsBatchGet); + Assert.assertEquals(3, count); + Assert.assertEquals(3, resultsBatchGet.size()); + Assert.assertNull(resultsBatchGet.get(0).getLeft()); + Assert.assertArrayEquals("basic_test_value_1".getBytes(), resultsBatchGet.get(0).getRight()); + Assert.assertNull(resultsBatchGet.get(1).getLeft()); + Assert.assertArrayEquals("basic_test_value_2".getBytes(), resultsBatchGet.get(1).getRight()); + Assert.assertNull(resultsBatchGet.get(2).getLeft()); + Assert.assertArrayEquals("basic_test_value_3".getBytes(), resultsBatchGet.get(2).getRight()); + + // batch del + List resultsBatchDel = new ArrayList(); + count = client.batchDel2(tableName, keys, resultsBatchDel); + Assert.assertEquals(3, count); + Assert.assertEquals(3, resultsBatchSet.size()); + Assert.assertNull(resultsBatchSet.get(0)); + Assert.assertNull(resultsBatchSet.get(1)); + Assert.assertNull(resultsBatchSet.get(2)); + + // check deleted + List values = new ArrayList(); + client.batchGet(tableName, keys, values); + Assert.assertEquals(3, values.size()); + Assert.assertNull(values.get(0)); + Assert.assertNull(values.get(1)); + Assert.assertNull(values.get(2)); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } - @Test - public void testMultiGetReverse() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = multiGetReverseHashKey.getBytes(); - - try { - // multi set - List> values = new ArrayList>(); - values.add(Pair.of("".getBytes(), "0".getBytes())); - values.add(Pair.of("1".getBytes(), "1".getBytes())); - values.add(Pair.of("1-abcdefg".getBytes(), "1-abcdefg".getBytes())); - values.add(Pair.of("2".getBytes(), "2".getBytes())); - values.add(Pair.of("2-abcdefg".getBytes(), "2-abcdefg".getBytes())); - values.add(Pair.of("3".getBytes(), "3".getBytes())); - values.add(Pair.of("3-efghijk".getBytes(), "3-efghijk".getBytes())); - values.add(Pair.of("4".getBytes(), "4".getBytes())); - values.add(Pair.of("4-hijklmn".getBytes(), "4-hijklmn".getBytes())); - values.add(Pair.of("5".getBytes(), "5".getBytes())); - values.add(Pair.of("5-hijklmn".getBytes(), "5-hijklmn".getBytes())); - values.add(Pair.of("6".getBytes(), "6".getBytes())); - values.add(Pair.of("7".getBytes(), "7".getBytes())); - client.multiSet(tableName, hashKey, values); - - // check sortkey count - long sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(13, sortKeyCount); - - // [null, null) - MultiGetOptions options = new MultiGetOptions(); - Assert.assertTrue(options.startInclusive); - Assert.assertFalse(options.stopInclusive); - options.reverse = true; - List> newValues = new ArrayList>(); - boolean ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(13, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(4).getKey()); - Assert.assertArrayEquals("3".getBytes(), newValues.get(5).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(6).getKey()); - Assert.assertArrayEquals("4".getBytes(), newValues.get(7).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(8).getKey()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(9).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(10).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(11).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(12).getKey()); - - // [null, null] - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(13, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(4).getKey()); - Assert.assertArrayEquals("3".getBytes(), newValues.get(5).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(6).getKey()); - Assert.assertArrayEquals("4".getBytes(), newValues.get(7).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(8).getKey()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(9).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(10).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(11).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(12).getKey()); - - // (null, null) - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = false; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(12, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("3".getBytes(), newValues.get(4).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(5).getKey()); - Assert.assertArrayEquals("4".getBytes(), newValues.get(6).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(7).getKey()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(8).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(9).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(10).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(11).getKey()); - - // (null, null] - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(12, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("2".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("3".getBytes(), newValues.get(4).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(5).getKey()); - Assert.assertArrayEquals("4".getBytes(), newValues.get(6).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(7).getKey()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(8).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(9).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(10).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(11).getKey()); - - // [null, 1] - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(1).getKey()); - - // [null, 1) - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = false; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("".getBytes(), newValues.get(0).getKey()); - - // (null, 1] - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - - // (null, 1) - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = false; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // [1, 1] - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - - // [1, 1) - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = false; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // (1, 1] - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // (1, 1) - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = false; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // [2, 1] - options = new MultiGetOptions(); - options.startInclusive = false; - options.stopInclusive = false; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "2".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-anywhere("-") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; - options.sortKeyFilterPattern = "-".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(5, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(3).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(4).getKey()); - - // match-anywhere("1") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; - options.sortKeyFilterPattern = "1".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-anywhere("1-") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; - options.sortKeyFilterPattern = "1-".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - - // match-anywhere("abc") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_ANYWHERE; - options.sortKeyFilterPattern = "abc".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-prefix("1") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-prefix("1") in [0, 1) - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = true; - options.stopInclusive = false; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "0".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("1") in [0, 1] - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = true; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "0".getBytes(), "1".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - - // match-prefix("1") in [1, 2] - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = true; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "2".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-prefix("1") in (1, 2] - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = false; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1".getBytes(), "2".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - - // match-prefix("1") in (1-abcdefg, 2] - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.startInclusive = false; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "1-abcdefg".getBytes(), "2".getBytes(), options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("1-") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1-".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - - // match-prefix("1-x") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "1-x".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("abc") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "abc".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("efg") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "efg".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("ijk") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "ijk".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-prefix("lnm") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "lmn".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-postfix("5-hijklmn") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "5-hijklmn".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(0).getKey()); - - // match-postfix("1") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "1".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("1".getBytes(), newValues.get(0).getKey()); - - // match-postfix("1-") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "1-".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-postfix("1-x") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "1-x".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-postfix("abc") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "abc".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(0, newValues.size()); - - // match-postfix("efg") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "efg".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("1-abcdefg".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("2-abcdefg".getBytes(), newValues.get(1).getKey()); - - // match-postfix("ijk") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "ijk".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("3-efghijk".getBytes(), newValues.get(0).getKey()); - - // match-postfix("lmn") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "lmn".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(2, newValues.size()); - Assert.assertArrayEquals("4-hijklmn".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(1).getKey()); - - // match-postfix("5-hijklmn") - options = new MultiGetOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; - options.sortKeyFilterPattern = "5-hijklmn".getBytes(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, newValues); - Assert.assertTrue(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(0).getKey()); - - // maxCount = 4 - options = new MultiGetOptions(); - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, null, null, options, 4, -1, newValues); - Assert.assertFalse(ret); - Assert.assertEquals(4, newValues.size()); - Assert.assertArrayEquals("5".getBytes(), newValues.get(0).getKey()); - Assert.assertArrayEquals("5-hijklmn".getBytes(), newValues.get(1).getKey()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(2).getKey()); - Assert.assertArrayEquals("7".getBytes(), newValues.get(3).getKey()); - - // maxCount = 1 - options = new MultiGetOptions(); - options.startInclusive = true; - options.stopInclusive = true; - options.reverse = true; - newValues.clear(); - ret = client.multiGet(tableName, hashKey, "5".getBytes(), "6".getBytes(), options,1, -1, newValues); - Assert.assertFalse(ret); - Assert.assertEquals(1, newValues.size()); - Assert.assertArrayEquals("6".getBytes(), newValues.get(0).getKey()); - - // multi del all - List sortKeys = new ArrayList(); - sortKeys.add("".getBytes()); - sortKeys.add("1".getBytes()); - sortKeys.add("1-abcdefg".getBytes()); - sortKeys.add("2".getBytes()); - sortKeys.add("2-abcdefg".getBytes()); - sortKeys.add("3".getBytes()); - sortKeys.add("3-efghijk".getBytes()); - sortKeys.add("4".getBytes()); - sortKeys.add("4-hijklmn".getBytes()); - sortKeys.add("5".getBytes()); - sortKeys.add("5-hijklmn".getBytes()); - sortKeys.add("6".getBytes()); - sortKeys.add("7".getBytes()); - client.multiDel(tableName, hashKey, sortKeys); - - // check sortkey count - sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(0, sortKeyCount); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testBatchMultiSetGetDel() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + + try { + // batch multi set + List items = new ArrayList(); + items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel_hash_key_1".getBytes())); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes()); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_3".getBytes(), "basic_test_value_3".getBytes()); + items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel_hash_key_2".getBytes())); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes()); + items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel_hash_key_3".getBytes())); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); + client.batchMultiSet(tableName, items); + + // batch multi get + List>> keys = new ArrayList>>(); + List nullSortKeys = null; + keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_1".getBytes(), nullSortKeys)); + keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_2".getBytes(), nullSortKeys)); + keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_3".getBytes(), nullSortKeys)); + keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_4".getBytes(), nullSortKeys)); + List values = new ArrayList(); + client.batchMultiGet(tableName, keys, values); + Assert.assertEquals(4, values.size()); + + Assert.assertArrayEquals(keys.get(0).getLeft(), values.get(0).hashKey); + Assert.assertEquals(3, values.get(0).values.size()); + Assert.assertArrayEquals( + "basic_test_sort_key_1".getBytes(), values.get(0).values.get(0).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_1".getBytes(), values.get(0).values.get(0).getRight()); + Assert.assertArrayEquals( + "basic_test_sort_key_2".getBytes(), values.get(0).values.get(1).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_2".getBytes(), values.get(0).values.get(1).getRight()); + Assert.assertArrayEquals( + "basic_test_sort_key_3".getBytes(), values.get(0).values.get(2).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_3".getBytes(), values.get(0).values.get(2).getRight()); + + Assert.assertArrayEquals(keys.get(1).getLeft(), values.get(1).hashKey); + Assert.assertEquals(2, values.get(1).values.size()); + Assert.assertArrayEquals( + "basic_test_sort_key_1".getBytes(), values.get(1).values.get(0).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_1".getBytes(), values.get(1).values.get(0).getRight()); + Assert.assertArrayEquals( + "basic_test_sort_key_2".getBytes(), values.get(1).values.get(1).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_2".getBytes(), values.get(1).values.get(1).getRight()); + + Assert.assertArrayEquals(keys.get(2).getLeft(), values.get(2).hashKey); + Assert.assertEquals(1, values.get(2).values.size()); + Assert.assertArrayEquals( + "basic_test_sort_key_1".getBytes(), values.get(2).values.get(0).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_1".getBytes(), values.get(2).values.get(0).getRight()); + + Assert.assertArrayEquals(keys.get(3).getLeft(), values.get(3).hashKey); + Assert.assertEquals(0, values.get(3).values.size()); + + // batch multi del + List>> delKeys = new ArrayList>>(); + List delSortKeys = new ArrayList(); + delSortKeys.add("basic_test_sort_key_1".getBytes()); + delSortKeys.add("basic_test_sort_key_2".getBytes()); + delSortKeys.add("basic_test_sort_key_3".getBytes()); + delSortKeys.add("basic_test_sort_key_4".getBytes()); + delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_1".getBytes(), delSortKeys)); + delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_2".getBytes(), delSortKeys)); + delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_3".getBytes(), delSortKeys)); + client.batchMultiDel(tableName, delKeys); + + // check deleted + client.batchMultiGet(tableName, keys, values); + Assert.assertEquals(4, values.size()); + Assert.assertEquals(0, values.get(0).values.size()); + Assert.assertEquals(0, values.get(1).values.size()); + Assert.assertEquals(0, values.get(2).values.size()); + Assert.assertEquals(0, values.get(3).values.size()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testBatchSetGetDel() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - - try { - // batch set - List items = new ArrayList(); - items.add(new SetItem("TestBasic_testBatchSetGetDel_hash_key_1".getBytes(), - "basic_test_sort_key".getBytes(), "basic_test_value_1".getBytes())); - items.add(new SetItem("TestBasic_testBatchSetGetDel_hash_key_2".getBytes(), - "basic_test_sort_key".getBytes(), "basic_test_value_2".getBytes())); - items.add(new SetItem("TestBasic_testBatchSetGetDel_hash_key_3".getBytes(), - "basic_test_sort_key".getBytes(), "basic_test_value_3".getBytes())); - client.batchSet(tableName, items); - - // check exist - boolean exist = client.exist(tableName, - "TestBasic_testBatchSetGetDel_hash_key_1".getBytes(), - "basic_test_sort_key".getBytes()); - Assert.assertTrue(exist); - exist = client.exist(tableName, - "TestBasic_testBatchSetGetDel_hash_key_2".getBytes(), - "basic_test_sort_key".getBytes()); - Assert.assertTrue(exist); - exist = client.exist(tableName, - "TestBasic_testBatchSetGetDel_hash_key_3".getBytes(), - "basic_test_sort_key".getBytes()); - Assert.assertTrue(exist); - - // batch get - List> keys = new ArrayList>(); - keys.add(Pair.of("TestBasic_testBatchSetGetDel_hash_key_1".getBytes(), - "basic_test_sort_key".getBytes())); - keys.add(Pair.of("TestBasic_testBatchSetGetDel_hash_key_2".getBytes(), - "basic_test_sort_key".getBytes())); - keys.add(Pair.of("TestBasic_testBatchSetGetDel_hash_key_3".getBytes(), - "basic_test_sort_key".getBytes())); - List values = new ArrayList(); - client.batchGet(tableName, keys, values); - Assert.assertEquals(3, values.size()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), values.get(0)); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), values.get(1)); - Assert.assertArrayEquals("basic_test_value_3".getBytes(), values.get(2)); - - // batch del - client.batchDel(tableName, keys); - - // check deleted - client.batchGet(tableName, keys, values); - Assert.assertEquals(3, values.size()); - Assert.assertNull(values.get(0)); - Assert.assertNull(values.get(1)); - Assert.assertNull(values.get(2)); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testBatchMultiSetGetDel2() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + + try { + // batch multi set + List items = new ArrayList(); + items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel2_hash_key_1".getBytes())); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes()); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_3".getBytes(), "basic_test_value_3".getBytes()); + items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel2_hash_key_2".getBytes())); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes()); + items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel2_hash_key_3".getBytes())); + items + .get(items.size() - 1) + .addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); + List resultsBatchMultiSet = new ArrayList(); + int count = client.batchMultiSet2(tableName, items, resultsBatchMultiSet); + Assert.assertEquals(3, count); + Assert.assertEquals(3, resultsBatchMultiSet.size()); + Assert.assertNull(resultsBatchMultiSet.get(0)); + Assert.assertNull(resultsBatchMultiSet.get(1)); + Assert.assertNull(resultsBatchMultiSet.get(2)); + + // batch multi get + List>> keys = new ArrayList>>(); + List nullSortKeys = null; + keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_1".getBytes(), nullSortKeys)); + keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_2".getBytes(), nullSortKeys)); + keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_3".getBytes(), nullSortKeys)); + keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_4".getBytes(), nullSortKeys)); + List> resultsBatchMultiGet = + new ArrayList>(); + count = client.batchMultiGet2(tableName, keys, resultsBatchMultiGet); + Assert.assertEquals(4, count); + Assert.assertEquals(4, resultsBatchMultiGet.size()); + + Assert.assertNull(resultsBatchMultiGet.get(0).getLeft()); + Assert.assertArrayEquals( + keys.get(0).getLeft(), resultsBatchMultiGet.get(0).getRight().hashKey); + Assert.assertEquals(3, resultsBatchMultiGet.get(0).getRight().values.size()); + Assert.assertArrayEquals( + "basic_test_sort_key_1".getBytes(), + resultsBatchMultiGet.get(0).getRight().values.get(0).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_1".getBytes(), + resultsBatchMultiGet.get(0).getRight().values.get(0).getRight()); + Assert.assertArrayEquals( + "basic_test_sort_key_2".getBytes(), + resultsBatchMultiGet.get(0).getRight().values.get(1).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_2".getBytes(), + resultsBatchMultiGet.get(0).getRight().values.get(1).getRight()); + Assert.assertArrayEquals( + "basic_test_sort_key_3".getBytes(), + resultsBatchMultiGet.get(0).getRight().values.get(2).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_3".getBytes(), + resultsBatchMultiGet.get(0).getRight().values.get(2).getRight()); + + Assert.assertNull(resultsBatchMultiGet.get(1).getLeft()); + Assert.assertArrayEquals( + keys.get(1).getLeft(), resultsBatchMultiGet.get(1).getRight().hashKey); + Assert.assertEquals(2, resultsBatchMultiGet.get(1).getRight().values.size()); + Assert.assertArrayEquals( + "basic_test_sort_key_1".getBytes(), + resultsBatchMultiGet.get(1).getRight().values.get(0).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_1".getBytes(), + resultsBatchMultiGet.get(1).getRight().values.get(0).getRight()); + Assert.assertArrayEquals( + "basic_test_sort_key_2".getBytes(), + resultsBatchMultiGet.get(1).getRight().values.get(1).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_2".getBytes(), + resultsBatchMultiGet.get(1).getRight().values.get(1).getRight()); + + Assert.assertNull(resultsBatchMultiGet.get(2).getLeft()); + Assert.assertArrayEquals( + keys.get(2).getLeft(), resultsBatchMultiGet.get(2).getRight().hashKey); + Assert.assertEquals(1, resultsBatchMultiGet.get(2).getRight().values.size()); + Assert.assertArrayEquals( + "basic_test_sort_key_1".getBytes(), + resultsBatchMultiGet.get(2).getRight().values.get(0).getLeft()); + Assert.assertArrayEquals( + "basic_test_value_1".getBytes(), + resultsBatchMultiGet.get(2).getRight().values.get(0).getRight()); + + Assert.assertNull(resultsBatchMultiGet.get(3).getLeft()); + Assert.assertArrayEquals( + keys.get(3).getLeft(), resultsBatchMultiGet.get(3).getRight().hashKey); + Assert.assertEquals(0, resultsBatchMultiGet.get(3).getRight().values.size()); + + // batch multi del + List>> delKeys = new ArrayList>>(); + List delSortKeys = new ArrayList(); + delSortKeys.add("basic_test_sort_key_1".getBytes()); + delSortKeys.add("basic_test_sort_key_2".getBytes()); + delSortKeys.add("basic_test_sort_key_3".getBytes()); + delSortKeys.add("basic_test_sort_key_4".getBytes()); + delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_1".getBytes(), delSortKeys)); + delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_2".getBytes(), delSortKeys)); + delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_3".getBytes(), delSortKeys)); + List resultsBatchMultiDel = new ArrayList(); + count = client.batchMultiDel2(tableName, delKeys, resultsBatchMultiDel); + Assert.assertEquals(3, count); + Assert.assertEquals(3, resultsBatchMultiSet.size()); + Assert.assertNull(resultsBatchMultiSet.get(0)); + Assert.assertNull(resultsBatchMultiSet.get(1)); + Assert.assertNull(resultsBatchMultiSet.get(2)); + + // check deleted + List values = new ArrayList(); + client.batchMultiGet(tableName, keys, values); + Assert.assertEquals(4, values.size()); + Assert.assertEquals(0, values.get(0).values.size()); + Assert.assertEquals(0, values.get(1).values.size()); + Assert.assertEquals(0, values.get(2).values.size()); + Assert.assertEquals(0, values.get(3).values.size()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testBatchSetGetDel2() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - - try { - // batch set - List items = new ArrayList(); - items.add(new SetItem("TestBasic_testBatchSetGetDel2_hash_key_1".getBytes(), - "basic_test_sort_key".getBytes(), "basic_test_value_1".getBytes())); - items.add(new SetItem("TestBasic_testBatchSetGetDel2_hash_key_2".getBytes(), - "basic_test_sort_key".getBytes(), "basic_test_value_2".getBytes())); - items.add(new SetItem("TestBasic_testBatchSetGetDel2_hash_key_3".getBytes(), - "basic_test_sort_key".getBytes(), "basic_test_value_3".getBytes())); - List resultsBatchSet = new ArrayList(); - int count = client.batchSet2(tableName, items, resultsBatchSet); - Assert.assertEquals(3, count); - Assert.assertEquals(3, resultsBatchSet.size()); - Assert.assertNull(resultsBatchSet.get(0)); - Assert.assertNull(resultsBatchSet.get(1)); - Assert.assertNull(resultsBatchSet.get(2)); - - // check exist - boolean exist = client.exist(tableName, - "TestBasic_testBatchSetGetDel2_hash_key_1".getBytes(), - "basic_test_sort_key".getBytes()); - Assert.assertTrue(exist); - exist = client.exist(tableName, - "TestBasic_testBatchSetGetDel2_hash_key_2".getBytes(), - "basic_test_sort_key".getBytes()); - Assert.assertTrue(exist); - exist = client.exist(tableName, - "TestBasic_testBatchSetGetDel2_hash_key_3".getBytes(), - "basic_test_sort_key".getBytes()); - Assert.assertTrue(exist); - - // batch get - List> keys = new ArrayList>(); - keys.add(Pair.of("TestBasic_testBatchSetGetDel2_hash_key_1".getBytes(), - "basic_test_sort_key".getBytes())); - keys.add(Pair.of("TestBasic_testBatchSetGetDel2_hash_key_2".getBytes(), - "basic_test_sort_key".getBytes())); - keys.add(Pair.of("TestBasic_testBatchSetGetDel2_hash_key_3".getBytes(), - "basic_test_sort_key".getBytes())); - List> resultsBatchGet = new ArrayList>(); - count = client.batchGet2(tableName, keys, resultsBatchGet); - Assert.assertEquals(3, count); - Assert.assertEquals(3, resultsBatchGet.size()); - Assert.assertNull(resultsBatchGet.get(0).getLeft()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), resultsBatchGet.get(0).getRight()); - Assert.assertNull(resultsBatchGet.get(1).getLeft()); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), resultsBatchGet.get(1).getRight()); - Assert.assertNull(resultsBatchGet.get(2).getLeft()); - Assert.assertArrayEquals("basic_test_value_3".getBytes(), resultsBatchGet.get(2).getRight()); - - // batch del - List resultsBatchDel = new ArrayList(); - count = client.batchDel2(tableName, keys, resultsBatchDel); - Assert.assertEquals(3, count); - Assert.assertEquals(3, resultsBatchSet.size()); - Assert.assertNull(resultsBatchSet.get(0)); - Assert.assertNull(resultsBatchSet.get(1)); - Assert.assertNull(resultsBatchSet.get(2)); - - // check deleted - List values = new ArrayList(); - client.batchGet(tableName, keys, values); - Assert.assertEquals(3, values.size()); - Assert.assertNull(values.get(0)); - Assert.assertNull(values.get(1)); - Assert.assertNull(values.get(2)); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void asyncApiTest() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + PegasusTableInterface tb = client.openTable("temp"); + + String asyncHashPrefix = "AsyncApiTestHash"; + String asyncSortPrefix = "AsyncApiTestSort"; + String asyncValuePrefix = "AsyncApiTestValue"; + String key = asyncHashPrefix + "_0"; + + // Exist + System.out.println("Test exist"); + try { + Assert.assertFalse(tb.asyncExist(key.getBytes(), key.getBytes(), 0).await().getNow()); + Assert.assertFalse(tb.asyncExist(null, null, 0).await().getNow()); + Assert.assertFalse(tb.asyncExist(null, key.getBytes(), 0).await().getNow()); + Assert.assertFalse(tb.asyncExist(key.getBytes(), null, 0).await().getNow()); + } catch (Throwable e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testBatchMultiSetGetDel() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - - try { - // batch multi set - List items = new ArrayList(); - items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel_hash_key_1".getBytes())); - items.get(items.size()-1).addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); - items.get(items.size()-1).addData("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes()); - items.get(items.size()-1).addData("basic_test_sort_key_3".getBytes(), "basic_test_value_3".getBytes()); - items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel_hash_key_2".getBytes())); - items.get(items.size()-1).addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); - items.get(items.size()-1).addData("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes()); - items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel_hash_key_3".getBytes())); - items.get(items.size()-1).addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); - client.batchMultiSet(tableName, items); - - // batch multi get - List>> keys = new ArrayList>>(); - List nullSortKeys = null; - keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_1".getBytes(), nullSortKeys)); - keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_2".getBytes(), nullSortKeys)); - keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_3".getBytes(), nullSortKeys)); - keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_4".getBytes(), nullSortKeys)); - List values = new ArrayList(); - client.batchMultiGet(tableName, keys, values); - Assert.assertEquals(4, values.size()); - - Assert.assertArrayEquals(keys.get(0).getLeft(), values.get(0).hashKey); - Assert.assertEquals(3, values.get(0).values.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), values.get(0).values.get(0).getLeft()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), values.get(0).values.get(0).getRight()); - Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), values.get(0).values.get(1).getLeft()); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), values.get(0).values.get(1).getRight()); - Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), values.get(0).values.get(2).getLeft()); - Assert.assertArrayEquals("basic_test_value_3".getBytes(), values.get(0).values.get(2).getRight()); - - Assert.assertArrayEquals(keys.get(1).getLeft(), values.get(1).hashKey); - Assert.assertEquals(2, values.get(1).values.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), values.get(1).values.get(0).getLeft()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), values.get(1).values.get(0).getRight()); - Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), values.get(1).values.get(1).getLeft()); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), values.get(1).values.get(1).getRight()); - - Assert.assertArrayEquals(keys.get(2).getLeft(), values.get(2).hashKey); - Assert.assertEquals(1, values.get(2).values.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), values.get(2).values.get(0).getLeft()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), values.get(2).values.get(0).getRight()); - - Assert.assertArrayEquals(keys.get(3).getLeft(), values.get(3).hashKey); - Assert.assertEquals(0, values.get(3).values.size()); - - // batch multi del - List>> delKeys = new ArrayList>>(); - List delSortKeys = new ArrayList(); - delSortKeys.add("basic_test_sort_key_1".getBytes()); - delSortKeys.add("basic_test_sort_key_2".getBytes()); - delSortKeys.add("basic_test_sort_key_3".getBytes()); - delSortKeys.add("basic_test_sort_key_4".getBytes()); - delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_1".getBytes(), delSortKeys)); - delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_2".getBytes(), delSortKeys)); - delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel_hash_key_3".getBytes(), delSortKeys)); - client.batchMultiDel(tableName, delKeys); - - // check deleted - client.batchMultiGet(tableName, keys, values); - Assert.assertEquals(4, values.size()); - Assert.assertEquals(0, values.get(0).values.size()); - Assert.assertEquals(0, values.get(1).values.size()); - Assert.assertEquals(0, values.get(2).values.size()); - Assert.assertEquals(0, values.get(3).values.size()); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + try { + Assert.assertNull( + tb.asyncSet(key.getBytes(), key.getBytes(), key.getBytes(), 0).await().getNow()); + Assert.assertTrue(tb.asyncExist(key.getBytes(), key.getBytes(), 0).await().getNow()); + } catch (Throwable e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testBatchMultiSetGetDel2() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - - try { - // batch multi set - List items = new ArrayList(); - items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel2_hash_key_1".getBytes())); - items.get(items.size()-1).addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); - items.get(items.size()-1).addData("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes()); - items.get(items.size()-1).addData("basic_test_sort_key_3".getBytes(), "basic_test_value_3".getBytes()); - items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel2_hash_key_2".getBytes())); - items.get(items.size()-1).addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); - items.get(items.size()-1).addData("basic_test_sort_key_2".getBytes(), "basic_test_value_2".getBytes()); - items.add(new HashKeyData("TestBasic_testBatchMultiSetGetDel2_hash_key_3".getBytes())); - items.get(items.size()-1).addData("basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); - List resultsBatchMultiSet = new ArrayList(); - int count = client.batchMultiSet2(tableName, items, resultsBatchMultiSet); - Assert.assertEquals(3, count); - Assert.assertEquals(3, resultsBatchMultiSet.size()); - Assert.assertNull(resultsBatchMultiSet.get(0)); - Assert.assertNull(resultsBatchMultiSet.get(1)); - Assert.assertNull(resultsBatchMultiSet.get(2)); - - // batch multi get - List>> keys = new ArrayList>>(); - List nullSortKeys = null; - keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_1".getBytes(), nullSortKeys)); - keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_2".getBytes(), nullSortKeys)); - keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_3".getBytes(), nullSortKeys)); - keys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_4".getBytes(), nullSortKeys)); - List> resultsBatchMultiGet = new ArrayList>(); - count = client.batchMultiGet2(tableName, keys, resultsBatchMultiGet); - Assert.assertEquals(4, count); - Assert.assertEquals(4, resultsBatchMultiGet.size()); - - Assert.assertNull(resultsBatchMultiGet.get(0).getLeft()); - Assert.assertArrayEquals(keys.get(0).getLeft(), resultsBatchMultiGet.get(0).getRight().hashKey); - Assert.assertEquals(3, resultsBatchMultiGet.get(0).getRight().values.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), resultsBatchMultiGet.get(0).getRight().values.get(0).getLeft()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), resultsBatchMultiGet.get(0).getRight().values.get(0).getRight()); - Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), resultsBatchMultiGet.get(0).getRight().values.get(1).getLeft()); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), resultsBatchMultiGet.get(0).getRight().values.get(1).getRight()); - Assert.assertArrayEquals("basic_test_sort_key_3".getBytes(), resultsBatchMultiGet.get(0).getRight().values.get(2).getLeft()); - Assert.assertArrayEquals("basic_test_value_3".getBytes(), resultsBatchMultiGet.get(0).getRight().values.get(2).getRight()); - - Assert.assertNull(resultsBatchMultiGet.get(1).getLeft()); - Assert.assertArrayEquals(keys.get(1).getLeft(), resultsBatchMultiGet.get(1).getRight().hashKey); - Assert.assertEquals(2, resultsBatchMultiGet.get(1).getRight().values.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), resultsBatchMultiGet.get(1).getRight().values.get(0).getLeft()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), resultsBatchMultiGet.get(1).getRight().values.get(0).getRight()); - Assert.assertArrayEquals("basic_test_sort_key_2".getBytes(), resultsBatchMultiGet.get(1).getRight().values.get(1).getLeft()); - Assert.assertArrayEquals("basic_test_value_2".getBytes(), resultsBatchMultiGet.get(1).getRight().values.get(1).getRight()); - - Assert.assertNull(resultsBatchMultiGet.get(2).getLeft()); - Assert.assertArrayEquals(keys.get(2).getLeft(), resultsBatchMultiGet.get(2).getRight().hashKey); - Assert.assertEquals(1, resultsBatchMultiGet.get(2).getRight().values.size()); - Assert.assertArrayEquals("basic_test_sort_key_1".getBytes(), resultsBatchMultiGet.get(2).getRight().values.get(0).getLeft()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), resultsBatchMultiGet.get(2).getRight().values.get(0).getRight()); - - Assert.assertNull(resultsBatchMultiGet.get(3).getLeft()); - Assert.assertArrayEquals(keys.get(3).getLeft(),resultsBatchMultiGet.get(3).getRight().hashKey); - Assert.assertEquals(0, resultsBatchMultiGet.get(3).getRight().values.size()); - - // batch multi del - List>> delKeys = new ArrayList>>(); - List delSortKeys = new ArrayList(); - delSortKeys.add("basic_test_sort_key_1".getBytes()); - delSortKeys.add("basic_test_sort_key_2".getBytes()); - delSortKeys.add("basic_test_sort_key_3".getBytes()); - delSortKeys.add("basic_test_sort_key_4".getBytes()); - delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_1".getBytes(), delSortKeys)); - delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_2".getBytes(), delSortKeys)); - delKeys.add(Pair.of("TestBasic_testBatchMultiSetGetDel2_hash_key_3".getBytes(), delSortKeys)); - List resultsBatchMultiDel = new ArrayList(); - count = client.batchMultiDel2(tableName, delKeys, resultsBatchMultiDel); - Assert.assertEquals(3, count); - Assert.assertEquals(3, resultsBatchMultiSet.size()); - Assert.assertNull(resultsBatchMultiSet.get(0)); - Assert.assertNull(resultsBatchMultiSet.get(1)); - Assert.assertNull(resultsBatchMultiSet.get(2)); - - // check deleted - List values = new ArrayList(); - client.batchMultiGet(tableName, keys, values); - Assert.assertEquals(4, values.size()); - Assert.assertEquals(0, values.get(0).values.size()); - Assert.assertEquals(0, values.get(1).values.size()); - Assert.assertEquals(0, values.get(2).values.size()); - Assert.assertEquals(0, values.get(3).values.size()); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + // SortKeyCount + System.out.println("Test sortkeycount"); + try { + Long ans = tb.asyncSortKeyCount(key.getBytes(), 0).await().getNow(); + Assert.assertEquals(1, (long) ans); + + Assert.assertNull(tb.asyncDel(key.getBytes(), key.getBytes(), 0).await().getNow()); + ans = tb.asyncSortKeyCount(key.getBytes(), 0).await().getNow(); + Assert.assertEquals(0, (long) ans); + + Future future = tb.asyncSortKeyCount(null, 0).await(); + Assert.assertFalse(future.isSuccess()); + Assert.assertTrue(future.cause() instanceof PException); + } catch (Throwable e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void asyncApiTest() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - PegasusTableInterface tb = client.openTable("temp"); - - String asyncHashPrefix = "AsyncApiTestHash"; - String asyncSortPrefix = "AsyncApiTestSort"; - String asyncValuePrefix = "AsyncApiTestValue"; - String key = asyncHashPrefix + "_0"; - - // Exist - System.out.println("Test exist"); - try { - Assert.assertFalse(tb.asyncExist(key.getBytes(), key.getBytes(), 0).await().getNow()); - Assert.assertFalse(tb.asyncExist(null, null, 0).await().getNow()); - Assert.assertFalse(tb.asyncExist(null, key.getBytes(), 0).await().getNow()); - Assert.assertFalse(tb.asyncExist(key.getBytes(), null, 0).await().getNow()); - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - Assert.assertNull( tb.asyncSet(key.getBytes(), key.getBytes(), key.getBytes(),0).await().getNow() ); - Assert.assertTrue( tb.asyncExist(key.getBytes(), key.getBytes(), 0).await().getNow() ); - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } - - // SortKeyCount - System.out.println("Test sortkeycount"); - try { - Long ans = tb.asyncSortKeyCount(key.getBytes(), 0).await().getNow(); - Assert.assertEquals(1, (long)ans); - - Assert.assertNull( tb.asyncDel(key.getBytes(), key.getBytes(), 0).await().getNow() ); - ans = tb.asyncSortKeyCount(key.getBytes(), 0).await().getNow(); - Assert.assertEquals(0, (long)ans); - - Future future = tb.asyncSortKeyCount(null, 0).await(); - Assert.assertFalse( future.isSuccess() ); - Assert.assertTrue( future.cause() instanceof PException ); - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } - - // Get - System.out.println("Test get"); - try { - Assert.assertNull( tb.asyncGet(null, null, 0).await().getNow() ); - Assert.assertNull( tb.asyncGet(null, key.getBytes(), 0).await().getNow() ); - Assert.assertNull( tb.asyncGet(key.getBytes(), null, 0).await().getNow() ); - Assert.assertNull( tb.asyncGet(key.getBytes(), key.getBytes(), 0).await().getNow() ); - - Assert.assertNull( tb.asyncSet(key.getBytes(), key.getBytes(), key.getBytes(), 0).await().getNow() ); - Assert.assertArrayEquals( key.getBytes(), tb.asyncGet(key.getBytes(), key.getBytes(), 0).await().getNow() ); - - Assert.assertNull( tb.asyncDel(key.getBytes(), key.getBytes(), 0).await().getNow() ); - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } - - // Set & ttl - System.out.println("Test set & ttl"); - try { - Assert.assertNull( tb.asyncSet(key.getBytes(), key.getBytes(), key.getBytes(), 5, 0).await().getNow() ); - Assert.assertArrayEquals( key.getBytes(), tb.asyncGet(key.getBytes(), key.getBytes(), 0).await().getNow() ); - - Integer ttlSeconds = tb.asyncTTL(key.getBytes(), key.getBytes(), 0).await().getNow(); - Assert.assertEquals( 5, (int)ttlSeconds ); - - Thread.sleep(6000); - Assert.assertFalse( tb.asyncExist(key.getBytes(), key.getBytes(), 0).await().getNow() ); - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } - - // multiGet exeception handle - System.out.println("Test multiget exception handle"); - try { - Future f = tb.asyncMultiGet(null, null, 0).await(); - Assert.assertFalse( f.isSuccess() ); - Assert.assertTrue( f.cause() instanceof PException ); - - f = tb.asyncMultiGet("".getBytes(), null, 0).await(); - Assert.assertFalse(f.isSuccess()); - Assert.assertTrue( f.cause() instanceof PException ); - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } - - // multiset exception handle - System.out.println("Test multiset exception handle"); - try { - Future f = tb.asyncMultiSet(null, null, 0).await(); - Assert.assertFalse( f.isSuccess() ); - Assert.assertTrue( f.cause() instanceof PException ); - - f = tb.asyncMultiSet("hehe".getBytes(), null, 0).await(); - Assert.assertFalse(f.isSuccess()); - Assert.assertTrue( f.cause() instanceof PException ); - - f = tb.asyncMultiSet("hehe".getBytes(), new ArrayList>(), 0).await(); - Assert.assertFalse(f.isSuccess()); - Assert.assertTrue( f.cause() instanceof PException ); - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } - - // multiDel exception handle - System.out.println("Test multidel exception handle"); - try { - Future f = tb.asyncMultiDel(null, null, 0).await(); - Assert.assertFalse( f.isSuccess() ); - Assert.assertTrue( f.cause() instanceof PException ); - - f = tb.asyncMultiDel("hehe".getBytes(), null, 0).await(); - Assert.assertFalse(f.isSuccess()); - Assert.assertTrue( f.cause() instanceof PException ); - - f = tb.asyncMultiDel("hehe".getBytes(), new ArrayList(), 0).await(); - Assert.assertFalse(f.isSuccess()); - Assert.assertTrue( f.cause() instanceof PException ); - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } - - // Set/get pipeline - System.out.println("Test set/get pipeline"); - try { - ArrayList> fl = new ArrayList>(); - - int totalNumber = 100; - for (int i=0; i f = tb.asyncSet(asyncHashPrefix.getBytes(), sortKey.getBytes(), value.getBytes(), 0, 0); - fl.add(f); - } + // Get + System.out.println("Test get"); + try { + Assert.assertNull(tb.asyncGet(null, null, 0).await().getNow()); + Assert.assertNull(tb.asyncGet(null, key.getBytes(), 0).await().getNow()); + Assert.assertNull(tb.asyncGet(key.getBytes(), null, 0).await().getNow()); + Assert.assertNull(tb.asyncGet(key.getBytes(), key.getBytes(), 0).await().getNow()); + + Assert.assertNull( + tb.asyncSet(key.getBytes(), key.getBytes(), key.getBytes(), 0).await().getNow()); + Assert.assertArrayEquals( + key.getBytes(), tb.asyncGet(key.getBytes(), key.getBytes(), 0).await().getNow()); + + Assert.assertNull(tb.asyncDel(key.getBytes(), key.getBytes(), 0).await().getNow()); + } catch (Throwable e) { + e.printStackTrace(); + Assert.fail(); + } - for (int i=0; i f = tb.asyncMultiGet(null, null, 0).await(); + Assert.assertFalse(f.isSuccess()); + Assert.assertTrue(f.cause() instanceof PException); + + f = tb.asyncMultiGet("".getBytes(), null, 0).await(); + Assert.assertFalse(f.isSuccess()); + Assert.assertTrue(f.cause() instanceof PException); + } catch (Throwable e) { + e.printStackTrace(); + Assert.fail(); + } - PegasusTableInterface.MultiGetSortKeysResult result = - tb.asyncMultiGetSortKeys(asyncHashPrefix.getBytes(), 150, 1000000, 0).await().getNow(); - Assert.assertEquals(totalNumber, result.keys.size()); + // multiset exception handle + System.out.println("Test multiset exception handle"); + try { + Future f = tb.asyncMultiSet(null, null, 0).await(); + Assert.assertFalse(f.isSuccess()); + Assert.assertTrue(f.cause() instanceof PException); + + f = tb.asyncMultiSet("hehe".getBytes(), null, 0).await(); + Assert.assertFalse(f.isSuccess()); + Assert.assertTrue(f.cause() instanceof PException); + + f = tb.asyncMultiSet("hehe".getBytes(), new ArrayList>(), 0).await(); + Assert.assertFalse(f.isSuccess()); + Assert.assertTrue(f.cause() instanceof PException); + } catch (Throwable e) { + e.printStackTrace(); + Assert.fail(); + } - ArrayList> fl2 = new ArrayList>(); - for (byte[] sortKey: result.keys) { - Future f = tb.asyncGet(asyncHashPrefix.getBytes(), sortKey, 0); - fl2.add(f); - } + // multiDel exception handle + System.out.println("Test multidel exception handle"); + try { + Future f = tb.asyncMultiDel(null, null, 0).await(); + Assert.assertFalse(f.isSuccess()); + Assert.assertTrue(f.cause() instanceof PException); + + f = tb.asyncMultiDel("hehe".getBytes(), null, 0).await(); + Assert.assertFalse(f.isSuccess()); + Assert.assertTrue(f.cause() instanceof PException); + + f = tb.asyncMultiDel("hehe".getBytes(), new ArrayList(), 0).await(); + Assert.assertFalse(f.isSuccess()); + Assert.assertTrue(f.cause() instanceof PException); + } catch (Throwable e) { + e.printStackTrace(); + Assert.fail(); + } - for (int i=0; i> fl = new ArrayList>(); + + int totalNumber = 100; + for (int i = 0; i < totalNumber; ++i) { + String formatted = String.format("%03d", i); + String sortKey = asyncSortPrefix + "_" + formatted; + String value = asyncValuePrefix + "_" + formatted; + Future f = + tb.asyncSet(asyncHashPrefix.getBytes(), sortKey.getBytes(), value.getBytes(), 0, 0); + fl.add(f); + } + + for (int i = 0; i < totalNumber; ++i) { + fl.get(i).await(); + Assert.assertTrue(fl.get(i).isSuccess()); + } + + Long sortCount = tb.asyncSortKeyCount(asyncHashPrefix.getBytes(), 0).await().getNow(); + Assert.assertEquals(totalNumber, (long) sortCount); + + PegasusTableInterface.MultiGetSortKeysResult result = + tb.asyncMultiGetSortKeys(asyncHashPrefix.getBytes(), 150, 1000000, 0).await().getNow(); + Assert.assertEquals(totalNumber, result.keys.size()); + + ArrayList> fl2 = new ArrayList>(); + for (byte[] sortKey : result.keys) { + Future f = tb.asyncGet(asyncHashPrefix.getBytes(), sortKey, 0); + fl2.add(f); + } + + for (int i = 0; i < totalNumber; ++i) { + byte[] value = fl2.get(i).await().getNow(); + String formatted = String.format("%03d", i); + String expectValue = asyncValuePrefix + "_" + formatted; + Assert.assertArrayEquals(expectValue.getBytes(), value); + } + + fl.clear(); + for (byte[] sortKey : result.keys) { + Future f = tb.asyncDel(asyncHashPrefix.getBytes(), sortKey, 0); + fl.add(f); + } + + for (Future f : fl) { + f.await(); + Assert.assertTrue(f.isSuccess()); + } + + Long sortKeyCount = tb.sortKeyCount(asyncHashPrefix.getBytes(), 0); + Assert.assertEquals(0, (long) sortKeyCount); + + } catch (Throwable e) { + e.printStackTrace(); + Assert.fail(); + } + } + + @Test + public void asyncApiSample() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + PegasusTableInterface table = client.openTable("temp"); + + String key = "hello"; + String value = "world"; + + Future f = table.asyncSet(null, null, null, 0); + f.addListener( + new PegasusTableInterface.SetListener() { + @Override + public void operationComplete(Future future) throws Exception { + if (future.isSuccess()) { + System.out.println("set succeed"); + Assert.fail(); + } else { + assert future.cause() instanceof PException; } + } + }); + f.awaitUninterruptibly(); + } + + @Test + public void scanWithFilter() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + PegasusTableInterface table = client.openTable("temp"); + byte[] hashKey = "x".getBytes(); + List> values = new ArrayList>(); + values.add(Pair.of("m_1".getBytes(), "a".getBytes())); + values.add(Pair.of("m_2".getBytes(), "a".getBytes())); + values.add(Pair.of("m_3".getBytes(), "a".getBytes())); + values.add(Pair.of("m_4".getBytes(), "a".getBytes())); + values.add(Pair.of("m_5".getBytes(), "a".getBytes())); + values.add(Pair.of("n_1".getBytes(), "b".getBytes())); + values.add(Pair.of("n_2".getBytes(), "b".getBytes())); + values.add(Pair.of("n_3".getBytes(), "b".getBytes())); + + try { + // multi set + table.multiSet(hashKey, values, 0); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } - fl.clear(); - for (byte[] sortKey: result.keys) { - Future f = tb.asyncDel(asyncHashPrefix.getBytes(), sortKey, 0); - fl.add(f); - } + try { + // scan with batch_size = 10 + ScanOptions options = new ScanOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "m".getBytes(); + options.batchSize = 10; + Map data = new HashMap(); + PegasusScannerInterface scanner = table.getScanner(hashKey, null, null, options); + Assert.assertNotNull(scanner); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + Assert.assertArrayEquals(hashKey, item.getLeft().getLeft()); + Assert.assertArrayEquals("a".getBytes(), item.getRight()); + data.put(new String(item.getLeft().getRight()), new String(item.getRight())); + } + Assert.assertEquals(5, data.size()); + Assert.assertTrue(data.containsKey("m_1")); + Assert.assertTrue(data.containsKey("m_2")); + Assert.assertTrue(data.containsKey("m_3")); + Assert.assertTrue(data.containsKey("m_4")); + Assert.assertTrue(data.containsKey("m_5")); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } - for (Future f: fl) { - f.await(); - Assert.assertTrue( f.isSuccess() ); - } + try { + // scan with batch_size = 3 + ScanOptions options = new ScanOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "m".getBytes(); + options.batchSize = 3; + Map data = new HashMap(); + PegasusScannerInterface scanner = table.getScanner(hashKey, null, null, options); + Assert.assertNotNull(scanner); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + Assert.assertArrayEquals(hashKey, item.getLeft().getLeft()); + Assert.assertArrayEquals("a".getBytes(), item.getRight()); + data.put(new String(item.getLeft().getRight()), new String(item.getRight())); + } + Assert.assertEquals(5, data.size()); + Assert.assertTrue(data.containsKey("m_1")); + Assert.assertTrue(data.containsKey("m_2")); + Assert.assertTrue(data.containsKey("m_3")); + Assert.assertTrue(data.containsKey("m_4")); + Assert.assertTrue(data.containsKey("m_5")); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } - Long sortKeyCount = tb.sortKeyCount(asyncHashPrefix.getBytes(), 0); - Assert.assertEquals(0, (long)sortKeyCount); + try { + // multi del + List sortKeys = new ArrayList(); + for (int i = 0; i < values.size(); i++) { + sortKeys.add(values.get(i).getKey()); + } + table.multiDel(hashKey, sortKeys, 0); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } - } catch (Throwable e) { - e.printStackTrace(); - Assert.fail(); - } + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void fullScanWithFilter() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + PegasusTableInterface table = client.openTable("temp"); + byte[] hashKey = "x".getBytes(); + List> values = new ArrayList>(); + values.add(Pair.of("m_1".getBytes(), "a".getBytes())); + values.add(Pair.of("m_2".getBytes(), "a".getBytes())); + values.add(Pair.of("m_3".getBytes(), "a".getBytes())); + values.add(Pair.of("m_4".getBytes(), "a".getBytes())); + values.add(Pair.of("m_5".getBytes(), "a".getBytes())); + values.add(Pair.of("n_1".getBytes(), "b".getBytes())); + values.add(Pair.of("n_2".getBytes(), "b".getBytes())); + values.add(Pair.of("n_3".getBytes(), "b".getBytes())); + + try { + // multi set + table.multiSet(hashKey, values, 0); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void asyncApiSample() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - PegasusTableInterface table = client.openTable("temp"); - - String key = "hello"; - String value = "world"; - - Future f = table.asyncSet(null, null, null, 0); - f.addListener( - new PegasusTableInterface.SetListener() { - @Override - public void operationComplete(Future future) throws Exception { - if (future.isSuccess()) { - System.out.println("set succeed"); - Assert.fail(); - } - else { - assert future.cause() instanceof PException; - } - } - } - ); - f.awaitUninterruptibly(); + try { + // scan with batch_size = 10 + ScanOptions options = new ScanOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "m".getBytes(); + options.batchSize = 10; + Map data = new HashMap(); + List scanners = table.getUnorderedScanners(1, options); + Assert.assertEquals(1, scanners.size()); + PegasusScannerInterface scanner = scanners.get(0); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + Assert.assertArrayEquals(hashKey, item.getLeft().getLeft()); + Assert.assertArrayEquals("a".getBytes(), item.getRight()); + data.put(new String(item.getLeft().getRight()), new String(item.getRight())); + } + Assert.assertEquals(5, data.size()); + Assert.assertTrue(data.containsKey("m_1")); + Assert.assertTrue(data.containsKey("m_2")); + Assert.assertTrue(data.containsKey("m_3")); + Assert.assertTrue(data.containsKey("m_4")); + Assert.assertTrue(data.containsKey("m_5")); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void scanWithFilter() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - PegasusTableInterface table = client.openTable("temp"); - byte[] hashKey = "x".getBytes(); - List> values = new ArrayList>(); - values.add(Pair.of("m_1".getBytes(), "a".getBytes())); - values.add(Pair.of("m_2".getBytes(), "a".getBytes())); - values.add(Pair.of("m_3".getBytes(), "a".getBytes())); - values.add(Pair.of("m_4".getBytes(), "a".getBytes())); - values.add(Pair.of("m_5".getBytes(), "a".getBytes())); - values.add(Pair.of("n_1".getBytes(), "b".getBytes())); - values.add(Pair.of("n_2".getBytes(), "b".getBytes())); - values.add(Pair.of("n_3".getBytes(), "b".getBytes())); - - try { - // multi set - table.multiSet(hashKey, values, 0); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - // scan with batch_size = 10 - ScanOptions options = new ScanOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "m".getBytes(); - options.batchSize = 10; - Map data = new HashMap(); - PegasusScannerInterface scanner = table.getScanner(hashKey, null, null, options); - Assert.assertNotNull(scanner); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - Assert.assertArrayEquals(hashKey, item.getLeft().getLeft()); - Assert.assertArrayEquals("a".getBytes(), item.getRight()); - data.put(new String(item.getLeft().getRight()), new String(item.getRight())); - } - Assert.assertEquals(5, data.size()); - Assert.assertTrue(data.containsKey("m_1")); - Assert.assertTrue(data.containsKey("m_2")); - Assert.assertTrue(data.containsKey("m_3")); - Assert.assertTrue(data.containsKey("m_4")); - Assert.assertTrue(data.containsKey("m_5")); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - // scan with batch_size = 3 - ScanOptions options = new ScanOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "m".getBytes(); - options.batchSize = 3; - Map data = new HashMap(); - PegasusScannerInterface scanner = table.getScanner(hashKey, null, null, options); - Assert.assertNotNull(scanner); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - Assert.assertArrayEquals(hashKey, item.getLeft().getLeft()); - Assert.assertArrayEquals("a".getBytes(), item.getRight()); - data.put(new String(item.getLeft().getRight()), new String(item.getRight())); - } - Assert.assertEquals(5, data.size()); - Assert.assertTrue(data.containsKey("m_1")); - Assert.assertTrue(data.containsKey("m_2")); - Assert.assertTrue(data.containsKey("m_3")); - Assert.assertTrue(data.containsKey("m_4")); - Assert.assertTrue(data.containsKey("m_5")); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - // multi del - List sortKeys = new ArrayList(); - for (int i = 0; i < values.size(); i++) { - sortKeys.add(values.get(i).getKey()); - } - table.multiDel(hashKey, sortKeys, 0); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + try { + // scan with batch_size = 3 + ScanOptions options = new ScanOptions(); + options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.sortKeyFilterPattern = "m".getBytes(); + options.batchSize = 3; + Map data = new HashMap(); + List scanners = table.getUnorderedScanners(1, options); + Assert.assertEquals(1, scanners.size()); + PegasusScannerInterface scanner = scanners.get(0); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + Assert.assertArrayEquals(hashKey, item.getLeft().getLeft()); + Assert.assertArrayEquals("a".getBytes(), item.getRight()); + data.put(new String(item.getLeft().getRight()), new String(item.getRight())); + } + Assert.assertEquals(5, data.size()); + Assert.assertTrue(data.containsKey("m_1")); + Assert.assertTrue(data.containsKey("m_2")); + Assert.assertTrue(data.containsKey("m_3")); + Assert.assertTrue(data.containsKey("m_4")); + Assert.assertTrue(data.containsKey("m_5")); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void fullScanWithFilter() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - PegasusTableInterface table = client.openTable("temp"); - byte[] hashKey = "x".getBytes(); - List> values = new ArrayList>(); - values.add(Pair.of("m_1".getBytes(), "a".getBytes())); - values.add(Pair.of("m_2".getBytes(), "a".getBytes())); - values.add(Pair.of("m_3".getBytes(), "a".getBytes())); - values.add(Pair.of("m_4".getBytes(), "a".getBytes())); - values.add(Pair.of("m_5".getBytes(), "a".getBytes())); - values.add(Pair.of("n_1".getBytes(), "b".getBytes())); - values.add(Pair.of("n_2".getBytes(), "b".getBytes())); - values.add(Pair.of("n_3".getBytes(), "b".getBytes())); - - try { - // multi set - table.multiSet(hashKey, values, 0); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - // scan with batch_size = 10 - ScanOptions options = new ScanOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "m".getBytes(); - options.batchSize = 10; - Map data = new HashMap(); - List scanners = table.getUnorderedScanners(1, options); - Assert.assertEquals(1, scanners.size()); - PegasusScannerInterface scanner = scanners.get(0); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - Assert.assertArrayEquals(hashKey, item.getLeft().getLeft()); - Assert.assertArrayEquals("a".getBytes(), item.getRight()); - data.put(new String(item.getLeft().getRight()), new String(item.getRight())); - } - Assert.assertEquals(5, data.size()); - Assert.assertTrue(data.containsKey("m_1")); - Assert.assertTrue(data.containsKey("m_2")); - Assert.assertTrue(data.containsKey("m_3")); - Assert.assertTrue(data.containsKey("m_4")); - Assert.assertTrue(data.containsKey("m_5")); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - // scan with batch_size = 3 - ScanOptions options = new ScanOptions(); - options.sortKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.sortKeyFilterPattern = "m".getBytes(); - options.batchSize = 3; - Map data = new HashMap(); - List scanners = table.getUnorderedScanners(1, options); - Assert.assertEquals(1, scanners.size()); - PegasusScannerInterface scanner = scanners.get(0); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - Assert.assertArrayEquals(hashKey, item.getLeft().getLeft()); - Assert.assertArrayEquals("a".getBytes(), item.getRight()); - data.put(new String(item.getLeft().getRight()), new String(item.getRight())); - } - Assert.assertEquals(5, data.size()); - Assert.assertTrue(data.containsKey("m_1")); - Assert.assertTrue(data.containsKey("m_2")); - Assert.assertTrue(data.containsKey("m_3")); - Assert.assertTrue(data.containsKey("m_4")); - Assert.assertTrue(data.containsKey("m_5")); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - // multi del - List sortKeys = new ArrayList(); - for (int i = 0; i < values.size(); i++) { - sortKeys.add(values.get(i).getKey()); - } - table.multiDel(hashKey, sortKeys, 0); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + try { + // multi del + List sortKeys = new ArrayList(); + for (int i = 0; i < values.size(); i++) { + sortKeys.add(values.get(i).getKey()); + } + table.multiDel(hashKey, sortKeys, 0); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } + + PegasusClientFactory.closeSingletonClient(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestBench.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestBench.java index 1871c121..872fa5cd 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestBench.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestBench.java @@ -3,157 +3,189 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import org.apache.commons.lang3.tuple.Pair; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * Created by mi on 16-3-23. - */ +/** Created by mi on 16-3-23. */ public class TestBench { - private static void clearDatabase() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; + private static void clearDatabase() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; - ScanOptions options = new ScanOptions(); - List scanners = client.getUnorderedScanners(tableName, 1, options); - Assert.assertEquals(1, scanners.size()); - Assert.assertNotNull(scanners.get(0)); + ScanOptions options = new ScanOptions(); + List scanners = client.getUnorderedScanners(tableName, 1, options); + Assert.assertEquals(1, scanners.size()); + Assert.assertNotNull(scanners.get(0)); - Pair, byte[]> item; - while((item = scanners.get(0).next()) != null ) { - client.del(tableName, item.getLeft().getLeft(), item.getLeft().getRight()); - } - scanners.get(0).close(); + Pair, byte[]> item; + while ((item = scanners.get(0).next()) != null) { + client.del(tableName, item.getLeft().getLeft(), item.getLeft().getRight()); + } + scanners.get(0).close(); - scanners = client.getUnorderedScanners(tableName, 1, options); - Assert.assertEquals(1, scanners.size()); - Assert.assertNotNull(scanners.get(0)); - item = scanners.get(0).next(); - scanners.get(0).close(); - Assert.assertNull( - item == null ? null : String.format("Database is cleared but not empty, hashKey=%s, sortKey=%s", - new String(item.getLeft().getLeft()), new String(item.getLeft().getRight())), - item); + scanners = client.getUnorderedScanners(tableName, 1, options); + Assert.assertEquals(1, scanners.size()); + Assert.assertNotNull(scanners.get(0)); + item = scanners.get(0).next(); + scanners.get(0).close(); + Assert.assertNull( + item == null + ? null + : String.format( + "Database is cleared but not empty, hashKey=%s, sortKey=%s", + new String(item.getLeft().getLeft()), new String(item.getLeft().getRight())), + item); - PegasusClientFactory.closeSingletonClient(); - } + PegasusClientFactory.closeSingletonClient(); + } - @AfterClass - public static void tearDownTestCase() throws PException { - clearDatabase(); - } + @AfterClass + public static void tearDownTestCase() throws PException { + clearDatabase(); + } - @Test - public void testBench() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; + @Test + public void testBench() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; - System.out.println("start to run single-thread test"); + System.out.println("start to run single-thread test"); - int total_count = 10000; - ArrayList keys = new ArrayList(); - ArrayList values = new ArrayList(); - for (int i = 0; i < total_count; i++) { - String key = "testBench-" + String.format("%06d", i); - String value = key + "-"; - StringBuilder sb = new StringBuilder(); - sb.append(key); - sb.append("-"); - while (sb.length() < 100) { - sb.append('0'); - } - keys.add(key.getBytes()); - values.add(value.getBytes()); - } + int total_count = 10000; + ArrayList keys = new ArrayList(); + ArrayList values = new ArrayList(); + for (int i = 0; i < total_count; i++) { + String key = "testBench-" + String.format("%06d", i); + String value = key + "-"; + StringBuilder sb = new StringBuilder(); + sb.append(key); + sb.append("-"); + while (sb.length() < 100) { + sb.append('0'); + } + keys.add(key.getBytes()); + values.add(value.getBytes()); + } - { - long min_time = Long.MAX_VALUE; - long max_time = 0; - long start_time = System.nanoTime(); - long last_time = start_time; - for (int i = 0; i < total_count; i++) { - long begin_time = System.nanoTime(); - client.set(tableName, keys.get(i), null, values.get(i), 0); - long end_time = System.nanoTime(); - long dur_time = end_time - begin_time; - if (dur_time < min_time) { - min_time = dur_time; - } - if (dur_time > max_time) { - max_time = dur_time; - } - if ((i + 1) % 1000 == 0) { - long cur_time = System.nanoTime(); - long last_dur_time = cur_time - last_time; - String last_dur_time_str = String.format("%d.%06d", last_dur_time / 1000000000, last_dur_time % 1000000000 / 1000); - long total_dur_time = cur_time - start_time; - String total_dur_time_str = String.format("%d.%06d", total_dur_time / 1000000000, total_dur_time % 1000000000 / 1000); - double last_qps = (double) 1000 / last_dur_time * 1000000000; - double total_qps = (double) (i + 1) / total_dur_time * 1000000000; - String last_qps_str = String.format("%.1f", last_qps); - String total_qps_str = String.format("%.1f", total_qps); - System.out.println("testBench: (1000," + (i + 1) + ") ops and (" + last_qps_str + "," + total_qps_str + ") ops/second in (" + last_dur_time_str + "," + total_dur_time_str + ") seconds"); - last_time = cur_time; - } - } - double avg_time = (double)(last_time - start_time) / 100000 / 1000; - String avg_time_str = String.format("%.4f", avg_time); - double total_qps = (double) 100000 / (last_time - start_time) * 1000000000; - String total_qps_str = String.format("%.1f", total_qps); - System.out.println("fillseq : " + avg_time_str + " micros/op " + total_qps_str + " ops/sec"); - System.out.println("Microseconds per op:"); - System.out.println("Count: 100000 Average: " + avg_time_str); - System.out.println("Min: " + (min_time / 1000) + ".0000 Max:" + (max_time / 1000) + ".0000"); + { + long min_time = Long.MAX_VALUE; + long max_time = 0; + long start_time = System.nanoTime(); + long last_time = start_time; + for (int i = 0; i < total_count; i++) { + long begin_time = System.nanoTime(); + client.set(tableName, keys.get(i), null, values.get(i), 0); + long end_time = System.nanoTime(); + long dur_time = end_time - begin_time; + if (dur_time < min_time) { + min_time = dur_time; } - - { - long min_time = Long.MAX_VALUE; - long max_time = 0; - long start_time = System.nanoTime(); - long last_time = start_time; - for (int i = 0; i < total_count; i++) { - long begin_time = System.nanoTime(); - byte[] value = client.get(tableName, keys.get(i), null); - long end_time = System.nanoTime(); - Assert.assertTrue(value != null); - Assert.assertTrue(Arrays.equals(values.get(i), value)); - long dur_time = end_time - begin_time; - if (dur_time < min_time) { - min_time = dur_time; - } - if (dur_time > max_time) { - max_time = dur_time; - } - if ((i + 1) % 1000 == 0) { - long cur_time = System.nanoTime(); - long last_dur_time = cur_time - last_time; - String last_dur_time_str = String.format("%d.%06d", last_dur_time / 1000000000, last_dur_time % 1000000000 / 1000); - long total_dur_time = cur_time - start_time; - String total_dur_time_str = String.format("%d.%06d", total_dur_time / 1000000000, total_dur_time % 1000000000 / 1000); - double last_qps = (double) 1000 / last_dur_time * 1000000000; - double total_qps = (double) (i + 1) / total_dur_time * 1000000000; - String last_qps_str = String.format("%.1f", last_qps); - String total_qps_str = String.format("%.1f", total_qps); - System.out.println("testBench: (1000," + (i + 1) + ") ops and (" + last_qps_str + "," + total_qps_str + ") ops/second in (" + last_dur_time_str + "," + total_dur_time_str + ") seconds"); - last_time = cur_time; - } - } - double avg_time = (double)(last_time - start_time) / 100000 / 1000; - String avg_time_str = String.format("%.4f", avg_time); - double total_qps = (double) 100000 / (last_time - start_time) * 1000000000; - String total_qps_str = String.format("%.1f", total_qps); - System.out.println("readrandom : " + avg_time_str + " micros/op " + total_qps_str + " ops/sec"); - System.out.println("Microseconds per op:"); - System.out.println("Count: 100000 Average: " + avg_time_str); - System.out.println("Min: " + (min_time / 1000) + ".0000 Max:" + (max_time / 1000) + ".0000"); + if (dur_time > max_time) { + max_time = dur_time; + } + if ((i + 1) % 1000 == 0) { + long cur_time = System.nanoTime(); + long last_dur_time = cur_time - last_time; + String last_dur_time_str = + String.format( + "%d.%06d", last_dur_time / 1000000000, last_dur_time % 1000000000 / 1000); + long total_dur_time = cur_time - start_time; + String total_dur_time_str = + String.format( + "%d.%06d", total_dur_time / 1000000000, total_dur_time % 1000000000 / 1000); + double last_qps = (double) 1000 / last_dur_time * 1000000000; + double total_qps = (double) (i + 1) / total_dur_time * 1000000000; + String last_qps_str = String.format("%.1f", last_qps); + String total_qps_str = String.format("%.1f", total_qps); + System.out.println( + "testBench: (1000," + + (i + 1) + + ") ops and (" + + last_qps_str + + "," + + total_qps_str + + ") ops/second in (" + + last_dur_time_str + + "," + + total_dur_time_str + + ") seconds"); + last_time = cur_time; } + } + double avg_time = (double) (last_time - start_time) / 100000 / 1000; + String avg_time_str = String.format("%.4f", avg_time); + double total_qps = (double) 100000 / (last_time - start_time) * 1000000000; + String total_qps_str = String.format("%.1f", total_qps); + System.out.println( + "fillseq : " + avg_time_str + " micros/op " + total_qps_str + " ops/sec"); + System.out.println("Microseconds per op:"); + System.out.println("Count: 100000 Average: " + avg_time_str); + System.out.println("Min: " + (min_time / 1000) + ".0000 Max:" + (max_time / 1000) + ".0000"); + } - PegasusClientFactory.closeSingletonClient(); + { + long min_time = Long.MAX_VALUE; + long max_time = 0; + long start_time = System.nanoTime(); + long last_time = start_time; + for (int i = 0; i < total_count; i++) { + long begin_time = System.nanoTime(); + byte[] value = client.get(tableName, keys.get(i), null); + long end_time = System.nanoTime(); + Assert.assertTrue(value != null); + Assert.assertTrue(Arrays.equals(values.get(i), value)); + long dur_time = end_time - begin_time; + if (dur_time < min_time) { + min_time = dur_time; + } + if (dur_time > max_time) { + max_time = dur_time; + } + if ((i + 1) % 1000 == 0) { + long cur_time = System.nanoTime(); + long last_dur_time = cur_time - last_time; + String last_dur_time_str = + String.format( + "%d.%06d", last_dur_time / 1000000000, last_dur_time % 1000000000 / 1000); + long total_dur_time = cur_time - start_time; + String total_dur_time_str = + String.format( + "%d.%06d", total_dur_time / 1000000000, total_dur_time % 1000000000 / 1000); + double last_qps = (double) 1000 / last_dur_time * 1000000000; + double total_qps = (double) (i + 1) / total_dur_time * 1000000000; + String last_qps_str = String.format("%.1f", last_qps); + String total_qps_str = String.format("%.1f", total_qps); + System.out.println( + "testBench: (1000," + + (i + 1) + + ") ops and (" + + last_qps_str + + "," + + total_qps_str + + ") ops/second in (" + + last_dur_time_str + + "," + + total_dur_time_str + + ") seconds"); + last_time = cur_time; + } + } + double avg_time = (double) (last_time - start_time) / 100000 / 1000; + String avg_time_str = String.format("%.4f", avg_time); + double total_qps = (double) 100000 / (last_time - start_time) * 1000000000; + String total_qps_str = String.format("%.1f", total_qps); + System.out.println( + "readrandom : " + avg_time_str + " micros/op " + total_qps_str + " ops/sec"); + System.out.println("Microseconds per op:"); + System.out.println("Count: 100000 Average: " + avg_time_str); + System.out.println("Min: " + (min_time / 1000) + ".0000 Max:" + (max_time / 1000) + ".0000"); } + + PegasusClientFactory.closeSingletonClient(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestCheckAndMutate.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestCheckAndMutate.java index 3629754d..12ac98e2 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestCheckAndMutate.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestCheckAndMutate.java @@ -3,1386 +3,1840 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; -/** - * @author huangwei - */ - +/** @author huangwei */ import org.junit.Assert; import org.junit.Test; public class TestCheckAndMutate { - @Test - public void testValueNotExist() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_value_not_exist".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - - // add set operations snowballed, in order to test the ability to handle multi mutations - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = false; - mutations.set("k1".getBytes(), "v1".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertFalse(result.checkValueReturned); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.del(tableName, hashKey, "k2".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k2".getBytes(), "".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k2".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k2".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k2".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k2".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k2".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k4".getBytes(), "v4".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + @Test + public void testValueNotExist() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_value_not_exist".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + + // add set operations snowballed, in order to test the ability to handle multi mutations + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = false; + mutations.set("k1".getBytes(), "v1".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertFalse(result.checkValueReturned); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + + try { + client.del(tableName, hashKey, "k2".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k2".getBytes(), "".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k2".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k2".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k2".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k2".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k2".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + + try { + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k4".getBytes(), "v4".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testValueExist() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_value_exist".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_EXIST, + null, + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + + try { + client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k4".getBytes(), "v4".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testValueNotEmpty() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_value_not_empty".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EMPTY, + null, + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EMPTY, + null, + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + client.set(tableName, hashKey, "k1".getBytes(), "v1".getBytes()); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EMPTY, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testValueExist() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_value_exist".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_EXIST, null, - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k4".getBytes(), "v4".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k4".getBytes(), "v4".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_NOT_EMPTY, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testValueNotEmpty() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_value_not_empty".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EMPTY, null, - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EMPTY, null, - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - client.set(tableName, hashKey, "k1".getBytes(), "v1".getBytes()); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EMPTY, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k4".getBytes(), "v4".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_NOT_EMPTY, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMatchAnywhere() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_value_match_anywhere".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "v".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "v".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v111v".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "111".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v3".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "y".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "v2v".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "v2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v3".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testMatchAnywhere() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_value_match_anywhere".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "v".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "v".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v111v".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "111".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v3".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "y".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "v2v".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "v2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v3".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k4".getBytes(), "v4".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "333".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k4".getBytes(), "v4".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "333".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testMatchPrefix() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_value_match_prefix".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v111v".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "111".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v111".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v3".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "y".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v2v".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v3".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - mutations.set("k4".getBytes(), "v4".getBytes(), 0); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v333".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMatchPrefix() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_value_match_prefix".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v111v".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "111".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v111".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v3".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "y".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v2v".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v3".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testMatchPostfix() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_value_match_postfix".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "v".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "v".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v111v".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "111".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "111v".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v3".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "y".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "2v2".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "v2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v3".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k4".getBytes(), "v4".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "333v".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + mutations.set("k4".getBytes(), "v4".getBytes(), 0); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v333".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testBytesCompare() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_bytes_compare".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "v1".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k4".getBytes(), "v4".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "v3".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k5".getBytes(), "v1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - // v1 < v2 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "v2".getBytes(), 0); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_LESS, "v2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - // v2 <= v2 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "v3".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_LESS_OR_EQUAL, "v2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v3".getBytes(), value); - - // v3 <= v4 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "v4".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_LESS_OR_EQUAL, "v4".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - // v4 >= v4 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "v5".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_GREATER_OR_EQUAL, "v4".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v4".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v5".getBytes(), value); - - // v5 >= v4 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "v6".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_GREATER_OR_EQUAL, "v4".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v5".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v6".getBytes(), value); - - // v6 > v5 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "v7".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_GREATER, "v5".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v6".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v7".getBytes(), value); - - client.del(tableName, hashKey, "k5".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMatchPostfix() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_value_match_postfix".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "v".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "v".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v111v".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "111".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "111v".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v3".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "y".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "2v2".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "v2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v3".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testIntCompare() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_int_compare".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - mutations.set("k1".getBytes(), "v1".getBytes(), 0); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "1".getBytes(), - mutations, options); - Assert.assertFalse(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - try { - client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "1".getBytes(), - mutations, options); - Assert.fail(); - } catch (PException ex) { - Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); - - } catch (Exception ex) { - Assert.fail(); - } - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - client.set(tableName, hashKey, "k1".getBytes(), "1".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "1".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "3".getBytes()); - try { - client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "".getBytes(), - mutations, options); - Assert.fail(); - } catch (PException ex) { - Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); - - } catch (Exception ex) { - Assert.fail(); - } - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - options.returnCheckValue = true; - try { - client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "v1".getBytes(), - mutations, options); - Assert.fail(); - } catch (PException ex) { - Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); - - } catch (Exception ex) { - Assert.fail(); - } - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - options.returnCheckValue = true; - - try { - client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, - "88888888888888888888888888888888888888888888888".getBytes(), - mutations, options); - Assert.fail(); - } catch (PException ex) { - Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); - - } catch (Exception ex) { - Assert.fail(); - } - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - client.set(tableName, hashKey, "k1".getBytes(), "0".getBytes()); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "-1".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "0".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("0".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("-1".getBytes(), value); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "-2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "-1".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("-1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("-2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - mutations.set("k4".getBytes(), "".getBytes(), 0); - - options.returnCheckValue = true; - mutations.set("k4".getBytes(), "4".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "3".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - try { - client.set(tableName, hashKey, "k5".getBytes(), "1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - // 1 < 2 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "2".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_LESS, "2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - // 2 <= 2 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "3".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_LESS_OR_EQUAL, "2".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("3".getBytes(), value); - - // 3 <= 4 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "4".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_LESS_OR_EQUAL, "4".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("4".getBytes(), value); - - // 4 >= 4 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "5".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_GREATER_OR_EQUAL, "4".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("4".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("5".getBytes(), value); - - // 5 >= 4 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "6".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_GREATER_OR_EQUAL, "4".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("5".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("6".getBytes(), value); - - // 6 > 5 - options.returnCheckValue = true; - mutations.set("k5".getBytes(), "7".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_GREATER, "5".getBytes(), - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("6".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("7".getBytes(), value); - - client.del(tableName, hashKey, "k5".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k4".getBytes(), "v4".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "333v".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testSetDel() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_set_del".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 10); - mutations.del("k1".getBytes()); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testBytesCompare() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_bytes_compare".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "v1".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testMultiGetMutations() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_multi_get_mutations".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 10); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - Thread.sleep(12000); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - // use the same mutations(the second time to call getMutations()) - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } catch (InterruptedException e) { - e.printStackTrace(); - Assert.fail(); - } + try { + client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k4".getBytes(), "v4".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "v3".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } - @Test - public void testMutationsExpireSeconds() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_mutate_java_test_mutations_expire_seconds".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndMutateOptions options = new CheckAndMutateOptions(); - PegasusTableInterface.CheckAndMutateResult result; - byte[] value; - Mutations mutations = new Mutations(); - - options.returnCheckValue = true; - mutations.set("k1".getBytes(), "v1".getBytes(), 10); - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - Thread.sleep(12000); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - mutations = new Mutations(); - options.returnCheckValue = true; - - // k1's expireSeconds will be calculate in checkAndMutate(), - // so it doesn't matter how long the mutations loading is going to take - mutations.set("k1".getBytes(), "v1".getBytes(), 10); - Thread.sleep(12000); - mutations.set("k2".getBytes(), "v2".getBytes(), 10); - - result = client.checkAndMutate(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - mutations, options); - Assert.assertTrue(result.mutateSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.fail(); - } catch (InterruptedException e) { - e.printStackTrace(); - Assert.fail(); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k5".getBytes(), "v1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + // v1 < v2 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "v2".getBytes(), 0); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_LESS, + "v2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + // v2 <= v2 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "v3".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_LESS_OR_EQUAL, + "v2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v3".getBytes(), value); + + // v3 <= v4 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "v4".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_LESS_OR_EQUAL, + "v4".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + // v4 >= v4 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "v5".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_GREATER_OR_EQUAL, + "v4".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v4".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v5".getBytes(), value); + + // v5 >= v4 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "v6".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_GREATER_OR_EQUAL, + "v4".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v5".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v6".getBytes(), value); + + // v6 > v5 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "v7".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_GREATER, + "v5".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v6".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v7".getBytes(), value); + + client.del(tableName, hashKey, "k5".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testIntCompare() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_int_compare".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + mutations.set("k1".getBytes(), "v1".getBytes(), 0); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "1".getBytes(), + mutations, + options); + Assert.assertFalse(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + try { + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "1".getBytes(), + mutations, + options); + Assert.fail(); + } catch (PException ex) { + Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); + + } catch (Exception ex) { + Assert.fail(); + } + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + client.set(tableName, hashKey, "k1".getBytes(), "1".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "1".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "3".getBytes()); + try { + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "".getBytes(), + mutations, + options); + Assert.fail(); + } catch (PException ex) { + Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); + + } catch (Exception ex) { + Assert.fail(); + } + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + options.returnCheckValue = true; + try { + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "v1".getBytes(), + mutations, + options); + Assert.fail(); + } catch (PException ex) { + Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); + + } catch (Exception ex) { + Assert.fail(); + } + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + options.returnCheckValue = true; + + try { + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "88888888888888888888888888888888888888888888888".getBytes(), + mutations, + options); + Assert.fail(); + } catch (PException ex) { + Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); + + } catch (Exception ex) { + Assert.fail(); + } + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + client.set(tableName, hashKey, "k1".getBytes(), "0".getBytes()); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "-1".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "0".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("0".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("-1".getBytes(), value); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "-2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "-1".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("-1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("-2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + + try { + client.set(tableName, hashKey, "k3".getBytes(), "3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + mutations.set("k4".getBytes(), "".getBytes(), 0); + + options.returnCheckValue = true; + mutations.set("k4".getBytes(), "4".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "3".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + + try { + client.set(tableName, hashKey, "k5".getBytes(), "1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + // 1 < 2 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "2".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_LESS, + "2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + // 2 <= 2 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "3".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_LESS_OR_EQUAL, + "2".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("3".getBytes(), value); + + // 3 <= 4 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "4".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_LESS_OR_EQUAL, + "4".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("4".getBytes(), value); + + // 4 >= 4 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "5".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_GREATER_OR_EQUAL, + "4".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("4".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("5".getBytes(), value); + + // 5 >= 4 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "6".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_GREATER_OR_EQUAL, + "4".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("5".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("6".getBytes(), value); + + // 6 > 5 + options.returnCheckValue = true; + mutations.set("k5".getBytes(), "7".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_GREATER, + "5".getBytes(), + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("6".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("7".getBytes(), value); + + client.del(tableName, hashKey, "k5".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testSetDel() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_set_del".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 10); + mutations.del("k1".getBytes()); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } + } + + @Test + public void testMultiGetMutations() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_multi_get_mutations".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 10); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + Thread.sleep(12000); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + // use the same mutations(the second time to call getMutations()) + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } catch (InterruptedException e) { + e.printStackTrace(); + Assert.fail(); + } + } + + @Test + public void testMutationsExpireSeconds() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_mutate_java_test_mutations_expire_seconds".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndMutateOptions options = new CheckAndMutateOptions(); + PegasusTableInterface.CheckAndMutateResult result; + byte[] value; + Mutations mutations = new Mutations(); + + options.returnCheckValue = true; + mutations.set("k1".getBytes(), "v1".getBytes(), 10); + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + Thread.sleep(12000); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + mutations = new Mutations(); + options.returnCheckValue = true; + + // k1's expireSeconds will be calculate in checkAndMutate(), + // so it doesn't matter how long the mutations loading is going to take + mutations.set("k1".getBytes(), "v1".getBytes(), 10); + Thread.sleep(12000); + mutations.set("k2".getBytes(), "v2".getBytes(), 10); + + result = + client.checkAndMutate( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + mutations, + options); + Assert.assertTrue(result.mutateSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.fail(); + } catch (InterruptedException e) { + e.printStackTrace(); + Assert.fail(); + } + + PegasusClientFactory.closeSingletonClient(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestCheckAndSet.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestCheckAndSet.java index d426a689..bd5521cd 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestCheckAndSet.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestCheckAndSet.java @@ -3,1225 +3,1727 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; -/** - * @author qinzuoyan - */ - +/** @author qinzuoyan */ import org.junit.Assert; import org.junit.Test; -/** - * Created by mi on 18-7-17. - */ +/** Created by mi on 18-7-17. */ public class TestCheckAndSet { - @Test - public void testValueNotExist() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_not_exist".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = false; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertFalse(result.checkValueReturned); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.del(tableName, hashKey, "k2".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k2".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - "k2".getBytes(), "".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k2".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k2".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - "k2".getBytes(), "v2".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k2".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_NOT_EXIST, null, - "k4".getBytes(), "v4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + @Test + public void testValueNotExist() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_not_exist".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = false; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertFalse(result.checkValueReturned); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + try { + client.del(tableName, hashKey, "k2".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k2".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + "k2".getBytes(), + "".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k2".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k2".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + "k2".getBytes(), + "v2".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k2".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + try { + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_NOT_EXIST, + null, + "k4".getBytes(), + "v4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testValueExist() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_exist".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_EXIST, + null, + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_EXIST, + null, + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_EXIST, + null, + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + try { + client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_EXIST, + null, + "k4".getBytes(), + "v4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testValueNotEmpty() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_not_empty".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EMPTY, + null, + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EMPTY, + null, + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + client.set(tableName, hashKey, "k1".getBytes(), "v1".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_NOT_EMPTY, + null, + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + try { + client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_NOT_EMPTY, + null, + "k4".getBytes(), + "v4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMatchAnywhere() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_match_anywhere".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "v".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "v".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "2".getBytes(), + "k1".getBytes(), + "v111v".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "111".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "y".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "v2v".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "v2".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v3".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + try { + client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_MATCH_ANYWHERE, + "333".getBytes(), + "k4".getBytes(), + "v4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMatchPrefix() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_match_prefix".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v".getBytes(), + "k1".getBytes(), + "v111v".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "111".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v111".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "y".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v2v".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v2".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v3".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + try { + client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_MATCH_PREFIX, + "v333".getBytes(), + "k4".getBytes(), + "v4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testValueExist() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_exist".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_EXIST, null, - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_EXIST, null, - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_EXIST, null, - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_EXIST, null, - "k4".getBytes(), "v4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testMatchPostfix() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_match_postfix".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "v".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "v".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "2".getBytes(), + "k1".getBytes(), + "v111v".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "111".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v111v".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "111v".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "y".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "2v2".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "v2".getBytes(), + "k1".getBytes(), + "v3".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v3".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testValueNotEmpty() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_not_empty".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EMPTY, null, - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EMPTY, null, - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - client.set(tableName, hashKey, "k1".getBytes(), "v1".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_NOT_EMPTY, null, - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_NOT_EMPTY, null, - "k4".getBytes(), "v4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_MATCH_POSTFIX, + "333v".getBytes(), + "k4".getBytes(), + "v4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testMatchAnywhere() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_match_anywhere".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "v".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "v".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "2".getBytes(), - "k1".getBytes(), "v111v".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "111".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "y".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "v2v".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "v2".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v3".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_MATCH_ANYWHERE, "333".getBytes(), - "k4".getBytes(), "v4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testBytesCompare() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_bytes_compare".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "".getBytes(), + "k1".getBytes(), + "v1".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "v1".getBytes(), + "k1".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testMatchPrefix() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_match_prefix".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v".getBytes(), - "k1".getBytes(), "v111v".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "111".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v111".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "y".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v2v".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v2".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v3".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_MATCH_PREFIX, "v333".getBytes(), - "k4".getBytes(), "v4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_BYTES_EQUAL, + "v3".getBytes(), + "k4".getBytes(), + "v4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testMatchPostfix() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_match_postfix".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "v".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "v".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "2".getBytes(), - "k1".getBytes(), "v111v".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "111".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v111v".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "111v".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v111v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "y".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "2v2".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "v2".getBytes(), - "k1".getBytes(), "v3".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v3".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v333v".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_MATCH_POSTFIX, "333v".getBytes(), - "k4".getBytes(), "v4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v333v".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k5".getBytes(), "v1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + // v1 < v2 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_LESS, + "v2".getBytes(), + "k5".getBytes(), + "v2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + // v2 <= v2 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_LESS_OR_EQUAL, + "v2".getBytes(), + "k5".getBytes(), + "v3".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v3".getBytes(), value); + + // v3 <= v4 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_LESS_OR_EQUAL, + "v4".getBytes(), + "k5".getBytes(), + "v4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v4".getBytes(), value); + + // v4 >= v4 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_GREATER_OR_EQUAL, + "v4".getBytes(), + "k5".getBytes(), + "v5".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v4".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v5".getBytes(), value); + + // v5 >= v4 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_GREATER_OR_EQUAL, + "v4".getBytes(), + "k5".getBytes(), + "v6".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v5".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v6".getBytes(), value); + + // v6 > v5 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_BYTES_GREATER, + "v5".getBytes(), + "k5".getBytes(), + "v7".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("v6".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("v7".getBytes(), value); + + client.del(tableName, hashKey, "k5".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testBytesCompare() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_bytes_compare".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "".getBytes(), - "k1".getBytes(), "v1".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "v1".getBytes(), - "k1".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "v3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_BYTES_EQUAL, "v3".getBytes(), - "k4".getBytes(), "v4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k5".getBytes(), "v1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - // v1 < v2 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_LESS, "v2".getBytes(), - "k5".getBytes(), "v2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - // v2 <= v2 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_LESS_OR_EQUAL, "v2".getBytes(), - "k5".getBytes(), "v3".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v3".getBytes(), value); - - // v3 <= v4 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_LESS_OR_EQUAL, "v4".getBytes(), - "k5".getBytes(), "v4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v4".getBytes(), value); - - // v4 >= v4 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_GREATER_OR_EQUAL, "v4".getBytes(), - "k5".getBytes(), "v5".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v4".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v5".getBytes(), value); - - // v5 >= v4 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_GREATER_OR_EQUAL, "v4".getBytes(), - "k5".getBytes(), "v6".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v5".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v6".getBytes(), value); - - // v6 > v5 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_BYTES_GREATER, "v5".getBytes(), - "k5".getBytes(), "v7".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("v6".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("v7".getBytes(), value); - - client.del(tableName, hashKey, "k5".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testIntCompare() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_value_int_compare".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "1".getBytes(), + "k1".getBytes(), + "2".getBytes(), + options); + Assert.assertFalse(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertFalse(result.checkValueExist); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + options.returnCheckValue = true; + try { + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "1".getBytes(), + "k1".getBytes(), + "2".getBytes(), + options); + Assert.assertTrue(false); + } catch (PException ex) { + Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); + + } catch (Exception ex) { + Assert.assertTrue(false); + } + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("".getBytes(), value); + + client.set(tableName, hashKey, "k1".getBytes(), "1".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "1".getBytes(), + "k1".getBytes(), + "2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + options.returnCheckValue = true; + try { + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "".getBytes(), + "k1".getBytes(), + "3".getBytes(), + options); + Assert.assertTrue(false); + } catch (PException ex) { + Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); + + } catch (Exception ex) { + Assert.assertTrue(false); + } + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + options.returnCheckValue = true; + try { + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "v1".getBytes(), + "k1".getBytes(), + "3".getBytes(), + options); + Assert.assertTrue(false); + } catch (PException ex) { + Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); + + } catch (Exception ex) { + Assert.assertTrue(false); + } + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + options.returnCheckValue = true; + try { + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "88888888888888888888888888888888888888888888888".getBytes(), + "k1".getBytes(), + "3".getBytes(), + options); + Assert.assertTrue(false); + } catch (PException ex) { + Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); + + } catch (Exception ex) { + Assert.assertTrue(false); + } + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + client.set(tableName, hashKey, "k1".getBytes(), "0".getBytes()); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "0".getBytes(), + "k1".getBytes(), + "-1".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("0".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("-1".getBytes(), value); + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k1".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "-1".getBytes(), + "k1".getBytes(), + "-2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("-1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("-2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testIntCompare() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_value_int_compare".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "1".getBytes(), - "k1".getBytes(), "2".getBytes(), options); - Assert.assertFalse(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertFalse(result.checkValueExist); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - options.returnCheckValue = true; - try { - client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "1".getBytes(), - "k1".getBytes(), "2".getBytes(), options); - Assert.assertTrue(false); - } catch (PException ex) { - Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); - - } catch (Exception ex) { - Assert.assertTrue(false); - } - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("".getBytes(), value); - - client.set(tableName, hashKey, "k1".getBytes(), "1".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "1".getBytes(), - "k1".getBytes(), "2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - options.returnCheckValue = true; - try { - client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "".getBytes(), - "k1".getBytes(), "3".getBytes(), options); - Assert.assertTrue(false); - } catch (PException ex) { - Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); - - } catch (Exception ex) { - Assert.assertTrue(false); - } - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - options.returnCheckValue = true; - try { - client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "v1".getBytes(), - "k1".getBytes(), "3".getBytes(), options); - Assert.assertTrue(false); - } catch (PException ex) { - Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); - - } catch (Exception ex) { - Assert.assertTrue(false); - } - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - options.returnCheckValue = true; - try { - client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, - "88888888888888888888888888888888888888888888888".getBytes(), - "k1".getBytes(), "3".getBytes(), options); - Assert.assertTrue(false); - } catch (PException ex) { - Assert.assertTrue(ex.getMessage(), ex.getMessage().endsWith("rocksdb error: 4")); - - } catch (Exception ex) { - Assert.assertTrue(false); - } - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - client.set(tableName, hashKey, "k1".getBytes(), "0".getBytes()); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "0".getBytes(), - "k1".getBytes(), "-1".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("0".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("-1".getBytes(), value); - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k1".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "-1".getBytes(), - "k1".getBytes(), "-2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("-1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("-2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k3".getBytes(), "3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k3".getBytes(), CheckType.CT_VALUE_INT_EQUAL, "3".getBytes(), - "k4".getBytes(), "4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k4".getBytes()); - Assert.assertArrayEquals("4".getBytes(), value); - - client.del(tableName, hashKey, "k3".getBytes()); - client.del(tableName, hashKey, "k4".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - try { - client.set(tableName, hashKey, "k5".getBytes(), "1".getBytes()); - - CheckAndSetOptions options = new CheckAndSetOptions(); - PegasusTableInterface.CheckAndSetResult result; - byte[] value; - - // 1 < 2 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_LESS, "2".getBytes(), - "k5".getBytes(), "2".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("1".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("2".getBytes(), value); - - // 2 <= 2 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_LESS_OR_EQUAL, "2".getBytes(), - "k5".getBytes(), "3".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("2".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("3".getBytes(), value); - - // 3 <= 4 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_LESS_OR_EQUAL, "4".getBytes(), - "k5".getBytes(), "4".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("3".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("4".getBytes(), value); - - // 4 >= 4 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_GREATER_OR_EQUAL, "4".getBytes(), - "k5".getBytes(), "5".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("4".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("5".getBytes(), value); - - // 5 >= 4 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_GREATER_OR_EQUAL, "4".getBytes(), - "k5".getBytes(), "6".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("5".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("6".getBytes(), value); - - // 6 > 5 - options.returnCheckValue = true; - result = client.checkAndSet(tableName, hashKey, - "k5".getBytes(), CheckType.CT_VALUE_INT_GREATER, "5".getBytes(), - "k5".getBytes(), "7".getBytes(), options); - Assert.assertTrue(result.setSucceed); - Assert.assertTrue(result.checkValueReturned); - Assert.assertTrue(result.checkValueExist); - Assert.assertArrayEquals("6".getBytes(), result.checkValue); - value = client.get(tableName, hashKey, "k5".getBytes()); - Assert.assertArrayEquals("7".getBytes(), value); - - client.del(tableName, hashKey, "k5".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k3".getBytes(), "3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k3".getBytes(), + CheckType.CT_VALUE_INT_EQUAL, + "3".getBytes(), + "k4".getBytes(), + "4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k4".getBytes()); + Assert.assertArrayEquals("4".getBytes(), value); + + client.del(tableName, hashKey, "k3".getBytes()); + client.del(tableName, hashKey, "k4".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } - @Test - public void testCompareExchange() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - byte[] hashKey = "check_and_set_java_test_compare_exchange".getBytes(); - - try { - client.del(tableName, hashKey, "k1".getBytes()); - - PegasusTableInterface.CompareExchangeResult result; - byte[] value; - - result = client.compareExchange(tableName, hashKey, "k1".getBytes(), "".getBytes(), "v1".getBytes(), 0); - Assert.assertFalse(result.setSucceed); - Assert.assertNull(result.actualValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertNull(value); - - client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); - - result = client.compareExchange(tableName, hashKey, "k1".getBytes(), "".getBytes(), "v1".getBytes(), 0); - Assert.assertTrue(result.setSucceed); - Assert.assertNull(result.actualValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - result = client.compareExchange(tableName, hashKey, "k1".getBytes(), "".getBytes(), "v2".getBytes(), 0); - Assert.assertFalse(result.setSucceed); - Assert.assertArrayEquals("v1".getBytes(), result.actualValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v1".getBytes(), value); - - result = client.compareExchange(tableName, hashKey, "k1".getBytes(), "v1".getBytes(), "v2".getBytes(), 0); - Assert.assertTrue(result.setSucceed); - Assert.assertNull(result.actualValue); - value = client.get(tableName, hashKey, "k1".getBytes()); - Assert.assertArrayEquals("v2".getBytes(), value); - - client.del(tableName, hashKey, "k1".getBytes()); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + try { + client.set(tableName, hashKey, "k5".getBytes(), "1".getBytes()); + + CheckAndSetOptions options = new CheckAndSetOptions(); + PegasusTableInterface.CheckAndSetResult result; + byte[] value; + + // 1 < 2 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_LESS, + "2".getBytes(), + "k5".getBytes(), + "2".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("1".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("2".getBytes(), value); + + // 2 <= 2 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_LESS_OR_EQUAL, + "2".getBytes(), + "k5".getBytes(), + "3".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("2".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("3".getBytes(), value); + + // 3 <= 4 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_LESS_OR_EQUAL, + "4".getBytes(), + "k5".getBytes(), + "4".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("3".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("4".getBytes(), value); + + // 4 >= 4 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_GREATER_OR_EQUAL, + "4".getBytes(), + "k5".getBytes(), + "5".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("4".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("5".getBytes(), value); + + // 5 >= 4 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_GREATER_OR_EQUAL, + "4".getBytes(), + "k5".getBytes(), + "6".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("5".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("6".getBytes(), value); + + // 6 > 5 + options.returnCheckValue = true; + result = + client.checkAndSet( + tableName, + hashKey, + "k5".getBytes(), + CheckType.CT_VALUE_INT_GREATER, + "5".getBytes(), + "k5".getBytes(), + "7".getBytes(), + options); + Assert.assertTrue(result.setSucceed); + Assert.assertTrue(result.checkValueReturned); + Assert.assertTrue(result.checkValueExist); + Assert.assertArrayEquals("6".getBytes(), result.checkValue); + value = client.get(tableName, hashKey, "k5".getBytes()); + Assert.assertArrayEquals("7".getBytes(), value); + + client.del(tableName, hashKey, "k5".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } + + PegasusClientFactory.closeSingletonClient(); + } + + @Test + public void testCompareExchange() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + byte[] hashKey = "check_and_set_java_test_compare_exchange".getBytes(); + + try { + client.del(tableName, hashKey, "k1".getBytes()); + + PegasusTableInterface.CompareExchangeResult result; + byte[] value; + + result = + client.compareExchange( + tableName, hashKey, "k1".getBytes(), "".getBytes(), "v1".getBytes(), 0); + Assert.assertFalse(result.setSucceed); + Assert.assertNull(result.actualValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertNull(value); + + client.set(tableName, hashKey, "k1".getBytes(), "".getBytes()); + + result = + client.compareExchange( + tableName, hashKey, "k1".getBytes(), "".getBytes(), "v1".getBytes(), 0); + Assert.assertTrue(result.setSucceed); + Assert.assertNull(result.actualValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + result = + client.compareExchange( + tableName, hashKey, "k1".getBytes(), "".getBytes(), "v2".getBytes(), 0); + Assert.assertFalse(result.setSucceed); + Assert.assertArrayEquals("v1".getBytes(), result.actualValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v1".getBytes(), value); + + result = + client.compareExchange( + tableName, hashKey, "k1".getBytes(), "v1".getBytes(), "v2".getBytes(), 0); + Assert.assertTrue(result.setSucceed); + Assert.assertNull(result.actualValue); + value = client.get(tableName, hashKey, "k1".getBytes()); + Assert.assertArrayEquals("v2".getBytes(), value); + + client.del(tableName, hashKey, "k1".getBytes()); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + + PegasusClientFactory.closeSingletonClient(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestIncr.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestIncr.java index 72bc0fb3..0d342b44 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestIncr.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestIncr.java @@ -3,104 +3,98 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; -/** - * @author qinzuoyan - */ - +/** @author qinzuoyan */ import org.junit.Assert; import org.junit.Test; -/** - * Created by mi on 18-7-10. - */ +/** Created by mi on 18-7-10. */ public class TestIncr { - @Test - public void testIncr() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; - - byte[] hashKey = "incrkeyforjava".getBytes(); - byte[] sortKey = "0".getBytes(); - byte[] value = "".getBytes(); - int ttlSeconds = 0; - - try { - System.out.println("set value ..."); - client.set(tableName, hashKey, sortKey, value, 0); - System.out.println("set value ok"); - - System.out.println("incr to empty value ..."); - long result = client.incr(tableName, hashKey, sortKey, 100); - Assert.assertEquals(100, result); - ttlSeconds = client.ttl(tableName, hashKey, sortKey); - Assert.assertEquals(-1, ttlSeconds); - System.out.println("incr to empty value ok"); - - System.out.println("incr zero ..."); - result = client.incr(tableName, hashKey, sortKey, 0); - Assert.assertEquals(100, result); - ttlSeconds = client.ttl(tableName, hashKey, sortKey); - Assert.assertEquals(-1, ttlSeconds); - System.out.println("incr zero ok"); - - System.out.println("incr negative ..."); - result = client.incr(tableName, hashKey, sortKey, -1); - Assert.assertEquals(99, result); - ttlSeconds = client.ttl(tableName, hashKey, sortKey); - Assert.assertEquals(-1, ttlSeconds); - System.out.println("incr negative ok"); - - System.out.println("del value ..."); - client.del(tableName, hashKey,sortKey); - System.out.println("del value ok"); - - System.out.println("incr to un-exist value ..."); - result = client.incr(tableName, hashKey, sortKey, 200); - Assert.assertEquals(200, result); - ttlSeconds = client.ttl(tableName, hashKey, sortKey); - Assert.assertEquals(-1, ttlSeconds); - System.out.println("incr to un-exist value ok"); - - System.out.println("incr with ttlSeconds > 0 ..."); - result = client.incr(tableName, hashKey, sortKey, 1, 10); - Assert.assertEquals(201, result); - ttlSeconds = client.ttl(tableName, hashKey, sortKey); - Assert.assertTrue(ttlSeconds > 0); - Assert.assertTrue(ttlSeconds <= 10); - System.out.println("incr with ttlSeconds > 0 ok"); - - System.out.println("incr with ttlSeconds == 0 ..."); - result = client.incr(tableName, hashKey, sortKey, 1); - Assert.assertEquals(202, result); - ttlSeconds = client.ttl(tableName, hashKey, sortKey); - Assert.assertTrue(ttlSeconds > 0); - Assert.assertTrue(ttlSeconds <= 10); - System.out.println("incr with ttlSeconds == 0 ok"); - - System.out.println("incr with ttlSeconds > 0 ..."); - result = client.incr(tableName, hashKey, sortKey, 1, 20); - Assert.assertEquals(203, result); - ttlSeconds = client.ttl(tableName, hashKey, sortKey); - Assert.assertTrue(ttlSeconds > 10); - Assert.assertTrue(ttlSeconds <= 20); - System.out.println("incr with ttlSeconds > 0 ok"); - - System.out.println("incr with ttlSeconds == -1 ..."); - result = client.incr(tableName, hashKey, sortKey, 1, -1); - Assert.assertEquals(204, result); - ttlSeconds = client.ttl(tableName, hashKey, sortKey); - Assert.assertEquals(-1, ttlSeconds); - System.out.println("incr with ttlSeconds == -1 ok"); - - System.out.println("del value ..."); - client.del(tableName, hashKey,sortKey); - System.out.println("del value ok"); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - PegasusClientFactory.closeSingletonClient(); + @Test + public void testIncr() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; + + byte[] hashKey = "incrkeyforjava".getBytes(); + byte[] sortKey = "0".getBytes(); + byte[] value = "".getBytes(); + int ttlSeconds = 0; + + try { + System.out.println("set value ..."); + client.set(tableName, hashKey, sortKey, value, 0); + System.out.println("set value ok"); + + System.out.println("incr to empty value ..."); + long result = client.incr(tableName, hashKey, sortKey, 100); + Assert.assertEquals(100, result); + ttlSeconds = client.ttl(tableName, hashKey, sortKey); + Assert.assertEquals(-1, ttlSeconds); + System.out.println("incr to empty value ok"); + + System.out.println("incr zero ..."); + result = client.incr(tableName, hashKey, sortKey, 0); + Assert.assertEquals(100, result); + ttlSeconds = client.ttl(tableName, hashKey, sortKey); + Assert.assertEquals(-1, ttlSeconds); + System.out.println("incr zero ok"); + + System.out.println("incr negative ..."); + result = client.incr(tableName, hashKey, sortKey, -1); + Assert.assertEquals(99, result); + ttlSeconds = client.ttl(tableName, hashKey, sortKey); + Assert.assertEquals(-1, ttlSeconds); + System.out.println("incr negative ok"); + + System.out.println("del value ..."); + client.del(tableName, hashKey, sortKey); + System.out.println("del value ok"); + + System.out.println("incr to un-exist value ..."); + result = client.incr(tableName, hashKey, sortKey, 200); + Assert.assertEquals(200, result); + ttlSeconds = client.ttl(tableName, hashKey, sortKey); + Assert.assertEquals(-1, ttlSeconds); + System.out.println("incr to un-exist value ok"); + + System.out.println("incr with ttlSeconds > 0 ..."); + result = client.incr(tableName, hashKey, sortKey, 1, 10); + Assert.assertEquals(201, result); + ttlSeconds = client.ttl(tableName, hashKey, sortKey); + Assert.assertTrue(ttlSeconds > 0); + Assert.assertTrue(ttlSeconds <= 10); + System.out.println("incr with ttlSeconds > 0 ok"); + + System.out.println("incr with ttlSeconds == 0 ..."); + result = client.incr(tableName, hashKey, sortKey, 1); + Assert.assertEquals(202, result); + ttlSeconds = client.ttl(tableName, hashKey, sortKey); + Assert.assertTrue(ttlSeconds > 0); + Assert.assertTrue(ttlSeconds <= 10); + System.out.println("incr with ttlSeconds == 0 ok"); + + System.out.println("incr with ttlSeconds > 0 ..."); + result = client.incr(tableName, hashKey, sortKey, 1, 20); + Assert.assertEquals(203, result); + ttlSeconds = client.ttl(tableName, hashKey, sortKey); + Assert.assertTrue(ttlSeconds > 10); + Assert.assertTrue(ttlSeconds <= 20); + System.out.println("incr with ttlSeconds > 0 ok"); + + System.out.println("incr with ttlSeconds == -1 ..."); + result = client.incr(tableName, hashKey, sortKey, 1, -1); + Assert.assertEquals(204, result); + ttlSeconds = client.ttl(tableName, hashKey, sortKey); + Assert.assertEquals(-1, ttlSeconds); + System.out.println("incr with ttlSeconds == -1 ok"); + + System.out.println("del value ..."); + client.del(tableName, hashKey, sortKey); + System.out.println("del value ok"); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } + + PegasusClientFactory.closeSingletonClient(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestMultiThread.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestMultiThread.java index 347f9947..caf3ae3e 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestMultiThread.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestMultiThread.java @@ -3,170 +3,168 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; +import java.util.ArrayList; import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; - -/** - * Created by mi on 16-3-23. - */ +/** Created by mi on 16-3-23. */ public class TestMultiThread { - public static final String TABLE_NAME = "temp"; + public static final String TABLE_NAME = "temp"; - private static class VisitThread extends Thread { - // so total operations = total_keys + removed_keys - private static int total_keys = 3000; - private static int removed_keys = 2000; + private static class VisitThread extends Thread { + // so total operations = total_keys + removed_keys + private static int total_keys = 3000; + private static int removed_keys = 2000; - private String name; - private PegasusClientInterface client; + private String name; + private PegasusClientInterface client; - public VisitThread(String name, PegasusClientInterface client) { - this.name = name; - this.client = client; - } + public VisitThread(String name, PegasusClientInterface client) { + this.name = name; + this.client = client; + } - public void run() { - ArrayList values = new ArrayList(total_keys); + public void run() { + ArrayList values = new ArrayList(total_keys); - long value_sum = 0; - for (int i = 0; i < total_keys; ++i) { - values.add((int) (Math.random() * 1000)); - } + long value_sum = 0; + for (int i = 0; i < total_keys; ++i) { + values.add((int) (Math.random() * 1000)); + } - int left_put = total_keys; - int key_cursor = 0; - int total_count = total_keys + removed_keys; - - for (int i = 1; i <= total_count; ++i) { - if (i % 1000 == 0) { - System.out.println(name + "round " + i); - } - int t = (int) (Math.random() * (total_count - i)); - if (t < left_put) { - String key = name + String.valueOf(key_cursor); - String value = String.valueOf(values.get(key_cursor)); - try { - client.set(TABLE_NAME, key.getBytes(), null, value.getBytes(), 0); - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - --left_put; - ++key_cursor; - } else { - int index = (int) (Math.random() * Math.min(key_cursor + 100, total_keys)); - String key = name + String.valueOf(index); - try { - client.del(TABLE_NAME, key.getBytes(), null); - if (index < key_cursor) { - values.set(index, 0); - } - } catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - } - } + int left_put = total_keys; + int key_cursor = 0; + int total_count = total_keys + removed_keys; - for (int i = 1; i <= total_keys; ++i) { - if (i % 1000 == 0) { - System.out.println(name + "get round " + i); - } - String key = name + String.valueOf(i-1); - try { - byte[] resp = client.get(TABLE_NAME, key.getBytes(), null); - if (resp != null) { - value_sum += Integer.valueOf(new String(resp)); - } - } catch (PException e) { - e.printStackTrace(); - } + for (int i = 1; i <= total_count; ++i) { + if (i % 1000 == 0) { + System.out.println(name + "round " + i); + } + int t = (int) (Math.random() * (total_count - i)); + if (t < left_put) { + String key = name + String.valueOf(key_cursor); + String value = String.valueOf(values.get(key_cursor)); + try { + client.set(TABLE_NAME, key.getBytes(), null, value.getBytes(), 0); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + --left_put; + ++key_cursor; + } else { + int index = (int) (Math.random() * Math.min(key_cursor + 100, total_keys)); + String key = name + String.valueOf(index); + try { + client.del(TABLE_NAME, key.getBytes(), null); + if (index < key_cursor) { + values.set(index, 0); } + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + } + } - long expected_sum = 0; - for (int v : values) { - expected_sum += v; - } - Assert.assertEquals(expected_sum, value_sum); + for (int i = 1; i <= total_keys; ++i) { + if (i % 1000 == 0) { + System.out.println(name + "get round " + i); + } + String key = name + String.valueOf(i - 1); + try { + byte[] resp = client.get(TABLE_NAME, key.getBytes(), null); + if (resp != null) { + value_sum += Integer.valueOf(new String(resp)); + } + } catch (PException e) { + e.printStackTrace(); } + } + + long expected_sum = 0; + for (int v : values) { + expected_sum += v; + } + Assert.assertEquals(expected_sum, value_sum); } + } - @Test - public void testMultiThread() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + @Test + public void testMultiThread() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - System.out.println("start to run multi-thread test"); + System.out.println("start to run multi-thread test"); - ArrayList threadList = new ArrayList(); - for (int i = 0; i < 10; ++i) { - threadList.add(new VisitThread("Thread_" + String.valueOf(i) + "_", client)); - } - for (VisitThread vt : threadList) { - vt.start(); - } - for (VisitThread vt : threadList) { - while (true) { - try { - vt.join(); - break; - } catch (InterruptedException e) { - } - } + ArrayList threadList = new ArrayList(); + for (int i = 0; i < 10; ++i) { + threadList.add(new VisitThread("Thread_" + String.valueOf(i) + "_", client)); + } + for (VisitThread vt : threadList) { + vt.start(); + } + for (VisitThread vt : threadList) { + while (true) { + try { + vt.join(); + break; + } catch (InterruptedException e) { } - - PegasusClientFactory.closeSingletonClient(); + } } -// private static final class FillThread extends Thread { -// private String name; -// private PegasusClientInterface client; -// -// public FillThread(String aName, PegasusClientInterface aClient) { -// name = aName; -// client = aClient; -// } -// -// public void run() { -// long i = 1; -// while (true) { -// String value = String.valueOf(i%10000); -// try { -// client.set(TABLE_NAME, value.getBytes(), value.getBytes(), value.getBytes()); -// i++; -// } catch (PException ex) { -// ex.printStackTrace(); -// } -// -// if (i%1000 == 0) { -// System.out.println(name + " round " + i); -// } -// } -// } -// } -// -// @Test -// public void testFillValue() throws PException { -// PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); -// -// System.out.println("start to run multithread fill"); -// -// ArrayList ftList = new ArrayList(); -// for (int i=0; i<3; ++i) { -// ftList.add(new FillThread("Thread" + String.valueOf(i), client)); -// } -// -// for (int i=0; i<3; ++i) { -// ftList.get(i).start(); -// } -// -// for (int i=0; i<3; ++i) { -// try { -// ftList.get(i).join(); -// } catch (Exception ex) { -// ex.printStackTrace(); -// } -// } -// } + PegasusClientFactory.closeSingletonClient(); + } + + // private static final class FillThread extends Thread { + // private String name; + // private PegasusClientInterface client; + // + // public FillThread(String aName, PegasusClientInterface aClient) { + // name = aName; + // client = aClient; + // } + // + // public void run() { + // long i = 1; + // while (true) { + // String value = String.valueOf(i%10000); + // try { + // client.set(TABLE_NAME, value.getBytes(), value.getBytes(), + // value.getBytes()); + // i++; + // } catch (PException ex) { + // ex.printStackTrace(); + // } + // + // if (i%1000 == 0) { + // System.out.println(name + " round " + i); + // } + // } + // } + // } + // + // @Test + // public void testFillValue() throws PException { + // PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + // + // System.out.println("start to run multithread fill"); + // + // ArrayList ftList = new ArrayList(); + // for (int i=0; i<3; ++i) { + // ftList.add(new FillThread("Thread" + String.valueOf(i), client)); + // } + // + // for (int i=0; i<3; ++i) { + // ftList.get(i).start(); + // } + // + // for (int i=0; i<3; ++i) { + // try { + // ftList.get(i).join(); + // } catch (Exception ex) { + // ex.printStackTrace(); + // } + // } + // } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestNoOperate.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestNoOperate.java index 3ec07e5f..a4e97e8d 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestNoOperate.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestNoOperate.java @@ -5,31 +5,29 @@ import org.junit.Test; -/** - * Created by weijiesun on 16-11-24. - */ +/** Created by weijiesun on 16-11-24. */ public class TestNoOperate { - @Test - public void testNoOperate() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; + @Test + public void testNoOperate() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; - System.out.println("start to write some keys"); - for (int i = 0; i < 100; ++i) { - String hashKey = "hello" + String.valueOf(i); - String sortKey = "0"; - String value = "world"; - client.set(tableName, hashKey.getBytes(), sortKey.getBytes(), value.getBytes()); - } - - System.out.println("start to wait some time"); + System.out.println("start to write some keys"); + for (int i = 0; i < 100; ++i) { + String hashKey = "hello" + String.valueOf(i); + String sortKey = "0"; + String value = "world"; + client.set(tableName, hashKey.getBytes(), sortKey.getBytes(), value.getBytes()); + } - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } + System.out.println("start to wait some time"); - PegasusClientFactory.closeSingletonClient(); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); } -} \ No newline at end of file + + PegasusClientFactory.closeSingletonClient(); + } +} diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestPing.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestPing.java index 7b542c92..ee2a40ce 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestPing.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestPing.java @@ -3,114 +3,111 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; +import java.util.*; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.junit.Assert; import org.junit.Test; -import java.util.*; - -/** - * Created by mi on 16-3-22. - */ +/** Created by mi on 16-3-22. */ public class TestPing { - @Test - public void testPing() throws PException { - PegasusClientInterface client = PegasusClientFactory.createClient("resource:///pegasus.properties"); - String tableName = "temp"; + @Test + public void testPing() throws PException { + PegasusClientInterface client = + PegasusClientFactory.createClient("resource:///pegasus.properties"); + String tableName = "temp"; - byte[] hashKey = "hello".getBytes(); - byte[] sortKey = "0".getBytes(); - byte[] value = "world".getBytes(); - byte[] sortKey1 = "1".getBytes(); - byte[] value1 = "pegasus".getBytes(); + byte[] hashKey = "hello".getBytes(); + byte[] sortKey = "0".getBytes(); + byte[] value = "world".getBytes(); + byte[] sortKey1 = "1".getBytes(); + byte[] value1 = "pegasus".getBytes(); - try { - System.out.println("set value ..."); - client.set(tableName, hashKey, sortKey, value, 0); - System.out.println("set value ok"); + try { + System.out.println("set value ..."); + client.set(tableName, hashKey, sortKey, value, 0); + System.out.println("set value ok"); - System.out.println("set value1 ..."); - client.set(tableName, hashKey, sortKey1, value1, 0); - System.out.println("set value1 ok"); + System.out.println("set value1 ..."); + client.set(tableName, hashKey, sortKey1, value1, 0); + System.out.println("set value1 ok"); - System.out.println("multi set ..."); - List> setValues = new ArrayList>(); - for (int i = 2; i < 9; ++i) { - byte[] k = Integer.toString(i).getBytes(); - byte[] v = ("value" + i).getBytes(); - setValues.add(new ImmutablePair(k, v)); - } - client.multiSet(tableName, hashKey, setValues); - System.out.println("multi set ..."); + System.out.println("multi set ..."); + List> setValues = new ArrayList>(); + for (int i = 2; i < 9; ++i) { + byte[] k = Integer.toString(i).getBytes(); + byte[] v = ("value" + i).getBytes(); + setValues.add(new ImmutablePair(k, v)); + } + client.multiSet(tableName, hashKey, setValues); + System.out.println("multi set ..."); - System.out.println("get value ..."); - byte[] result = client.get(tableName, hashKey, sortKey); - Assert.assertTrue(Arrays.equals(value, result)); - System.out.println("get value ok"); + System.out.println("get value ..."); + byte[] result = client.get(tableName, hashKey, sortKey); + Assert.assertTrue(Arrays.equals(value, result)); + System.out.println("get value ok"); - System.out.println("get ttl ..."); - int ttl = client.ttl(tableName, hashKey, sortKey); - Assert.assertEquals(-1, ttl); - System.out.println("get ttl ok"); + System.out.println("get ttl ..."); + int ttl = client.ttl(tableName, hashKey, sortKey); + Assert.assertEquals(-1, ttl); + System.out.println("get ttl ok"); - System.out.println("multi get ..."); - List sortKeys = new ArrayList(); - sortKeys.add("unexist-sort-key".getBytes()); - sortKeys.add(sortKey1); - sortKeys.add(sortKey1); - sortKeys.add(sortKey); - List> values = new ArrayList>(); - boolean getAll = client.multiGet(tableName, hashKey, sortKeys, values); - Assert.assertTrue(getAll); - Assert.assertEquals(2, values.size()); - Assert.assertEquals(sortKey, values.get(0).getKey()); - Assert.assertArrayEquals(sortKey, values.get(0).getKey()); - Assert.assertArrayEquals(value, values.get(0).getValue()); - Assert.assertEquals(sortKey1, values.get(1).getKey()); - Assert.assertArrayEquals(sortKey1, values.get(1).getKey()); - Assert.assertArrayEquals(value1, values.get(1).getValue()); - System.out.println("multi get ok"); + System.out.println("multi get ..."); + List sortKeys = new ArrayList(); + sortKeys.add("unexist-sort-key".getBytes()); + sortKeys.add(sortKey1); + sortKeys.add(sortKey1); + sortKeys.add(sortKey); + List> values = new ArrayList>(); + boolean getAll = client.multiGet(tableName, hashKey, sortKeys, values); + Assert.assertTrue(getAll); + Assert.assertEquals(2, values.size()); + Assert.assertEquals(sortKey, values.get(0).getKey()); + Assert.assertArrayEquals(sortKey, values.get(0).getKey()); + Assert.assertArrayEquals(value, values.get(0).getValue()); + Assert.assertEquals(sortKey1, values.get(1).getKey()); + Assert.assertArrayEquals(sortKey1, values.get(1).getKey()); + Assert.assertArrayEquals(value1, values.get(1).getValue()); + System.out.println("multi get ok"); - System.out.println("multi get partial ..."); - sortKeys.clear(); - values.clear(); - sortKeys.add(sortKey); - sortKeys.add(sortKey1); - for (Pair p : setValues) { - sortKeys.add(p.getKey()); - } - getAll = client.multiGet(tableName, hashKey, sortKeys, 5, 1000000, values); - Assert.assertFalse(getAll); - Assert.assertEquals(5, values.size()); - Assert.assertEquals(sortKey, values.get(0).getKey()); - Assert.assertArrayEquals(sortKey, values.get(0).getKey()); - Assert.assertArrayEquals(value, values.get(0).getValue()); - Assert.assertEquals(sortKey1, values.get(1).getKey()); - Assert.assertArrayEquals(sortKey1, values.get(1).getKey()); - Assert.assertArrayEquals(value1, values.get(1).getValue()); - for (int i = 2; i < 5; ++i) { - Assert.assertEquals(setValues.get(i - 2).getKey(), values.get(i).getKey()); - Assert.assertArrayEquals(setValues.get(i - 2).getKey(), values.get(i).getKey()); - Assert.assertArrayEquals(setValues.get(i - 2).getValue(), values.get(i).getValue()); - } - System.out.println("multi get partial ok"); + System.out.println("multi get partial ..."); + sortKeys.clear(); + values.clear(); + sortKeys.add(sortKey); + sortKeys.add(sortKey1); + for (Pair p : setValues) { + sortKeys.add(p.getKey()); + } + getAll = client.multiGet(tableName, hashKey, sortKeys, 5, 1000000, values); + Assert.assertFalse(getAll); + Assert.assertEquals(5, values.size()); + Assert.assertEquals(sortKey, values.get(0).getKey()); + Assert.assertArrayEquals(sortKey, values.get(0).getKey()); + Assert.assertArrayEquals(value, values.get(0).getValue()); + Assert.assertEquals(sortKey1, values.get(1).getKey()); + Assert.assertArrayEquals(sortKey1, values.get(1).getKey()); + Assert.assertArrayEquals(value1, values.get(1).getValue()); + for (int i = 2; i < 5; ++i) { + Assert.assertEquals(setValues.get(i - 2).getKey(), values.get(i).getKey()); + Assert.assertArrayEquals(setValues.get(i - 2).getKey(), values.get(i).getKey()); + Assert.assertArrayEquals(setValues.get(i - 2).getValue(), values.get(i).getValue()); + } + System.out.println("multi get partial ok"); - System.out.println("del value ..."); - client.del(tableName, hashKey,sortKey); - System.out.println("del value ok"); + System.out.println("del value ..."); + client.del(tableName, hashKey, sortKey); + System.out.println("del value ok"); - System.out.println("get deleted value ..."); - result = client.get(tableName, hashKey, sortKey); - Assert.assertEquals(result, null); - System.out.println("get deleted value ok"); - } - catch (PException e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - - client.close(); - client.close(); + System.out.println("get deleted value ..."); + result = client.get(tableName, hashKey, sortKey); + Assert.assertEquals(result, null); + System.out.println("get deleted value ok"); + } catch (PException e) { + e.printStackTrace(); + Assert.assertTrue(false); } + + client.close(); + client.close(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestPingZK.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestPingZK.java index ca300d22..ead83468 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestPingZK.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestPingZK.java @@ -3,74 +3,69 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; -import org.junit.Assert; -import org.junit.Test; - -import org.I0Itec.zkclient.ZkClient; -import org.I0Itec.zkclient.serialize.BytesPushThroughSerializer; - import java.io.InputStream; import java.util.Arrays; import java.util.Scanner; +import org.I0Itec.zkclient.ZkClient; +import org.I0Itec.zkclient.serialize.BytesPushThroughSerializer; +import org.junit.Assert; +import org.junit.Test; -/** - * @author qinzuoyan - */ +/** @author qinzuoyan */ public class TestPingZK { - @Test - public void testPingZK() throws PException { - String zkServer = "127.0.0.1:22181"; - String zkPath = "/databases/pegasus/test-java-client"; - String configPath = "zk://" + zkServer + zkPath; + @Test + public void testPingZK() throws PException { + String zkServer = "127.0.0.1:22181"; + String zkPath = "/databases/pegasus/test-java-client"; + String configPath = "zk://" + zkServer + zkPath; - // init zk config - ZkClient zkClient = new ZkClient(zkServer, 30000, 30000, new BytesPushThroughSerializer()); - String[] components = zkPath.split("/"); - String curPath = ""; - for (int i = 0; i < components.length; ++i) { - if (components[i].isEmpty()) - continue; - curPath += "/" + components[i]; - if (!zkClient.exists(curPath)) { - zkClient.createPersistent(curPath); - } - } - InputStream is = PegasusClient.class.getResourceAsStream("/pegasus.properties"); - Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); - String configData = s.hasNext() ? s.next() : ""; - System.out.println("write config to " + configPath); - zkClient.writeData(zkPath, configData.getBytes()); + // init zk config + ZkClient zkClient = new ZkClient(zkServer, 30000, 30000, new BytesPushThroughSerializer()); + String[] components = zkPath.split("/"); + String curPath = ""; + for (int i = 0; i < components.length; ++i) { + if (components[i].isEmpty()) continue; + curPath += "/" + components[i]; + if (!zkClient.exists(curPath)) { + zkClient.createPersistent(curPath); + } + } + InputStream is = PegasusClient.class.getResourceAsStream("/pegasus.properties"); + Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); + String configData = s.hasNext() ? s.next() : ""; + System.out.println("write config to " + configPath); + zkClient.writeData(zkPath, configData.getBytes()); - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(configPath); - String tableName = "temp"; + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(configPath); + String tableName = "temp"; - byte[] hashKey = "hello".getBytes(); - byte[] sortKey = "0".getBytes(); - byte[] value = "world".getBytes(); + byte[] hashKey = "hello".getBytes(); + byte[] sortKey = "0".getBytes(); + byte[] value = "world".getBytes(); - System.out.println("set value ..."); - client.set(tableName, hashKey, sortKey, value, 0); - System.out.println("set value ok"); + System.out.println("set value ..."); + client.set(tableName, hashKey, sortKey, value, 0); + System.out.println("set value ok"); - System.out.println("get value ..."); - byte[] result = client.get(tableName, hashKey, sortKey); - Assert.assertTrue(Arrays.equals(value, result)); - System.out.println("get value ok"); + System.out.println("get value ..."); + byte[] result = client.get(tableName, hashKey, sortKey); + Assert.assertTrue(Arrays.equals(value, result)); + System.out.println("get value ok"); - System.out.println("del value ..."); - client.del(tableName, hashKey,sortKey); - System.out.println("del value ok"); + System.out.println("del value ..."); + client.del(tableName, hashKey, sortKey); + System.out.println("del value ok"); - System.out.println("get deleted value ..."); - result = client.get(tableName, hashKey, sortKey); - Assert.assertEquals(result, null); - System.out.println("get deleted value ok"); + System.out.println("get deleted value ..."); + result = client.get(tableName, hashKey, sortKey); + Assert.assertEquals(result, null); + System.out.println("get deleted value ok"); - System.out.println("set value ..."); - client.set(tableName, hashKey, sortKey, value, 0); - System.out.println("set value ok"); + System.out.println("set value ..."); + client.set(tableName, hashKey, sortKey, value, 0); + System.out.println("set value ok"); - PegasusClientFactory.closeSingletonClient(); - } + PegasusClientFactory.closeSingletonClient(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestScan.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestScan.java index b0764216..5b2edf32 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestScan.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestScan.java @@ -3,11 +3,9 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.client; +import io.netty.util.concurrent.*; import java.util.*; -import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import io.netty.util.concurrent.*; - import org.apache.commons.lang3.tuple.Pair; import org.junit.AfterClass; import org.junit.Assert; @@ -15,437 +13,467 @@ import org.junit.Test; public class TestScan { - static char[] CCH = "_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); - static char[] buffer = new char[256]; - static Random random; - - private static PegasusClientInterface client; - private static String tableName = "temp"; - - private static TreeMap> base; - private static String expectedHashKey; - - @BeforeClass - public static void setupTestCase() throws PException { - client = PegasusClientFactory.getSingletonClient(); - random = new Random(); - base = new TreeMap>(); - expectedHashKey = randomString(); - - for (int i = 0; i < buffer.length; i++) { - buffer[i] = CCH[random.nextInt(CCH.length)]; - } - - clearDatabase(); - - TreeMap hashMap = new TreeMap(); - for (int i = 0; i < 1000 || hashMap.size() < 1000; i++) { - String sortKey = randomString(); - String value = randomString(); - - client.set(tableName, expectedHashKey.getBytes(), sortKey.getBytes(), - value.getBytes(), 0); - hashMap.put(sortKey, value); - } - base.put(expectedHashKey, hashMap); - - for (int i = 0; i < 1000 || base.size() < 1000; i++) - { - String hashKey = randomString(); - TreeMap sortMap = base.get(hashKey); - if (sortMap == null) { - sortMap = new TreeMap(); - base.put(hashKey, sortMap); - } - for (int j = 0; j < 10 || sortMap.size() < 10; j++) { - String sortKey = randomString(); - String value = randomString(); - client.set(tableName, hashKey.getBytes(), sortKey.getBytes(), value.getBytes(), 0); - sortMap.put(sortKey, value); - } - } + static char[] CCH = + "_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); + static char[] buffer = new char[256]; + static Random random; + + private static PegasusClientInterface client; + private static String tableName = "temp"; + + private static TreeMap> base; + private static String expectedHashKey; + + @BeforeClass + public static void setupTestCase() throws PException { + client = PegasusClientFactory.getSingletonClient(); + random = new Random(); + base = new TreeMap>(); + expectedHashKey = randomString(); + + for (int i = 0; i < buffer.length; i++) { + buffer[i] = CCH[random.nextInt(CCH.length)]; } - @AfterClass - public static void tearDownTestCase() throws PException { - clearDatabase(); - } + clearDatabase(); - @Test - public void testAllSortKey() throws PException { - /**** ALL SORT_KEYS ****/ - System.out.println("TESTING_HASH_SCAN, ALL SORT_KEYS ...."); - ScanOptions options = new ScanOptions(); - TreeMap data = new TreeMap(); - PegasusScannerInterface scanner = - client.getScanner(tableName, expectedHashKey.getBytes(), new byte[]{}, new byte[]{}, options); - Assert.assertNotNull(scanner); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - Assert.assertEquals(expectedHashKey, new String(item.getLeft().getLeft())); - checkAndPutSortMap(data, expectedHashKey, - new String(item.getLeft().getRight()), new String(item.getRight())); - } - scanner.close(); - compareSortMap(data, base.get(expectedHashKey), expectedHashKey); - } + TreeMap hashMap = new TreeMap(); + for (int i = 0; i < 1000 || hashMap.size() < 1000; i++) { + String sortKey = randomString(); + String value = randomString(); - @Test - public void testInclusive() throws PException { - /**** [start, stop] ****/ - System.out.println("TESTING_HASH_SCAN, [start, stop]..."); - Iterator iterator = base.get(expectedHashKey).keySet().iterator(); - for (int i = random.nextInt(500); i >= 0; i--) - iterator.next(); - String start = iterator.next(); - for (int i = random.nextInt(400) + 50; i >= 0; i--) - iterator.next(); - String stop = iterator.next(); - - ScanOptions options = new ScanOptions(); - options.startInclusive = true; - options.stopInclusive = true; - TreeMap data = new TreeMap(); - PegasusScannerInterface scanner = client.getScanner(tableName, - expectedHashKey.getBytes(), start.getBytes(), stop.getBytes(), options); - Assert.assertNotNull(scanner); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - Assert.assertEquals(expectedHashKey, new String(item.getLeft().getLeft())); - checkAndPutSortMap(data, expectedHashKey, - new String(item.getLeft().getRight()), new String(item.getRight())); - } - scanner.close(); - compareSortMap(data, - base.get(expectedHashKey).subMap(start, true, stop, true), - expectedHashKey); + client.set(tableName, expectedHashKey.getBytes(), sortKey.getBytes(), value.getBytes(), 0); + hashMap.put(sortKey, value); } - - @Test - public void testExclusive() throws PException { - /**** (start, stop) ****/ - System.out.println("TESTING_HASH_SCAN, (start, stop)..."); - Iterator iterator = base.get(expectedHashKey).keySet().iterator(); - for (int i = random.nextInt(500); i >= 0; i--) - iterator.next(); - String start = iterator.next(); - for (int i = random.nextInt(400) + 50; i >= 0; i--) - iterator.next(); - String stop = iterator.next(); - - ScanOptions options = new ScanOptions(); - options.startInclusive = false; - options.stopInclusive = false; - TreeMap data = new TreeMap(); - PegasusScannerInterface scanner = client.getScanner(tableName, - expectedHashKey.getBytes(), start.getBytes(), stop.getBytes(), options); - Assert.assertNotNull(scanner); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - Assert.assertEquals(expectedHashKey, new String(item.getLeft().getLeft())); - checkAndPutSortMap(data, expectedHashKey, - new String(item.getLeft().getRight()), new String(item.getRight())); - } - scanner.close(); - compareSortMap(data, - base.get(expectedHashKey).subMap(start, false, stop, false), - expectedHashKey); - } - - @Test - public void testOnePoint() throws PException { - System.out.println("TESTING_HASH_SCAN, [start, start]"); - Iterator iterator = base.get(expectedHashKey).keySet().iterator(); - for (int i = random.nextInt(800); i >= 0; i--) - iterator.next(); - String start = iterator.next(); - - ScanOptions options = new ScanOptions(); - options.startInclusive = true; - options.stopInclusive = true; - PegasusScannerInterface scanner = client.getScanner(tableName, - expectedHashKey.getBytes(), start.getBytes(), start.getBytes(), options); - Assert.assertNotNull(scanner); - Pair, byte[]> item = scanner.next(); - Assert.assertEquals(start, new String(item.getLeft().getRight())); - item = scanner.next(); - Assert.assertNull(item); - scanner.close(); + base.put(expectedHashKey, hashMap); + + for (int i = 0; i < 1000 || base.size() < 1000; i++) { + String hashKey = randomString(); + TreeMap sortMap = base.get(hashKey); + if (sortMap == null) { + sortMap = new TreeMap(); + base.put(hashKey, sortMap); + } + for (int j = 0; j < 10 || sortMap.size() < 10; j++) { + String sortKey = randomString(); + String value = randomString(); + client.set(tableName, hashKey.getBytes(), sortKey.getBytes(), value.getBytes(), 0); + sortMap.put(sortKey, value); + } } - - @Test - public void testHalfInclusive() throws PException { - System.out.println("TESTING_HASH_SCAN, [start, start)"); - Iterator iterator = base.get(expectedHashKey).keySet().iterator(); - for (int i = random.nextInt(800); i >= 0; i--) - iterator.next(); - String start = iterator.next(); - - ScanOptions options = new ScanOptions(); - options.startInclusive = true; - options.stopInclusive = false; - PegasusScannerInterface scanner = client.getScanner(tableName, - expectedHashKey.getBytes(), start.getBytes(), start.getBytes(), options); - Assert.assertNotNull(scanner); - Pair, byte[]> item = scanner.next(); - Assert.assertNull(item); - scanner.close(); + } + + @AfterClass + public static void tearDownTestCase() throws PException { + clearDatabase(); + } + + @Test + public void testAllSortKey() throws PException { + /** ** ALL SORT_KEYS *** */ + System.out.println("TESTING_HASH_SCAN, ALL SORT_KEYS ...."); + ScanOptions options = new ScanOptions(); + TreeMap data = new TreeMap(); + PegasusScannerInterface scanner = + client.getScanner( + tableName, expectedHashKey.getBytes(), new byte[] {}, new byte[] {}, options); + Assert.assertNotNull(scanner); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + Assert.assertEquals(expectedHashKey, new String(item.getLeft().getLeft())); + checkAndPutSortMap( + data, + expectedHashKey, + new String(item.getLeft().getRight()), + new String(item.getRight())); } - - @Test - public void testVoidSpan() throws PException { - /**** [stop, start] ****/ - System.out.println("TESTING_HASH_SCAN, [stop, start]..."); - Iterator iterator = base.get(expectedHashKey).keySet().iterator(); - for (int i = random.nextInt(500); i >= 0; i--) - iterator.next(); - String start = iterator.next(); - for (int i = random.nextInt(400) + 50; i >= 0; i--) - iterator.next(); - String stop = iterator.next(); - - ScanOptions options = new ScanOptions(); - options.startInclusive = true; - options.stopInclusive = true; - PegasusScannerInterface scanner = client.getScanner(tableName, - expectedHashKey.getBytes(), stop.getBytes(), start.getBytes(), options); - Assert.assertNotNull(scanner); - Pair, byte[]> item = scanner.next(); - Assert.assertNull(item); - scanner.close(); + scanner.close(); + compareSortMap(data, base.get(expectedHashKey), expectedHashKey); + } + + @Test + public void testInclusive() throws PException { + /** ** [start, stop] *** */ + System.out.println("TESTING_HASH_SCAN, [start, stop]..."); + Iterator iterator = base.get(expectedHashKey).keySet().iterator(); + for (int i = random.nextInt(500); i >= 0; i--) iterator.next(); + String start = iterator.next(); + for (int i = random.nextInt(400) + 50; i >= 0; i--) iterator.next(); + String stop = iterator.next(); + + ScanOptions options = new ScanOptions(); + options.startInclusive = true; + options.stopInclusive = true; + TreeMap data = new TreeMap(); + PegasusScannerInterface scanner = + client.getScanner( + tableName, expectedHashKey.getBytes(), start.getBytes(), stop.getBytes(), options); + Assert.assertNotNull(scanner); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + Assert.assertEquals(expectedHashKey, new String(item.getLeft().getLeft())); + checkAndPutSortMap( + data, + expectedHashKey, + new String(item.getLeft().getRight()), + new String(item.getRight())); } - - @Test - public void testOverallScan() throws PException { - System.out.println("TEST OVERALL_SCAN..."); - - ScanOptions options = new ScanOptions(); - TreeMap> data = new TreeMap>(); - List scanners = client.getUnorderedScanners(tableName, 3, options); - Assert.assertTrue(scanners.size() <= 3); - - for (int i = scanners.size() - 1; i >= 0; i--) - { - PegasusScannerInterface scanner = scanners.get(i); - Assert.assertNotNull(scanner); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - checkAndPut(data, new String(item.getLeft().getLeft()), new String(item.getLeft().getRight()), - new String(item.getRight())); - } - scanner.close(); - } - compare(data, base); + scanner.close(); + compareSortMap( + data, base.get(expectedHashKey).subMap(start, true, stop, true), expectedHashKey); + } + + @Test + public void testExclusive() throws PException { + /** ** (start, stop) *** */ + System.out.println("TESTING_HASH_SCAN, (start, stop)..."); + Iterator iterator = base.get(expectedHashKey).keySet().iterator(); + for (int i = random.nextInt(500); i >= 0; i--) iterator.next(); + String start = iterator.next(); + for (int i = random.nextInt(400) + 50; i >= 0; i--) iterator.next(); + String stop = iterator.next(); + + ScanOptions options = new ScanOptions(); + options.startInclusive = false; + options.stopInclusive = false; + TreeMap data = new TreeMap(); + PegasusScannerInterface scanner = + client.getScanner( + tableName, expectedHashKey.getBytes(), start.getBytes(), stop.getBytes(), options); + Assert.assertNotNull(scanner); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + Assert.assertEquals(expectedHashKey, new String(item.getLeft().getLeft())); + checkAndPutSortMap( + data, + expectedHashKey, + new String(item.getLeft().getRight()), + new String(item.getRight())); } - - @Test - public void testAsyncScan() throws PException { - System.out.println("TEST asyncNext..."); - - ScanOptions options = new ScanOptions(); - TreeMap> data = new TreeMap>(); - List scanners = client.getUnorderedScanners(tableName, 3, options); - Assert.assertTrue(scanners.size() <= 3); - - for (int i = scanners.size() - 1; i >= 0; i--) - { - PegasusScannerInterface scanner = scanners.get(i); - Assert.assertNotNull(scanner); - Future, byte[]>> item; - while (true) { - item = scanner.asyncNext(); - try { - Pair, byte[]> pair = item.get(); - if (pair == null) - break; - checkAndPut(data, new String(pair.getLeft().getLeft()), new String(pair.getLeft().getRight()), - new String(pair.getRight())); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - } - } - compare(data, base); + scanner.close(); + compareSortMap( + data, base.get(expectedHashKey).subMap(start, false, stop, false), expectedHashKey); + } + + @Test + public void testOnePoint() throws PException { + System.out.println("TESTING_HASH_SCAN, [start, start]"); + Iterator iterator = base.get(expectedHashKey).keySet().iterator(); + for (int i = random.nextInt(800); i >= 0; i--) iterator.next(); + String start = iterator.next(); + + ScanOptions options = new ScanOptions(); + options.startInclusive = true; + options.stopInclusive = true; + PegasusScannerInterface scanner = + client.getScanner( + tableName, expectedHashKey.getBytes(), start.getBytes(), start.getBytes(), options); + Assert.assertNotNull(scanner); + Pair, byte[]> item = scanner.next(); + Assert.assertEquals(start, new String(item.getLeft().getRight())); + item = scanner.next(); + Assert.assertNull(item); + scanner.close(); + } + + @Test + public void testHalfInclusive() throws PException { + System.out.println("TESTING_HASH_SCAN, [start, start)"); + Iterator iterator = base.get(expectedHashKey).keySet().iterator(); + for (int i = random.nextInt(800); i >= 0; i--) iterator.next(); + String start = iterator.next(); + + ScanOptions options = new ScanOptions(); + options.startInclusive = true; + options.stopInclusive = false; + PegasusScannerInterface scanner = + client.getScanner( + tableName, expectedHashKey.getBytes(), start.getBytes(), start.getBytes(), options); + Assert.assertNotNull(scanner); + Pair, byte[]> item = scanner.next(); + Assert.assertNull(item); + scanner.close(); + } + + @Test + public void testVoidSpan() throws PException { + /** ** [stop, start] *** */ + System.out.println("TESTING_HASH_SCAN, [stop, start]..."); + Iterator iterator = base.get(expectedHashKey).keySet().iterator(); + for (int i = random.nextInt(500); i >= 0; i--) iterator.next(); + String start = iterator.next(); + for (int i = random.nextInt(400) + 50; i >= 0; i--) iterator.next(); + String stop = iterator.next(); + + ScanOptions options = new ScanOptions(); + options.startInclusive = true; + options.stopInclusive = true; + PegasusScannerInterface scanner = + client.getScanner( + tableName, expectedHashKey.getBytes(), stop.getBytes(), start.getBytes(), options); + Assert.assertNotNull(scanner); + Pair, byte[]> item = scanner.next(); + Assert.assertNull(item); + scanner.close(); + } + + @Test + public void testOverallScan() throws PException { + System.out.println("TEST OVERALL_SCAN..."); + + ScanOptions options = new ScanOptions(); + TreeMap> data = new TreeMap>(); + List scanners = client.getUnorderedScanners(tableName, 3, options); + Assert.assertTrue(scanners.size() <= 3); + + for (int i = scanners.size() - 1; i >= 0; i--) { + PegasusScannerInterface scanner = scanners.get(i); + Assert.assertNotNull(scanner); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + checkAndPut( + data, + new String(item.getLeft().getLeft()), + new String(item.getLeft().getRight()), + new String(item.getRight())); + } + scanner.close(); } - - @Test - public void testConcurrentCallAsyncScan() throws PException { - int [] batchSizes = {10, 100, 500, 1000}; - int totalKvSize = 10000; // we just use scan to acquire 10000 kv-pairs - long start_time_ms = 0, end_time_ms = 0; - Deque, byte[]>>> futures = - new LinkedList,byte[]>>>(); - for (int idx = 0; idx < batchSizes.length; idx ++) { - int batchSize = batchSizes[idx]; - ScanOptions options = new ScanOptions(); - options.batchSize = batchSize; - TreeMap> data = new TreeMap>(); - List scanners = client.getUnorderedScanners(tableName, 1, options); - Assert.assertTrue(scanners.size() <= 1); - System.out.println("start to scan " + totalKvSize + " kv-pairs with scan batch size " + batchSize); - start_time_ms = System.currentTimeMillis(); - futures.clear(); - for (int cnt = 0; cnt < totalKvSize; cnt ++) { - futures.add(scanners.get(0).asyncNext()); - } - try { - for (Future, byte[]>> fu : futures) { - fu.get(); - } - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - end_time_ms = System.currentTimeMillis(); - System.out.println("consuing time: " + (end_time_ms - start_time_ms) + "ms"); + compare(data, base); + } + + @Test + public void testAsyncScan() throws PException { + System.out.println("TEST asyncNext..."); + + ScanOptions options = new ScanOptions(); + TreeMap> data = new TreeMap>(); + List scanners = client.getUnorderedScanners(tableName, 3, options); + Assert.assertTrue(scanners.size() <= 3); + + for (int i = scanners.size() - 1; i >= 0; i--) { + PegasusScannerInterface scanner = scanners.get(i); + Assert.assertNotNull(scanner); + Future, byte[]>> item; + while (true) { + item = scanner.asyncNext(); + try { + Pair, byte[]> pair = item.get(); + if (pair == null) break; + checkAndPut( + data, + new String(pair.getLeft().getLeft()), + new String(pair.getLeft().getRight()), + new String(pair.getRight())); + } catch (Exception e) { + e.printStackTrace(); + Assert.assertTrue(false); } + } } - - @Test - public void testHashKeyFilteringScan() throws PException { - System.out.println("TEST HASHKEY_FILTERING_SCAN..."); - ScanOptions options = new ScanOptions(); - options.hashKeyFilterType = FilterType.FT_MATCH_PREFIX; - options.hashKeyFilterPattern = expectedHashKey.getBytes(); - TreeMap data = new TreeMap(); - List scanners = client.getUnorderedScanners(tableName, 1, options); - PegasusScannerInterface scanner = scanners.get(0); - Assert.assertNotNull(scanner); - Pair, byte[]> item; - while((item = scanner.next()) != null) { - Assert.assertArrayEquals(expectedHashKey.getBytes(), item.getLeft().getLeft()); - checkAndPutSortMap(data, expectedHashKey, - new String(item.getLeft().getRight()), new String(item.getRight())); + compare(data, base); + } + + @Test + public void testConcurrentCallAsyncScan() throws PException { + int[] batchSizes = {10, 100, 500, 1000}; + int totalKvSize = 10000; // we just use scan to acquire 10000 kv-pairs + long start_time_ms = 0, end_time_ms = 0; + Deque, byte[]>>> futures = + new LinkedList, byte[]>>>(); + for (int idx = 0; idx < batchSizes.length; idx++) { + int batchSize = batchSizes[idx]; + ScanOptions options = new ScanOptions(); + options.batchSize = batchSize; + TreeMap> data = + new TreeMap>(); + List scanners = client.getUnorderedScanners(tableName, 1, options); + Assert.assertTrue(scanners.size() <= 1); + System.out.println( + "start to scan " + totalKvSize + " kv-pairs with scan batch size " + batchSize); + start_time_ms = System.currentTimeMillis(); + futures.clear(); + for (int cnt = 0; cnt < totalKvSize; cnt++) { + futures.add(scanners.get(0).asyncNext()); + } + try { + for (Future, byte[]>> fu : futures) { + fu.get(); } - scanner.close(); - compareSortMap(data, base.get(expectedHashKey), expectedHashKey); + } catch (Exception e) { + e.printStackTrace(); + Assert.assertTrue(false); + } + end_time_ms = System.currentTimeMillis(); + System.out.println("consuing time: " + (end_time_ms - start_time_ms) + "ms"); } - - private static void clearDatabase() throws PException { - ScanOptions options = new ScanOptions(); - List scanners = client.getUnorderedScanners(tableName, 1, options); - Assert.assertEquals(1, scanners.size()); - Assert.assertNotNull(scanners.get(0)); - - Pair, byte[]> item; - while((item = scanners.get(0).next()) != null ) { - client.del(tableName, item.getLeft().getLeft(), item.getLeft().getRight()); - } - scanners.get(0).close(); - - scanners = client.getUnorderedScanners(tableName, 1, options); - Assert.assertEquals(1, scanners.size()); - Assert.assertNotNull(scanners.get(0)); - item = scanners.get(0).next(); - scanners.get(0).close(); - Assert.assertNull( - item == null ? null : String.format("Database is cleared but not empty, hashKey=%s, sortKey=%s", - new String(item.getLeft().getLeft()), new String(item.getLeft().getRight())), - item); + } + + @Test + public void testHashKeyFilteringScan() throws PException { + System.out.println("TEST HASHKEY_FILTERING_SCAN..."); + ScanOptions options = new ScanOptions(); + options.hashKeyFilterType = FilterType.FT_MATCH_PREFIX; + options.hashKeyFilterPattern = expectedHashKey.getBytes(); + TreeMap data = new TreeMap(); + List scanners = client.getUnorderedScanners(tableName, 1, options); + PegasusScannerInterface scanner = scanners.get(0); + Assert.assertNotNull(scanner); + Pair, byte[]> item; + while ((item = scanner.next()) != null) { + Assert.assertArrayEquals(expectedHashKey.getBytes(), item.getLeft().getLeft()); + checkAndPutSortMap( + data, + expectedHashKey, + new String(item.getLeft().getRight()), + new String(item.getRight())); } - - private static String randomString() { - int pos = random.nextInt(buffer.length); - buffer[pos] = CCH[random.nextInt(CCH.length)]; - int length = random.nextInt(buffer.length) + 1; - if (pos + length < buffer.length) { - return new String(buffer, pos, length); - } else { - return new String(buffer, pos, buffer.length - pos) + - new String(buffer, 0, length + pos - buffer.length); - } + scanner.close(); + compareSortMap(data, base.get(expectedHashKey), expectedHashKey); + } + + private static void clearDatabase() throws PException { + ScanOptions options = new ScanOptions(); + List scanners = client.getUnorderedScanners(tableName, 1, options); + Assert.assertEquals(1, scanners.size()); + Assert.assertNotNull(scanners.get(0)); + + Pair, byte[]> item; + while ((item = scanners.get(0).next()) != null) { + client.del(tableName, item.getLeft().getLeft(), item.getLeft().getRight()); } - - private static void checkAndPut(TreeMap> data, - String hashKey, String sortKey, String value) { - TreeMap sortMap = data.get(hashKey); - if (sortMap == null) { - sortMap = new TreeMap(); - data.put(hashKey, sortMap); - } else { - Assert.assertNull( - String.format("Duplicate: hashKey=%s, sortKye=%s, oldValue=%s, newValue=%s", - hashKey, sortKey, sortMap.get(sortKey), value), - sortMap.get(sortKey)); - } - sortMap.put(sortKey, value); + scanners.get(0).close(); + + scanners = client.getUnorderedScanners(tableName, 1, options); + Assert.assertEquals(1, scanners.size()); + Assert.assertNotNull(scanners.get(0)); + item = scanners.get(0).next(); + scanners.get(0).close(); + Assert.assertNull( + item == null + ? null + : String.format( + "Database is cleared but not empty, hashKey=%s, sortKey=%s", + new String(item.getLeft().getLeft()), new String(item.getLeft().getRight())), + item); + } + + private static String randomString() { + int pos = random.nextInt(buffer.length); + buffer[pos] = CCH[random.nextInt(CCH.length)]; + int length = random.nextInt(buffer.length) + 1; + if (pos + length < buffer.length) { + return new String(buffer, pos, length); + } else { + return new String(buffer, pos, buffer.length - pos) + + new String(buffer, 0, length + pos - buffer.length); } - - private static void checkAndPutSortMap(TreeMap data, - String hashKey, String sortKey, String value) { + } + + private static void checkAndPut( + TreeMap> data, String hashKey, String sortKey, String value) { + TreeMap sortMap = data.get(hashKey); + if (sortMap == null) { + sortMap = new TreeMap(); + data.put(hashKey, sortMap); + } else { Assert.assertNull( - String.format("Duplicate: hashKey=%s, sortKye=%s, oldValue=%s, newValue=%s", - hashKey, sortKey, data.get(sortKey), value), - data.get(sortKey)); - data.put(sortKey, value); + String.format( + "Duplicate: hashKey=%s, sortKye=%s, oldValue=%s, newValue=%s", + hashKey, sortKey, sortMap.get(sortKey), value), + sortMap.get(sortKey)); } - - private static void compare(TreeMap> data, - TreeMap> base) { - Iterator>> iterator1 = data.entrySet().iterator(); - Iterator>> iterator2 = base.entrySet().iterator(); - while(true) { - Map.Entry> kv1 = - iterator1.hasNext() ? iterator1.next() : null; - Map.Entry> kv2 = - iterator2.hasNext() ? iterator2.next() : null; - if (kv1 == null) { - Assert.assertNull( - kv2 == null ? null : String.format("Only in base: hashKey=%s", kv2.getKey()), - kv2); - break; - } - Assert.assertNotNull(String.format("Only in data: hashKey=%s", kv1.getKey()), - kv2); - Assert.assertEquals( - String.format("Diff: dataHashKey=%s, baseHashKey=%s", kv1.getKey(), kv2.getKey()), - kv1.getKey(), kv2.getKey()); - Iterator> iterator3 = kv1.getValue().entrySet().iterator(); - Iterator> iterator4 = kv2.getValue().entrySet().iterator(); - while(true) { - Map.Entry kv3 = iterator3.hasNext() ? iterator3.next() : null; - Map.Entry kv4 = iterator4.hasNext() ? iterator4.next() : null; - if (kv3 == null) { - Assert.assertNull( - kv4 == null ? null : String.format("Only in base: hashKey=%s, sortKey=%s, value=%s", - kv1.getKey(), kv4.getKey(), kv4.getValue()), - kv4); - break; - } - Assert.assertNotNull( - String.format("Only in data: hashKey=%s, sortKey=%s, value=%s", - kv1.getKey(), kv3.getKey(), kv3.getValue()), - kv4); - Assert.assertEquals( - String.format("Diff: hashKey=%s, dataSortKey=%s, dataValue=%s, baseSortKey=%s, baseValue=%s", - kv1.getKey(), kv3.getKey(), kv3.getValue(), kv4.getKey(), kv4.getValue()), - kv3, kv4); - } + sortMap.put(sortKey, value); + } + + private static void checkAndPutSortMap( + TreeMap data, String hashKey, String sortKey, String value) { + Assert.assertNull( + String.format( + "Duplicate: hashKey=%s, sortKye=%s, oldValue=%s, newValue=%s", + hashKey, sortKey, data.get(sortKey), value), + data.get(sortKey)); + data.put(sortKey, value); + } + + private static void compare( + TreeMap> data, + TreeMap> base) { + Iterator>> iterator1 = data.entrySet().iterator(); + Iterator>> iterator2 = base.entrySet().iterator(); + while (true) { + Map.Entry> kv1 = + iterator1.hasNext() ? iterator1.next() : null; + Map.Entry> kv2 = + iterator2.hasNext() ? iterator2.next() : null; + if (kv1 == null) { + Assert.assertNull( + kv2 == null ? null : String.format("Only in base: hashKey=%s", kv2.getKey()), kv2); + break; + } + Assert.assertNotNull(String.format("Only in data: hashKey=%s", kv1.getKey()), kv2); + Assert.assertEquals( + String.format("Diff: dataHashKey=%s, baseHashKey=%s", kv1.getKey(), kv2.getKey()), + kv1.getKey(), + kv2.getKey()); + Iterator> iterator3 = kv1.getValue().entrySet().iterator(); + Iterator> iterator4 = kv2.getValue().entrySet().iterator(); + while (true) { + Map.Entry kv3 = iterator3.hasNext() ? iterator3.next() : null; + Map.Entry kv4 = iterator4.hasNext() ? iterator4.next() : null; + if (kv3 == null) { + Assert.assertNull( + kv4 == null + ? null + : String.format( + "Only in base: hashKey=%s, sortKey=%s, value=%s", + kv1.getKey(), kv4.getKey(), kv4.getValue()), + kv4); + break; } + Assert.assertNotNull( + String.format( + "Only in data: hashKey=%s, sortKey=%s, value=%s", + kv1.getKey(), kv3.getKey(), kv3.getValue()), + kv4); + Assert.assertEquals( + String.format( + "Diff: hashKey=%s, dataSortKey=%s, dataValue=%s, baseSortKey=%s, baseValue=%s", + kv1.getKey(), kv3.getKey(), kv3.getValue(), kv4.getKey(), kv4.getValue()), + kv3, + kv4); + } } - - private static void compareSortMap(NavigableMap data, NavigableMap base, - String hashKey) { - Iterator> iterator1 = data.entrySet().iterator(); - Iterator> iterator2 = base.entrySet().iterator(); - while(true) { - Map.Entry kv1 = iterator1.hasNext() ? iterator1.next() : null; - Map.Entry kv2 = iterator2.hasNext() ? iterator2.next() : null; - if (kv1 == null) { - Assert.assertNull( - kv2 == null ? null : String.format("Only in base: hashKey=%s, sortKey=%s, value=%s", - hashKey, kv2.getKey(), kv2.getValue()), - kv2); - break; - } - Assert.assertNotNull( String.format("Only in data: hashKey=%s, sortKey=%s, value=%s", - hashKey, kv1.getKey(), kv1.getValue()), - kv2); - Assert.assertEquals( - String.format("Diff: hashKey=%s, dataSortKey=%s, dataValue=%s, baseSortKey=%s, baseValue=%s", - hashKey, kv1.getKey(), kv1.getValue(), kv2.getKey(), kv2.getValue()), - kv1, kv2); - } + } + + private static void compareSortMap( + NavigableMap data, NavigableMap base, String hashKey) { + Iterator> iterator1 = data.entrySet().iterator(); + Iterator> iterator2 = base.entrySet().iterator(); + while (true) { + Map.Entry kv1 = iterator1.hasNext() ? iterator1.next() : null; + Map.Entry kv2 = iterator2.hasNext() ? iterator2.next() : null; + if (kv1 == null) { + Assert.assertNull( + kv2 == null + ? null + : String.format( + "Only in base: hashKey=%s, sortKey=%s, value=%s", + hashKey, kv2.getKey(), kv2.getValue()), + kv2); + break; + } + Assert.assertNotNull( + String.format( + "Only in data: hashKey=%s, sortKey=%s, value=%s", + hashKey, kv1.getKey(), kv1.getValue()), + kv2); + Assert.assertEquals( + String.format( + "Diff: hashKey=%s, dataSortKey=%s, dataValue=%s, baseSortKey=%s, baseValue=%s", + hashKey, kv1.getKey(), kv1.getValue(), kv2.getKey(), kv2.getValue()), + kv1, + kv2); } + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestTimeout.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestTimeout.java index c86581cc..94b6d792 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestTimeout.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestTimeout.java @@ -6,36 +6,46 @@ import org.junit.*; public class TestTimeout { - @Test - public void testTimeout() throws PException { - PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); - String tableName = "temp"; + @Test + public void testTimeout() throws PException { + PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); + String tableName = "temp"; - for (int i=0; i<1; ++i) { - byte[] hashKey = "hello".getBytes(); - byte[] sortKey = "0".getBytes(); - byte[] value = ("world" + String.valueOf(i)).getBytes(); + for (int i = 0; i < 1; ++i) { + byte[] hashKey = "hello".getBytes(); + byte[] sortKey = "0".getBytes(); + byte[] value = ("world" + String.valueOf(i)).getBytes(); - long current = System.currentTimeMillis(); - try { - client.set(tableName, hashKey, sortKey, value, 0); - System.out.println("set succeed, time takes: " + (System.currentTimeMillis() - current) + " ms"); - } catch (PException e) { - System.out.println("set encounter exception, time takes: " + (System.currentTimeMillis() - current) + " ms, " + e.getMessage()); - } - - current = System.currentTimeMillis(); - try { - byte[] result = client.get(tableName, hashKey, sortKey); - System.out.println("get succeed, time takes: " + (System.currentTimeMillis() - current) + " ms"); - if (result != null) { - System.out.println("get result: " + new String(result)); - } - } catch (PException e) { - System.out.println("get encounter exception, time takes: " + (System.currentTimeMillis() - current) + " ms, " + e.getMessage()); - e.printStackTrace(); - } + long current = System.currentTimeMillis(); + try { + client.set(tableName, hashKey, sortKey, value, 0); + System.out.println( + "set succeed, time takes: " + (System.currentTimeMillis() - current) + " ms"); + } catch (PException e) { + System.out.println( + "set encounter exception, time takes: " + + (System.currentTimeMillis() - current) + + " ms, " + + e.getMessage()); + } + + current = System.currentTimeMillis(); + try { + byte[] result = client.get(tableName, hashKey, sortKey); + System.out.println( + "get succeed, time takes: " + (System.currentTimeMillis() - current) + " ms"); + if (result != null) { + System.out.println("get result: " + new String(result)); } - PegasusClientFactory.closeSingletonClient(); + } catch (PException e) { + System.out.println( + "get encounter exception, time takes: " + + (System.currentTimeMillis() - current) + + " ms, " + + e.getMessage()); + e.printStackTrace(); + } } + PegasusClientFactory.closeSingletonClient(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/metrics/MetricsPoolTest.java b/src/test/java/com/xiaomi/infra/pegasus/metrics/MetricsPoolTest.java index bafe8f14..435fe5a9 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/metrics/MetricsPoolTest.java +++ b/src/test/java/com/xiaomi/infra/pegasus/metrics/MetricsPoolTest.java @@ -12,142 +12,129 @@ import org.junit.Before; import org.junit.Test; -/** - * Created by weijiesun on 18-3-9. - */ +/** Created by weijiesun on 18-3-9. */ public class MetricsPoolTest { - @Before - public void before() { - r = new MetricRegistry(); + @Before + public void before() { + r = new MetricRegistry(); + } + + @Test + public void genJsonsFromMeter() throws Exception { + String host = "simple-test-host.bj"; + String tags = "what=you,like=another"; + MetricsPool pool = new MetricsPool(host, tags, 20); + Meter m = r.meter("TestName"); + + m.mark(1); + m.mark(1); + + StringBuilder builder = new StringBuilder(); + pool.genJsonsFromMeter("TestName", m, builder); + + JSONArray array = new JSONArray("[" + builder.toString() + "]"); + Assert.assertEquals(4, array.length()); + + String[] metrics = { + "TestName.cps-1sec", "TestName.cps-1min", "TestName.cps-5min", "TestName.cps-15min" + }; + + for (int i = 0; i < array.length(); ++i) { + JSONObject j = array.getJSONObject(i); + + Assert.assertEquals(tags, j.getString("tags")); + Assert.assertEquals(metrics[i], j.getString("metric")); + Assert.assertEquals("GAUGE", j.getString("counterType")); + Assert.assertEquals(20, j.getInt("step")); + Assert.assertEquals(host, j.getString("endpoint")); } + } - @Test - public void genJsonsFromMeter() throws Exception { - String host = "simple-test-host.bj"; - String tags = "what=you,like=another"; - MetricsPool pool = new MetricsPool(host, tags, 20); - Meter m = r.meter("TestName"); - - m.mark(1); - m.mark(1); - - StringBuilder builder = new StringBuilder(); - pool.genJsonsFromMeter("TestName", m, builder); - - JSONArray array = new JSONArray("[" + builder.toString() + "]"); - Assert.assertEquals(4, array.length()); - - String[] metrics = { - "TestName.cps-1sec", - "TestName.cps-1min", - "TestName.cps-5min", - "TestName.cps-15min" - }; - - for (int i=0; i> callbacks = new ArrayList>(); - for (int i=0; i<1000; ++i) { - query_cfg_request req = new query_cfg_request("temp", new ArrayList()); - final client_operator op = new query_cfg_operator(new gpid(-1, -1), req); - FutureTask callback = new FutureTask(new Callable() { + ArrayList> callbacks = new ArrayList>(); + for (int i = 0; i < 1000; ++i) { + query_cfg_request req = new query_cfg_request("temp", new ArrayList()); + final client_operator op = new query_cfg_operator(new gpid(-1, -1), req); + FutureTask callback = + new FutureTask( + new Callable() { @Override public Void call() throws Exception { - Assert.assertEquals(error_code.error_types.ERR_OK, op.rpc_error.errno); - return null; + Assert.assertEquals(error_code.error_types.ERR_OK, op.rpc_error.errno); + return null; } - }); - callbacks.add(callback); - session.asyncQuery(op, callback, 10); - } - - Toollet.closeServer(addr); - for (FutureTask cb: callbacks) { - try { - Tools.waitUninterruptable(cb, Integer.MAX_VALUE); - } catch (ExecutionException e) { - e.printStackTrace(); - Assert.fail(); - } - } + }); + callbacks.add(callback); + session.asyncQuery(op, callback, 10); + } - manager.close(); + Toollet.closeServer(addr); + for (FutureTask cb : callbacks) { + try { + Tools.waitUninterruptable(cb, Integer.MAX_VALUE); + } catch (ExecutionException e) { + e.printStackTrace(); + Assert.fail(); + } } + + manager.close(); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/rpc/async/ReplicaSessionTest.java b/src/test/java/com/xiaomi/infra/pegasus/rpc/async/ReplicaSessionTest.java index 3ece8708..6db97f3d 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/rpc/async/ReplicaSessionTest.java +++ b/src/test/java/com/xiaomi/infra/pegasus/rpc/async/ReplicaSessionTest.java @@ -3,160 +3,161 @@ // can be found in the LICENSE file in the root directory of this source tree. package com.xiaomi.infra.pegasus.rpc.async; +import com.xiaomi.infra.pegasus.apps.*; +import com.xiaomi.infra.pegasus.base.blob; import com.xiaomi.infra.pegasus.base.error_code; import com.xiaomi.infra.pegasus.base.rpc_address; -import com.xiaomi.infra.pegasus.base.blob; +import com.xiaomi.infra.pegasus.operator.*; import com.xiaomi.infra.pegasus.thrift.protocol.TMessage; import com.xiaomi.infra.pegasus.tools.Toollet; import com.xiaomi.infra.pegasus.tools.Tools; -import com.xiaomi.infra.pegasus.apps.*; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.Before; -import org.junit.After; - -import com.xiaomi.infra.pegasus.operator.*; -import org.slf4j.Logger; - import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; -/** -* ReplicaSession Tester. -* -* @author sunweijie@xiaomi.com -* @version 1.0 -*/ +/** + * ReplicaSession Tester. + * + * @author sunweijie@xiaomi.com + * @version 1.0 + */ public class ReplicaSessionTest { - private String[] metaList = {"127.0.0.1:34601", "127.0.0.1:34602", "127.0.0.1:34603"}; - private final Logger logger = org.slf4j.LoggerFactory.getLogger(ReplicaSessionTest.class); - private ClusterManager manager; - - @Before - public void before() throws Exception { - manager = new ClusterManager(1000, 1, false, - null, 60, metaList); - } - - @After - public void after() throws Exception { - manager.close(); - } - - /** - * Method: connect() - */ - @Test - public void testConnect() throws Exception { - //test1: connect to a invalid address - rpc_address addr = new rpc_address(); - addr.fromString("127.0.0.1:12345"); - ReplicaSession rs = manager.getReplicaSession(addr); - - ArrayList > callbacks = new ArrayList>(); - - for (int i=0; i<100; ++i) { - final client_operator op = new rrdb_put_operator(new com.xiaomi.infra.pegasus.base.gpid(-1, -1), - "", - null); - final FutureTask cb = new FutureTask(new Callable() { + private String[] metaList = {"127.0.0.1:34601", "127.0.0.1:34602", "127.0.0.1:34603"}; + private final Logger logger = org.slf4j.LoggerFactory.getLogger(ReplicaSessionTest.class); + private ClusterManager manager; + + @Before + public void before() throws Exception { + manager = new ClusterManager(1000, 1, false, null, 60, metaList); + } + + @After + public void after() throws Exception { + manager.close(); + } + + /** Method: connect() */ + @Test + public void testConnect() throws Exception { + // test1: connect to a invalid address + rpc_address addr = new rpc_address(); + addr.fromString("127.0.0.1:12345"); + ReplicaSession rs = manager.getReplicaSession(addr); + + ArrayList> callbacks = new ArrayList>(); + + for (int i = 0; i < 100; ++i) { + final client_operator op = + new rrdb_put_operator(new com.xiaomi.infra.pegasus.base.gpid(-1, -1), "", null); + final FutureTask cb = + new FutureTask( + new Callable() { @Override public Void call() throws Exception { - Assert.assertEquals(error_code.error_types.ERR_SESSION_RESET, op.rpc_error.errno); - return null; + Assert.assertEquals(error_code.error_types.ERR_SESSION_RESET, op.rpc_error.errno); + return null; } - }); - - callbacks.add(cb); - rs.asyncSend(op, cb, 1000); - } - - for (FutureTask cb: callbacks) { - try { - Tools.waitUninterruptable(cb, Integer.MAX_VALUE); - } catch (ExecutionException e) { - Assert.fail(); - } - } - - final ReplicaSession cp_rs = rs; - Toollet.waitCondition(new Toollet.BoolCallable() { - @Override - public boolean call() { - return ReplicaSession.ConnState.DISCONNECTED==cp_rs.getState(); - } - }, 5); - - //test2: connect to an valid address, and then close the server - addr.fromString("127.0.0.1:34801"); - callbacks.clear(); - - rs = manager.getReplicaSession(addr); - rs.setMessageResponseFilter(new ReplicaSession.MessageResponseFilter() { - @Override - public boolean abandonIt(error_code.error_types err, TMessage header) { - return true; - } + }); + + callbacks.add(cb); + rs.asyncSend(op, cb, 1000); + } + + for (FutureTask cb : callbacks) { + try { + Tools.waitUninterruptable(cb, Integer.MAX_VALUE); + } catch (ExecutionException e) { + Assert.fail(); + } + } + + final ReplicaSession cp_rs = rs; + Toollet.waitCondition( + new Toollet.BoolCallable() { + @Override + public boolean call() { + return ReplicaSession.ConnState.DISCONNECTED == cp_rs.getState(); + } + }, + 5); + + // test2: connect to an valid address, and then close the server + addr.fromString("127.0.0.1:34801"); + callbacks.clear(); + + rs = manager.getReplicaSession(addr); + rs.setMessageResponseFilter( + new ReplicaSession.MessageResponseFilter() { + @Override + public boolean abandonIt(error_code.error_types err, TMessage header) { + return true; + } }); - for (int i=0; i<20; ++i) { - // we send query request to replica server. We expect it to discard it. - final int index = i; - update_request req = new update_request( - new blob("hello".getBytes()), - new blob("world".getBytes()), - 0); - - final client_operator op = new Toollet.test_operator(new com.xiaomi.infra.pegasus.base.gpid(-1, -1), req); - final rpc_address cp_addr = addr; - final FutureTask cb = new FutureTask(new Callable() { + for (int i = 0; i < 20; ++i) { + // we send query request to replica server. We expect it to discard it. + final int index = i; + update_request req = + new update_request(new blob("hello".getBytes()), new blob("world".getBytes()), 0); + + final client_operator op = + new Toollet.test_operator(new com.xiaomi.infra.pegasus.base.gpid(-1, -1), req); + final rpc_address cp_addr = addr; + final FutureTask cb = + new FutureTask( + new Callable() { @Override public Void call() throws Exception { - Assert.assertEquals(error_code.error_types.ERR_TIMEOUT, op.rpc_error.errno); - // for the last request, we kill the server - if (index == 19) { - Toollet.closeServer(cp_addr); - } - return null; + Assert.assertEquals(error_code.error_types.ERR_TIMEOUT, op.rpc_error.errno); + // for the last request, we kill the server + if (index == 19) { + Toollet.closeServer(cp_addr); + } + return null; } - }); - - callbacks.add(cb); - rs.asyncSend(op, cb, 500); - } - - for (int i=0; i<80; ++i) { - // then we still send query request to replica server. But the timeout is longer. - update_request req = new update_request( - new blob("hello".getBytes()), - new blob("world".getBytes()), - 0); - final client_operator op = new Toollet.test_operator(new com.xiaomi.infra.pegasus.base.gpid(-1, -1), req); - final FutureTask cb = new FutureTask(new Callable() { + }); + + callbacks.add(cb); + rs.asyncSend(op, cb, 500); + } + + for (int i = 0; i < 80; ++i) { + // then we still send query request to replica server. But the timeout is longer. + update_request req = + new update_request(new blob("hello".getBytes()), new blob("world".getBytes()), 0); + final client_operator op = + new Toollet.test_operator(new com.xiaomi.infra.pegasus.base.gpid(-1, -1), req); + final FutureTask cb = + new FutureTask( + new Callable() { @Override public Void call() throws Exception { - Assert.assertEquals(error_code.error_types.ERR_SESSION_RESET, op.rpc_error.errno); - return null; + Assert.assertEquals(error_code.error_types.ERR_SESSION_RESET, op.rpc_error.errno); + return null; } - }); - - callbacks.add(cb); - //these requests have longer timeout, so they should be responsed later than the server is killed - rs.asyncSend(op, cb, 2000); - } - - for (FutureTask cb: callbacks) { - try { - Tools.waitUninterruptable(cb, Integer.MAX_VALUE); - } catch (ExecutionException e) { - e.printStackTrace(); - Assert.fail(); - } - } - rs.setMessageResponseFilter(null); - - Toollet.tryStartServer(addr); + }); + + callbacks.add(cb); + // these requests have longer timeout, so they should be responsed later than the server is + // killed + rs.asyncSend(op, cb, 2000); } + + for (FutureTask cb : callbacks) { + try { + Tools.waitUninterruptable(cb, Integer.MAX_VALUE); + } catch (ExecutionException e) { + e.printStackTrace(); + Assert.fail(); + } + } + rs.setMessageResponseFilter(null); + + Toollet.tryStartServer(addr); + } } diff --git a/src/test/java/com/xiaomi/infra/pegasus/rpc/async/TableHandlerTest.java b/src/test/java/com/xiaomi/infra/pegasus/rpc/async/TableHandlerTest.java index c66272c2..fa52fcc0 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/rpc/async/TableHandlerTest.java +++ b/src/test/java/com/xiaomi/infra/pegasus/rpc/async/TableHandlerTest.java @@ -1,190 +1,183 @@ // Copyright (c) 2017, Xiaomi, Inc. All rights reserved. // This source code is licensed under the Apache License Version 2.0, which // can be found in the LICENSE file in the root directory of this source tree. -package com.xiaomi.infra.pegasus.rpc.async; - -import com.xiaomi.infra.pegasus.rpc.KeyHasher; -import com.xiaomi.infra.pegasus.rpc.ReplicationException; +package com.xiaomi.infra.pegasus.rpc.async; import com.xiaomi.infra.pegasus.base.error_code; import com.xiaomi.infra.pegasus.base.error_code.error_types; import com.xiaomi.infra.pegasus.base.rpc_address; import com.xiaomi.infra.pegasus.operator.*; - +import com.xiaomi.infra.pegasus.rpc.KeyHasher; +import com.xiaomi.infra.pegasus.rpc.ReplicationException; import com.xiaomi.infra.pegasus.rpc.async.TableHandler.ReplicaConfiguration; import com.xiaomi.infra.pegasus.tools.Toollet; - +import java.util.ArrayList; +import org.junit.After; import org.junit.Assert; -import org.junit.Test; import org.junit.Before; -import org.junit.After; +import org.junit.Test; import org.slf4j.Logger; -import java.util.ArrayList; - /** -* TableHandler Tester. -* -* @author sunweijie@xiaomi.com -* @version 1.0 -*/ + * TableHandler Tester. + * + * @author sunweijie@xiaomi.com + * @version 1.0 + */ public class TableHandlerTest { - private static final Logger logger = org.slf4j.LoggerFactory.getLogger(TableHandlerTest.class); + private static final Logger logger = org.slf4j.LoggerFactory.getLogger(TableHandlerTest.class); + + private String[] addr_list = {"127.0.0.1:34601", "127.0.0.1:34602", "127.0.0.1:34603"}; + private String[] replica_servers = {"127.0.0.1:34801", "127.0.0.1:34802", "127.0.01:34803"}; - private String[] addr_list = {"127.0.0.1:34601", "127.0.0.1:34602", "127.0.0.1:34603"}; - private String[] replica_servers = {"127.0.0.1:34801", "127.0.0.1:34802", "127.0.01:34803"}; + private ClusterManager testManager; - private ClusterManager testManager; + @Before + public void before() throws Exception { + testManager = new ClusterManager(1000, 1, false, null, 60, addr_list); + } - @Before - public void before() throws Exception { - testManager = new ClusterManager(1000, 1, false, - null, 60, addr_list); + @After + public void after() throws Exception {} + + private rpc_address getValidWrongServer(final rpc_address right_address) { + ArrayList replicas = new ArrayList(); + for (int i = 0; i < replica_servers.length; ++i) { + rpc_address a = new rpc_address(); + boolean ans = a.fromString(replica_servers[i]); + assert ans; + + if (a.get_port() != right_address.get_port()) replicas.add(a); } - @After - public void after() throws Exception { + int p = (int) (Math.random() * replicas.size()); + return replicas.get(p); + } + + /** Method: operate(client_operator op) */ + @Test + public void testOperateOp() throws Exception { + System.out.println("test synchronized opearate"); + TableHandler table = null; + try { + table = testManager.openTable("temp", KeyHasher.DEFAULT); + } catch (ReplicationException e) { + Assert.fail(); } + Assert.assertNotNull(table); + + com.xiaomi.infra.pegasus.apps.update_request request = + new com.xiaomi.infra.pegasus.apps.update_request(); + request.key = new com.xiaomi.infra.pegasus.base.blob("hello".getBytes()); + request.value = new com.xiaomi.infra.pegasus.base.blob("value".getBytes()); + final com.xiaomi.infra.pegasus.base.gpid pid = table.getGpid(request.key.data); - private rpc_address getValidWrongServer(final rpc_address right_address) { - ArrayList replicas = new ArrayList(); - for (int i=0; i 1) { - try { - process_id = Integer.valueOf(words[1]); - } catch (Throwable ex) { - } - } - if (process_id == -1) { - logger.warn("can not get process id from {}", line); - throw new IllegalArgumentException("Toollet initialize"); - } - break; - } + private static final Logger logger = org.slf4j.LoggerFactory.getLogger(Toollet.class); + private static String PegasusRunScriptPath; + + static { + try { + Process process = + Runtime.getRuntime().exec(new String[] {"bash", "-c", "ps aux | grep pegasus_server"}); + process.waitFor(); + BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + int process_id = -1; + while ((line = input.readLine()) != null) { + if (line.contains("pegasus_server") && line.contains("meta")) { + String[] words = line.split("\\s+"); + if (words.length > 1) { + try { + process_id = Integer.valueOf(words[1]); + } catch (Throwable ex) { } - - String get_pegasus_dir = String.format("readlink /proc/%d/cwd", process_id); - process = Runtime.getRuntime().exec(get_pegasus_dir); - process.waitFor(); - input = new BufferedReader(new InputStreamReader(process.getInputStream())); - line = input.readLine().trim(); - PegasusRunScriptPath = line + "/../../"; - input.close(); - - } catch (IOException ex) { - logger.warn(ex.getMessage()); - } catch (InterruptedException e) { - logger.warn(e.getMessage()); + } + if (process_id == -1) { + logger.warn("can not get process id from {}", line); + throw new IllegalArgumentException("Toollet initialize"); + } + break; } + } + + String get_pegasus_dir = String.format("readlink /proc/%d/cwd", process_id); + process = Runtime.getRuntime().exec(get_pegasus_dir); + process.waitFor(); + input = new BufferedReader(new InputStreamReader(process.getInputStream())); + line = input.readLine().trim(); + PegasusRunScriptPath = line + "/../../"; + input.close(); + + } catch (IOException ex) { + logger.warn(ex.getMessage()); + } catch (InterruptedException e) { + logger.warn(e.getMessage()); } - - public static boolean tryExecuteCommand(String command) { - try { - Process process = Runtime.getRuntime().exec(new String[]{"bash", "-c", command}); - int result = process.waitFor(); - if (0 != result) { - logger.warn("exec command {} failed, error code {}", command, result); - return false; - } - return true; - } catch (IOException e) { - logger.warn(e.getMessage()); - return false; - } catch (InterruptedException e) { - logger.warn(e.getMessage()); - return false; - } + } + + public static boolean tryExecuteCommand(String command) { + try { + Process process = Runtime.getRuntime().exec(new String[] {"bash", "-c", command}); + int result = process.waitFor(); + if (0 != result) { + logger.warn("exec command {} failed, error code {}", command, result); + return false; + } + return true; + } catch (IOException e) { + logger.warn(e.getMessage()); + return false; + } catch (InterruptedException e) { + logger.warn(e.getMessage()); + return false; } - - public static boolean closeServer(rpc_address server) { - String command = String.format( - "netstat -anp 2>/dev/null | " + - "grep %d | grep LISTEN | " + - "awk '{print $7}' | " + - "cut -d \"/\" -f 1 | " + - "xargs kill -9", server.get_port()); - return tryExecuteCommand(command); + } + + public static boolean closeServer(rpc_address server) { + String command = + String.format( + "netstat -anp 2>/dev/null | " + + "grep %d | grep LISTEN | " + + "awk '{print $7}' | " + + "cut -d \"/\" -f 1 | " + + "xargs kill -9", + server.get_port()); + return tryExecuteCommand(command); + } + + public static boolean tryStartServer(rpc_address server) { + String role; + int index; + + if (server.get_port() < 34800) { + // so this is meta server + assert server.get_port() > 34600; + role = "-m"; + index = server.get_port() - 34600; + } else { + // so this is replica server + assert server.get_port() < 34900; + role = "-r"; + index = server.get_port() - 34800; } - public static boolean tryStartServer(rpc_address server) { - String role; - int index; - - if (server.get_port() < 34800) { - // so this is meta server - assert server.get_port() > 34600; - role = "-m"; - index = server.get_port() - 34600; - } else { - // so this is replica server - assert server.get_port() < 34900; - role = "-r"; - index = server.get_port() - 34800; - } - - String startCommand = String.format("pushd %s && ./run.sh start_onebox_instance %s %d && popd", PegasusRunScriptPath, role, index); - boolean ans = tryExecuteCommand(startCommand); - try { - logger.info("sleep for a while for a killed server to recover"); - Thread.sleep(15000); - } catch (Throwable e) { - e.printStackTrace(); - } - - return ans; + String startCommand = + String.format( + "pushd %s && ./run.sh start_onebox_instance %s %d && popd", + PegasusRunScriptPath, role, index); + boolean ans = tryExecuteCommand(startCommand); + try { + logger.info("sleep for a while for a killed server to recover"); + Thread.sleep(15000); + } catch (Throwable e) { + e.printStackTrace(); } - public static boolean waitCondition(BoolCallable callable, int seconds) { - do { - if (callable.call()) - return true; - try { - Thread.sleep(1000l); - } catch (Throwable e) { - e.printStackTrace(); - } - seconds--; - } while (seconds > 0); - return false; + return ans; + } + + public static boolean waitCondition(BoolCallable callable, int seconds) { + do { + if (callable.call()) return true; + try { + Thread.sleep(1000l); + } catch (Throwable e) { + e.printStackTrace(); + } + seconds--; + } while (seconds > 0); + return false; + } + + public interface BoolCallable { + boolean call(); + } + + public static class test_operator extends rrdb_put_operator { + public test_operator(gpid gpid, update_request request) { + super(gpid, "", request); } - public interface BoolCallable { - boolean call(); + public void send_data(com.xiaomi.infra.pegasus.thrift.protocol.TProtocol oprot, int seqid) + throws TException { + TMessage msg = new TMessage("RPC_RRDB_RRDB_TEST_PUT", TMessageType.CALL, seqid); + oprot.writeMessageBegin(msg); + rrdb.put_args put_args = new rrdb.put_args(req); + put_args.write(oprot); + oprot.writeMessageEnd(); } - public static class test_operator extends rrdb_put_operator { - public test_operator(gpid gpid, update_request request) { - super(gpid, "", request); - } - - public void send_data(com.xiaomi.infra.pegasus.thrift.protocol.TProtocol oprot, int seqid) throws TException { - TMessage msg = new TMessage("RPC_RRDB_RRDB_TEST_PUT", TMessageType.CALL, seqid); - oprot.writeMessageBegin(msg); - rrdb.put_args put_args = new rrdb.put_args(req); - put_args.write(oprot); - oprot.writeMessageEnd(); - } - - public void recv_data(TProtocol iprot) throws TException { - rrdb.put_result result = new rrdb.put_result(); - result.read(iprot); - if (result.isSetSuccess()) - resp = result.success; - else - throw new com.xiaomi.infra.pegasus.thrift.TApplicationException( - com.xiaomi.infra.pegasus.thrift.TApplicationException.MISSING_RESULT, - "put failed: unknown result"); - } - - private update_request req; - private update_response resp; + public void recv_data(TProtocol iprot) throws TException { + rrdb.put_result result = new rrdb.put_result(); + result.read(iprot); + if (result.isSetSuccess()) resp = result.success; + else + throw new com.xiaomi.infra.pegasus.thrift.TApplicationException( + com.xiaomi.infra.pegasus.thrift.TApplicationException.MISSING_RESULT, + "put failed: unknown result"); } + + private update_request req; + private update_response resp; + } }