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: Some more cleanup Created Sept. 29, 2017, 4:12 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:
34 void BINDINGS_EXPORTED Add(const FilterPtr&); 56 static Matcher* BINDINGS_EXPORTED Create()
35 void BINDINGS_EXPORTED Remove(const FilterPtr&); 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
69 void BINDINGS_EXPORTED Add(Filter&);
70 void BINDINGS_EXPORTED Remove(Filter&);
36 void BINDINGS_EXPORTED Clear() ; 71 void BINDINGS_EXPORTED Clear() ;
37 bool BINDINGS_EXPORTED HasFilter(const FilterPtr&) const; 72 bool BINDINGS_EXPORTED HasFilter(const Filter&) const;
38 const String& BINDINGS_EXPORTED GetKeywordForFilter(const FilterPtr& filter); 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); 76 const String& sitekey, bool specificOnly) const;
42 OwnedString BINDINGS_EXPORTED FindKeyword(const FilterPtr&); 77 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter&) const;
78
43 private: 79 private:
44 FilterPtr CheckEntryMatch(const String& keyword, 80 FilterPtr CheckEntryMatch(const String& keyword,
45 const String& location, 81 const String& location,
46 int typeMask, DependentString& docDomain, bool thirdParty, 82 int typeMask, DependentString& docDomain, bool thirdParty,
47 const String& sitekey, bool specificOnly); 83 const String& sitekey, bool specificOnly) const;
84 };
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;
48 }; 106 };
49 107
50 class CombinedMatcher : public ref_counted 108 class CombinedMatcher : public ref_counted
51 { 109 {
52 private: 110 private:
53 static const size_t MAX_CACHE_ENTRIES; 111 static const size_t MAX_CACHE_ENTRIES;
54 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'
55 Matcher mBlacklist; 113 Matcher mBlacklist;
56 Matcher mWhitelist; 114 Matcher mWhitelist;
115 int mMatchReId;
57 116
58 public: 117 public:
59 void BINDINGS_EXPORTED Add(const FilterPtr& filter); 118 static CombinedMatcher* BINDINGS_EXPORTED Create()
60 void BINDINGS_EXPORTED Remove(const FilterPtr& filter); 119 {
120 return new CombinedMatcher;
121 }
122
123 BINDINGS_EXPORTED CombinedMatcher();
124 ~CombinedMatcher()
125 {
126 DeleteRegExp(mMatchReId);
127 }
128
129 void BINDINGS_EXPORTED Add(Filter& filter);
130 void BINDINGS_EXPORTED Remove(Filter& filter);
61 void BINDINGS_EXPORTED Clear(); 131 void BINDINGS_EXPORTED Clear();
62 bool BINDINGS_EXPORTED HasFilter(const FilterPtr& filter) const; 132 bool BINDINGS_EXPORTED HasFilter(const Filter& filter) const;
63 const String& BINDINGS_EXPORTED GetKeywordForFilter(const FilterPtr& filter); 133 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t;
64 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, 134 Filter* BINDINGS_EXPORTED MatchesAny(const String& location,
65 int typeMask, DependentString& docDomain, bool thirdParty, 135 int typeMask, DependentString& docDomain, bool thirdParty,
66 const String& sitekey, bool specificOnly); 136 const String& sitekey, bool specificOnly);
67 OwnedString BINDINGS_EXPORTED FindKeyword(const FilterPtr& filter); 137 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter& filter) const;
68 138
69 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 }
70 void ResetCache(); 148 void ResetCache();
71 FilterPtr MatchesAnyInternal(const String& location, 149 FilterPtr MatchesAnyInternal(const String& location,
72 int typeMask, DependentString& docDomain, bool thirdParty, 150 int typeMask, DependentString& docDomain, bool thirdParty,
73 const String& sitekey, bool specificOnly); 151 const String& sitekey, bool specificOnly) const;
74 }; 152 };
153
LEFTRIGHT

Powered by Google App Engine
This is Rietveld