Index: components/previews/core/previews_opt_out_store_sql.cc |
diff --git a/components/previews/core/previews_opt_out_store_sql.cc b/components/previews/core/previews_opt_out_store_sql.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..384b161f74cd3e5565e58928757a4e8aef4aa8b7 |
--- /dev/null |
+++ b/components/previews/core/previews_opt_out_store_sql.cc |
@@ -0,0 +1,193 @@ |
+// Copyright 2016 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. |
+ |
+#include "components/previews/core/previews_opt_out_store_sql.h" |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/files/file_util.h" |
+#include "base/location.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/sequenced_task_runner.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "components/previews/core/previews_black_list_item.h" |
+#include "components/previews/core/previews_experiments.h" |
+#include "sql/connection.h" |
+#include "sql/statement.h" |
+#include "sql/transaction.h" |
+ |
+namespace previews { |
+ |
+namespace { |
+ |
+// This is a macro instead of a const, so it can be used inline in other SQL |
+// statements below. |
+#define PREVIEWS_TABLE_NAME "previews_v1" |
+ |
+// New columns should be added at the end of the list in order to avoid |
+// complicated table upgrade. |
+const char kPreviewsColumns[] = |
+ "(host_name VARCHAR NOT NULL DEFAULT ''," |
+ " time INTEGER NOT NULL," |
+ " opt_out INTEGER NOT NULL," |
+ " type INTEGER NOT NULL)"; |
Scott Hess - ex-Googler
2016/10/06 17:24:28
Any unique key? Otherwise it's just a bag of data
RyanSturm
2016/10/06 20:05:39
Besides adding a unique key just to add a unique k
Scott Hess - ex-Googler
2016/10/07 00:08:46
I'm not really sure what "unbounded in the db" mea
RyanSturm
2016/10/07 20:07:05
Good point. The deleting query I wrote seem like t
Scott Hess - ex-Googler
2016/10/08 00:04:21
How often will a new entry come in?
If it's frequ
RyanSturm
2016/10/10 18:50:35
It's a per navigation under certain conditions (2G
Scott Hess - ex-Googler
2016/10/10 19:57:45
OK, then a mixed bag, I guess. The dominant cost
RyanSturm
2016/10/10 20:16:57
Makes sense to me.
|
+ |
+struct TableInfo { |
+ const char* table_name; |
+ const char* columns; |
+}; |
+ |
+// This is the order the columns are in within the table and within queries that |
+// return columns from the table. |
+enum : int { |
+ OP_HOST_NAME = 0, |
+ OP_TIME, |
+ OP_OPT_OUT, |
+ OP_TYPE, |
+}; |
+ |
+const TableInfo kPreviewsTable{PREVIEWS_TABLE_NAME, kPreviewsColumns}; |
+ |
+bool CreateTable(sql::Connection* db, const TableInfo& table_info) { |
+ std::string sql("CREATE TABLE "); |
+ sql += table_info.table_name; |
+ sql += table_info.columns; |
+ return db->Execute(sql.c_str()); |
+} |
+ |
+void CreateSchema(sql::Connection* db) { |
+ sql::Transaction transaction(db); |
+ if (!transaction.Begin()) |
+ return; |
+ |
+ if (!db->DoesTableExist(kPreviewsTable.table_name)) { |
+ if (!CreateTable(db, kPreviewsTable)) |
+ return; |
+ } |
+ |
+ transaction.Commit(); |
+} |
+ |
+void InitDatabase(sql::Connection* db, base::FilePath path) { |
+ db->set_page_size(4096); |
+ db->set_cache_size(250); |
Scott Hess - ex-Googler
2016/10/06 17:24:28
How big is all this data going to be? Like averag
RyanSturm
2016/10/06 20:05:39
I added a pretty verbose comment. Comments on it w
|
+ db->set_histogram_tag("PreviewsOptOut"); |
+ db->set_exclusive_locking(); |
+ |
+ base::File::Error err; |
+ if (!base::CreateDirectoryAndGetError(path.DirName(), &err)) { |
+ return; |
+ } |
+ if (!db->Open(path)) { |
+ return; |
+ } |
+ db->Preload(); |
+ |
+ CreateSchema(db); |
+} |
Scott Hess - ex-Googler
2016/10/06 17:24:28
So if all of this fails, you're just going to give
RyanSturm
2016/10/06 20:05:39
If it fails for this session, I don't plan for it
Scott Hess - ex-Googler
2016/10/07 00:08:45
It's persistent on disk, so most failures will be
RyanSturm
2016/10/07 20:07:06
I've gone ahead and used The DatabaseErrorCallback
Scott Hess - ex-Googler
2016/10/08 00:04:21
Things are wired so that all future calls fail.
T
RyanSturm
2016/10/10 18:50:35
Acknowledged.
|
+ |
+void LoadBlackListFromDataBase( |
+ sql::Connection* db, |
+ scoped_refptr<base::SingleThreadTaskRunner> runner, |
+ LoadBlackListCallback callback) { |
+ // Gets the table sorted by host and time. Limits the number of hosts using |
+ // most recent opt_out time as the limiting function. |
+ const char kSql[] = |
+ "SELECT full_table.host_name, full_table.time, full_table.opt_out" |
+ " FROM " PREVIEWS_TABLE_NAME |
+ " full_table INNER JOIN" |
+ " (SELECT host_name," |
+ " MAX(CASE WHEN opt_out > 0 THEN time ELSE 0 END) AS max_opt_out_time," |
+ " MAX(time) AS max_time FROM " PREVIEWS_TABLE_NAME |
+ " GROUP BY host_name" |
+ " ORDER BY max_opt_out_time DESC, max_time DESC" |
+ " LIMIT ?) max_host_table" |
+ " ON max_host_table.host_name == full_table.host_name"; |
Scott Hess - ex-Googler
2016/10/06 17:24:28
This is going to cause multiple full table scans.
RyanSturm
2016/10/06 20:05:39
Thanks. C++ sounds like the way to go.
|
+ |
+ sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
+ statement.BindInt(0, static_cast<int>(params::MaxInMemoryHostsInBlackList())); |
+ std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap()); |
+ // Add the host name, the visit time, and opt out history to |
+ // |black_list_item_map|. |
+ while (statement.Step()) { |
+ std::string host_name = statement.ColumnString(OP_HOST_NAME); |
+ BlackListItemMap::iterator iter = black_list_item_map->find(host_name); |
+ PreviewsBlackListItem* black_list_item; |
+ if (iter == black_list_item_map->end()) { |
+ black_list_item = new PreviewsBlackListItem( |
+ params::MaxStoredHistoryLengthForBlackList(), |
+ params::BlackListOptOutThreshold(), params::BlackListDuration()); |
+ black_list_item_map->operator[](host_name) = |
+ base::WrapUnique(black_list_item); |
Scott Hess - ex-Googler
2016/10/06 17:24:28
Took me a second to parse this one because of the
RyanSturm
2016/10/06 20:05:39
I've moved to using a static in PreviewsBlackList;
|
+ } else { |
+ black_list_item = iter->second.get(); |
+ } |
+ // Allows the internal logic of PreviewsBlackListItem to determine how to |
+ // evict entries when there are more than |
+ // |StoredHistoryLengthForBlackList()| for the host. |
+ black_list_item->AddPreviewNavigation( |
+ statement.ColumnBool(OP_OPT_OUT), |
+ base::Time::FromInternalValue(statement.ColumnInt64(OP_TIME))); |
+ } |
+ |
+ runner->PostTask(FROM_HERE, |
+ base::Bind(callback, base::Passed(&black_list_item_map))); |
+} |
+ |
+// Synchronous implementation, this is run on the background thread |
+// and actually does the work to access SQL. |
+void LoadBlackListSync(sql::Connection* db, |
+ const base::FilePath& path, |
+ bool needs_initialization, |
+ scoped_refptr<base::SingleThreadTaskRunner> runner, |
+ LoadBlackListCallback callback) { |
+ if (needs_initialization) { |
Scott Hess - ex-Googler
2016/10/06 17:24:28
Any reason you're using this odd flag-passing rath
RyanSturm
2016/10/06 20:05:39
Done.
|
+ InitDatabase(db, path); |
+ } |
+ |
+ if (!db->is_open() || !db->DoesTableExist(kPreviewsTable.table_name)) { |
+ std::unique_ptr<BlackListItemMap> black_list_item_map( |
+ new BlackListItemMap()); |
+ runner->PostTask(FROM_HERE, |
+ base::Bind(callback, base::Passed(&black_list_item_map))); |
+ } |
+ |
+ LoadBlackListFromDataBase(db, runner, callback); |
Scott Hess - ex-Googler
2016/10/06 17:24:28
I think that if you implemented LoadBlackListFromD
RyanSturm
2016/10/06 20:05:39
I think I understand what you are saying, so let m
Scott Hess - ex-Googler
2016/10/07 00:08:45
I was thinking more like having LoadBlackListFromD
RyanSturm
2016/10/07 20:07:06
Acknowledged.
|
+} |
+ |
+} // namespace |
+ |
+PreviewsOptOutStoreSQL::PreviewsOptOutStoreSQL( |
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
+ scoped_refptr<base::SequencedTaskRunner> background_task_runner, |
+ const base::FilePath& path) |
+ : io_task_runner_(io_task_runner), |
+ background_task_runner_(background_task_runner), |
+ db_file_path_(path.AppendASCII("Previews.db")), |
+ initialized_(false) {} |
+ |
+PreviewsOptOutStoreSQL::~PreviewsOptOutStoreSQL() { |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
+ if (db_.get()) { |
+ background_task_runner_->DeleteSoon(FROM_HERE, db_.release()); |
+ } |
+} |
Scott Hess - ex-Googler
2016/10/06 17:24:28
Is there any protection against an outstanding Loa
RyanSturm
2016/10/06 20:05:39
My understanding of task runners is that the LoadB
Scott Hess - ex-Googler
2016/10/07 00:08:45
Yeah. I found it - the LoadBlackList() callback t
RyanSturm
2016/10/07 20:07:05
Acknowledged.
|
+ |
+void PreviewsOptOutStoreSQL::AddPreviewNavigation(bool opt_out, |
+ const std::string& host_name, |
+ PreviewsType type, |
+ base::Time now) {} |
+ |
+void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
+ if (!db_) |
+ db_.reset(new sql::Connection()); |
+ background_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, !initialized_, |
+ base::ThreadTaskRunnerHandle::Get(), callback)); |
+ initialized_ = true; |
+} |
+ |
+} // namespace previews |