Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 // All methods except the constructor and destructor need to be called on the DB | 33 // All methods except the constructor and destructor need to be called on the DB |
| 34 // thread. | 34 // thread. |
| 35 // | 35 // |
| 36 // Currently manages: | 36 // Currently manages: |
| 37 // - UrlResourceTable - resources per Urls. | 37 // - UrlResourceTable - resources per Urls. |
| 38 // - UrlMetadataTable - misc data for Urls (like last visit time). | 38 // - UrlMetadataTable - misc data for Urls (like last visit time). |
| 39 // - HostResourceTable - resources per host. | 39 // - HostResourceTable - resources per host. |
| 40 // - HostMetadataTable - misc data for hosts. | 40 // - HostMetadataTable - misc data for hosts. |
| 41 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 41 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| 42 public: | 42 public: |
| 43 // Used in the UrlResourceTable and HostResourceTable to store resources | 43 // Sorts the resources by score, decreasing |
| 44 // required for the page or host. | 44 static void SortResources(std::vector<ResourceData>* resources); |
| 45 struct ResourceRow { | |
| 46 ResourceRow(); | |
| 47 ResourceRow(const ResourceRow& other); | |
| 48 ResourceRow(const std::string& resource_url, | |
| 49 content::ResourceType resource_type, | |
| 50 int number_of_hits, | |
| 51 int number_of_misses, | |
| 52 int consecutive_misses, | |
| 53 double average_position, | |
| 54 net::RequestPriority priority, | |
| 55 bool has_validators, | |
| 56 bool always_revalidate); | |
| 57 void UpdateScore(); | |
| 58 bool operator==(const ResourceRow& rhs) const; | |
| 59 static void FromProto(const ResourceData& proto, ResourceRow* row); | |
| 60 void ToProto(ResourceData* resource_data) const; | |
| 61 | |
| 62 GURL resource_url; | |
| 63 content::ResourceType resource_type; | |
| 64 size_t number_of_hits; | |
| 65 size_t number_of_misses; | |
| 66 size_t consecutive_misses; | |
| 67 double average_position; | |
| 68 net::RequestPriority priority; | |
| 69 bool has_validators; | |
| 70 bool always_revalidate; | |
| 71 | |
| 72 // Not stored. | |
| 73 float score; | |
| 74 }; | |
| 75 typedef std::vector<ResourceRow> ResourceRows; | |
| 76 | |
| 77 // Sorts the resource rows by score, decreasing. | |
| 78 static void SortResourceRows(ResourceRows* rows); | |
| 79 | 45 |
| 80 // Aggregated data for a Url or Host. Although the data differs slightly, we | 46 // Aggregated data for a Url or Host. Although the data differs slightly, we |
| 81 // store them in the same structure, because most of the fields are common and | 47 // store them in the same structure, because most of the fields are common and |
| 82 // it allows us to use the same functions. | 48 // it allows us to use the same functions. |
| 83 struct PrefetchData { | 49 struct PrefetchData { |
| 84 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); | 50 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); |
| 85 PrefetchData(const PrefetchData& other); | 51 PrefetchData(const PrefetchData& other); |
| 86 ~PrefetchData(); | 52 ~PrefetchData(); |
| 87 bool operator==(const PrefetchData& rhs) const; | |
| 88 | 53 |
| 89 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } | 54 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } |
| 90 | 55 |
| 91 // Is the data a host as opposed to a Url? | 56 // Is the data a host as opposed to a Url? |
| 92 PrefetchKeyType key_type; // Not const to be able to assign. | 57 PrefetchKeyType key_type; // Not const to be able to assign. |
| 93 std::string primary_key; // is_host() ? main frame url : host. | 58 std::string primary_key; // is_host() ? host : main frame url. |
| 94 | 59 |
| 95 base::Time last_visit; | 60 base::Time last_visit; |
| 96 ResourceRows resources; | 61 std::vector<ResourceData> resources; |
| 97 }; | 62 }; |
| 98 // Map from primary key to PrefetchData for the key. | 63 // Map from primary key to PrefetchData for the key. |
| 99 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 64 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| 100 | 65 |
| 101 // Returns data for all Urls and Hosts. | 66 // Returns data for all Urls and Hosts. |
| 102 virtual void GetAllData(PrefetchDataMap* url_data_map, | 67 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 103 PrefetchDataMap* host_data_map); | 68 PrefetchDataMap* host_data_map); |
| 104 | 69 |
| 105 // Updates data for a Url and a host. If either of the |url_data| or | 70 // Updates data for a Url and a host. If either of the |url_data| or |
| 106 // |host_data| has an empty primary key, it will be ignored. | 71 // |host_data| has an empty primary key, it will be ignored. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 123 // The maximum length of the string that can be stored in the DB. | 88 // The maximum length of the string that can be stored in the DB. |
| 124 static constexpr size_t kMaxStringLength = 1024; | 89 static constexpr size_t kMaxStringLength = 1024; |
| 125 | 90 |
| 126 private: | 91 private: |
| 127 friend class PredictorDatabaseInternal; | 92 friend class PredictorDatabaseInternal; |
| 128 friend class MockResourcePrefetchPredictorTables; | 93 friend class MockResourcePrefetchPredictorTables; |
| 129 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 94 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 130 DatabaseVersionIsSet); | 95 DatabaseVersionIsSet); |
| 131 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 96 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 132 DatabaseIsResetWhenIncompatible); | 97 DatabaseIsResetWhenIncompatible); |
| 98 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, ComputeScore); | |
| 133 | 99 |
| 134 ResourcePrefetchPredictorTables(); | 100 ResourcePrefetchPredictorTables(); |
| 135 ~ResourcePrefetchPredictorTables() override; | 101 ~ResourcePrefetchPredictorTables() override; |
| 136 | 102 |
| 137 // Helper functions below help perform functions on the Url and host table | 103 // Helper functions below help perform functions on the Url and host table |
| 138 // using the same code. | 104 // using the same code. |
| 139 void GetAllDataHelper(PrefetchKeyType key_type, | 105 void GetAllDataHelper(PrefetchKeyType key_type, |
| 140 PrefetchDataMap* data_map, | 106 PrefetchDataMap* data_map, |
| 141 std::vector<std::string>* to_delete); | 107 std::vector<std::string>* to_delete); |
| 142 bool UpdateDataHelper(const PrefetchData& data); | 108 bool UpdateDataHelper(const PrefetchData& data); |
| 143 void DeleteDataHelper(PrefetchKeyType key_type, | 109 void DeleteDataHelper(PrefetchKeyType key_type, |
| 144 const std::vector<std::string>& keys); | 110 const std::vector<std::string>& keys); |
| 145 | 111 |
| 146 // Returns true if the strings in the |data| are less than |kMaxStringLength| | 112 // Returns true if the strings in the |data| are less than |kMaxStringLength| |
| 147 // in length. | 113 // in length. |
| 148 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); | 114 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); |
| 149 | 115 |
| 116 // Computes score of |data| | |
|
pasko
2016/09/21 17:38:36
Except for end-of-line case, all comments should b
alexilin
2016/09/22 11:21:15
Feel free to leave more grammar-nazi comments!
| |
| 117 static float ComputeScore(const ResourceData& data); | |
| 118 | |
| 150 // PredictorTableBase methods. | 119 // PredictorTableBase methods. |
| 151 void CreateTableIfNonExistent() override; | 120 void CreateTableIfNonExistent() override; |
| 152 void LogDatabaseStats() override; | 121 void LogDatabaseStats() override; |
| 153 | 122 |
| 154 // Database version. Always increment it when any change is made to the data | 123 // Database version. Always increment it when any change is made to the data |
| 155 // schema (including the .proto). | 124 // schema (including the .proto). |
| 156 static constexpr int kDatabaseVersion = 2; | 125 static constexpr int kDatabaseVersion = 2; |
| 157 | 126 |
| 158 static bool DropTablesIfOutdated(sql::Connection* db); | 127 static bool DropTablesIfOutdated(sql::Connection* db); |
| 159 static int GetDatabaseVersion(sql::Connection* db); | 128 static int GetDatabaseVersion(sql::Connection* db); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 170 sql::Statement* GetHostResourceUpdateStatement(); | 139 sql::Statement* GetHostResourceUpdateStatement(); |
| 171 sql::Statement* GetHostMetadataDeleteStatement(); | 140 sql::Statement* GetHostMetadataDeleteStatement(); |
| 172 sql::Statement* GetHostMetadataUpdateStatement(); | 141 sql::Statement* GetHostMetadataUpdateStatement(); |
| 173 | 142 |
| 174 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 143 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 175 }; | 144 }; |
| 176 | 145 |
| 177 } // namespace predictors | 146 } // namespace predictors |
| 178 | 147 |
| 179 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 148 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |