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

Delta Between Two Patch Sets: lib/matcher.js

Issue 29551648: Issue 5735 - Use JS Map instead of Object for matcher properties filterByKeyword and keywordByFilter (Closed) Base URL: https://github.com/adblockplus/adblockpluscore.git
Left Patch Set: Created Sept. 21, 2017, 1:03 p.m.
Right Patch Set: use objects as keys Created Sept. 25, 2017, 1:44 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/filterListener.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 24 matching lines...) Expand all
35 exports.Matcher = Matcher; 35 exports.Matcher = Matcher;
36 36
37 Matcher.prototype = { 37 Matcher.prototype = {
38 /** 38 /**
39 * Lookup table for filters by their associated keyword 39 * Lookup table for filters by their associated keyword
40 * @type {Map.<string,Filter>} 40 * @type {Map.<string,Filter>}
41 */ 41 */
42 filterByKeyword: null, 42 filterByKeyword: null,
43 43
44 /** 44 /**
45 * Lookup table for keywords by the filter text 45 * Lookup table for keywords by the filter
46 * @type {Map.<string,string>} 46 * @type {Map.<Filter,string>}
sergei 2017/09/21 13:22:21 I will check the difference between Map.<string,st
Wladimir Palant 2017/09/25 11:07:46 I'd prefer to have this change done in the same go
sergei 2017/09/25 13:46:33 Done. I have checked it and there is no visible d
47 */ 47 */
48 keywordByFilter: null, 48 keywordByFilter: null,
sergei 2017/09/21 13:22:21 strictly speaking the change of type of keywordByF
49 49
50 /** 50 /**
51 * Removes all known filters 51 * Removes all known filters
52 */ 52 */
53 clear() 53 clear()
54 { 54 {
55 this.filterByKeyword = new Map(); 55 this.filterByKeyword = new Map();
56 this.keywordByFilter = new Map(); 56 this.keywordByFilter = new Map();
57 }, 57 },
58 58
59 /** 59 /**
60 * Adds a filter to the matcher 60 * Adds a filter to the matcher
61 * @param {RegExpFilter} filter 61 * @param {RegExpFilter} filter
62 */ 62 */
63 add(filter) 63 add(filter)
64 { 64 {
65 if (this.keywordByFilter.has(filter.text)) 65 if (this.keywordByFilter.has(filter))
66 return; 66 return;
67 67
68 // Look for a suitable keyword 68 // Look for a suitable keyword
69 let keyword = this.findKeyword(filter); 69 let keyword = this.findKeyword(filter);
70 let oldEntry = this.filterByKeyword.get(keyword); 70 let oldEntry = this.filterByKeyword.get(keyword);
71 if (typeof oldEntry == "undefined") 71 if (typeof oldEntry == "undefined")
72 this.filterByKeyword.set(keyword, filter); 72 this.filterByKeyword.set(keyword, filter);
73 else if (oldEntry.length == 1) 73 else if (oldEntry.length == 1)
74 this.filterByKeyword.set(keyword, [oldEntry, filter]); 74 this.filterByKeyword.set(keyword, [oldEntry, filter]);
75 else 75 else
76 oldEntry.push(filter); 76 oldEntry.push(filter);
77 this.keywordByFilter.set(filter.text, keyword); 77 this.keywordByFilter.set(filter, keyword);
78 }, 78 },
79 79
80 /** 80 /**
81 * Removes a filter from the matcher 81 * Removes a filter from the matcher
82 * @param {RegExpFilter} filter 82 * @param {RegExpFilter} filter
83 */ 83 */
84 remove(filter) 84 remove(filter)
85 { 85 {
86 let keyword = this.keywordByFilter.get(filter.text); 86 let keyword = this.keywordByFilter.get(filter);
87 if (typeof keyword == "undefined") 87 if (typeof keyword == "undefined")
88 return; 88 return;
89 89
90 let list = this.filterByKeyword.get(keyword); 90 let list = this.filterByKeyword.get(keyword);
91 if (list.length <= 1) 91 if (list.length <= 1)
92 this.filterByKeyword.delete(keyword); 92 this.filterByKeyword.delete(keyword);
93 else 93 else
94 { 94 {
95 let index = list.indexOf(filter); 95 let index = list.indexOf(filter);
96 if (index >= 0) 96 if (index >= 0)
97 { 97 {
98 list.splice(index, 1); 98 list.splice(index, 1);
99 if (list.length == 1) 99 if (list.length == 1)
100 this.filterByKeyword.set(keyword, list[0]); 100 this.filterByKeyword.set(keyword, list[0]);
101 } 101 }
102 } 102 }
103 103
104 this.keywordByFilter.delete(filter.text); 104 this.keywordByFilter.delete(filter);
105 }, 105 },
106 106
107 /** 107 /**
108 * Chooses a keyword to be associated with the filter 108 * Chooses a keyword to be associated with the filter
109 * @param {Filter} filter 109 * @param {Filter} filter
110 * @return {string} keyword or an empty string if no keyword could be found 110 * @return {string} keyword or an empty string if no keyword could be found
111 */ 111 */
112 findKeyword(filter) 112 findKeyword(filter)
113 { 113 {
114 let result = ""; 114 let result = "";
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 return result; 150 return result;
151 }, 151 },
152 152
153 /** 153 /**
154 * Checks whether a particular filter is being matched against. 154 * Checks whether a particular filter is being matched against.
155 * @param {RegExpFilter} filter 155 * @param {RegExpFilter} filter
156 * @return {boolean} 156 * @return {boolean}
157 */ 157 */
158 hasFilter(filter) 158 hasFilter(filter)
159 { 159 {
160 return this.keywordByFilter.has(filter.text); 160 return this.keywordByFilter.has(filter);
161 }, 161 },
162 162
163 /** 163 /**
164 * Returns the keyword used for a filter, null for unknown filters. 164 * Returns the keyword used for a filter, null for unknown filters.
165 * @param {RegExpFilter} filter 165 * @param {RegExpFilter} filter
166 * @return {?string} 166 * @return {?string}
167 */ 167 */
168 getKeywordForFilter(filter) 168 getKeywordForFilter(filter)
169 { 169 {
170 let keyword = this.keywordByFilter.get(filter.text); 170 let keyword = this.keywordByFilter.get(filter);
171 return typeof keyword != "undefined" ? keyword : null; 171 return typeof keyword != "undefined" ? keyword : null;
Wladimir Palant 2017/09/25 11:07:46 It seems that this can be simplified: return th
sergei 2017/09/25 13:46:33 It changes the behavior because if the value of `k
172 }, 172 },
173 173
174 /** 174 /**
175 * Checks whether the entries for a particular keyword match a URL 175 * Checks whether the entries for a particular keyword match a URL
176 * @param {string} keyword 176 * @param {string} keyword
177 * @param {string} location 177 * @param {string} location
178 * @param {number} typeMask 178 * @param {number} typeMask
179 * @param {string} docDomain 179 * @param {string} docDomain
180 * @param {boolean} thirdParty 180 * @param {boolean} thirdParty
181 * @param {string} sitekey 181 * @param {string} sitekey
182 * @param {boolean} specificOnly 182 * @param {boolean} specificOnly
183 * @return {?Filter} 183 * @return {?Filter}
184 */ 184 */
185 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, 185 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey,
186 specificOnly) 186 specificOnly)
187 { 187 {
188 let list = this.filterByKeyword.get(keyword); 188 let list = this.filterByKeyword.get(keyword);
189 if (typeof list == "undefined") 189 if (typeof list == "undefined")
sergei 2017/09/21 13:22:21 This change is additional to the change of type. P
190 return null; 190 return null;
191 for (let i = 0; i < list.length; i++) 191 for (let i = 0; i < list.length; i++)
192 { 192 {
193 let filter = list[i]; 193 let filter = list[i];
194 194
195 if (specificOnly && filter.isGeneric() && 195 if (specificOnly && filter.isGeneric() &&
196 !(filter instanceof WhitelistFilter)) 196 !(filter instanceof WhitelistFilter))
197 continue; 197 continue;
198 198
199 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey)) 199 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey))
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 441
442 return result; 442 return result;
443 } 443 }
444 }; 444 };
445 445
446 /** 446 /**
447 * Shared CombinedMatcher instance that should usually be used. 447 * Shared CombinedMatcher instance that should usually be used.
448 * @type {CombinedMatcher} 448 * @type {CombinedMatcher}
449 */ 449 */
450 exports.defaultMatcher = new CombinedMatcher(); 450 exports.defaultMatcher = new CombinedMatcher();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld