Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/matcher.js

Issue 29965589: Issue 7181 - Keep a keyword-by-filter map after all (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Clear map Created Dec. 20, 2018, 10:57 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/matcher.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 exports.isSlowFilter = isSlowFilter; 144 exports.isSlowFilter = isSlowFilter;
145 145
146 /** 146 /**
147 * Blacklist/whitelist filter matching 147 * Blacklist/whitelist filter matching
148 */ 148 */
149 class Matcher 149 class Matcher
150 { 150 {
151 constructor() 151 constructor()
152 { 152 {
153 /** 153 /**
154 * Lookup table for keywords by their associated filter
155 * @type {Map.<RegExpFilter,string>}
156 * @private
157 */
158 this._keywordsByFilter = new Map();
159
160 /**
154 * Lookup table for simple filters by their associated keyword 161 * Lookup table for simple filters by their associated keyword
155 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} 162 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>}
156 * @private 163 * @private
157 */ 164 */
158 this._simpleFiltersByKeyword = new Map(); 165 this._simpleFiltersByKeyword = new Map();
159 166
160 /** 167 /**
161 * Lookup table for complex filters by their associated keyword 168 * Lookup table for complex filters by their associated keyword
162 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} 169 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>}
163 * @private 170 * @private
164 */ 171 */
165 this._complexFiltersByKeyword = new Map(); 172 this._complexFiltersByKeyword = new Map();
166 173
167 /** 174 /**
168 * Lookup table of type-specific lookup tables for complex filters by their 175 * Lookup table of type-specific lookup tables for complex filters by their
169 * associated keyword 176 * associated keyword
170 * @type {Map.<string,Map.<string,(RegExpFilter|Set.<RegExpFilter>)>>} 177 * @type {Map.<string,Map.<string,(RegExpFilter|Set.<RegExpFilter>)>>}
171 * @private 178 * @private
172 */ 179 */
173 this._filterMapsByType = new Map(); 180 this._filterMapsByType = new Map();
174 } 181 }
175 182
176 /** 183 /**
177 * Removes all known filters 184 * Removes all known filters
178 */ 185 */
179 clear() 186 clear()
180 { 187 {
188 this._keywordsByFilter.clear();
181 this._simpleFiltersByKeyword.clear(); 189 this._simpleFiltersByKeyword.clear();
182 this._complexFiltersByKeyword.clear(); 190 this._complexFiltersByKeyword.clear();
183 this._filterMapsByType.clear(); 191 this._filterMapsByType.clear();
184 } 192 }
185 193
186 /** 194 /**
187 * Adds a filter to the matcher 195 * Adds a filter to the matcher
188 * @param {RegExpFilter} filter 196 * @param {RegExpFilter} filter
189 */ 197 */
190 add(filter) 198 add(filter)
191 { 199 {
200 if (this._keywordsByFilter.has(filter))
201 return;
202
192 // Look for a suitable keyword 203 // Look for a suitable keyword
193 let keyword = this.findKeyword(filter); 204 let keyword = this.findKeyword(filter);
194 let locationOnly = filter.isLocationOnly(); 205 let locationOnly = filter.isLocationOnly();
195 206
196 addFilterByKeyword(filter, keyword, 207 addFilterByKeyword(filter, keyword,
197 locationOnly ? this._simpleFiltersByKeyword : 208 locationOnly ? this._simpleFiltersByKeyword :
198 this._complexFiltersByKeyword); 209 this._complexFiltersByKeyword);
199 210
211 this._keywordsByFilter.set(filter, keyword);
212
200 if (locationOnly) 213 if (locationOnly)
201 return; 214 return;
202 215
203 for (let type of nonDefaultTypes(filter.contentType)) 216 for (let type of nonDefaultTypes(filter.contentType))
204 { 217 {
205 let map = this._filterMapsByType.get(type); 218 let map = this._filterMapsByType.get(type);
206 if (!map) 219 if (!map)
207 this._filterMapsByType.set(type, map = new Map()); 220 this._filterMapsByType.set(type, map = new Map());
208 221
209 addFilterByKeyword(filter, keyword, map); 222 addFilterByKeyword(filter, keyword, map);
210 } 223 }
211 } 224 }
212 225
213 /** 226 /**
214 * Removes a filter from the matcher 227 * Removes a filter from the matcher
215 * @param {RegExpFilter} filter 228 * @param {RegExpFilter} filter
216 */ 229 */
217 remove(filter) 230 remove(filter)
218 { 231 {
219 let keyword = this.findKeyword(filter); 232 let keyword = this._keywordsByFilter.get(filter);
233 if (typeof keyword == "undefined")
234 return;
235
220 let locationOnly = filter.isLocationOnly(); 236 let locationOnly = filter.isLocationOnly();
221 237
222 removeFilterByKeyword(filter, keyword, 238 removeFilterByKeyword(filter, keyword,
223 locationOnly ? this._simpleFiltersByKeyword : 239 locationOnly ? this._simpleFiltersByKeyword :
224 this._complexFiltersByKeyword); 240 this._complexFiltersByKeyword);
225 241
242 this._keywordsByFilter.delete(filter);
243
226 if (locationOnly) 244 if (locationOnly)
227 return; 245 return;
228 246
229 for (let type of nonDefaultTypes(filter.contentType)) 247 for (let type of nonDefaultTypes(filter.contentType))
230 { 248 {
231 let map = this._filterMapsByType.get(type); 249 let map = this._filterMapsByType.get(type);
232 if (map) 250 if (map)
233 removeFilterByKeyword(filter, keyword, map); 251 removeFilterByKeyword(filter, keyword, map);
234 } 252 }
235 } 253 }
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 570
553 exports.CombinedMatcher = CombinedMatcher; 571 exports.CombinedMatcher = CombinedMatcher;
554 572
555 /** 573 /**
556 * Shared {@link CombinedMatcher} instance that should usually be used. 574 * Shared {@link CombinedMatcher} instance that should usually be used.
557 * @type {CombinedMatcher} 575 * @type {CombinedMatcher}
558 */ 576 */
559 let defaultMatcher = new CombinedMatcher(); 577 let defaultMatcher = new CombinedMatcher();
560 578
561 exports.defaultMatcher = defaultMatcher; 579 exports.defaultMatcher = defaultMatcher;
OLDNEW
« no previous file with comments | « no previous file | test/matcher.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld