forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
215 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package filtering | ||
|
||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist" | ||
"github.com/AdguardTeam/golibs/log" | ||
) | ||
|
||
// idGenerator generates filtering-list IDs in a way broadly compatible with the | ||
// legacy approach of AdGuard Home. | ||
// | ||
// TODO(a.garipov): Get rid of this once we switch completely to the new | ||
// rule-list architecture. | ||
type idGenerator struct { | ||
current *atomic.Int32 | ||
} | ||
|
||
// newIDGenerator returns a new ID generator initialized with the given seed | ||
// value. | ||
func newIDGenerator(seed int32) (g *idGenerator) { | ||
g = &idGenerator{ | ||
current: &atomic.Int32{}, | ||
} | ||
|
||
g.current.Store(seed) | ||
|
||
return g | ||
} | ||
|
||
// next returns the next ID from the generator. It is safe for concurrent use. | ||
func (g *idGenerator) next() (id rulelist.URLFilterID) { | ||
id32 := g.current.Add(1) | ||
if id32 < 0 { | ||
panic(fmt.Errorf("invalid current id value %d", id32)) | ||
} | ||
|
||
return rulelist.URLFilterID(id32) | ||
} | ||
|
||
// fix ensures that flts all have unique IDs. | ||
func (g *idGenerator) fix(flts []FilterYAML) { | ||
set := map[rulelist.URLFilterID]struct{}{} | ||
for i, f := range flts { | ||
id := f.ID | ||
if id == 0 { | ||
id = g.next() | ||
flts[i].ID = id | ||
} | ||
|
||
if _, ok := set[id]; !ok { | ||
set[id] = struct{}{} | ||
|
||
continue | ||
} | ||
|
||
newID := g.next() | ||
for _, ok := set[newID]; ok; _, ok = set[newID] { | ||
newID = g.next() | ||
} | ||
|
||
log.Info( | ||
"filtering: warning: filter at index %d has duplicated id %d; reassigning to %d", | ||
i, | ||
id, | ||
newID, | ||
) | ||
|
||
flts[i].ID = newID | ||
set[newID] = struct{}{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package filtering | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist" | ||
) | ||
|
||
func TestIDGenerator_Fix(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
in []FilterYAML | ||
}{{ | ||
name: "nil", | ||
in: nil, | ||
}, { | ||
name: "empty", | ||
in: []FilterYAML{}, | ||
}, { | ||
name: "one_zero", | ||
in: []FilterYAML{{}}, | ||
}, { | ||
name: "two_zeros", | ||
in: []FilterYAML{{}, {}}, | ||
}, { | ||
name: "many_good", | ||
in: []FilterYAML{{ | ||
Filter: Filter{ | ||
ID: 1, | ||
}, | ||
}, { | ||
Filter: Filter{ | ||
ID: 2, | ||
}, | ||
}, { | ||
Filter: Filter{ | ||
ID: 3, | ||
}, | ||
}}, | ||
}, { | ||
name: "two_dups", | ||
in: []FilterYAML{{ | ||
Filter: Filter{ | ||
ID: 1, | ||
}, | ||
}, { | ||
Filter: Filter{ | ||
ID: 3, | ||
}, | ||
}, { | ||
Filter: Filter{ | ||
ID: 1, | ||
}, | ||
}, { | ||
Filter: Filter{ | ||
ID: 2, | ||
}, | ||
}}, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
g := newIDGenerator(1) | ||
g.fix(tc.in) | ||
|
||
assertUniqueIDs(t, tc.in) | ||
}) | ||
} | ||
} | ||
|
||
// assertUniqueIDs is a test helper that asserts that the IDs of filters are | ||
// unique. | ||
func assertUniqueIDs(t testing.TB, flts []FilterYAML) { | ||
t.Helper() | ||
|
||
set := map[rulelist.URLFilterID]struct{}{} | ||
for _, f := range flts { | ||
id := f.ID | ||
if _, ok := set[id]; ok { | ||
t.Errorf("duplicated id %d", id) | ||
} | ||
|
||
set[id] = struct{}{} | ||
} | ||
} |
Oops, something went wrong.