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

Delta Between Two Patch Sets: compiled/filter/Matcher.h

Issue 29556737: Issue 5141 - Convert filter match to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Addressed most of the comment. Fixed some issues. Created Oct. 3, 2017, 7:31 p.m.
Right Patch Set: Fixed many issues. One test left out. Created Oct. 6, 2017, 1:45 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
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
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 #pragma once 18 #pragma once
19 19
20 #include <vector> 20 #include <vector>
21 21
22 #include "../debug.h" 22 #include "../debug.h"
23 #include "../bindings/runtime.h" 23 #include "../bindings/runtime.h"
24 #include "../StringMap.h" 24 #include "../StringMap.h"
25 #include "Filter.h" 25 #include "Filter.h"
26 26
27 struct FilterKeyword
28 {
29 FilterKeyword()
30 {}
31 FilterKeyword(OwnedString&& keyword, Filter& filter)
32 : mKeyword(std::move(keyword)), mFilter(&filter)
33 {
34 }
35
36 explicit operator const String&() const
37 {
38 return mKeyword;
39 }
40 private:
41 OwnedString mKeyword;
Wladimir Palant 2017/10/09 08:39:47 A "keyword" is a substring of a filter text, so it
sergei 2017/10/09 15:27:53 The current way of obtaining of the keyword string
42 FilterPtr mFilter;
43 };
44
27 class Matcher : public ref_counted 45 class Matcher : public ref_counted
28 { 46 {
29 private: 47 private:
30 friend class CombinedMatcher; 48 friend class CombinedMatcher;
31 StringMap<std::vector<FilterPtr>> mFilterByKeyword; 49 StringMap<std::vector<FilterPtr>> mFilterByKeyword;
32 StringMap<OwnedString> mKeywordByFilter; 50 StringMap<FilterKeyword> mKeywordByFilter;
51 int mReId;
52 int mOptionsReId;
53 int mCandidatesReId;
54 int mMatchReId;
33 public: 55 public:
56 static Matcher* BINDINGS_EXPORTED Create()
57 {
58 return new Matcher;
59 }
60 Matcher();
61 ~Matcher()
62 {
63 DeleteRegExp(mReId);
64 DeleteRegExp(mOptionsReId);
65 DeleteRegExp(mCandidatesReId);
66 DeleteRegExp(mMatchReId);
67 }
68
34 void BINDINGS_EXPORTED Add(Filter&); 69 void BINDINGS_EXPORTED Add(Filter&);
35 void BINDINGS_EXPORTED Remove(Filter&); 70 void BINDINGS_EXPORTED Remove(Filter&);
36 void BINDINGS_EXPORTED Clear() ; 71 void BINDINGS_EXPORTED Clear() ;
37 bool BINDINGS_EXPORTED HasFilter(const Filter&) const; 72 bool BINDINGS_EXPORTED HasFilter(const Filter&) const;
38 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t; 73 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t;
39 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, 74 Filter* BINDINGS_EXPORTED MatchesAny(const String& location,
40 int typeMask, DependentString& docDomain, bool thirdParty, 75 int typeMask, DependentString& docDomain, bool thirdParty,
41 const String& sitekey, bool specificOnly) const; 76 const String& sitekey, bool specificOnly) const;
42 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter&) const; 77 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter&) const;
43 78
44 private: 79 private:
45 FilterPtr CheckEntryMatch(const String& keyword, 80 FilterPtr CheckEntryMatch(const String& keyword,
46 const String& location, 81 const String& location,
47 int typeMask, DependentString& docDomain, bool thirdParty, 82 int typeMask, DependentString& docDomain, bool thirdParty,
48 const String& sitekey, bool specificOnly) const; 83 const String& sitekey, bool specificOnly) const;
49 }; 84 };
50 85
86 // only needed because StringMap uses DependentString as keys.
87 struct CacheEntry
88 {
89 CacheEntry()
90 {}
91 CacheEntry(OwnedString&& key, FilterPtr filter)
92 : mKey(std::move(key)), mFilter(filter)
93 {}
94
95 const String& key() const
96 {
97 return mKey;
98 }
99 FilterPtr filter() const
100 {
101 return mFilter;
102 }
103 private:
104 OwnedString mKey;
105 FilterPtr mFilter;
106 };
107
51 class CombinedMatcher : public ref_counted 108 class CombinedMatcher : public ref_counted
52 { 109 {
53 private: 110 private:
54 static const size_t MAX_CACHE_ENTRIES; 111 static const size_t MAX_CACHE_ENTRIES;
55 StringMap<FilterPtr> mResultCache; 112 StringMap<CacheEntry> mResultCache;
Wladimir Palant 2017/10/09 08:39:47 Before we copy the caching approach from the JS co
sergei 2017/10/09 15:27:53 It seems not trivial because it requires at least
Wladimir Palant 2017/10/10 07:39:05 You are getting it backwards. The caching shouldn'
56 Matcher mBlacklist; 113 Matcher mBlacklist;
57 Matcher mWhitelist; 114 Matcher mWhitelist;
115 int mMatchReId;
58 116
59 public: 117 public:
118 static CombinedMatcher* BINDINGS_EXPORTED Create()
119 {
120 return new CombinedMatcher;
121 }
122
123 BINDINGS_EXPORTED CombinedMatcher();
124 ~CombinedMatcher()
125 {
126 DeleteRegExp(mMatchReId);
127 }
128
60 void BINDINGS_EXPORTED Add(Filter& filter); 129 void BINDINGS_EXPORTED Add(Filter& filter);
61 void BINDINGS_EXPORTED Remove(Filter& filter); 130 void BINDINGS_EXPORTED Remove(Filter& filter);
62 void BINDINGS_EXPORTED Clear(); 131 void BINDINGS_EXPORTED Clear();
63 bool BINDINGS_EXPORTED HasFilter(const Filter& filter) const; 132 bool BINDINGS_EXPORTED HasFilter(const Filter& filter) const;
64 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t; 133 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t;
65 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, 134 Filter* BINDINGS_EXPORTED MatchesAny(const String& location,
66 int typeMask, DependentString& docDomain, bool thirdParty, 135 int typeMask, DependentString& docDomain, bool thirdParty,
67 const String& sitekey, bool specificOnly); 136 const String& sitekey, bool specificOnly);
68 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter& filter) const; 137 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter& filter) const;
69 138
70 private: 139 private:
140 Matcher& GetMatcher(const Filter& filter)
141 {
142 return filter.mType == Filter::Type::WHITELIST ? mWhitelist : mBlacklist;
143 }
144 const Matcher& GetMatcher(const Filter& filter) const
145 {
146 return filter.mType == Filter::Type::WHITELIST ? mWhitelist : mBlacklist;
147 }
71 void ResetCache(); 148 void ResetCache();
72 FilterPtr MatchesAnyInternal(const String& location, 149 FilterPtr MatchesAnyInternal(const String& location,
73 int typeMask, DependentString& docDomain, bool thirdParty, 150 int typeMask, DependentString& docDomain, bool thirdParty,
74 const String& sitekey, bool specificOnly) const; 151 const String& sitekey, bool specificOnly) const;
75 }; 152 };
153
LEFTRIGHT

Powered by Google App Engine
This is Rietveld