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 <cstddef> | 8 #include <cstddef> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "chrome/browser/predictors/predictor_table_base.h" | 16 #include "chrome/browser/predictors/predictor_table_base.h" |
| 17 #include "chrome/browser/predictors/resource_prefetch_common.h" | 17 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor.pb.h" |
| 19 #include "components/precache/core/proto/precache.pb.h" | |
| 19 | 20 |
| 20 namespace sql { | 21 namespace sql { |
| 21 class Statement; | 22 class Statement; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace predictors { | 25 namespace predictors { |
| 25 | 26 |
| 26 // From resource_prefetch_predictor.proto. | 27 // From resource_prefetch_predictor.proto. |
| 27 using RedirectStat = RedirectData_RedirectStat; | 28 using RedirectStat = RedirectData_RedirectStat; |
| 28 | 29 |
| 29 // Interface for database tables used by the ResourcePrefetchPredictor. | 30 // Interface for database tables used by the ResourcePrefetchPredictor. |
| 30 // All methods except the constructor and destructor need to be called on the DB | 31 // All methods except the constructor and destructor need to be called on the DB |
| 31 // thread. | 32 // thread. |
| 32 // | 33 // |
| 33 // Currently manages: | 34 // Currently manages: |
| 34 // - UrlResourceTable - resources per Urls. | 35 // - UrlResourceTable - resources per Urls. |
| 35 // - UrlRedirectTable - redirects per Urls. | 36 // - UrlRedirectTable - redirects per Urls. |
| 36 // - HostResourceTable - resources per host. | 37 // - HostResourceTable - resources per host. |
| 37 // - HostRedirectTable - redirects per host. | 38 // - HostRedirectTable - redirects per host. |
| 38 class ResourcePrefetchPredictorTables : public PredictorTableBase { | 39 class ResourcePrefetchPredictorTables : public PredictorTableBase { |
| 39 public: | 40 public: |
| 40 // Map from primary key to PrefetchData for the key. | |
|
alexilin
2017/03/07 16:08:15
Get rid of obvious comments.
| |
| 41 typedef std::map<std::string, PrefetchData> PrefetchDataMap; | 41 typedef std::map<std::string, PrefetchData> PrefetchDataMap; |
| 42 | |
| 43 // Map from primary key to RedirectData for the key. | |
| 44 typedef std::map<std::string, RedirectData> RedirectDataMap; | 42 typedef std::map<std::string, RedirectData> RedirectDataMap; |
| 43 typedef std::map<std::string, precache::PrecacheManifest> ManifestDataMap; | |
| 45 | 44 |
| 46 // Returns data for all Urls and Hosts. | 45 // Returns data for all Urls and Hosts. |
| 47 virtual void GetAllData(PrefetchDataMap* url_data_map, | 46 virtual void GetAllData(PrefetchDataMap* url_data_map, |
| 48 PrefetchDataMap* host_data_map, | 47 PrefetchDataMap* host_data_map, |
| 49 RedirectDataMap* url_redirect_data_map, | 48 RedirectDataMap* url_redirect_data_map, |
| 50 RedirectDataMap* host_redirect_data_map); | 49 RedirectDataMap* host_redirect_data_map, |
| 50 ManifestDataMap* manifest_map); | |
| 51 | 51 |
| 52 // Updates data for a Url and a host. If either of the |url_data| or | 52 // Updates data for a Url and a host. If either of the |url_data| or |
| 53 // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty | 53 // |host_data| or |url_redirect_data| or |host_redirect_data| has an empty |
| 54 // primary key, it will be ignored. | 54 // primary key, it will be ignored. |
| 55 // Note that the Urls and primary key in |url_data|, |host_data|, | 55 // Note that the Urls and primary key in |url_data|, |host_data|, |
| 56 // |url_redirect_data| and |host_redirect_data| should be less than | 56 // |url_redirect_data| and |host_redirect_data| should be less than |
| 57 // |kMaxStringLength| in length. | 57 // |kMaxStringLength| in length. |
| 58 virtual void UpdateData(const PrefetchData& url_data, | 58 virtual void UpdateData(const PrefetchData& url_data, |
| 59 const PrefetchData& host_data, | 59 const PrefetchData& host_data, |
| 60 const RedirectData& url_redirect_data, | 60 const RedirectData& url_redirect_data, |
| 61 const RedirectData& host_redirect_data); | 61 const RedirectData& host_redirect_data); |
| 62 | 62 |
| 63 virtual void UpdateManifestData( | |
| 64 const std::string& host, | |
| 65 const precache::PrecacheManifest& manifest_data); | |
| 66 | |
| 63 // Delete data for the input |urls| and |hosts|. | 67 // Delete data for the input |urls| and |hosts|. |
| 64 virtual void DeleteResourceData(const std::vector<std::string>& urls, | 68 virtual void DeleteResourceData(const std::vector<std::string>& urls, |
| 65 const std::vector<std::string>& hosts); | 69 const std::vector<std::string>& hosts); |
| 66 | 70 |
| 67 // Wrapper over DeleteResourceData for convenience. | 71 // Wrapper over DeleteResourceData for convenience. |
| 68 virtual void DeleteSingleResourceDataPoint(const std::string& key, | 72 virtual void DeleteSingleResourceDataPoint(const std::string& key, |
| 69 PrefetchKeyType key_type); | 73 PrefetchKeyType key_type); |
| 70 | 74 |
| 71 // Delete data for the input |urls| and |hosts|. | 75 // Delete data for the input |urls| and |hosts|. |
| 72 virtual void DeleteRedirectData(const std::vector<std::string>& urls, | 76 virtual void DeleteRedirectData(const std::vector<std::string>& urls, |
| 73 const std::vector<std::string>& hosts); | 77 const std::vector<std::string>& hosts); |
| 74 | 78 |
| 75 // Wrapper over DeleteRedirectData for convenience. | 79 // Wrapper over DeleteRedirectData for convenience. |
| 76 virtual void DeleteSingleRedirectDataPoint(const std::string& key, | 80 virtual void DeleteSingleRedirectDataPoint(const std::string& key, |
| 77 PrefetchKeyType key_type); | 81 PrefetchKeyType key_type); |
| 78 | 82 |
| 83 // Delete data for the input |hosts|. | |
| 84 virtual void DeleteManifestData(const std::vector<std::string>& hosts); | |
| 85 | |
| 79 // Deletes all data in all the tables. | 86 // Deletes all data in all the tables. |
| 80 virtual void DeleteAllData(); | 87 virtual void DeleteAllData(); |
| 81 | 88 |
| 82 // Removes the resources with more than |max_consecutive_misses| consecutive | 89 // Removes the resources with more than |max_consecutive_misses| consecutive |
| 83 // misses from |data|. | 90 // misses from |data|. |
| 84 static void TrimResources(PrefetchData* data, size_t max_consecutive_misses); | 91 static void TrimResources(PrefetchData* data, size_t max_consecutive_misses); |
| 85 | 92 |
| 86 // Sorts the resources by score, decreasing. | 93 // Sorts the resources by score, decreasing. |
| 87 static void SortResources(PrefetchData* data); | 94 static void SortResources(PrefetchData* data); |
| 88 | 95 |
| 89 // Computes score of |data|. | 96 // Computes score of |data|. |
| 90 static float ComputeResourceScore(const ResourceData& data); | 97 static float ComputeResourceScore(const ResourceData& data); |
| 91 | 98 |
| 92 // Removes the redirects with more than |max_consecutive_misses| consecutive | 99 // Removes the redirects with more than |max_consecutive_misses| consecutive |
| 93 // misses from |data|. | 100 // misses from |data|. |
| 94 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); | 101 static void TrimRedirects(RedirectData* data, size_t max_consecutive_misses); |
| 95 | 102 |
| 96 // The maximum length of the string that can be stored in the DB. | 103 // The maximum length of the string that can be stored in the DB. |
| 97 static constexpr size_t kMaxStringLength = 1024; | 104 static constexpr size_t kMaxStringLength = 1024; |
| 98 | 105 |
| 99 protected: | 106 protected: |
| 100 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() | 107 // Protected for testing. Use PredictorDatabase::resource_prefetch_tables() |
| 101 // instead of this constructor. | 108 // instead of this constructor. |
| 102 ResourcePrefetchPredictorTables(); | 109 ResourcePrefetchPredictorTables(); |
| 103 ~ResourcePrefetchPredictorTables() override; | 110 ~ResourcePrefetchPredictorTables() override; |
| 104 | 111 |
| 105 private: | 112 private: |
| 106 // Represents the type of information that is stored in prefetch database. | 113 // Represents the type of information that is stored in prefetch database. |
| 107 enum class PrefetchDataType { RESOURCE, REDIRECT }; | 114 enum class PrefetchDataType { RESOURCE, REDIRECT, MANIFEST }; |
| 108 | 115 |
| 109 enum class TableOperationType { INSERT, REMOVE }; | 116 enum class TableOperationType { INSERT, REMOVE }; |
| 110 | 117 |
| 111 friend class PredictorDatabaseInternal; | 118 friend class PredictorDatabaseInternal; |
| 112 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 119 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 113 DatabaseVersionIsSet); | 120 DatabaseVersionIsSet); |
| 114 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, | 121 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTablesTest, |
| 115 DatabaseIsResetWhenIncompatible); | 122 DatabaseIsResetWhenIncompatible); |
| 116 | 123 |
| 117 // Database version. Always increment it when any change is made to the data | 124 // Database version. Always increment it when any change is made to the data |
| 118 // schema (including the .proto). | 125 // schema (including the .proto). |
| 119 static constexpr int kDatabaseVersion = 5; | 126 static constexpr int kDatabaseVersion = 5; |
| 120 | 127 |
| 121 // Helper functions below help perform functions on the Url and host table | 128 // Helper functions below help perform functions on the Url and host table |
| 122 // using the same code. | 129 // using the same code. |
| 123 void GetAllResourceDataHelper(PrefetchKeyType key_type, | 130 void GetAllResourceDataHelper(PrefetchKeyType key_type, |
| 124 PrefetchDataMap* data_map); | 131 PrefetchDataMap* data_map); |
| 125 void GetAllRedirectDataHelper(PrefetchKeyType key_type, | 132 void GetAllRedirectDataHelper(PrefetchKeyType key_type, |
| 126 RedirectDataMap* redirect_map); | 133 RedirectDataMap* redirect_map); |
| 134 void GetAllManifestDataHelper(ManifestDataMap* manifest_map); | |
| 135 | |
| 127 bool UpdateDataHelper(PrefetchKeyType key_type, | 136 bool UpdateDataHelper(PrefetchKeyType key_type, |
| 128 PrefetchDataType data_type, | 137 PrefetchDataType data_type, |
| 129 const std::string& key, | 138 const std::string& key, |
| 130 const google::protobuf::MessageLite& data); | 139 const google::protobuf::MessageLite& data); |
| 131 void DeleteDataHelper(PrefetchKeyType key_type, | 140 void DeleteDataHelper(PrefetchKeyType key_type, |
| 132 PrefetchDataType data_type, | 141 PrefetchDataType data_type, |
| 133 const std::vector<std::string>& keys); | 142 const std::vector<std::string>& keys); |
| 134 | 143 |
| 135 // PredictorTableBase: | 144 // PredictorTableBase: |
| 136 void CreateTableIfNonExistent() override; | 145 void CreateTableIfNonExistent() override; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 148 | 157 |
| 149 static const char* GetTableName(PrefetchKeyType key_type, | 158 static const char* GetTableName(PrefetchKeyType key_type, |
| 150 PrefetchDataType data_type); | 159 PrefetchDataType data_type); |
| 151 | 160 |
| 152 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); | 161 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); |
| 153 }; | 162 }; |
| 154 | 163 |
| 155 } // namespace predictors | 164 } // namespace predictors |
| 156 | 165 |
| 157 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ | 166 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ |
| OLD | NEW |