1
1
package io .cloudquery .schema ;
2
2
3
+ import io .cloudquery .helper .GlobMatcher ;
3
4
import lombok .Builder ;
4
5
import lombok .Getter ;
5
6
7
+ import java .util .ArrayList ;
6
8
import java .util .Collections ;
7
9
import java .util .HashMap ;
8
10
import java .util .List ;
9
11
import java .util .Map ;
12
+ import java .util .Optional ;
13
+ import java .util .function .Predicate ;
10
14
11
15
@ Builder (toBuilder = true )
12
16
@ Getter
@@ -23,6 +27,67 @@ public static List<Table> flattenTables(List<Table> tables) {
23
27
return flattenMap .values ().stream ().toList ();
24
28
}
25
29
30
+ public static List <Table > filterDFS (List <Table > tables , List <String > includeConfiguration , List <String > skipConfiguration , boolean skipDependentTables ) throws SchemaException {
31
+ List <GlobMatcher > includes = includeConfiguration .stream ().map (GlobMatcher ::new ).toList ();
32
+ List <GlobMatcher > excludes = skipConfiguration .stream ().map (GlobMatcher ::new ).toList ();
33
+
34
+ List <Table > flattenedTables = flattenTables (tables );
35
+ for (GlobMatcher includeMatcher : includes ) {
36
+ boolean includeMatch = false ;
37
+ for (Table table : flattenedTables ) {
38
+ if (includeMatcher .matches (table .getName ())) {
39
+ includeMatch = true ;
40
+ break ;
41
+ }
42
+ }
43
+ if (!includeMatch ) {
44
+ throw new SchemaException ("table configuration includes a pattern \" " + includeMatcher .getStringMatch () + "\" with no matches" );
45
+ }
46
+ }
47
+ for (GlobMatcher excludeMatcher : excludes ) {
48
+ boolean excludeMatch = false ;
49
+ for (Table table : flattenedTables ) {
50
+ if (excludeMatcher .matches (table .getName ())) {
51
+ excludeMatch = true ;
52
+ break ;
53
+ }
54
+ }
55
+ if (!excludeMatch ) {
56
+ throw new SchemaException ("skip configuration includes a pattern \" " + excludeMatcher .getStringMatch () + "\" with no matches" );
57
+ }
58
+ }
59
+
60
+ Predicate <Table > include = table -> {
61
+ for (GlobMatcher matcher : includes ) {
62
+ if (matcher .matches (table .getName ())) {
63
+ return true ;
64
+ }
65
+ }
66
+ return false ;
67
+ };
68
+
69
+ Predicate <Table > exclude = table -> {
70
+ for (GlobMatcher matcher : excludes ) {
71
+ if (matcher .matches (table .getName ())) {
72
+ return true ;
73
+ }
74
+ }
75
+ return false ;
76
+ };
77
+
78
+ return filterDFSFunc (tables , include , exclude , skipDependentTables );
79
+ }
80
+
81
+ private static List <Table > filterDFSFunc (List <Table > tables , Predicate <Table > include , Predicate <Table > exclude , boolean skipDependentTables ) {
82
+ List <Table > filteredTables = new ArrayList <>();
83
+ for (Table table : tables ) {
84
+ Table filteredTable = table .toBuilder ().parent (null ).build ();
85
+ Optional <Table > optionalFilteredTable = filteredTable .filterDfs (false , include , exclude , skipDependentTables );
86
+ optionalFilteredTable .ifPresent (filteredTables ::add );
87
+ }
88
+ return filteredTables ;
89
+ }
90
+
26
91
public static int maxDepth (List <Table > tables ) {
27
92
int depth = 0 ;
28
93
if (tables .isEmpty ()) {
@@ -39,6 +104,32 @@ public static int maxDepth(List<Table> tables) {
39
104
40
105
private String name ;
41
106
107
+ private Table parent ;
108
+
42
109
@ Builder .Default
43
110
private List <Table > relations = Collections .emptyList ();
111
+
112
+ private Optional <Table > filterDfs (boolean parentMatched , Predicate <Table > include , Predicate <Table > exclude , boolean skipDependentTables ) {
113
+ if (exclude .test (this )) {
114
+ return Optional .empty ();
115
+ }
116
+ boolean matched = parentMatched && !skipDependentTables ;
117
+ if (include .test (this )) {
118
+ matched = true ;
119
+ }
120
+ List <Table > filteredRelations = new ArrayList <>();
121
+ for (Table relation : relations ) {
122
+ Optional <Table > filteredChild = relation .filterDfs (matched , include , exclude , skipDependentTables );
123
+ if (filteredChild .isPresent ()) {
124
+ matched = true ;
125
+ filteredRelations .add (filteredChild .get ());
126
+ }
127
+ }
128
+ this .relations = filteredRelations ;
129
+ if (matched ) {
130
+ return Optional .of (this );
131
+ }
132
+ return Optional .empty ();
133
+ }
134
+
44
135
}
0 commit comments