1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_
#define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_
#include <map>
#include <string>
#include <vector>
#include "base/time/time.h"
#include "chrome/browser/predictors/predictor_table_base.h"
#include "chrome/browser/predictors/resource_prefetch_common.h"
#include "content/public/common/resource_type.h"
#include "url/gurl.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:
// - 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:
// 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,
content::ResourceType resource_type,
int number_of_hits,
int number_of_misses,
int consecutive_misses,
double average_position);
void UpdateScore();
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 resource_url;
content::ResourceType resource_type;
size_t number_of_hits;
size_t number_of_misses;
size_t consecutive_misses;
double average_position;
// Not stored.
float score;
};
typedef std::vector<ResourceRow> ResourceRows;
// Sorts the ResourceRows by score, descending.
struct ResourceRowSorter {
bool operator()(const ResourceRow& x, const ResourceRow& y) 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.
base::Time last_visit;
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);
// 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;
friend class MockResourcePrefetchPredictorTables;
ResourcePrefetchPredictorTables();
~ResourcePrefetchPredictorTables() override;
// 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.
void CreateTableIfNonExistent() override;
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);
};
} // namespace predictors
#endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TABLES_H_
|