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

Side by Side Diff: lib/matcher.js

Issue 30007559: Issue 7285 - Abstract caching logic (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Feb. 14, 2019, 5:36 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
« lib/caching.js ('K') | « lib/elemHide.js ('k') | no next file » | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Matcher class implementing matching addresses against 21 * @fileOverview Matcher class implementing matching addresses against
22 * a list of filters. 22 * a list of filters.
23 */ 23 */
24 24
25 const {RegExpFilter, WhitelistFilter} = require("./filterClasses"); 25 const {RegExpFilter, WhitelistFilter} = require("./filterClasses");
26 const {normalizeHostname, suffixes} = require("./domain"); 26 const {normalizeHostname, suffixes} = require("./domain");
27 const {Cache} = require("./caching");
27 28
28 /** 29 /**
29 * Regular expression for matching a keyword in a filter. 30 * Regular expression for matching a keyword in a filter.
30 * @type {RegExp} 31 * @type {RegExp}
31 */ 32 */
32 const keywordRegExp = /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/; 33 const keywordRegExp = /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/;
33 34
34 /** 35 /**
35 * Regular expression for matching all keywords in a filter. 36 * Regular expression for matching all keywords in a filter.
36 * @type {RegExp} 37 * @type {RegExp}
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 618
618 /** 619 /**
619 * Combines a matcher for blocking and exception rules, automatically sorts 620 * Combines a matcher for blocking and exception rules, automatically sorts
620 * rules into two {@link Matcher} instances. 621 * rules into two {@link Matcher} instances.
621 */ 622 */
622 class CombinedMatcher 623 class CombinedMatcher
623 { 624 {
624 constructor() 625 constructor()
625 { 626 {
626 /** 627 /**
627 * Maximal number of matching cache entries to be kept
628 * @type {number}
629 */
630 this.maxCacheEntries = 10000;
631
632 /**
633 * Matcher for blocking rules. 628 * Matcher for blocking rules.
634 * @type {Matcher} 629 * @type {Matcher}
635 * @private 630 * @private
636 */ 631 */
637 this._blacklist = new Matcher(); 632 this._blacklist = new Matcher();
638 633
639 /** 634 /**
640 * Matcher for exception rules. 635 * Matcher for exception rules.
641 * @type {Matcher} 636 * @type {Matcher}
642 * @private 637 * @private
643 */ 638 */
644 this._whitelist = new Matcher(); 639 this._whitelist = new Matcher();
645 640
646 /** 641 /**
647 * Lookup table of previous {@link Matcher#matchesAny} results 642 * Lookup table of previous {@link Matcher#matchesAny} results
648 * @type {Map.<string,Filter>} 643 * @type {Cache.<string, ?Filter>}
649 * @private 644 * @private
650 */ 645 */
651 this._resultCache = new Map(); 646 this._resultCache = new Cache(10000);
652 } 647 }
653 648
654 /** 649 /**
655 * @see Matcher#clear 650 * @see Matcher#clear
656 */ 651 */
657 clear() 652 clear()
658 { 653 {
659 this._blacklist.clear(); 654 this._blacklist.clear();
660 this._whitelist.clear(); 655 this._whitelist.clear();
661 this._resultCache.clear(); 656 this._resultCache.clear();
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 return hits; 796 return hits;
802 } 797 }
803 798
804 /** 799 /**
805 * @see Matcher#matchesAny 800 * @see Matcher#matchesAny
806 * @inheritdoc 801 * @inheritdoc
807 */ 802 */
808 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) 803 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
809 { 804 {
810 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + 805 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty +
811 " " + sitekey + " " + specificOnly; 806 " " + sitekey + " " + specificOnly;
812 807
813 let result = this._resultCache.get(key); 808 let result = this._resultCache.read(key);
814 if (typeof result != "undefined") 809 if (typeof result != "undefined")
815 return result; 810 return result;
816 811
817 result = this._matchesAnyInternal(location, typeMask, docDomain, 812 result = this._matchesAnyInternal(location, typeMask, docDomain,
818 thirdParty, sitekey, specificOnly); 813 thirdParty, sitekey, specificOnly);
819 814
820 if (this._resultCache.size >= this.maxCacheEntries) 815 this._resultCache.save(key, result);
821 this._resultCache.clear();
822
823 this._resultCache.set(key, result);
824 816
825 return result; 817 return result;
826 } 818 }
827 819
828 /** 820 /**
829 * @typedef {object} MatcherSearchResults 821 * @typedef {object} MatcherSearchResults
830 * @property {Array.<BlockingFilter>} [blocking] List of blocking filters 822 * @property {Array.<BlockingFilter>} [blocking] List of blocking filters
831 * found. 823 * found.
832 * @property {Array.<WhitelistFilter>} [whitelist] List of whitelist filters 824 * @property {Array.<WhitelistFilter>} [whitelist] List of whitelist filters
833 * found. 825 * found.
(...skipping 15 matching lines...) Expand all
849 * 841 *
850 * @returns {MatcherSearchResults} 842 * @returns {MatcherSearchResults}
851 */ 843 */
852 search(location, typeMask, docDomain, thirdParty, sitekey, specificOnly, 844 search(location, typeMask, docDomain, thirdParty, sitekey, specificOnly,
853 filterType = "all") 845 filterType = "all")
854 { 846 {
855 let key = "* " + location + " " + typeMask + " " + docDomain + " " + 847 let key = "* " + location + " " + typeMask + " " + docDomain + " " +
856 thirdParty + " " + sitekey + " " + specificOnly + " " + 848 thirdParty + " " + sitekey + " " + specificOnly + " " +
857 filterType; 849 filterType;
858 850
859 let result = this._resultCache.get(key); 851 let result = this._resultCache.read(key);
860 if (typeof result != "undefined") 852 if (typeof result != "undefined")
861 return result; 853 return result;
862 854
863 result = this._searchInternal(location, typeMask, docDomain, thirdParty, 855 result = this._searchInternal(location, typeMask, docDomain, thirdParty,
864 sitekey, specificOnly, filterType); 856 sitekey, specificOnly, filterType);
865 857
866 if (this._resultCache.size >= this.maxCacheEntries) 858 this._resultCache.save(key, result);
867 this._resultCache.clear();
868
869 this._resultCache.set(key, result);
870 859
871 return result; 860 return result;
872 } 861 }
873 862
874 /** 863 /**
875 * Tests whether the URL is whitelisted 864 * Tests whether the URL is whitelisted
876 * @see Matcher#matchesAny 865 * @see Matcher#matchesAny
877 * @inheritdoc 866 * @inheritdoc
878 * @returns {boolean} 867 * @returns {boolean}
879 */ 868 */
880 isWhitelisted(location, typeMask, docDomain, thirdParty, sitekey) 869 isWhitelisted(location, typeMask, docDomain, thirdParty, sitekey)
881 { 870 {
882 return !!this._whitelist.matchesAny(location, typeMask, docDomain, 871 return !!this._whitelist.matchesAny(location, typeMask, docDomain,
883 thirdParty, sitekey); 872 thirdParty, sitekey);
884 } 873 }
885 } 874 }
886 875
887 exports.CombinedMatcher = CombinedMatcher; 876 exports.CombinedMatcher = CombinedMatcher;
888 877
889 /** 878 /**
890 * Shared {@link CombinedMatcher} instance that should usually be used. 879 * Shared {@link CombinedMatcher} instance that should usually be used.
891 * @type {CombinedMatcher} 880 * @type {CombinedMatcher}
892 */ 881 */
893 let defaultMatcher = new CombinedMatcher(); 882 let defaultMatcher = new CombinedMatcher();
894 883
895 exports.defaultMatcher = defaultMatcher; 884 exports.defaultMatcher = defaultMatcher;
OLDNEW
« lib/caching.js ('K') | « lib/elemHide.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld