OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include <limits> | 5 #include <limits> |
6 #include <set> | 6 #include <set> |
7 | 7 |
8 #include "chrome/browser/history/text_database.h" | 8 #include "chrome/browser/history/text_database.h" |
9 | 9 |
10 #include "app/sql/connection.h" | 10 #include "app/sql/connection.h" |
11 #include "app/sql/statement.h" | 11 #include "app/sql/statement.h" |
12 #include "app/sql/transaction.h" | 12 #include "app/sql/transaction.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/histogram.h" | 14 #include "base/histogram.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
17 | 18 |
18 // There are two tables in each database, one full-text search (FTS) table which | 19 // There are two tables in each database, one full-text search (FTS) table which |
19 // indexes the contents and title of the pages. The other is a regular SQLITE | 20 // indexes the contents and title of the pages. The other is a regular SQLITE |
20 // table which contains non-indexed information about the page. All columns of | 21 // table which contains non-indexed information about the page. All columns of |
21 // a FTS table are indexed using the text search algorithm, which isn't what we | 22 // a FTS table are indexed using the text search algorithm, which isn't what we |
22 // want for things like times. If this were in the FTS table, there would be | 23 // want for things like times. If this were in the FTS table, there would be |
23 // different words in the index for each time number. | 24 // different words in the index for each time number. |
24 // | 25 // |
25 // "pages" FTS table: | 26 // "pages" FTS table: |
26 // url URL of the page so searches will match the URL. | 27 // url URL of the page so searches will match the URL. |
(...skipping 19 matching lines...) Expand all Loading... |
46 // create statement. These are the 0-based indices (as strings) of the | 47 // create statement. These are the 0-based indices (as strings) of the |
47 // corresponding columns. | 48 // corresponding columns. |
48 const char kTitleColumnIndex[] = "1"; | 49 const char kTitleColumnIndex[] = "1"; |
49 const char kBodyColumnIndex[] = "2"; | 50 const char kBodyColumnIndex[] = "2"; |
50 | 51 |
51 // The string prepended to the database identifier to generate the filename. | 52 // The string prepended to the database identifier to generate the filename. |
52 const FilePath::CharType kFilePrefix[] = FILE_PATH_LITERAL("History Index "); | 53 const FilePath::CharType kFilePrefix[] = FILE_PATH_LITERAL("History Index "); |
53 | 54 |
54 } // namespace | 55 } // namespace |
55 | 56 |
56 // This class handles the exceptional sqlite errors that we might encounter | |
57 // if for example the db is corrupted. Right now we just generate a UMA | |
58 // histogram for release and an assert for debug builds. | |
59 class TextDbSqliteErrrorHandler : public sql::ErrorDelegate { | |
60 public: | |
61 virtual int OnError(int error, sql::Connection* connection, | |
62 sql::Statement* stmt) { | |
63 NOTREACHED() << "history db sqlite error " << error; | |
64 RecordErrorInHistogram(error); | |
65 return error; | |
66 } | |
67 private: | |
68 static void RecordErrorInHistogram(int error) { | |
69 // The histogram values from sqlite result codes go currently from 1 to | |
70 // 26 currently but 100 gives them room to grow. | |
71 static LinearHistogram histogram("Sqlite.History.Error", 1, 50, 51); | |
72 histogram.SetFlags(kUmaTargetedHistogramFlag); | |
73 histogram.Add(error); | |
74 } | |
75 }; | |
76 | |
77 TextDatabase::TextDatabase(const FilePath& path, | 57 TextDatabase::TextDatabase(const FilePath& path, |
78 DBIdent id, | 58 DBIdent id, |
79 bool allow_create) | 59 bool allow_create) |
80 : path_(path), | 60 : path_(path), |
81 ident_(id), | 61 ident_(id), |
82 allow_create_(allow_create) { | 62 allow_create_(allow_create) { |
83 // Compute the file name. | 63 // Compute the file name. |
84 file_name_ = path_.Append(IDToFileName(ident_)); | 64 file_name_ = path_.Append(IDToFileName(ident_)); |
85 } | 65 } |
86 | 66 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 110 } |
131 | 111 |
132 bool TextDatabase::Init() { | 112 bool TextDatabase::Init() { |
133 // Make sure, if we're not allowed to create the file, that it exists. | 113 // Make sure, if we're not allowed to create the file, that it exists. |
134 if (!allow_create_) { | 114 if (!allow_create_) { |
135 if (!file_util::PathExists(file_name_)) | 115 if (!file_util::PathExists(file_name_)) |
136 return false; | 116 return false; |
137 } | 117 } |
138 | 118 |
139 // Set the exceptional sqlite error handler. | 119 // Set the exceptional sqlite error handler. |
140 db_.set_error_delegate(new TextDbSqliteErrrorHandler()); | 120 db_.set_error_delegate(GetErrorHandlerForTextDb()); |
141 | 121 |
142 // Set the database page size to something a little larger to give us | 122 // Set the database page size to something a little larger to give us |
143 // better performance (we're typically seek rather than bandwidth limited). | 123 // better performance (we're typically seek rather than bandwidth limited). |
144 // This only has an effect before any tables have been created, otherwise | 124 // This only has an effect before any tables have been created, otherwise |
145 // this is a NOP. Must be a power of 2 and a max of 8192. | 125 // this is a NOP. Must be a power of 2 and a max of 8192. |
146 db_.set_page_size(2096); | 126 db_.set_page_size(2096); |
147 | 127 |
148 // The default cache size is 2000 which give >8MB of data. Since we will often | 128 // The default cache size is 2000 which give >8MB of data. Since we will often |
149 // have 2-3 of these objects, each with their own 8MB, this adds up very fast. | 129 // have 2-3 of these objects, each with their own 8MB, this adds up very fast. |
150 // We therefore reduce the size so when there are multiple objects, we're not | 130 // We therefore reduce the size so when there are multiple objects, we're not |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 } else { | 367 } else { |
388 // Since we got the results in order, we know the last item is the last | 368 // Since we got the results in order, we know the last item is the last |
389 // time we considered. | 369 // time we considered. |
390 *first_time_searched = results->back().time; | 370 *first_time_searched = results->back().time; |
391 } | 371 } |
392 | 372 |
393 statement.Reset(); | 373 statement.Reset(); |
394 } | 374 } |
395 | 375 |
396 } // namespace history | 376 } // namespace history |
OLD | NEW |