Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: chrome/browser/predictors/predictor_database.h

Issue 9610006: Refactoring, moving and renaming the NetworkActionPredictor. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <ostream>
dominich 2012/03/06 16:42:13 why is ostream needed?
Shishir 2012/03/14 21:14:37 From the earlier file. Removed.
10 #include <string>
11 #include <vector>
12
13 #include "base/file_path.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/string16.h"
16 #include "base/synchronization/cancellation_flag.h"
17 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h"
18 #include "googleurl/src/gurl.h"
19 #include "sql/connection.h"
20
21 class Profile;
22
23 namespace predictors {
24
25 // Refcounted as it is created and destroyed in the UI thread, but its database
26 // related functions have to be called in the DB thread.
27 class PredictorTableBase
dominich 2012/03/06 16:42:13 Could be TableBase as we're in a namespace
Shishir 2012/03/14 21:14:37 As we discussed, leaving as is. We want the names
28 : public base::RefCountedThreadSafe<PredictorTableBase> {
29 protected:
30 PredictorTableBase();
31 virtual ~PredictorTableBase();
32 virtual void CreateTableIfNonExistant() = 0;
dominich 2012/03/06 16:42:13 nit: Existent
Shishir 2012/03/14 21:14:37 Done.
33
34 sql::Connection* db_;
35 base::CancellationFlag cancelled_;
36
37 private:
38 friend class PredictorDatabase;
39 friend class base::RefCountedThreadSafe<PredictorTableBase>;
40
41 void SetCancelled();
dominich 2012/03/06 16:42:13 note for dominic: it's a bit weird to have the fla
Shishir 2012/03/14 21:14:37 Yes, a little wierd, maybe move to protected?
42 void Initialize(sql::Connection* db);
43
44 DISALLOW_COPY_AND_ASSIGN(PredictorTableBase);
45 };
46
47
48 // This manages the autocomplete predictor table within the SQLite database
49 // passed in to the constructor. It expects the following scheme:
50 //
51 // network_action_predictor
52 // id A unique id.
53 // user_text What the user typed.
54 // url The URL of the entry.
55 // number_of_hits Number of times the entry was shown to the user and
56 // selected.
57 // number_of_misses Number of times the entry was shown to the user but not
58 // selected.
59 //
60 // TODO(dominich): Consider adding this table to one of the history databases.
61 // In memory is currently used, but adding to the on-disk visits database
62 // would allow DeleteOldEntries to be cheaper through use of a join.
63 class AutocompleteActionPredictorTable : public PredictorTableBase {
dominich 2012/03/06 16:42:13 could drop 'Predictor' from the class name
Shishir 2012/03/14 21:14:37 As discussed we want to keep this.
64 public:
65 struct Row {
66 // TODO(dominich): Make this 64-bit integer as an optimization. This
67 // requires some investigation into how to make sure the id is unique for
68 // each user_text/url pair.
69 // http://crbug.com/102020
70 typedef std::string Id;
71
72 Row();
73
74 // Only used by unit tests.
75 Row(const Id& id,
76 const string16& user_text,
77 const GURL& url,
78 int number_of_hits,
79 int number_of_misses);
80
81 Row(const Row& row);
82
83 Id id;
84 string16 user_text;
85 GURL url;
86 int number_of_hits;
87 int number_of_misses;
88 };
89
90 void GetRow(const Row::Id& id, Row* row);
91 void GetAllRows(std::vector<Row>* row_buffer);
92
93 void AddRow(const Row& row);
94 void UpdateRow(const Row& row);
95 void AddAndUpdateRows(const std::vector<Row>& rows_to_add,
96 const std::vector<Row>& rows_to_update);
97 void DeleteRow(const Row::Id& id);
98 void DeleteRows(const std::vector<Row::Id>& id_list);
99 void DeleteAllRows();
100
101 private:
102 friend class PredictorDatabase;
103
104 AutocompleteActionPredictorTable();
105 virtual ~AutocompleteActionPredictorTable();
106
107 void CreateTableIfNonExistant() OVERRIDE;
dominich 2012/03/06 16:42:13 nit: Existent
Shishir 2012/03/14 21:14:37 Done.
108
109 bool StepAndInitializeRow(sql::Statement* statement, Row* row);
110 void BindRowToStatement(const Row& row, sql::Statement* statement);
111
112 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictorTable);
113 };
114
115
116 // RefcountedProfileKeyedService as it is created and destroyed on a different
117 // thread to the DB thread that is required for all methods performing database
118 // access.
119 class PredictorDatabase : public RefcountedProfileKeyedService {
dominich 2012/03/06 16:42:13 could drop 'Predictor' from name
Shishir 2012/03/14 21:14:37 As we discussed, leaving as is. We want the names
120 public:
121 explicit PredictorDatabase(Profile* profile);
122 virtual ~PredictorDatabase();
123
124 // Opens the database file from the profile path. Separated from the
125 // constructor to ease construction/destruction of this object on one thread
126 // but database access on the DB thread.
127 void Initialize();
128
129 AutocompleteActionPredictorTable* autocomplete_table() {
130 return autocomplete_table_.get();
131 }
132
133 private:
134 friend class AutocompleteActionPredictorTableTest;
135
136 // RefcountedProfileKeyedService.
137 virtual void ShutdownOnUIThread() OVERRIDE;
138
139 FilePath db_path_;
140 sql::Connection db_;
141 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_;
142
143 DISALLOW_COPY_AND_ASSIGN(PredictorDatabase);
144 };
145
146 } // namespace predictors
147
148 #endif // CHROME_BROWSER_PREDICTORS_PREDICTOR_DATABASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698