Skip to content

Commit

Permalink
HDDS-11546. Add regex operation to filter option of ldb scan command. (
Browse files Browse the repository at this point in the history
…apache#7289)

(cherry picked from commit 256aad9)
  • Loading branch information
Tejaskriya authored Oct 10, 2024
1 parent a353f21 commit 68a9fd1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
10 changes: 10 additions & 0 deletions hadoop-ozone/dist/src/main/smoketest/debug/ozone-debug-ldb.robot
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ Test ozone debug ldb scan with filter option success
Should not contain ${output} testfile1
Should not contain ${output} testfile2
Should not contain ${output} testfile3
# test filter option with regex matching numbers
${output} = Execute ozone debug ldb --db=/data/metadata/om.db scan --cf=keyTable --filter="dataSize:regex:^1[0-2]{3}$"
Should contain ${output} testfile1
Should not contain ${output} testfile2
Should not contain ${output} testfile3
# test filter option with regex matching string
${output} = Execute ozone debug ldb --db=/data/metadata/om.db scan --cf=keyTable --filter="keyName:regex:^test.*[0-1]$"
Should contain ${output} testfile1
Should not contain ${output} testfile2
Should not contain ${output} testfile3

Test ozone debug ldb scan with filter option failure
# test filter option with invalid operator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void shutdown() throws IOException {
/**
* Defines ldb tool test cases.
*/
@SuppressWarnings({"methodlength"})
private static Stream<Arguments> scanTestCases() {
return Stream.of(
Arguments.of(
Expand Down Expand Up @@ -206,6 +207,19 @@ private static Stream<Arguments> scanTestCases() {
Named.of("Filter dataSize>2000", Arrays.asList("--filter", "dataSize:greater:2000")),
Named.of("Expect empty result", null)
),
Arguments.of(
Named.of(KEY_TABLE, Pair.of(KEY_TABLE, false)),
Named.of("Default", Pair.of(0, "")),
Named.of("Filter key3 regex", Arrays.asList("--filter", "keyName:regex:^.*3$")),
Named.of("Expect key3", Pair.of("key3", "key4"))
),
Arguments.of(
Named.of(KEY_TABLE, Pair.of(KEY_TABLE, false)),
Named.of("Default", Pair.of(0, "")),
Named.of("Filter keys whose dataSize digits start with 5 using regex",
Arrays.asList("--filter", "dataSize:regex:^5.*$")),
Named.of("Expect empty result", null)
),
Arguments.of(
Named.of(BLOCK_DATA + " V3", Pair.of(BLOCK_DATA, true)),
Named.of("Default", Pair.of(0, "")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand Down Expand Up @@ -132,9 +134,11 @@ public class DBScanner implements Callable<Void>, SubcommandWithParent {
@CommandLine.Option(names = {"--filter"},
description = "Comma-separated list of \"<field>:<operator>:<value>\" where " +
"<field> is any valid field of the record, " +
"<operator> is (EQUALS,LESSER or GREATER) and " +
"<value> is the value of the field. " +
"eg.) \"dataSize:equals:1000\" for showing records having the value 1000 for dataSize")
"<operator> is [EQUALS,LESSER, GREATER or REGEX]. (EQUALS compares the exact string, " +
"REGEX compares with a valid regular expression passed, and LESSER/GREATER works with numeric values), " +
"<value> is the value of the field. \n" +
"eg.) \"dataSize:equals:1000\" for showing records having the value 1000 for dataSize, \n" +
" \"keyName:regex:^key.*$\" for showing records having keyName that matches the given regex.")
private String filter;

@CommandLine.Option(names = {"--dnSchema", "--dn-schema", "-d"},
Expand Down Expand Up @@ -396,7 +400,6 @@ private boolean checkFilteredObject(Object obj, Class<?> clazz, Map<String, Filt
throw new IOException("Invalid filter passed");
} else if (fieldValue.getNextLevel() == null) {
// reached the end of fields hierarchy, check if they match the filter
// Currently, only equals operation is supported
try {
switch (fieldValue.getOperator()) {
case EQUALS:
Expand All @@ -416,8 +419,15 @@ private boolean checkFilteredObject(Object obj, Class<?> clazz, Map<String, Filt
return false;
}
break;
case REGEX:
Pattern p = Pattern.compile(String.valueOf(fieldValue.getValue()));
Matcher m = p.matcher(String.valueOf(valueObject));
if (!m.find()) {
return false;
}
break;
default:
err().println("Only EQUALS/LESSER/GREATER operator is supported currently.");
err().println("Only EQUALS/LESSER/GREATER/REGEX operator is supported currently.");
throw new IOException("Invalid filter passed");
}
} catch (NumberFormatException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public FilterOperator getFilterOperator(String op) {
return FilterOperator.GREATER;
} else if (op.equalsIgnoreCase("LESSER")) {
return FilterOperator.LESSER;
} else if (op.equalsIgnoreCase("REGEX")) {
return FilterOperator.REGEX;
} else {
return null;
}
Expand All @@ -102,6 +104,7 @@ public String toString() {
public enum FilterOperator {
EQUALS,
LESSER,
GREATER;
GREATER,
REGEX;
}
}
Binary file added lib/gson-2.10.1.jar
Binary file not shown.

0 comments on commit 68a9fd1

Please sign in to comment.