diff --git a/pom.xml b/pom.xml index 0732fece..eedad5b9 100644 --- a/pom.xml +++ b/pom.xml @@ -8,9 +8,15 @@ Pegasus Java Client - junit - junit - 4.8.1 + org.junit.jupiter + junit-jupiter-engine + 5.3.2 + test + + + org.junit.platform + junit-platform-runner + 1.3.0 test diff --git a/src/main/java/com/xiaomi/infra/pegasus/client/DelRangeOptions.java b/src/main/java/com/xiaomi/infra/pegasus/client/DelRangeOptions.java index 98266a64..84a60b3d 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/client/DelRangeOptions.java +++ b/src/main/java/com/xiaomi/infra/pegasus/client/DelRangeOptions.java @@ -4,7 +4,7 @@ package com.xiaomi.infra.pegasus.client; public class DelRangeOptions { - public String nextSortKey = ""; + public byte[] nextSortKey = null; public boolean startInclusive = true; // if the startSortKey is included public boolean stopInclusive = false; // if the stopSortKey is included public FilterType sortKeyFilterType = FilterType.FT_NO_FILTER; // filter type for sort key diff --git a/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java b/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java index ecec2f9d..15f64e43 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java +++ b/src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java @@ -1455,6 +1455,9 @@ public void multiDel(byte[] hashKey, List sortKeys, int timeout) throws public void delRange( byte[] hashKey, byte[] startSortKey, byte[] stopSortKey, DelRangeOptions options, int timeout) throws PException { + if (hashKey == null || hashKey.length == 0) { + throw new PException("Invalid parameter: hash key can't be empty"); + } if (timeout <= 0) timeout = defaultTimeout; long startTime = System.currentTimeMillis(); long lastCheckTime = startTime; @@ -1469,18 +1472,20 @@ public void delRange( scanOptions.sortKeyFilterType = options.sortKeyFilterType; scanOptions.sortKeyFilterPattern = options.sortKeyFilterPattern; - options.nextSortKey = new String(startSortKey); + options.nextSortKey = startSortKey; PegasusScannerInterface pegasusScanner = getScanner(hashKey, startSortKey, stopSortKey, scanOptions); lastCheckTime = System.currentTimeMillis(); if (lastCheckTime >= deadlineTime) { + String startSortKeyStr = startSortKey == null ? "" : new String(startSortKey); + String stopSortKeyStr = stopSortKey == null ? "" : new String(stopSortKey); throw new PException( "Getting pegasusScanner takes too long time when delete hashKey:" + new String(hashKey) + ",startSortKey:" - + new String(startSortKey) + + startSortKeyStr + ",stopSortKey:" - + new String(stopSortKey) + + stopSortKeyStr + ",timeUsed:" + (lastCheckTime - startTime) + ":", @@ -1494,7 +1499,7 @@ public void delRange( while ((pairs = pegasusScanner.next()) != null) { sortKeys.add(pairs.getKey().getValue()); if (sortKeys.size() == maxBatchDelCount) { - options.nextSortKey = new String(sortKeys.get(0)); + options.nextSortKey = sortKeys.get(0); asyncMultiDel(hashKey, sortKeys, remainingTime).get(remainingTime, TimeUnit.MILLISECONDS); lastCheckTime = System.currentTimeMillis(); remainingTime = (int) (deadlineTime - lastCheckTime); @@ -1510,11 +1515,12 @@ public void delRange( options.nextSortKey = null; } } catch (InterruptedException | ExecutionException e) { + String nextSortKeyStr = options.nextSortKey == null ? "" : new String(options.nextSortKey); throw new PException( "delRange of hashKey:" + new String(hashKey) + " from sortKey:" - + options.nextSortKey + + nextSortKeyStr + "[index:" + count * maxBatchDelCount + "]" 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 e877aec5..86aa56aa 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java @@ -12,7 +12,9 @@ import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.tuple.Pair; import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; /** Created by mi on 16-3-22. */ public class TestBasic { @@ -2364,124 +2366,147 @@ public void delRange() throws PException { PegasusClientInterface client = PegasusClientFactory.getSingletonClient(); DelRangeOptions delRangeOptions = new DelRangeOptions(); + String tableName = "temp"; + // multi set values List> values = new ArrayList>(); int count = 0; - try { - while (count < 150) { - values.add(Pair.of(("k_" + count).getBytes(), ("v_" + count).getBytes())); - count++; - } - client.multiSet("temp", "delRange".getBytes(), values); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - // delRange with default delRangeOptions - try { - client.delRange( - "temp", "delRange".getBytes(), "k_0".getBytes(), "k_90".getBytes(), delRangeOptions); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); + while (count < 150) { + values.add(Pair.of(("k_" + count).getBytes(), ("v_" + count).getBytes())); + count++; } - - Assert.assertTrue(delRangeOptions.nextSortKey == null); List remainingSortKey = new ArrayList(); - - remainingSortKey.add("k_90".getBytes()); - remainingSortKey.add("k_91".getBytes()); - remainingSortKey.add("k_92".getBytes()); - remainingSortKey.add("k_93".getBytes()); - remainingSortKey.add("k_94".getBytes()); - remainingSortKey.add("k_95".getBytes()); - remainingSortKey.add("k_96".getBytes()); - remainingSortKey.add("k_97".getBytes()); - remainingSortKey.add("k_98".getBytes()); - remainingSortKey.add("k_99".getBytes()); List> remainingValue = new ArrayList>(); - try { - client.multiGet("temp", "delRange".getBytes(), remainingSortKey, remainingValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } + Assertions.assertNull( + Assertions.assertDoesNotThrow( + () -> { + client.multiSet(tableName, "delRange".getBytes(), values); + client.delRange( + tableName, + "delRange".getBytes(), + "k_0".getBytes(), + "k_90".getBytes(), + delRangeOptions); + + remainingSortKey.add("k_90".getBytes()); + remainingSortKey.add("k_91".getBytes()); + remainingSortKey.add("k_92".getBytes()); + remainingSortKey.add("k_93".getBytes()); + remainingSortKey.add("k_94".getBytes()); + remainingSortKey.add("k_95".getBytes()); + remainingSortKey.add("k_96".getBytes()); + remainingSortKey.add("k_97".getBytes()); + remainingSortKey.add("k_98".getBytes()); + remainingSortKey.add("k_99".getBytes()); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + + return delRangeOptions.nextSortKey; + })); List valueStr = new ArrayList(); for (Pair pair : remainingValue) { valueStr.add(new String(pair.getValue())); } - Assert.assertEquals(10, valueStr.size()); - Assert.assertTrue(valueStr.contains("v_90")); - Assert.assertTrue(valueStr.contains("v_91")); - Assert.assertTrue(valueStr.contains("v_92")); - Assert.assertTrue(valueStr.contains("v_93")); - Assert.assertTrue(valueStr.contains("v_94")); - Assert.assertTrue(valueStr.contains("v_95")); - Assert.assertTrue(valueStr.contains("v_96")); - Assert.assertTrue(valueStr.contains("v_97")); - Assert.assertTrue(valueStr.contains("v_98")); - Assert.assertTrue(valueStr.contains("v_99")); + Assertions.assertEquals(10, valueStr.size()); + Assertions.assertTrue(valueStr.contains("v_90")); + Assertions.assertTrue(valueStr.contains("v_91")); + Assertions.assertTrue(valueStr.contains("v_92")); + Assertions.assertTrue(valueStr.contains("v_93")); + Assertions.assertTrue(valueStr.contains("v_94")); + Assertions.assertTrue(valueStr.contains("v_95")); + Assertions.assertTrue(valueStr.contains("v_96")); + Assertions.assertTrue(valueStr.contains("v_97")); + Assertions.assertTrue(valueStr.contains("v_98")); + Assertions.assertTrue(valueStr.contains("v_99")); + remainingValue.clear(); + valueStr.clear(); // delRange with FT_MATCH_POSTFIX option delRangeOptions.sortKeyFilterType = FilterType.FT_MATCH_POSTFIX; delRangeOptions.sortKeyFilterPattern = "k_93".getBytes(); - try { - client.delRange( - "temp", "delRange".getBytes(), "k_90".getBytes(), "k_95".getBytes(), delRangeOptions); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } - remainingValue.clear(); - valueStr.clear(); - try { - client.multiGet("temp", "delRange".getBytes(), remainingSortKey, remainingValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } + Assertions.assertDoesNotThrow( + () -> { + client.delRange( + tableName, + "delRange".getBytes(), + "k_90".getBytes(), + "k_95".getBytes(), + delRangeOptions); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + }); for (Pair pair : remainingValue) { valueStr.add(new String(pair.getValue())); } - - Assert.assertEquals(9, valueStr.size()); - Assert.assertTrue(!valueStr.contains("v_93")); + Assertions.assertEquals(9, valueStr.size()); + Assertions.assertTrue(!valueStr.contains("v_93")); + remainingValue.clear(); + valueStr.clear(); // delRange with "*Inclusive" option delRangeOptions.startInclusive = false; delRangeOptions.stopInclusive = true; delRangeOptions.sortKeyFilterType = FilterType.FT_NO_FILTER; delRangeOptions.sortKeyFilterPattern = null; - try { - client.delRange( - "temp", "delRange".getBytes(), "k_90".getBytes(), "k_95".getBytes(), delRangeOptions); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } + Assertions.assertDoesNotThrow( + () -> { + client.delRange( + tableName, + "delRange".getBytes(), + "k_90".getBytes(), + "k_95".getBytes(), + delRangeOptions); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + }); - remainingValue.clear(); - valueStr.clear(); - try { - client.multiGet("temp", "delRange".getBytes(), remainingSortKey, remainingValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.assertTrue(false); - } for (Pair pair : remainingValue) { valueStr.add(new String(pair.getValue())); } - Assert.assertEquals(5, valueStr.size()); - Assert.assertTrue(valueStr.contains("v_90")); - Assert.assertTrue(valueStr.contains("v_96")); - Assert.assertTrue(valueStr.contains("v_97")); - Assert.assertTrue(valueStr.contains("v_98")); - Assert.assertTrue(valueStr.contains("v_99")); + Assertions.assertEquals(5, valueStr.size()); + Assertions.assertTrue(valueStr.contains("v_90")); + Assertions.assertTrue(valueStr.contains("v_96")); + Assertions.assertTrue(valueStr.contains("v_97")); + Assertions.assertTrue(valueStr.contains("v_98")); + Assertions.assertTrue(valueStr.contains("v_99")); + remainingValue.clear(); + valueStr.clear(); + + DelRangeOptions delRangeOptions2 = new DelRangeOptions(); + // test hashKey can't be null or "" + Assertions.assertEquals( + "{version}: Invalid parameter: hash key can't be empty", + Assertions.assertThrows( + PException.class, + () -> { + client.delRange( + tableName, null, "k1".getBytes(), "k2".getBytes(), delRangeOptions2); + }) + .getMessage()); + + Assertions.assertEquals( + "{version}: Invalid parameter: hash key can't be empty", + Assertions.assertThrows( + PException.class, + () -> { + client.delRange( + tableName, "".getBytes(), "k1".getBytes(), "k2".getBytes(), delRangeOptions2); + }) + .getMessage()); + + // test sortKey can be null, means delete from first to last + Assertions.assertNull( + Assertions.assertDoesNotThrow( + () -> { + client.multiSet(tableName, "delRange".getBytes(), values); + client.delRange(tableName, "delRange".getBytes(), null, null, delRangeOptions2); + client.multiGet(tableName, "delRange".getBytes(), remainingSortKey, remainingValue); + return delRangeOptions2.nextSortKey; + })); + + Assertions.assertEquals(remainingValue.size(), 0); } @Test