Skip to content

feat: adding Table filterDFS functionaility #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ dependencies {
implementation "io.grpc:grpc-testing:1.57.1"
implementation "io.cloudquery:plugin-pb-java:0.0.5"
implementation "org.apache.arrow:arrow-vector:12.0.1"

testImplementation 'org.assertj:assertj-core:3.24.2'
}

testing {
Expand Down
23 changes: 23 additions & 0 deletions lib/src/main/java/io/cloudquery/helper/GlobMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.cloudquery.helper;

import lombok.Getter;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;

public class GlobMatcher {
private final PathMatcher pathMatcher;

@Getter
private final String stringMatch;

public GlobMatcher(String stringMatch) {
this.stringMatch = stringMatch;
this.pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + stringMatch);
}

public boolean matches(String name) {
return pathMatcher.matches(Path.of(name));
}
}
22 changes: 22 additions & 0 deletions lib/src/main/java/io/cloudquery/schema/SchemaException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.cloudquery.schema;

public class SchemaException extends Exception {
public SchemaException() {
}

public SchemaException(String message) {
super(message);
}

public SchemaException(String message, Throwable cause) {
super(message, cause);
}

public SchemaException(Throwable cause) {
super(cause);
}

public SchemaException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
91 changes: 91 additions & 0 deletions lib/src/main/java/io/cloudquery/schema/Table.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.cloudquery.schema;

import io.cloudquery.helper.GlobMatcher;
import lombok.Builder;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

@Builder(toBuilder = true)
@Getter
Expand All @@ -23,6 +27,67 @@ public static List<Table> flattenTables(List<Table> tables) {
return flattenMap.values().stream().toList();
}

public static List<Table> filterDFS(List<Table> tables, List<String> includeConfiguration, List<String> skipConfiguration, boolean skipDependentTables) throws SchemaException {
List<GlobMatcher> includes = includeConfiguration.stream().map(GlobMatcher::new).toList();
List<GlobMatcher> excludes = skipConfiguration.stream().map(GlobMatcher::new).toList();

List<Table> flattenedTables = flattenTables(tables);
for (GlobMatcher includeMatcher : includes) {
boolean includeMatch = false;
for (Table table : flattenedTables) {
if (includeMatcher.matches(table.getName())) {
includeMatch = true;
break;
}
}
if (!includeMatch) {
throw new SchemaException("table configuration includes a pattern \"" + includeMatcher.getStringMatch() + "\" with no matches");
}
}
for (GlobMatcher excludeMatcher : excludes) {
boolean excludeMatch = false;
for (Table table : flattenedTables) {
if (excludeMatcher.matches(table.getName())) {
excludeMatch = true;
break;
}
}
if (!excludeMatch) {
throw new SchemaException("skip configuration includes a pattern \"" + excludeMatcher.getStringMatch() + "\" with no matches");
}
}

Predicate<Table> include = table -> {
for (GlobMatcher matcher : includes) {
if (matcher.matches(table.getName())) {
return true;
}
}
return false;
};

Predicate<Table> exclude = table -> {
for (GlobMatcher matcher : excludes) {
if (matcher.matches(table.getName())) {
return true;
}
}
return false;
};

return filterDFSFunc(tables, include, exclude, skipDependentTables);
}

private static List<Table> filterDFSFunc(List<Table> tables, Predicate<Table> include, Predicate<Table> exclude, boolean skipDependentTables) {
List<Table> filteredTables = new ArrayList<>();
for (Table table : tables) {
Table filteredTable = table.toBuilder().parent(null).build();
Optional<Table> optionalFilteredTable = filteredTable.filterDfs(false, include, exclude, skipDependentTables);
optionalFilteredTable.ifPresent(filteredTables::add);
}
return filteredTables;
}

public static int maxDepth(List<Table> tables) {
int depth = 0;
if (tables.isEmpty()) {
Expand All @@ -39,6 +104,32 @@ public static int maxDepth(List<Table> tables) {

private String name;

private Table parent;

@Builder.Default
private List<Table> relations = Collections.emptyList();

private Optional<Table> filterDfs(boolean parentMatched, Predicate<Table> include, Predicate<Table> exclude, boolean skipDependentTables) {
if (exclude.test(this)) {
return Optional.empty();
}
boolean matched = parentMatched && !skipDependentTables;
if (include.test(this)) {
matched = true;
}
List<Table> filteredRelations = new ArrayList<>();
for (Table relation : relations) {
Optional<Table> filteredChild = relation.filterDfs(matched, include, exclude, skipDependentTables);
if (filteredChild.isPresent()) {
matched = true;
filteredRelations.add(filteredChild.get());
}
}
this.relations = filteredRelations;
if (matched) {
return Optional.of(this);
}
return Optional.empty();
}

}
41 changes: 41 additions & 0 deletions lib/src/test/java/io/cloudquery/helper/GlobMatcherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.cloudquery.helper;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class GlobMatcherTest {
@Test
public void shouldMatchWildcard() {
GlobMatcher globMatcher = new GlobMatcher("*");

assertTrue(globMatcher.matches("aws_ec2_vpc"));
assertTrue(globMatcher.matches("aws_ec2_eip"));
assertTrue(globMatcher.matches("aws_ec2_instance"));
}

@Test
public void shouldMatchWildcardSuffix() {
GlobMatcher globMatcher = new GlobMatcher("aws_*");

assertTrue(globMatcher.matches("aws_ec2_vpc"));
assertTrue(globMatcher.matches("aws_ec2_eip"));
assertTrue(globMatcher.matches("aws_ec2_instance"));

assertFalse(globMatcher.matches("gcp_project"));
assertFalse(globMatcher.matches("other_aws_resource"));
}

@Test
public void shouldMatchWildcardPrefixAndSuffix() {
GlobMatcher globMatcher = new GlobMatcher("*ec2*");

assertTrue(globMatcher.matches("aws_ec2_vpc"));
assertTrue(globMatcher.matches("aws_ec2_eip"));
assertTrue(globMatcher.matches("aws_ec2_instance"));

assertFalse(globMatcher.matches("gcp_project"));
assertFalse(globMatcher.matches("other_aws_resource"));
}
}
Loading