Skip to content

Commit f041e34

Browse files
authored
PLM-178: Fix selective filter logic (#119)
1 parent 61d2893 commit f041e34

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

sel/sel.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,39 @@ func MakeFilter(include, exclude []string) NSFilter {
2121
excludeFilter := doMakeFitler(exclude)
2222

2323
return func(db, coll string) bool {
24-
if len(includeFilter) != 0 && !includeFilter.Has(db, coll) {
24+
_, dbIncluded := includeFilter[db]
25+
_, dbExcluded := excludeFilter[db]
26+
27+
nsIncluded := len(includeFilter) > 0 && includeFilter.Has(db, coll)
28+
nsExcluded := len(excludeFilter) > 0 && excludeFilter.Has(db, coll)
29+
30+
if nsIncluded && dbIncluded && !nsExcluded {
31+
// If the namespace is included, it is allowed.
32+
// Also make sure that the namespace is not excluded,
33+
// because exclusion takes precedence.
34+
return true
35+
}
36+
37+
if dbIncluded && !nsExcluded {
38+
// If the database is included in the filter,
39+
// but the namespace is not included, it is not allowed.
40+
// Also make sure that the namespace is not excluded,
41+
// because exclusion takes precedence.
2542
return false
2643
}
2744

28-
if len(excludeFilter) != 0 && excludeFilter.Has(db, coll) {
45+
if nsExcluded && dbExcluded {
46+
// If the namespace is excluded, it is not allowed.
2947
return false
3048
}
3149

50+
if dbExcluded {
51+
// If the database is included in the filter,
52+
// but the namespace is not excluded, it is allowed.
53+
return true
54+
}
55+
56+
// If the namespace is not present in either filter, it is allowed by default.
3257
return true
3358
}
3459
}

sel/sel_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ func TestFilter(t *testing.T) {
3030
"coll_2": false,
3131
},
3232
"db_2": {
33-
"coll_0": false,
34-
"coll_1": false,
35-
"coll_2": false,
33+
"coll_0": true,
34+
"coll_1": true,
35+
"coll_2": true,
3636
},
3737
}
3838

@@ -99,6 +99,7 @@ func TestFilter(t *testing.T) {
9999
excludedFilter := []string{
100100
"db_0.*",
101101
"db_1.coll_0",
102+
"db_3.coll_1",
102103
}
103104

104105
namespaces := map[string]map[string]bool{
@@ -117,6 +118,11 @@ func TestFilter(t *testing.T) {
117118
"coll_1": true,
118119
"coll_2": false,
119120
},
121+
"db_3": {
122+
"coll_0": true,
123+
"coll_1": false,
124+
"coll_2": true,
125+
},
120126
}
121127

122128
isIncluded := sel.MakeFilter(includedFilter, excludedFilter)

tests/test_selective.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ def test_create_collection(t: testing.Testing, phase: Runner.Phase):
9393
t.plm,
9494
phase,
9595
include_ns=["db_0.*", "db_1.coll_0", "db_1.coll_1", "db_2.coll_0", "db_2.coll_1"],
96-
exclude_ns=["db_0.*", "db_1.coll_0"],
96+
exclude_ns=["db_0.*", "db_1.coll_0", "db_3.coll_1"],
9797
):
98-
for db in range(3):
98+
for db in range(4):
9999
for coll in range(3):
100100
t.source["db_1"]["coll_1"].create_index({"i": 1})
101101
t.source[f"db_{db}"][f"coll_{coll}"].insert_one({})
@@ -104,12 +104,18 @@ def test_create_collection(t: testing.Testing, phase: Runner.Phase):
104104
# "db_0.coll_0",
105105
# "db_0.coll_1",
106106
# "db_0.coll_2",
107+
107108
# "db_1.coll_0",
108109
"db_1.coll_1",
109110
# "db_1.coll_2",
111+
110112
"db_2.coll_0",
111113
"db_2.coll_1",
112114
# "db_2.coll_2",
115+
116+
"db_3.coll_0",
117+
# "db_3.coll_1",
118+
"db_3.coll_2",
113119
}
114120

115121
assert expected == set(testing.list_all_namespaces(t.target))

0 commit comments

Comments
 (0)