Skip to content

Commit

Permalink
Refactoring FilterPath.parse by using an iterative approach instead o…
Browse files Browse the repository at this point in the history
…f recursion. (#14200) (#14629)

* Refactor FilterPath parse function (#12067)


* Implement unit tests for FilterPathTests (#12067)


* Write warn log if Filter is empty; Add comments (#12067)


* Add changelog



* Remove unnecessary log statement



* Remove unused logger



* Spotless apply



* Remove incorrect changelog



---------



(cherry picked from commit 0742453)

Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Robin Friedmann <robinfriedmann.rf@gmail.com>
  • Loading branch information
3 people committed Jul 2, 2024
1 parent 6feaa4a commit 489a942
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix FuzzyQuery in keyword field will use IndexOrDocValuesQuery when both of index and doc_value are true ([#14378](https://github.com/opensearch-project/OpenSearch/pull/14378))
- Fix file cache initialization ([#14004](https://github.com/opensearch-project/OpenSearch/pull/14004))
- Handle NPE in GetResult if "found" field is missing ([#14552](https://github.com/opensearch-project/OpenSearch/pull/14552))
- Refactoring FilterPath.parse by using an iterative approach ([#14200](https://github.com/opensearch-project/OpenSearch/pull/14200))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
public class FilterPath {

static final FilterPath EMPTY = new FilterPath();

private final String filter;
private final String segment;
private final FilterPath next;
Expand Down Expand Up @@ -99,32 +98,29 @@ public static FilterPath[] compile(Set<String> filters) {

List<FilterPath> paths = new ArrayList<>();
for (String filter : filters) {
if (filter != null) {
if (filter != null && !filter.isEmpty()) {
filter = filter.trim();
if (filter.length() > 0) {
paths.add(parse(filter, filter));
paths.add(parse(filter));
}
}
}
return paths.toArray(new FilterPath[paths.size()]);
}

private static FilterPath parse(final String filter, final String segment) {
int end = segment.length();

for (int i = 0; i < end;) {
char c = segment.charAt(i);
private static FilterPath parse(final String filter) {
// Split the filter into segments using a regex
// that avoids splitting escaped dots.
String[] segments = filter.split("(?<!\\\\)\\.");
FilterPath next = EMPTY;

if (c == '.') {
String current = segment.substring(0, i).replaceAll("\\\\.", ".");
return new FilterPath(filter, current, parse(filter, segment.substring(i + 1)));
}
++i;
if ((c == '\\') && (i < end) && (segment.charAt(i) == '.')) {
++i;
}
for (int i = segments.length - 1; i >= 0; i--) {
// Replace escaped dots with actual dots in the current segment.
String segment = segments[i].replaceAll("\\\\.", ".");
next = new FilterPath(filter, segment, next);
}
return new FilterPath(filter, segment.replaceAll("\\\\.", "."), EMPTY);

return next;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.opensearch.common.util.set.Sets;
import org.opensearch.test.OpenSearchTestCase;

import java.util.HashSet;
import java.util.Set;

import static java.util.Collections.singleton;
Expand Down Expand Up @@ -369,4 +370,20 @@ public void testMultipleFilterPaths() {
assertThat(filterPath.getSegment(), is(emptyString()));
assertSame(filterPath, FilterPath.EMPTY);
}

public void testCompileWithEmptyString() {
Set<String> filters = new HashSet<>();
filters.add("");
FilterPath[] filterPaths = FilterPath.compile(filters);
assertNotNull(filterPaths);
assertEquals(0, filterPaths.length);
}

public void testCompileWithNull() {
Set<String> filters = new HashSet<>();
filters.add(null);
FilterPath[] filterPaths = FilterPath.compile(filters);
assertNotNull(filterPaths);
assertEquals(0, filterPaths.length);
}
}

0 comments on commit 489a942

Please sign in to comment.