Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
fix: startSortKey can't pass null (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
foreverneverer authored May 21, 2020
1 parent 67d48d6 commit 717eefe
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 96 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
<name>Pegasus Java Client</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/xiaomi/infra/pegasus/client/PegasusTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,9 @@ public void multiDel(byte[] hashKey, List<byte[]> 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;
Expand All @@ -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)
+ ":",
Expand All @@ -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);
Expand All @@ -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
+ "]"
Expand Down
199 changes: 112 additions & 87 deletions src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -2364,124 +2366,147 @@ public void delRange() throws PException {
PegasusClientInterface client = PegasusClientFactory.getSingletonClient();
DelRangeOptions delRangeOptions = new DelRangeOptions();

String tableName = "temp";

// multi set values
List<Pair<byte[], byte[]>> values = new ArrayList<Pair<byte[], byte[]>>();
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<byte[]> remainingSortKey = new ArrayList<byte[]>();

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<Pair<byte[], byte[]>> remainingValue = new ArrayList<Pair<byte[], byte[]>>();

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<String> valueStr = new ArrayList<String>();
for (Pair<byte[], byte[]> 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<byte[], byte[]> 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<byte[], byte[]> 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
Expand Down

0 comments on commit 717eefe

Please sign in to comment.