This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilterTable.go
259 lines (216 loc) · 6.19 KB
/
filterTable.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package contentpubsub
import (
"fmt"
"sync"
"github.com/libp2p/go-libp2p-core/peer"
dht "github.com/libp2p/go-libp2p-kad-dht"
)
// RouteStats keeps filters for each pubsub peer it is
// connected and its backups in case of his failure
type RouteStats struct {
filters map[int][]*Predicate
backups []string
addr string
routeLock *sync.RWMutex
}
func NewRouteStats(addr string) *RouteStats {
r := &RouteStats{
filters: make(map[int][]*Predicate),
routeLock: &sync.RWMutex{},
addr: addr,
}
return r
}
// FilterTable keeps filter information of all peers by
// keeping its peers' routeStats and redirect support
type FilterTable struct {
routes map[string]*RouteStats
redirectTable map[string]map[string]string
routeTracker map[string][]string
redirectLock *sync.Mutex
}
func NewFilterTable(dht *dht.IpfsDHT, addrOption bool) *FilterTable {
peers := dht.RoutingTable().GetPeerInfos()
ft := &FilterTable{
routes: make(map[string]*RouteStats),
redirectTable: make(map[string]map[string]string),
routeTracker: make(map[string][]string),
redirectLock: &sync.Mutex{},
}
for _, peerStat := range peers {
addrs := dht.FindLocal(peerStat.Id).Addrs
if addrs != nil {
dialAddr := addrForPubSubServer(addrs, addrOption)
ft.routes[peer.Encode(peerStat.Id)] = NewRouteStats(dialAddr)
}
}
return ft
}
// addRedirect just adds a shortcut address for a specific
// set of events to the peer's redirect support and updates
// the route tracking mechanism
func (ft *FilterTable) addRedirect(route string, rvID string, addr string) {
ft.redirectLock.Lock()
defer ft.redirectLock.Unlock()
if ft.redirectTable[route] == nil {
ft.redirectTable[route] = make(map[string]string)
}
ft.redirectTable[route][rvID] = addr
for _, r := range ft.routeTracker[rvID] {
if r == route {
return
}
}
ft.routeTracker[rvID] = append(ft.routeTracker[rvID], route)
}
// turnOffRedirect disallows a specific redirect from
// use and updates the route tracking mechanism
func (ft *FilterTable) turnOffRedirect(route string, rvID string) {
ft.redirectLock.Lock()
defer ft.redirectLock.Unlock()
if ft.redirectTable[route] == nil {
ft.redirectTable[route] = make(map[string]string)
}
ft.redirectTable[route][rvID] = "!"
for _, r := range ft.routeTracker[rvID] {
if r == route {
return
}
}
ft.routeTracker[rvID] = append(ft.routeTracker[rvID], route)
}
// addToRouteTracker updates the route tracking mechanism
func (ft *FilterTable) addToRouteTracker(rvID string, peer string) {
ft.redirectLock.Lock()
defer ft.redirectLock.Unlock()
for _, p := range ft.routeTracker[rvID] {
if p == peer {
return
}
}
ft.routeTracker[rvID] = append(ft.routeTracker[rvID], peer)
}
func (ft *FilterTable) PrintFilterTable() {
fmt.Println("Printing Table:")
for i, route := range ft.routes {
fmt.Println("From: " + i)
for _, filters := range route.filters {
for _, filter := range filters {
fmt.Println(filter)
}
}
}
}
// SimpleAddSummarizedFilter is called upon receiving a subscription
// filter to see if it should be added if exclusive, merged
// with others, or encompass or be encompassed by others
func (rs *RouteStats) SimpleAddSummarizedFilter(p *Predicate, backups []string) (bool, *Predicate) {
rs.routeLock.Lock()
defer rs.routeLock.Unlock()
rs.backups = backups
merge := false
for i, filters := range rs.filters {
if len(p.attributes) > i {
for _, f := range filters {
if f.SimplePredicateMatch(p) {
return true, f
}
}
} else if len(p.attributes) == i {
for j := 0; j < len(filters); j++ {
if rs.filters[i][j].SimplePredicateMatch(p) {
return true, rs.filters[i][j]
} else if p.SimplePredicateMatch(rs.filters[i][j]) {
if j == 0 && len(rs.filters[i]) == 1 {
rs.filters[i] = nil
} else if j == 0 {
rs.filters[i] = rs.filters[i][1:]
j--
} else if len(rs.filters[i]) == j+1 {
rs.filters[i] = rs.filters[i][:j]
} else {
rs.filters[i] = append(rs.filters[i][:j], rs.filters[i][j+1:]...)
j--
}
} else if ok, pNew := rs.filters[i][j].TryMergePredicates(p); ok {
p = pNew
merge = true
if j == 0 && len(rs.filters[i]) == 1 {
rs.filters[i] = nil
} else if j == 0 {
rs.filters[i] = rs.filters[i][1:]
j--
} else if len(rs.filters[i]) == j+1 {
rs.filters[i] = rs.filters[i][:j]
} else {
rs.filters[i] = append(rs.filters[i][:j], rs.filters[i][j+1:]...)
j--
}
}
}
} else {
for j := 0; j < len(rs.filters[i]); j++ {
if p.SimplePredicateMatch(rs.filters[i][j]) {
if j == 0 && len(rs.filters[i]) == 1 {
rs.filters[i] = nil
} else if j == 0 {
rs.filters[i] = rs.filters[i][1:]
j--
} else if len(rs.filters[i]) == j+1 {
rs.filters[i] = rs.filters[i][:j]
} else {
rs.filters[i] = append(rs.filters[i][:j], rs.filters[i][j+1:]...)
j--
}
}
}
}
}
rs.filters[len(p.attributes)] = append(rs.filters[len(p.attributes)], p)
if merge {
return false, p
}
return false, nil
}
// SimpleSubtractFilter removes all the emcompassed filters by the
// info string predicate ignoring partial encompassing for now
func (rs *RouteStats) SimpleSubtractFilter(p *Predicate) {
rs.routeLock.Lock()
defer rs.routeLock.Unlock()
for i := range rs.filters {
if len(p.attributes) <= i {
for j := 0; j < len(rs.filters[i]); j++ {
if p.SimplePredicateMatch(rs.filters[i][j]) {
if j == 0 && len(rs.filters[i]) == 1 {
rs.filters[i] = nil
} else if j == 0 {
rs.filters[i] = rs.filters[i][1:]
j--
} else if len(rs.filters[i]) == j+1 {
rs.filters[i] = rs.filters[i][:j]
} else {
rs.filters[i] = append(rs.filters[i][:j], rs.filters[i][j+1:]...)
j--
}
}
}
}
}
}
// IsInterested checks if there are any filters compatible to a specific
// predicate inside a routeStat and returns true if there are
func (rs *RouteStats) IsInterested(p *Predicate) bool {
rs.routeLock.RLock()
defer rs.routeLock.RUnlock()
for i, filters := range rs.filters {
for _, filter := range filters {
if filter.SimplePredicateMatch(p) {
return true
}
}
if len(p.attributes) == i {
break
}
}
return false
}