OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // A policy for storing activity log data to a database that performs | 5 // A policy for storing activity log data to a database that performs |
6 // aggregation to reduce the size of the database. The database layout is | 6 // aggregation to reduce the size of the database. The database layout is |
7 // nearly the same as FullStreamUIPolicy, which stores a complete log, with a | 7 // nearly the same as FullStreamUIPolicy, which stores a complete log, with a |
8 // few changes: | 8 // few changes: |
9 // - a "count" column is added to track how many log records were merged | 9 // - a "count" column is added to track how many log records were merged |
10 // together into this row | 10 // together into this row |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
511 static_cast<base::DictionaryValue*>(parsed_value.release()))); | 511 static_cast<base::DictionaryValue*>(parsed_value.release()))); |
512 } | 512 } |
513 } | 513 } |
514 action->set_count(query.ColumnInt(9)); | 514 action->set_count(query.ColumnInt(9)); |
515 actions->push_back(action); | 515 actions->push_back(action); |
516 } | 516 } |
517 | 517 |
518 return actions.Pass(); | 518 return actions.Pass(); |
519 } | 519 } |
520 | 520 |
521 void CountingPolicy::DoRemoveActions(const std::vector<int64>& action_ids) { | |
mvrable
2014/02/11 17:44:35
Have you done any comparisons between this version
pmarch
2014/02/12 18:19:04
I removed the complexity and now the function dele
| |
522 if (action_ids.empty()) | |
523 return; | |
524 | |
525 sql::Connection* db = GetDatabaseConnection(); | |
526 if (!db) { | |
527 LOG(ERROR) << "Unable to connect to database"; | |
528 return; | |
529 } | |
530 | |
mvrable
2014/02/11 17:44:35
I would wrap all the deletions in a transaction (s
pmarch
2014/02/12 18:19:04
Done.
| |
531 // Flush data first so the activity removal affects queued-up data as well. | |
532 activity_database()->AdviseFlush(ActivityDatabase::kFlushImmediately); | |
533 | |
534 int batch_size = 50; | |
535 int iter = action_ids.size() / batch_size; | |
536 for (int i = 0; i < iter; i++) { | |
537 int start = i * batch_size; | |
538 int end = start + batch_size; | |
539 std::string rowid_set; | |
540 for (int j = start; j < end; j++) { | |
541 rowid_set += "?,"; | |
542 } | |
543 // Delete the last comma from the set. | |
544 rowid_set = rowid_set.substr(0, rowid_set.length() - 1); | |
545 std::string query_str = base::StringPrintf( | |
546 "DELETE FROM %s WHERE rowid in (%s)", kTableName, rowid_set.c_str()); | |
547 sql::Statement query(db->GetUniqueStatement(query_str.c_str())); | |
548 for (int j = start; j < end; j++) { | |
549 query.BindInt64(j - start, action_ids[j]); | |
550 } | |
551 if (!query.Run()) { | |
552 LOG(ERROR) << "Removing activities from database failed: " | |
553 << query.GetSQLStatement(); | |
554 CleanStringTables(db); | |
555 return; | |
556 } | |
557 } | |
558 int start = iter * batch_size; | |
mvrable
2014/02/11 17:44:35
What happens when action_ids.size() == batch_size?
pmarch
2014/02/12 18:19:04
Correct. I removed this code.
| |
559 int end = action_ids.size(); | |
560 std::string rowid_set; | |
561 for (int i = start; i < end; i++) { | |
562 rowid_set += "?,"; | |
563 } | |
564 // Delete the last comma from the set. | |
565 rowid_set = rowid_set.substr(0, rowid_set.length() - 1); | |
566 std::string query_str = base::StringPrintf( | |
567 "DELETE FROM %s WHERE rowid in (%s)", kTableName, rowid_set.c_str()); | |
568 sql::Statement query(db->GetUniqueStatement(query_str.c_str())); | |
569 for (int i = start; i < end; i++) { | |
570 query.BindInt64(i - start, action_ids[i]); | |
571 } | |
572 if (!query.Run()) { | |
573 LOG(ERROR) << "Removing activities from database failed: " | |
574 << query.GetSQLStatement(); | |
575 CleanStringTables(db); | |
576 return; | |
577 } | |
578 | |
579 // Clean up unused strings from the strings and urls table to really delete | |
580 // the urls and page titles. Should be called even if an error occured when | |
581 // removing a URL as there may some things to clean up. | |
582 CleanStringTables(db); | |
583 } | |
584 | |
521 void CountingPolicy::DoRemoveURLs(const std::vector<GURL>& restrict_urls) { | 585 void CountingPolicy::DoRemoveURLs(const std::vector<GURL>& restrict_urls) { |
522 sql::Connection* db = GetDatabaseConnection(); | 586 sql::Connection* db = GetDatabaseConnection(); |
523 if (!db) { | 587 if (!db) { |
524 LOG(ERROR) << "Unable to connect to database"; | 588 LOG(ERROR) << "Unable to connect to database"; |
525 return; | 589 return; |
526 } | 590 } |
527 | 591 |
528 // Flush data first so the URL clearing affects queued-up data as well. | 592 // Flush data first so the URL clearing affects queued-up data as well. |
529 activity_database()->AdviseFlush(ActivityDatabase::kFlushImmediately); | 593 activity_database()->AdviseFlush(ActivityDatabase::kFlushImmediately); |
530 | 594 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
685 base::Unretained(this), | 749 base::Unretained(this), |
686 extension_id, | 750 extension_id, |
687 type, | 751 type, |
688 api_name, | 752 api_name, |
689 page_url, | 753 page_url, |
690 arg_url, | 754 arg_url, |
691 days_ago), | 755 days_ago), |
692 callback); | 756 callback); |
693 } | 757 } |
694 | 758 |
759 void CountingPolicy::RemoveActions(const std::vector<int64>& action_ids) { | |
760 ScheduleAndForget(this, &CountingPolicy::DoRemoveActions, action_ids); | |
761 } | |
762 | |
695 void CountingPolicy::RemoveURLs(const std::vector<GURL>& restrict_urls) { | 763 void CountingPolicy::RemoveURLs(const std::vector<GURL>& restrict_urls) { |
696 ScheduleAndForget(this, &CountingPolicy::DoRemoveURLs, restrict_urls); | 764 ScheduleAndForget(this, &CountingPolicy::DoRemoveURLs, restrict_urls); |
697 } | 765 } |
698 | 766 |
699 void CountingPolicy::RemoveExtensionData(const std::string& extension_id) { | 767 void CountingPolicy::RemoveExtensionData(const std::string& extension_id) { |
700 ScheduleAndForget(this, &CountingPolicy::DoRemoveExtensionData, extension_id); | 768 ScheduleAndForget(this, &CountingPolicy::DoRemoveExtensionData, extension_id); |
701 } | 769 } |
702 | 770 |
703 void CountingPolicy::DeleteDatabase() { | 771 void CountingPolicy::DeleteDatabase() { |
704 ScheduleAndForget(this, &CountingPolicy::DoDeleteDatabase); | 772 ScheduleAndForget(this, &CountingPolicy::DoDeleteDatabase); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
745 return true; | 813 return true; |
746 } | 814 } |
747 | 815 |
748 void CountingPolicy::Close() { | 816 void CountingPolicy::Close() { |
749 // The policy object should have never been created if there's no DB thread. | 817 // The policy object should have never been created if there's no DB thread. |
750 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); | 818 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); |
751 ScheduleAndForget(activity_database(), &ActivityDatabase::Close); | 819 ScheduleAndForget(activity_database(), &ActivityDatabase::Close); |
752 } | 820 } |
753 | 821 |
754 } // namespace extensions | 822 } // namespace extensions |
OLD | NEW |