diff options
author | shishir@chromium.org <shishir@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-05 21:42:37 +0000 |
---|---|---|
committer | shishir@chromium.org <shishir@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-05 21:42:37 +0000 |
commit | 4b37027faf1933d77a88370001c01d1f2bd45e32 (patch) | |
tree | aad9fc90710ac0910f3e9b00ba08ff35c0f75d1f /chrome/browser/predictors/resource_prefetch_predictor_tables.h | |
parent | d76806f88db3cf111f2f4bdc6f3cc11f2f865912 (diff) | |
download | chromium_src-4b37027faf1933d77a88370001c01d1f2bd45e32.zip chromium_src-4b37027faf1933d77a88370001c01d1f2bd45e32.tar.gz chromium_src-4b37027faf1933d77a88370001c01d1f2bd45e32.tar.bz2 |
Adding hosts based prefetching.
Currently the ResourcePrefetchPredictor learns about resource required on a per URL basis. This change enables it to learn the same on a per Host basis.
NOTE: Tests will be fixed once the changes looks ok.
BUG=149743
Review URL: https://chromiumcodereview.appspot.com/11369080
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171325 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/predictors/resource_prefetch_predictor_tables.h')
-rw-r--r-- | chrome/browser/predictors/resource_prefetch_predictor_tables.h | 133 |
1 files changed, 100 insertions, 33 deletions
diff --git a/chrome/browser/predictors/resource_prefetch_predictor_tables.h b/chrome/browser/predictors/resource_prefetch_predictor_tables.h index 7d6f8e0..9e7e18a 100644 --- a/chrome/browser/predictors/resource_prefetch_predictor_tables.h +++ b/chrome/browser/predictors/resource_prefetch_predictor_tables.h @@ -5,40 +5,54 @@ #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_ -#include "chrome/browser/predictors/predictor_table_base.h" +#include <map> #include <string> #include <vector> #include "base/time.h" +#include "chrome/browser/predictors/predictor_table_base.h" +#include "chrome/browser/predictors/resource_prefetch_common.h" #include "googleurl/src/gurl.h" #include "webkit/glue/resource_type.h" +namespace sql { +class Statement; +} + namespace predictors { // Interface for database tables used by the ResourcePrefetchPredictor. // All methods except the constructor and destructor need to be called on the DB // thread. // -// Currently manages two tables: -// - UrlResourceTable - Keeps track of resources per Urls. -// - UrlMetadataTable - Keeps misc data for Urls (like last visit time). +// Currently manages: +// - UrlResourceTable - resources per Urls. +// - UrlMetadataTable - misc data for Urls (like last visit time). +// - HostResourceTable - resources per host. +// - HostMetadataTable - misc data for hosts. class ResourcePrefetchPredictorTables : public PredictorTableBase { public: - struct UrlResourceRow { - UrlResourceRow(); - UrlResourceRow(const UrlResourceRow& other); - UrlResourceRow(const std::string& main_frame_url, - const std::string& resource_url, - ResourceType::Type resource_type, - int number_of_hits, - int number_of_misses, - int consecutive_misses, - double average_position); + // Used in the UrlResourceTable and HostResourceTable to store resources + // required for the page or host. + struct ResourceRow { + ResourceRow(); + ResourceRow(const ResourceRow& other); + ResourceRow(const std::string& main_frame_url, + const std::string& resource_url, + ResourceType::Type resource_type, + int number_of_hits, + int number_of_misses, + int consecutive_misses, + double average_position); void UpdateScore(); - bool operator==(const UrlResourceRow& rhs) const; + bool operator==(const ResourceRow& rhs) const; + + // Stores the host for host based data, main frame Url for the Url based + // data. This field is cleared for efficiency reasons and the code outside + // this class should not assume it is set. + std::string primary_key; - GURL main_frame_url; GURL resource_url; ResourceType::Type resource_type; int number_of_hits; @@ -49,30 +63,58 @@ class ResourcePrefetchPredictorTables : public PredictorTableBase { // Not stored. float score; }; - typedef std::vector<UrlResourceRow> UrlResourceRows; + typedef std::vector<ResourceRow> ResourceRows; - // Sorts the UrlResourceRows by score, descending. - struct UrlResourceRowSorter { - bool operator()(const UrlResourceRow& x, const UrlResourceRow& y) const; + // Sorts the ResourceRows by score, descending. + struct ResourceRowSorter { + bool operator()(const ResourceRow& x, const ResourceRow& y) const; }; - // Data from both the tables for a particular main frame url. - struct UrlData { - explicit UrlData(const GURL& main_frame_url); - UrlData(const UrlData& other); - ~UrlData(); - bool operator==(const UrlData& rhs) const; + // Aggregated data for a Url or Host. Although the data differs slightly, we + // store them in the same structure, because most of the fields are common and + // it allows us to use the same functions. + struct PrefetchData { + PrefetchData(PrefetchKeyType key_type, const std::string& primary_key); + PrefetchData(const PrefetchData& other); + ~PrefetchData(); + bool operator==(const PrefetchData& rhs) const; + + bool is_host() const { return key_type == PREFETCH_KEY_TYPE_HOST; } + + // Is the data a host as opposed to a Url? + PrefetchKeyType key_type; // Not const to be able to assign. + std::string primary_key; // is_host() ? main frame url : host. - GURL main_frame_url; base::Time last_visit; - UrlResourceRows resources; + ResourceRows resources; }; + // Map from primary key to PrefetchData for the key. + typedef std::map<std::string, PrefetchData> PrefetchDataMap; + + // Returns data for all Urls and Hosts. + virtual void GetAllData(PrefetchDataMap* url_data_map, + PrefetchDataMap* host_data_map); - // |url_data_buffer| should be empty for GetAllRows. - virtual void GetAllUrlData(std::vector<UrlData>* url_data_buffer); - virtual void UpdateDataForUrl(const UrlData& url_data); - virtual void DeleteDataForUrls(const std::vector<GURL>& urls); - virtual void DeleteAllUrlData(); + // Updates data for a Url and a host. If either of the |url_data| or + // |host_data| has an empty primary key, it will be ignored. + // Note that the Urls and primary key in |url_data| and |host_data| should be + // less than |kMaxStringLength| in length. + virtual void UpdateData(const PrefetchData& url_data, + const PrefetchData& host_data); + + // Delete data for the input |urls| and |hosts|. + virtual void DeleteData(const std::vector<std::string>& urls, + const std::vector<std::string>& hosts); + + // Wrapper over DeleteData for convenience. + virtual void DeleteSingleDataPoint(const std::string& key, + PrefetchKeyType key_type); + + // Deletes all data in all the tables. + virtual void DeleteAllData(); + + // The maximum length of the string that can be stored in the DB. + static const size_t kMaxStringLength; private: friend class PredictorDatabaseInternal; @@ -81,10 +123,35 @@ class ResourcePrefetchPredictorTables : public PredictorTableBase { ResourcePrefetchPredictorTables(); virtual ~ResourcePrefetchPredictorTables(); + // Helper functions below help perform functions on the Url and host table + // using the same code. + void GetAllDataHelper(PrefetchKeyType key_type, + PrefetchDataMap* data_map, + std::vector<std::string>* to_delete); + bool UpdateDataHelper(const PrefetchData& data); + void DeleteDataHelper(PrefetchKeyType key_type, + const std::vector<std::string>& keys); + + // Returns true if the strings in the |data| are less than |kMaxStringLength| + // in length. + bool StringsAreSmallerThanDBLimit(const PrefetchData& data) const; + // PredictorTableBase methods. virtual void CreateTableIfNonExistent() OVERRIDE; virtual void LogDatabaseStats() OVERRIDE; + // Helpers to return Statements for cached Statements. The caller must take + // ownership of the return Statements. + sql::Statement* GetUrlResourceDeleteStatement(); + sql::Statement* GetUrlResourceUpdateStatement(); + sql::Statement* GetUrlMetadataDeleteStatement(); + sql::Statement* GetUrlMetadataUpdateStatement(); + + sql::Statement* GetHostResourceDeleteStatement(); + sql::Statement* GetHostResourceUpdateStatement(); + sql::Statement* GetHostMetadataDeleteStatement(); + sql::Statement* GetHostMetadataUpdateStatement(); + DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTables); }; |