Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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_PREDICTOR_DATABASE_H_ | |
| 6 #define CHROME_BROWSER_PREDICTORS_PREDICTOR_DATABASE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/file_path.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/string16.h" | |
| 15 #include "base/synchronization/cancellation_flag.h" | |
| 16 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 #include "sql/connection.h" | |
| 19 | |
| 20 class Profile; | |
| 21 | |
| 22 namespace predictors { | |
| 23 | |
| 24 // Refcounted as it is created and destroyed in the UI thread, but its database | |
| 25 // related functions have to be called in the DB thread. | |
| 26 class PredictorTableBase | |
|
dominich
2012/03/13 14:55:21
this should be in its own file.
Shishir
2012/03/14 21:14:37
Done.
| |
| 27 : public base::RefCountedThreadSafe<PredictorTableBase> { | |
| 28 protected: | |
| 29 PredictorTableBase(); | |
| 30 virtual ~PredictorTableBase(); | |
| 31 virtual void CreateTableIfNonExistent() = 0; | |
| 32 | |
| 33 sql::Connection* db_; | |
|
dominich
2012/03/13 14:55:21
if each table has its own pointer to the same data
Shishir
2012/03/14 21:14:37
Done.
| |
| 34 base::CancellationFlag cancelled_; | |
| 35 | |
| 36 private: | |
| 37 friend class PredictorDatabase; | |
| 38 friend class base::RefCountedThreadSafe<PredictorTableBase>; | |
| 39 | |
| 40 void Initialize(sql::Connection* db); | |
| 41 | |
| 42 DISALLOW_COPY_AND_ASSIGN(PredictorTableBase); | |
| 43 }; | |
| 44 | |
| 45 | |
| 46 // This manages the autocomplete predictor table within the SQLite database | |
| 47 // passed in to the constructor. It expects the following scheme: | |
| 48 // | |
| 49 // network_action_predictor | |
| 50 // id A unique id. | |
| 51 // user_text What the user typed. | |
| 52 // url The URL of the entry. | |
| 53 // number_of_hits Number of times the entry was shown to the user and | |
| 54 // selected. | |
| 55 // number_of_misses Number of times the entry was shown to the user but not | |
| 56 // selected. | |
| 57 // | |
| 58 // TODO(dominich): Consider adding this table to one of the history databases. | |
| 59 // In memory is currently used, but adding to the on-disk visits database | |
| 60 // would allow DeleteOldEntries to be cheaper through use of a join. | |
| 61 class AutocompleteActionPredictorTable : public PredictorTableBase { | |
|
dominich
2012/03/13 14:55:21
this should be in its own file.
Shishir
2012/03/14 21:14:37
Done.
| |
| 62 public: | |
| 63 struct Row { | |
| 64 // TODO(dominich): Make this 64-bit integer as an optimization. This | |
| 65 // requires some investigation into how to make sure the id is unique for | |
| 66 // each user_text/url pair. | |
| 67 // http://crbug.com/102020 | |
| 68 typedef std::string Id; | |
| 69 | |
| 70 Row(); | |
| 71 | |
| 72 // Only used by unit tests. | |
| 73 Row(const Id& id, | |
| 74 const string16& user_text, | |
| 75 const GURL& url, | |
| 76 int number_of_hits, | |
| 77 int number_of_misses); | |
| 78 | |
| 79 Row(const Row& row); | |
| 80 | |
| 81 Id id; | |
| 82 string16 user_text; | |
| 83 GURL url; | |
| 84 int number_of_hits; | |
| 85 int number_of_misses; | |
| 86 }; | |
| 87 | |
| 88 typedef std::vector<Row> Rows; | |
| 89 | |
| 90 void GetRow(const Row::Id& id, Row* row); | |
| 91 void GetAllRows(Rows* row_buffer); | |
| 92 | |
| 93 void AddRow(const Row& row); | |
| 94 void UpdateRow(const Row& row); | |
| 95 void AddAndUpdateRows(const Rows& rows_to_add, const Rows& rows_to_update); | |
| 96 void DeleteRow(const Row::Id& id); | |
| 97 void DeleteRows(const std::vector<Row::Id>& id_list); | |
| 98 void DeleteAllRows(); | |
| 99 | |
| 100 private: | |
| 101 friend class PredictorDatabase; | |
| 102 | |
| 103 AutocompleteActionPredictorTable(); | |
| 104 virtual ~AutocompleteActionPredictorTable(); | |
| 105 | |
| 106 void CreateTableIfNonExistent() OVERRIDE; | |
| 107 | |
| 108 bool StepAndInitializeRow(sql::Statement* statement, Row* row); | |
| 109 void BindRowToStatement(const Row& row, sql::Statement* statement); | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictorTable); | |
| 112 }; | |
| 113 | |
| 114 | |
| 115 // RefcountedProfileKeyedService as it is created and destroyed on a different | |
| 116 // thread to the DB thread that is required for all methods performing database | |
| 117 // access. | |
| 118 class PredictorDatabase : public RefcountedProfileKeyedService { | |
| 119 public: | |
| 120 explicit PredictorDatabase(Profile* profile); | |
| 121 virtual ~PredictorDatabase(); | |
| 122 | |
| 123 // Opens the database file from the profile path. Separated from the | |
| 124 // constructor to ease construction/destruction of this object on one thread | |
| 125 // but database access on the DB thread. | |
| 126 void Initialize(); | |
| 127 | |
| 128 AutocompleteActionPredictorTable* autocomplete_table() { | |
| 129 return autocomplete_table_.get(); | |
| 130 } | |
| 131 | |
| 132 private: | |
| 133 friend class AutocompleteActionPredictorTableTest; | |
| 134 | |
| 135 // RefcountedProfileKeyedService. | |
| 136 virtual void ShutdownOnUIThread() OVERRIDE; | |
| 137 | |
| 138 FilePath db_path_; | |
| 139 sql::Connection db_; | |
| 140 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(PredictorDatabase); | |
| 143 }; | |
| 144 | |
| 145 } // namespace predictors | |
| 146 | |
| 147 #endif // CHROME_BROWSER_PREDICTORS_PREDICTOR_DATABASE_H_ | |
| OLD | NEW |