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