-
Notifications
You must be signed in to change notification settings - Fork 0
/
blacklist.go
189 lines (155 loc) · 4.08 KB
/
blacklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package lizt
import (
"fmt"
"os"
"strings"
"sync"
)
// BlacklistingIterator is an iterator that skips blacklists while iterating.
type BlacklistingIterator struct {
Blacklister
PointerIterator
blacklist *BlacklistManager
}
// BlacklistingIteratorConfig is the config for a blacklisting iterator.
type BlacklistingIteratorConfig struct {
PointerIter PointerIterator
Blacklisted *BlacklistManager
}
// NewBlacklistingIterator returns a new persistent iterator. It will set the pointer to the last known pointer.
func NewBlacklistingIterator(cfg BlacklistingIteratorConfig) (*BlacklistingIterator, error) {
blkIter := &BlacklistingIterator{
PointerIterator: cfg.PointerIter,
blacklist: cfg.Blacklisted,
}
return blkIter, nil
}
// Next returns the next line from the iterator.
func (bi *BlacklistingIterator) Next(count int) ([]string, error) {
var clean []string
for len(clean) < count {
next, err := bi.PointerIterator.Next(count - len(clean))
if err != nil {
return nil, fmt.Errorf("next: name: %s -> %w", bi.Name(), err)
}
for _, n := range next {
if !bi.IsBlacklisted(n) {
clean = append(clean, n)
}
}
}
return clean, nil
}
// IsBlacklisted returns true if the given line is blacklisted.
func (bi *BlacklistingIterator) IsBlacklisted(line string) bool {
return bi.blacklist.Has(line)
}
// MustNext returns the next lines, of a given count, from the iterator. Panics on error.
func (bi *BlacklistingIterator) MustNext(count int) []string {
lines, err := bi.Next(count)
if err != nil {
panic(err)
}
return lines
}
// NextOne returns the next line from the iterator.
func (bi *BlacklistingIterator) NextOne() (string, error) {
lines, err := bi.Next(1)
if err != nil {
return "", err
}
return lines[0], nil
}
// MustNextOne returns the next line from the iterator. Panics on error.
func (bi *BlacklistingIterator) MustNextOne() string {
line, err := bi.NextOne()
if err != nil {
panic(err)
}
return line
}
// ScrubFileWithBlacklist iterates over every line in a file and saves to a new file with the blacklisted lines removed.
func ScrubFileWithBlacklist(blkMap BlacklistMap, sourcePath, destPath string) (n int, err error) {
// Read from source file
source, err := ReadFromFile(sourcePath)
if err != nil {
return 0, fmt.Errorf("read source: %w", err)
}
// Write to dest file
dest, err := os.Create(destPath)
if err != nil {
return 0, fmt.Errorf("create dest: %w", err)
}
sb := strings.Builder{}
for _, line := range source {
if _, ok := blkMap[line]; !ok {
sb.WriteString(line + "\n")
} else {
n++
}
}
_, err = dest.WriteString(sb.String())
if err != nil {
return 0, fmt.Errorf("write line: %w", err)
}
return n, nil
}
type BlacklistMap map[string]struct{}
type BlacklistManager struct {
mu sync.Mutex
items BlacklistMap
}
func NewBlacklistManager(items BlacklistMap) *BlacklistManager {
return &BlacklistManager{
items: items,
}
}
// Has returns true if the given string is in the list
func (l *BlacklistManager) Has(who string) bool {
l.mu.Lock()
defer l.mu.Unlock()
_, ok := l.items[who]
return ok
}
// Map returns the map of items in the list
func (l *BlacklistManager) Map() BlacklistMap {
l.mu.Lock()
defer l.mu.Unlock()
return l.items
}
// Add adds a string to the list and appends to a file at the given path
func (l *BlacklistManager) Add(who string) error {
l.mu.Lock()
defer l.mu.Unlock()
if _, ok := l.items[who]; ok {
return fmt.Errorf("already in list")
}
l.items[who] = struct{}{}
return nil
}
// Len returns the length of the list
func (l *BlacklistManager) Len() int {
l.mu.Lock()
defer l.mu.Unlock()
return len(l.items)
}
// Remove removes a string from the list
func (l *BlacklistManager) Remove(who string) error {
l.mu.Lock()
defer l.mu.Unlock()
if _, ok := l.items[who]; !ok {
return fmt.Errorf("not in list")
}
delete(l.items, who)
return nil
}
// ToStringSlice returns a slice of strings from the list
func (l *BlacklistManager) ToStringSlice() []string {
l.mu.Lock()
defer l.mu.Unlock()
var items []string
for item := range l.items {
items = append(items, item)
}
return items
}