Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_tables.h

Issue 2388783002: predictors: Refactor resource_prefetch_predictor_tables. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_
6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <map> 10 #include <map>
11 #include <memory>
11 #include <string> 12 #include <string>
12 #include <vector> 13 #include <vector>
13 14
14 #include "base/gtest_prod_util.h" 15 #include "base/gtest_prod_util.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/predictors/predictor_table_base.h" 18 #include "chrome/browser/predictors/predictor_table_base.h"
19 #include "chrome/browser/predictors/resource_prefetch_common.h" 19 #include "chrome/browser/predictors/resource_prefetch_common.h"
20 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" 20 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h"
21 #include "content/public/common/resource_type.h" 21 #include "content/public/common/resource_type.h"
22 #include "net/base/request_priority.h" 22 #include "net/base/request_priority.h"
23 #include "url/gurl.h" 23 #include "url/gurl.h"
24 24
25 namespace sql { 25 namespace sql {
26 class Statement; 26 class Statement;
27 } 27 }
28 28
29 namespace predictors { 29 namespace predictors {
30 30
31 // From resource_prefetch_predictor.proto. 31 // From resource_prefetch_predictor.proto.
32 using ResourceData = PrefetchData_ResourceData;
32 using RedirectStat = RedirectData_RedirectStat; 33 using RedirectStat = RedirectData_RedirectStat;
33 34
34 // Interface for database tables used by the ResourcePrefetchPredictor. 35 // Interface for database tables used by the ResourcePrefetchPredictor.
35 // All methods except the constructor and destructor need to be called on the DB 36 // All methods except the constructor and destructor need to be called on the DB
36 // thread. 37 // thread.
37 // 38 //
38 // Currently manages: 39 // Currently manages:
39 // - UrlResourceTable - resources per Urls. 40 // - UrlResourceTable - resources per Urls.
40 // - UrlMetadataTable - misc data for Urls (like last visit time).
41 // - UrlRedirectTable - redirects per Urls. 41 // - UrlRedirectTable - redirects per Urls.
42 // - HostResourceTable - resources per host. 42 // - HostResourceTable - resources per host.
43 // - HostMetadataTable - misc data for hosts.
44 // - HostRedirectTable - redirects per host. 43 // - HostRedirectTable - redirects per host.
45 class ResourcePrefetchPredictorTables : public PredictorTableBase { 44 class ResourcePrefetchPredictorTables : public PredictorTableBase {
46 public: 45 public:
47 // Aggregated data for a Url or Host. Although the data differs slightly, we
48 // store them in the same structure, because most of the fields are common and
49 // it allows us to use the same functions.
50 struct PrefetchData {
51 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key);
52 PrefetchData(const PrefetchData& other);
53 ~PrefetchData();
54
55 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; }
56
57 // Is the data a host as opposed to a Url?
58 PrefetchKeyType key_type; // Not const to be able to assign.
59 std::string primary_key; // is_host() ? host : main frame url.
60
61 base::Time last_visit;
62 std::vector<ResourceData> resources;
63 };
64 // Map from primary key to PrefetchData for the key. 46 // Map from primary key to PrefetchData for the key.
65 typedef std::map<std::string, PrefetchData> PrefetchDataMap; 47 typedef std::map<std::string, PrefetchData> PrefetchDataMap;
66 48
67 // Map from primary key to RedirectData for the key. 49 // Map from primary key to RedirectData for the key.
68 typedef std::map<std::string, RedirectData> RedirectDataMap; 50 typedef std::map<std::string, RedirectData> RedirectDataMap;
69 51
70 // Returns data for all Urls and Hosts. 52 // Returns data for all Urls and Hosts.
71 virtual void GetAllData(PrefetchDataMap* url_data_map, 53 virtual void GetAllData(PrefetchDataMap* url_data_map,
72 PrefetchDataMap* host_data_map, 54 PrefetchDataMap* host_data_map,
73 RedirectDataMap* url_redirect_data_map, 55 RedirectDataMap* url_redirect_data_map,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 static void SortResources(std::vector<ResourceData>* resources); 89 static void SortResources(std::vector<ResourceData>* resources);
108 90
109 // Sorts the redirects by score, decreasing. 91 // Sorts the redirects by score, decreasing.
110 static void SortRedirects(std::vector<RedirectStat>* redirects); 92 static void SortRedirects(std::vector<RedirectStat>* redirects);
111 93
112 // The maximum length of the string that can be stored in the DB. 94 // The maximum length of the string that can be stored in the DB.
113 static constexpr size_t kMaxStringLength = 1024; 95 static constexpr size_t kMaxStringLength = 1024;
114 96
115 private: 97 private:
116 // Represents the type of information that is stored in prefetch database. 98 // Represents the type of information that is stored in prefetch database.
117 enum class PrefetchDataType { RESOURCE, REDIRECT, METADATA }; 99 enum class PrefetchDataType { RESOURCE, REDIRECT };
118 100
119 enum class TableOperationType { INSERT, REMOVE }; 101 enum class TableOperationType { INSERT, REMOVE };
120 102
121 friend class PredictorDatabaseInternal; 103 friend class PredictorDatabaseInternal;
122 friend class MockResourcePrefetchPredictorTables; 104 friend class MockResourcePrefetchPredictorTables;
123 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, 105 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest,
124 DatabaseVersionIsSet); 106 DatabaseVersionIsSet);
125 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, 107 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest,
126 DatabaseIsResetWhenIncompatible); 108 DatabaseIsResetWhenIncompatible);
127 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, 109 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest,
128 ComputeResourceScore); 110 ComputeResourceScore);
129 111
130 ResourcePrefetchPredictorTables(); 112 ResourcePrefetchPredictorTables();
131 ~ResourcePrefetchPredictorTables() override; 113 ~ResourcePrefetchPredictorTables() override;
132 114
133 // Helper functions below help perform functions on the Url and host table 115 // Helper functions below help perform functions on the Url and host table
134 // using the same code. 116 // using the same code.
135 void GetAllResourceDataHelper(PrefetchKeyType key_type, 117 void GetAllResourceDataHelper(PrefetchKeyType key_type,
136 PrefetchDataMap* data_map, 118 PrefetchDataMap* data_map);
137 std::vector<std::string>* to_delete);
138 void GetAllRedirectDataHelper(PrefetchKeyType key_type, 119 void GetAllRedirectDataHelper(PrefetchKeyType key_type,
139 RedirectDataMap* redirect_map); 120 RedirectDataMap* redirect_map);
140 bool UpdateResourceDataHelper(PrefetchKeyType key_type, 121 bool UpdateResourceDataHelper(PrefetchKeyType key_type,
141 const PrefetchData& data); 122 const PrefetchData& data);
142 bool UpdateRedirectDataHelper(PrefetchKeyType key_type, 123 bool UpdateRedirectDataHelper(PrefetchKeyType key_type,
143 const RedirectData& data); 124 const RedirectData& data);
144 void DeleteDataHelper(PrefetchKeyType key_type, 125 void DeleteDataHelper(PrefetchKeyType key_type,
145 PrefetchDataType data_type, 126 PrefetchDataType data_type,
146 const std::vector<std::string>& keys); 127 const std::vector<std::string>& keys);
147 128
148 // Returns true if the strings in the |data| are less than |kMaxStringLength| 129 // Returns true if the strings in the |data| are less than |kMaxStringLength|
149 // in length. 130 // in length.
150 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); 131 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data);
151 static bool StringsAreSmallerThanDBLimit(const RedirectData& data); 132 static bool StringsAreSmallerThanDBLimit(const RedirectData& data);
152 133
153 // Computes score of |data|. 134 // Computes score of |data|.
154 static float ComputeResourceScore(const ResourceData& data); 135 static float ComputeResourceScore(const ResourceData& data);
155 static float ComputeRedirectScore(const RedirectStat& data); 136 static float ComputeRedirectScore(const RedirectStat& data);
156 137
157 // PredictorTableBase methods. 138 // PredictorTableBase methods.
158 void CreateTableIfNonExistent() override; 139 void CreateTableIfNonExistent() override;
159 void LogDatabaseStats() override; 140 void LogDatabaseStats() override;
160 141
161 // Database version. Always increment it when any change is made to the data 142 // Database version. Always increment it when any change is made to the data
162 // schema (including the .proto). 143 // schema (including the .proto).
163 static constexpr int kDatabaseVersion = 3; 144 static constexpr int kDatabaseVersion = 3;
Benoit L 2016/10/04 08:22:42 This needs to change.
alexilin 2016/10/04 09:43:48 Done.
164 145
165 static bool DropTablesIfOutdated(sql::Connection* db); 146 static bool DropTablesIfOutdated(sql::Connection* db);
166 static int GetDatabaseVersion(sql::Connection* db); 147 static int GetDatabaseVersion(sql::Connection* db);
167 static bool SetDatabaseVersion(sql::Connection* db, int version); 148 static bool SetDatabaseVersion(sql::Connection* db, int version);
168 149
169 // Helper to return Statements for cached Statements. 150 // Helper to return Statements for cached Statements.
170 std::unique_ptr<sql::Statement> GetTableUpdateStatement( 151 std::unique_ptr<sql::Statement> GetTableUpdateStatement(
171 PrefetchKeyType key_type, 152 PrefetchKeyType key_type,
172 PrefetchDataType data_type, 153 PrefetchDataType data_type,
173 TableOperationType op_type); 154 TableOperationType op_type);
174 155
175 static const char* GetTableUpdateStatementTemplate( 156 static const char* GetTableUpdateStatementTemplate(
176 TableOperationType op_type, 157 TableOperationType op_type,
177 PrefetchDataType data_type); 158 PrefetchDataType data_type);
178 static const char* GetTableUpdateStatementTableName( 159 static const char* GetTableUpdateStatementTableName(
179 PrefetchKeyType key_type, 160 PrefetchKeyType key_type,
180 PrefetchDataType data_type); 161 PrefetchDataType data_type);
181 162
182 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); 163 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables);
183 }; 164 };
184 165
185 } // namespace predictors 166 } // namespace predictors
186 167
187 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ 168 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698