| 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 21 matching lines...) Expand all Loading... |
| 32 // All methods except the constructor and destructor need to be called on the DB | 32 // All methods except the constructor and destructor need to be called on the DB |
| 33 // thread. | 33 // thread. |
| 34 // | 34 // |
| 35 // Currently manages: | 35 // Currently manages: |
| 36 // - UrlResourceTable - resources per Urls. | 36 // - UrlResourceTable - resources per Urls. |
| 37 // - UrlMetadataTable - misc data for Urls (like last visit time). | 37 // - UrlMetadataTable - misc data for Urls (like last visit time). |
| 38 // - HostResourceTable - resources per host. | 38 // - HostResourceTable - resources per host. |
| 39 // - HostMetadataTable - misc data for hosts. | 39 // - HostMetadataTable - misc data for hosts. |
| 40 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 40 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| 41 public: | 41 public: |
| 42 // Used in the UrlResourceTable and HostResourceTable to store resources | |
| 43 // required for the page or host. | |
| 44 struct ResourceRow { | |
| 45 ResourceRow(); | |
| 46 ResourceRow(const ResourceRow& other); | |
| 47 ResourceRow(const std::string& main_frame_url, | |
| 48 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 // Stores the host for host based data, main frame Url for the Url based | |
| 63 // data. This field is cleared for efficiency reasons and the code outside | |
| 64 // this class should not assume it is set. | |
| 65 std::string primary_key; | |
| 66 | |
| 67 GURL resource_url; | |
| 68 content::ResourceType resource_type; | |
| 69 size_t number_of_hits; | |
| 70 size_t number_of_misses; | |
| 71 size_t consecutive_misses; | |
| 72 double average_position; | |
| 73 net::RequestPriority priority; | |
| 74 bool has_validators; | |
| 75 bool always_revalidate; | |
| 76 | |
| 77 // Not stored. | |
| 78 float score; | |
| 79 }; | |
| 80 typedef std::vector<ResourceRow> ResourceRows; | |
| 81 | |
| 82 // Sorts the resource rows by score, decreasing. | |
| 83 static void SortResourceRows(ResourceRows* rows); | |
| 84 | |
| 85 // Aggregated data for a Url or Host. Although the data differs slightly, we | 42 // Aggregated data for a Url or Host. Although the data differs slightly, we |
| 86 // store them in the same structure, because most of the fields are common and | 43 // store them in the same structure, because most of the fields are common and |
| 87 // it allows us to use the same functions. | 44 // it allows us to use the same functions. |
| 88 struct PrefetchData { | 45 struct PrefetchData { |
| 89 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); | 46 PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); |
| 90 PrefetchData(const PrefetchData& other); | 47 PrefetchData(const PrefetchData& other); |
| 91 ~PrefetchData(); | 48 ~PrefetchData(); |
| 92 bool operator==(const PrefetchData& rhs) const; | |
| 93 | 49 |
| 94 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } | 50 bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } |
| 95 | 51 |
| 96 // Is the data a host as opposed to a Url? | 52 // Is the data a host as opposed to a Url? |
| 97 PrefetchKeyType key_type; // Not const to be able to assign. | 53 PrefetchKeyType key_type; // Not const to be able to assign. |
| 98 std::string primary_key; // is_host() ? main frame url : host. | 54 std::string primary_key; // is_host() ? main frame url : host. |
| 99 | 55 |
| 100 base::Time last_visit; | 56 base::Time last_visit; |
| 101 ResourceRows resources; | 57 std::vector<ResourceData> resources; |
| 102 }; | 58 }; |
| 59 |
| 60 // Sorts the resources by score, decreasing. |
| 61 static void SortResources(std::vector<ResourceData>* resource); |
| 62 |
| 63 // Updates the score of a resource. |
| 64 static void UpdateResourceScore(ResourceData* resource); |
| 65 |
| 103 // Map from primary key to PrefetchData for the key. | 66 // Map from primary key to PrefetchData for the key. |
| 104 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 67 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| 105 | 68 |
| 106 // Returns data for all Urls and Hosts. | 69 // Returns data for all Urls and Hosts. |
| 107 virtual void GetAllData(PrefetchDataMap* url_data_map, | 70 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 108 PrefetchDataMap* host_data_map); | 71 PrefetchDataMap* host_data_map); |
| 109 | 72 |
| 110 // Updates data for a Url and a host. If either of the |url_data| or | 73 // Updates data for a Url and a host. If either of the |url_data| or |
| 111 // |host_data| has an empty primary key, it will be ignored. | 74 // |host_data| has an empty primary key, it will be ignored. |
| 112 // Note that the Urls and primary key in |url_data| and |host_data| should be | 75 // Note that the Urls and primary key in |url_data| and |host_data| should be |
| 113 // less than |kMaxStringLength| in length. | 76 // less than |kMaxStringLength| in length. |
| 114 virtual void UpdateData(const PrefetchData& url_data, | 77 virtual void UpdateData(const PrefetchData& url_data, |
| 115 const PrefetchData& host_data); | 78 const PrefetchData& host_data); |
| 116 | 79 |
| 117 // Delete data for the input |urls| and |hosts|. | 80 // Delete data for the input |urls| and |hosts|. |
| 118 virtual void DeleteData(const std::vector<std::string>& urls, | 81 virtual void DeleteData(const std::vector<std::string>& urls, |
| 119 const std::vector<std::string>& hosts); | 82 const std::vector<std::string>& hosts); |
| 120 | 83 |
| 121 // Wrapper over DeleteData for convenience. | 84 // Wrapper over DeleteData for convenience. |
| 122 virtual void DeleteSingleDataPoint(const std::string& key, | 85 virtual void DeleteSingleDataPoint(const std::string& key, |
| 123 PrefetchKeyType key_type); | 86 PrefetchKeyType key_type); |
| 124 | 87 |
| 125 // Deletes all data in all the tables. | 88 // Deletes all data in all the tables. |
| 126 virtual void DeleteAllData(); | 89 virtual void DeleteAllData(); |
| 127 | 90 |
| 128 // The maximum length of the string that can be stored in the DB. | 91 // The maximum length of the string that can be stored in the DB. |
| 129 static const size_t kMaxStringLength; | 92 static const size_t kMaxStringLength = 1024; |
| 130 | 93 |
| 131 private: | 94 private: |
| 132 friend class PredictorDatabaseInternal; | 95 friend class PredictorDatabaseInternal; |
| 133 friend class MockResourcePrefetchPredictorTables; | 96 friend class MockResourcePrefetchPredictorTables; |
| 134 | 97 |
| 135 ResourcePrefetchPredictorTables(); | 98 ResourcePrefetchPredictorTables(); |
| 136 ~ResourcePrefetchPredictorTables() override; | 99 ~ResourcePrefetchPredictorTables() override; |
| 137 | 100 |
| 138 // Helper functions below help perform functions on the Url and host table | 101 // Helper functions below help perform functions on the Url and host table |
| 139 // using the same code. | 102 // using the same code. |
| 140 void GetAllDataHelper(PrefetchKeyType key_type, | 103 void GetAllDataHelper(PrefetchKeyType key_type, |
| 141 PrefetchDataMap* data_map, | 104 PrefetchDataMap* data_map, |
| 142 std::vector<std::string>* to_delete); | 105 std::vector<std::string>* to_delete); |
| 143 bool UpdateDataHelper(const PrefetchData& data); | 106 bool UpdateDataHelper(const PrefetchData& data); |
| 144 void DeleteDataHelper(PrefetchKeyType key_type, | 107 void DeleteDataHelper(PrefetchKeyType key_type, |
| 145 const std::vector<std::string>& keys); | 108 const std::vector<std::string>& keys); |
| 146 | 109 |
| 147 // Returns true if the strings in the |data| are less than |kMaxStringLength| | 110 // Returns true if the strings in the |data| are less than |kMaxStringLength| |
| 148 // in length. | 111 // in length. |
| 149 bool StringsAreSmallerThanDBLimit(const PrefetchData& data) const; | 112 static bool StringsAreSmallerThanDBLimit(const PrefetchData& data); |
| 150 | 113 |
| 151 // PredictorTableBase methods. | 114 // PredictorTableBase methods. |
| 152 void CreateTableIfNonExistent() override; | 115 void CreateTableIfNonExistent() override; |
| 153 void LogDatabaseStats() override; | 116 void LogDatabaseStats() override; |
| 154 | 117 |
| 155 bool DropTablesIfOutdated(sql::Connection* db); | 118 bool DropTablesIfOutdated(sql::Connection* db); |
| 156 | 119 |
| 157 // Helpers to return Statements for cached Statements. The caller must take | 120 // Helpers to return Statements for cached Statements. The caller must take |
| 158 // ownership of the return Statements. | 121 // ownership of the return Statements. |
| 159 sql::Statement* GetUrlResourceDeleteStatement(); | 122 sql::Statement* GetUrlResourceDeleteStatement(); |
| 160 sql::Statement* GetUrlResourceUpdateStatement(); | 123 sql::Statement* GetUrlResourceUpdateStatement(); |
| 161 sql::Statement* GetUrlMetadataDeleteStatement(); | 124 sql::Statement* GetUrlMetadataDeleteStatement(); |
| 162 sql::Statement* GetUrlMetadataUpdateStatement(); | 125 sql::Statement* GetUrlMetadataUpdateStatement(); |
| 163 | 126 |
| 164 sql::Statement* GetHostResourceDeleteStatement(); | 127 sql::Statement* GetHostResourceDeleteStatement(); |
| 165 sql::Statement* GetHostResourceUpdateStatement(); | 128 sql::Statement* GetHostResourceUpdateStatement(); |
| 166 sql::Statement* GetHostMetadataDeleteStatement(); | 129 sql::Statement* GetHostMetadataDeleteStatement(); |
| 167 sql::Statement* GetHostMetadataUpdateStatement(); | 130 sql::Statement* GetHostMetadataUpdateStatement(); |
| 168 | 131 |
| 169 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 132 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 170 }; | 133 }; |
| 171 | 134 |
| 172 } // namespace predictors | 135 } // namespace predictors |
| 173 | 136 |
| 174 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 137 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |