OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_DATABASE_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_DATABASE_H_ |
6 #define CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_DATABASE_H_ | 6 #define CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_DATABASE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <ostream> | 9 #include <ostream> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/string16.h" | 16 #include "base/string16.h" |
17 #include "base/synchronization/cancellation_flag.h" | 17 #include "base/synchronization/cancellation_flag.h" |
18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
19 #include "sql/connection.h" | 19 #include "sql/connection.h" |
20 | 20 |
21 class Profile; | 21 class Profile; |
22 | 22 |
23 // This manages the network action predictor table within the SQLite database | 23 // This manages the autocomplete action predictor table within the SQLite |
24 // passed in to the constructor. It expects the following scheme: | 24 // database passed in to the constructor. It expects the following scheme: |
25 // | 25 // |
26 // network_action_predictor | 26 // network_action_predictor |
27 // id A unique id. | 27 // id A unique id. |
28 // user_text What the user typed. | 28 // user_text What the user typed. |
29 // url The URL of the entry. | 29 // url The URL of the entry. |
30 // number_of_hits Number of times the entry was shown to the user and | 30 // number_of_hits Number of times the entry was shown to the user and |
31 // selected. | 31 // selected. |
32 // number_of_misses Number of times the entry was shown to the user but not | 32 // number_of_misses Number of times the entry was shown to the user but not |
33 // selected. | 33 // selected. |
34 // | 34 // |
35 // Ref-counted as it is created and destroyed on a different thread to the DB | 35 // Ref-counted as it is created and destroyed on a different thread to the DB |
36 // thread that is required for all methods performing database access. | 36 // thread that is required for all methods performing database access. |
37 class NetworkActionPredictorDatabase | 37 class AutocompleteActionPredictorDatabase |
38 : public base::RefCountedThreadSafe<NetworkActionPredictorDatabase> { | 38 : public base::RefCountedThreadSafe<AutocompleteActionPredictorDatabase> { |
39 public: | 39 public: |
40 struct Row { | 40 struct Row { |
41 // TODO(dominich): Make this 64-bit integer as an optimization. This | 41 // TODO(dominich): Make this 64-bit integer as an optimization. This |
42 // requires some investigation into how to make sure the id is unique for | 42 // requires some investigation into how to make sure the id is unique for |
43 // each user_text/url pair. | 43 // each user_text/url pair. |
44 // http://crbug.com/102020 | 44 // http://crbug.com/102020 |
45 typedef std::string Id; | 45 typedef std::string Id; |
46 | 46 |
47 Row(); | 47 Row(); |
48 | 48 |
49 // Only used by unit tests. | 49 // Only used by unit tests. |
50 Row(const Id& id, | 50 Row(const Id& id, |
51 const string16& user_text, | 51 const string16& user_text, |
52 const GURL& url, | 52 const GURL& url, |
53 int number_of_hits, | 53 int number_of_hits, |
54 int number_of_misses); | 54 int number_of_misses); |
55 | 55 |
56 Id id; | 56 Id id; |
57 string16 user_text; | 57 string16 user_text; |
58 GURL url; | 58 GURL url; |
59 int number_of_hits; | 59 int number_of_hits; |
60 int number_of_misses; | 60 int number_of_misses; |
61 }; | 61 }; |
62 | 62 |
63 explicit NetworkActionPredictorDatabase(Profile* profile); | 63 explicit AutocompleteActionPredictorDatabase(Profile* profile); |
64 | 64 |
65 // Opens the database file from the profile path. Separated from the | 65 // Opens the database file from the profile path. Separated from the |
66 // constructor to ease construction/destruction of this object on one thread | 66 // constructor to ease construction/destruction of this object on one thread |
67 // but database access on the DB thread. | 67 // but database access on the DB thread. |
68 void Initialize(); | 68 void Initialize(); |
69 | 69 |
70 void GetRow(const Row::Id& id, Row* row); | 70 void GetRow(const Row::Id& id, Row* row); |
71 void GetAllRows(std::vector<Row>* row_buffer); | 71 void GetAllRows(std::vector<Row>* row_buffer); |
72 | 72 |
73 void AddRow(const Row& row); | 73 void AddRow(const Row& row); |
74 void UpdateRow(const Row& row); | 74 void UpdateRow(const Row& row); |
75 void DeleteRow(const Row::Id& id); | 75 void DeleteRow(const Row::Id& id); |
76 void DeleteRows(const std::vector<Row::Id>& id_list); | 76 void DeleteRows(const std::vector<Row::Id>& id_list); |
77 void DeleteAllRows(); | 77 void DeleteAllRows(); |
78 | 78 |
79 // For batching database operations. | 79 // For batching database operations. |
80 void BeginTransaction(); | 80 void BeginTransaction(); |
81 void CommitTransaction(); | 81 void CommitTransaction(); |
82 | 82 |
83 void OnPredictorDestroyed(); | 83 void OnPredictorDestroyed(); |
84 | 84 |
85 private: | 85 private: |
86 friend class NetworkActionPredictorDatabaseTest; | 86 friend class AutocompleteActionPredictorDatabaseTest; |
87 friend class base::RefCountedThreadSafe<NetworkActionPredictorDatabase>; | 87 friend class base::RefCountedThreadSafe<AutocompleteActionPredictorDatabase>; |
88 virtual ~NetworkActionPredictorDatabase(); | 88 virtual ~AutocompleteActionPredictorDatabase(); |
89 | 89 |
90 void CreateTable(); | 90 void CreateTable(); |
91 | 91 |
92 // TODO(dominich): Consider adding this table to one of the history databases. | 92 // TODO(dominich): Consider adding this table to one of the history databases. |
93 // In memory is currently used, but adding to the on-disk visits database | 93 // In memory is currently used, but adding to the on-disk visits database |
94 // would allow DeleteOldEntries to be cheaper through use of a join. | 94 // would allow DeleteOldEntries to be cheaper through use of a join. |
95 FilePath db_path_; | 95 FilePath db_path_; |
96 sql::Connection db_; | 96 sql::Connection db_; |
97 | 97 |
98 // Set when the NetworkActionPredictor is destroyed so we can cancel any | 98 // Set when the AutocompleteActionPredictor is destroyed so we can cancel any |
99 // posted database requests. | 99 // posted database requests. |
100 base::CancellationFlag canceled_; | 100 base::CancellationFlag canceled_; |
101 | 101 |
102 DISALLOW_COPY_AND_ASSIGN(NetworkActionPredictorDatabase); | 102 DISALLOW_COPY_AND_ASSIGN(AutocompleteActionPredictorDatabase); |
103 }; | 103 }; |
104 | 104 |
105 #endif // CHROME_BROWSER_AUTOCOMPLETE_NETWORK_ACTION_PREDICTOR_DATABASE_H_ | 105 #endif // CHROME_BROWSER_PREDICTORS_AUTOCOMPLETE_ACTION_PREDICTOR_DATABASE_H_ |
OLD | NEW |