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

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

Issue 2145163003: PVer4: Keep track of the smallest hashes not their sizes. Replace counters with iterators. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@01_read_map_from_disk
Patch Set: Update comment about iterator initialization Created 4 years, 5 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
« no previous file with comments | « no previous file | components/safe_browsing_db/v4_store.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
7 7
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/sequenced_task_runner.h" 10 #include "base/sequenced_task_runner.h"
(...skipping 14 matching lines...) Expand all
25 // A hash prefix sent by the SafeBrowsing PVer4 service. 25 // A hash prefix sent by the SafeBrowsing PVer4 service.
26 typedef std::string HashPrefix; 26 typedef std::string HashPrefix;
27 27
28 // The sorted list of hash prefixes. 28 // The sorted list of hash prefixes.
29 typedef std::string HashPrefixes; 29 typedef std::string HashPrefixes;
30 30
31 // Stores the list of sorted hash prefixes, by size. 31 // Stores the list of sorted hash prefixes, by size.
32 // For instance: {4: ["abcd", "bcde", "cdef", "gggg"], 5: ["fffff"]} 32 // For instance: {4: ["abcd", "bcde", "cdef", "gggg"], 5: ["fffff"]}
33 typedef base::hash_map<PrefixSize, HashPrefixes> HashPrefixMap; 33 typedef base::hash_map<PrefixSize, HashPrefixes> HashPrefixMap;
34 34
35 // Stores the index of the last element merged from the HashPrefixMap for a 35 // Stores the iterator to the last element merged from the HashPrefixMap for a
36 // given prefix size. For instance: {4:3, 5:1} means that we have already merged 36 // given prefix size.
37 // For instance: {4:iter(3), 5:iter(1)} means that we have already merged
37 // 3 hash prefixes of length 4, and 1 hash prefix of length 5. 38 // 3 hash prefixes of length 4, and 1 hash prefix of length 5.
38 typedef base::hash_map<PrefixSize, size_t> CounterMap; 39 typedef base::hash_map<PrefixSize, HashPrefixes::const_iterator> IteratorMap;
39 40
40 // Enumerate different failure events while parsing the file read from disk for 41 // Enumerate different failure events while parsing the file read from disk for
41 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. 42 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES.
42 enum StoreReadResult { 43 enum StoreReadResult {
43 // No errors. 44 // No errors.
44 READ_SUCCESS = 0, 45 READ_SUCCESS = 0,
45 46
46 // Reserved for errors in parsing this enum. 47 // Reserved for errors in parsing this enum.
47 UNEXPECTED_READ_FAILURE = 1, 48 UNEXPECTED_READ_FAILURE = 1,
48 49
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); 179 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile);
179 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); 180 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType);
180 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); 181 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType);
181 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); 182 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType);
182 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); 183 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto);
183 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 184 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
184 TestAddUnlumpedHashesWithInvalidAddition); 185 TestAddUnlumpedHashesWithInvalidAddition);
185 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes); 186 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes);
186 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString); 187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString);
187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 188 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
188 TestGetNextSmallestPrefixSizeWithEmptyPrefixMap); 189 TestGetNextSmallestUnmergedPrefixWithEmptyPrefixMap);
189 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestPrefixSize); 190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestUnmergedPrefix);
190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextUnmergedPrefix);
191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesWithSameSizesInEachMap); 191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesWithSameSizesInEachMap);
192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
193 TestMergeUpdatesWithDifferentSizesInEachMap); 193 TestMergeUpdatesWithDifferentSizesInEachMap);
194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesOldMapRunsOutFirst); 194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesOldMapRunsOutFirst);
195 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 195 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
196 TestMergeUpdatesAdditionsMapRunsOutFirst); 196 TestMergeUpdatesAdditionsMapRunsOutFirst);
197 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 197 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
198 TestMergeUpdatesFailsForRepeatedHashPrefix); 198 TestMergeUpdatesFailsForRepeatedHashPrefix);
199 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 199 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
200 TestReadFullResponseWithValidHashPrefixMap); 200 TestReadFullResponseWithValidHashPrefixMap);
201 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 201 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
202 TestReadFullResponseWithInvalidHashPrefixMap); 202 TestReadFullResponseWithInvalidHashPrefixMap);
203 203
204 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, 204 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid,
205 // then it sets |raw_hashes| as the value at key |prefix_size| in 205 // then it sets |raw_hashes| as the value at key |prefix_size| in
206 // |additions_map| 206 // |additions_map|
207 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, 207 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size,
208 const std::string& raw_hashes, 208 const std::string& raw_hashes,
209 HashPrefixMap* additions_map); 209 HashPrefixMap* additions_map);
210 210
211 // Get the size of the next unmerged hash prefix in dictionary order from 211 // Get the next unmerged hash prefix in dictionary order from
212 // |hash_prefix_map|. |counter_map| is used to determine which hash prefixes 212 // |hash_prefix_map|. |iterator_map| is used to determine which hash prefixes
213 // have been merged already. Returns true if there are any unmerged hash 213 // have been merged already. Returns true if there are any unmerged hash
214 // prefixes in the list. 214 // prefixes in the list.
215 static bool GetNextSmallestPrefixSize(const HashPrefixMap& hash_prefix_map, 215 static bool GetNextSmallestUnmergedPrefix(
216 const CounterMap& counter_map, 216 const HashPrefixMap& hash_prefix_map,
217 PrefixSize* smallest_prefix_size); 217 const IteratorMap& iterator_map,
218 HashPrefix* smallest_hash_prefix);
218 219
219 // Returns the next hash prefix of length |prefix_size| from |hash_prefix_map| 220 // For each key in |hash_prefix_map|, sets the iterator at that key
220 // that hasn't been merged already. |counter_map| is used to determine the 221 // |iterator_map| to hash_prefix_map[key].begin().
221 // index of the next prefix of size |prefix_size| to merge. 222 static void InitializeIteratorMap(const HashPrefixMap& hash_prefix_map,
222 static HashPrefix GetNextUnmergedPrefixForSize( 223 IteratorMap* iterator_map);
223 PrefixSize prefix_size,
224 const HashPrefixMap& hash_prefix_map,
225 const CounterMap& counter_map);
226
227 // Sets a value of 0 in |counter_map| for all keys in |hash_prefix_map|.
228 static void InitializeCounterMap(const HashPrefixMap& hash_prefix_map,
229 CounterMap* counter_map);
230 224
231 // Reserve the appropriate string size so that the string size of the merged 225 // Reserve the appropriate string size so that the string size of the merged
232 // list is exact. This ignores the space that would otherwise be released by 226 // list is exact. This ignores the space that would otherwise be released by
233 // deletions specified in the update because it is non-trivial to calculate 227 // deletions specified in the update because it is non-trivial to calculate
234 // those deletions upfront. This isn't so bad since deletions are supposed to 228 // those deletions upfront. This isn't so bad since deletions are supposed to
235 // be small and infrequent. 229 // be small and infrequent.
236 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, 230 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map,
237 HashPrefixMap* prefix_map_to_update); 231 HashPrefixMap* prefix_map_to_update);
238 232
239 // Updates the |additions_map| with the additions received in the partial 233 // Updates the |additions_map| with the additions received in the partial
(...skipping 22 matching lines...) Expand all
262 const base::FilePath store_path_; 256 const base::FilePath store_path_;
263 HashPrefixMap hash_prefix_map_; 257 HashPrefixMap hash_prefix_map_;
264 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 258 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
265 }; 259 };
266 260
267 std::ostream& operator<<(std::ostream& os, const V4Store& store); 261 std::ostream& operator<<(std::ostream& os, const V4Store& store);
268 262
269 } // namespace safe_browsing 263 } // namespace safe_browsing
270 264
271 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 265 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
OLDNEW
« no previous file with comments | « no previous file | components/safe_browsing_db/v4_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698