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

Side by Side Diff: components/safe_browsing_db/v4_store.cc

Issue 2403913004: Small: Serialize the store's hash_prefix_map_ to file in WriteToDisk() (Closed)
Patch Set: Serialize the store's hash_prefix_map_ to file in WriteToDisk() Created 4 years, 2 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/base64.h" 5 #include "base/base64.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
10 #include "base/metrics/sparse_histogram.h" 10 #include "base/metrics/sparse_histogram.h"
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 ApplyUpdateResult V4Store::ProcessPartialUpdateAndWriteToDisk( 184 ApplyUpdateResult V4Store::ProcessPartialUpdateAndWriteToDisk(
185 const HashPrefixMap& hash_prefix_map_old, 185 const HashPrefixMap& hash_prefix_map_old,
186 std::unique_ptr<ListUpdateResponse> response) { 186 std::unique_ptr<ListUpdateResponse> response) {
187 DCHECK(response->has_response_type()); 187 DCHECK(response->has_response_type());
188 DCHECK_EQ(ListUpdateResponse::PARTIAL_UPDATE, response->response_type()); 188 DCHECK_EQ(ListUpdateResponse::PARTIAL_UPDATE, response->response_type());
189 189
190 TimeTicks before = TimeTicks::Now(); 190 TimeTicks before = TimeTicks::Now();
191 ApplyUpdateResult result = ProcessUpdate(hash_prefix_map_old, response); 191 ApplyUpdateResult result = ProcessUpdate(hash_prefix_map_old, response);
192 if (result == APPLY_UPDATE_SUCCESS) { 192 if (result == APPLY_UPDATE_SUCCESS) {
193 RecordProcessPartialUpdateTime(TimeTicks::Now() - before, store_path_); 193 RecordProcessPartialUpdateTime(TimeTicks::Now() - before, store_path_);
194 // TODO(vakh): Create a ListUpdateResponse containing RICE encoded 194 RecordStoreWriteResult(
195 // hash prefixes and response_type as FULL_UPDATE, and write that to disk. 195 WriteToDisk(ListIdentifier(*response), response->checksum()));
196 } 196 }
197 return result; 197 return result;
198 } 198 }
199 199
200 ApplyUpdateResult V4Store::ProcessFullUpdateAndWriteToDisk( 200 ApplyUpdateResult V4Store::ProcessFullUpdateAndWriteToDisk(
201 std::unique_ptr<ListUpdateResponse> response) { 201 std::unique_ptr<ListUpdateResponse> response) {
202 TimeTicks before = TimeTicks::Now(); 202 TimeTicks before = TimeTicks::Now();
203 ApplyUpdateResult result = ProcessFullUpdate(response); 203 ApplyUpdateResult result = ProcessFullUpdate(response);
204 if (result == APPLY_UPDATE_SUCCESS) { 204 if (result == APPLY_UPDATE_SUCCESS) {
205 RecordProcessFullUpdateTime(TimeTicks::Now() - before, store_path_); 205 RecordProcessFullUpdateTime(TimeTicks::Now() - before, store_path_);
206 RecordStoreWriteResult(WriteToDisk(std::move(response))); 206 RecordStoreWriteResult(
207 WriteToDisk(ListIdentifier(*response), response->checksum()));
207 } 208 }
208 return result; 209 return result;
209 } 210 }
210 211
211 ApplyUpdateResult V4Store::ProcessFullUpdate( 212 ApplyUpdateResult V4Store::ProcessFullUpdate(
212 const std::unique_ptr<ListUpdateResponse>& response) { 213 const std::unique_ptr<ListUpdateResponse>& response) {
213 DCHECK(response->has_response_type()); 214 DCHECK(response->has_response_type());
214 DCHECK_EQ(ListUpdateResponse::FULL_UPDATE, response->response_type()); 215 DCHECK_EQ(ListUpdateResponse::FULL_UPDATE, response->response_type());
215 // TODO(vakh): For a full update, we don't need to process the update in 216 // TODO(vakh): For a full update, we don't need to process the update in
216 // lexographical order to store it, but we do need to do that for calculating 217 // lexographical order to store it, but we do need to do that for calculating
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 RecordApplyUpdateResultWhenReadingFromDisk(apply_update_result); 632 RecordApplyUpdateResultWhenReadingFromDisk(apply_update_result);
632 if (apply_update_result != APPLY_UPDATE_SUCCESS) { 633 if (apply_update_result != APPLY_UPDATE_SUCCESS) {
633 hash_prefix_map_.clear(); 634 hash_prefix_map_.clear();
634 return HASH_PREFIX_MAP_GENERATION_FAILURE; 635 return HASH_PREFIX_MAP_GENERATION_FAILURE;
635 } 636 }
636 RecordReadFromDiskTime(TimeTicks::Now() - before, store_path_); 637 RecordReadFromDiskTime(TimeTicks::Now() - before, store_path_);
637 638
638 return READ_SUCCESS; 639 return READ_SUCCESS;
639 } 640 }
640 641
641 StoreWriteResult V4Store::WriteToDisk( 642 StoreWriteResult V4Store::WriteToDisk(const ListIdentifier& list_id,
642 std::unique_ptr<ListUpdateResponse> response) const { 643 const Checksum& checksum) const {
643 // Do not write partial updates to the disk. 644 V4StoreFileFormat file_format;
644 // After merging the updates, the ListUpdateResponse passed to this method 645 ListUpdateResponse* lur = file_format.mutable_list_update_response();
645 // should be a FULL_UPDATE. 646 *(lur->mutable_checksum()) = checksum;
Nathan Parker 2016/10/11 20:29:41 nit: lur->set_checksum(checksum)?
vakh (use Gerrit instead) 2016/10/11 21:50:39 set_ is only available for primitive types. See: h
646 if (!response->has_response_type() || 647 lur->set_new_client_state(state_);
647 response->response_type() != ListUpdateResponse::FULL_UPDATE) { 648 lur->set_platform_type(list_id.platform_type());
648 DVLOG(1) << "Failure: response->has_response_type(): " 649 lur->set_response_type(ListUpdateResponse::FULL_UPDATE);
649 << response->has_response_type() 650 lur->set_threat_entry_type(list_id.threat_entry_type());
650 << " : response->response_type(): " << response->response_type(); 651 lur->set_threat_type(list_id.threat_type());
651 return INVALID_RESPONSE_TYPE_FAILURE; 652 for (auto map_iter : hash_prefix_map_) {
653 ThreatEntrySet* additions = lur->add_additions();
654 // TODO(vakh): Write RICE encoded hash prefixes on disk. Not doing so
655 // currently since it takes a long time to decode them on startup, which
656 // blocks resource load.
657 additions->set_compression_type(RAW);
658 additions->mutable_raw_hashes()->set_prefix_size(map_iter.first);
659 additions->mutable_raw_hashes()->set_raw_hashes(map_iter.second);
Nathan Parker 2016/10/11 20:29:41 Note: This creates another full copy of the whole
vakh (use Gerrit instead) 2016/10/11 21:50:39 Ack.
652 } 660 }
653 661
654 // Attempt writing to a temporary file first and at the end, swap the files. 662 // Attempt writing to a temporary file first and at the end, swap the files.
655 const base::FilePath new_filename = TemporaryFileForFilename(store_path_); 663 const base::FilePath new_filename = TemporaryFileForFilename(store_path_);
656 664
657 V4StoreFileFormat file_format;
658 file_format.set_magic_number(kFileMagic); 665 file_format.set_magic_number(kFileMagic);
659 file_format.set_version_number(kFileVersion); 666 file_format.set_version_number(kFileVersion);
660 ListUpdateResponse* response_to_write =
661 file_format.mutable_list_update_response();
662 response_to_write->Swap(response.get());
663 std::string file_format_string; 667 std::string file_format_string;
664 file_format.SerializeToString(&file_format_string); 668 file_format.SerializeToString(&file_format_string);
665 size_t written = base::WriteFile(new_filename, file_format_string.data(), 669 size_t written = base::WriteFile(new_filename, file_format_string.data(),
666 file_format_string.size()); 670 file_format_string.size());
667 DCHECK_EQ(file_format_string.size(), written); 671 DCHECK_EQ(file_format_string.size(), written);
668 672
669 if (!base::Move(new_filename, store_path_)) { 673 if (!base::Move(new_filename, store_path_)) {
670 return UNABLE_TO_RENAME_FAILURE; 674 return UNABLE_TO_RENAME_FAILURE;
671 } 675 }
672 676
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 << "; expected: " << expected_checksum_b64 764 << "; expected: " << expected_checksum_b64
761 << "; store: " << *this; 765 << "; store: " << *this;
762 #endif 766 #endif
763 return false; 767 return false;
764 } 768 }
765 } 769 }
766 return true; 770 return true;
767 } 771 }
768 772
769 } // namespace safe_browsing 773 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698