OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | |
6 #define CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <deque> | |
10 #include <map> | |
11 #include <utility> | |
12 #include <vector> | |
13 | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/logging.h" | |
16 #include "base/stl_util.h" | |
17 #include "chrome/common/instant_types.h" | |
18 #include "googleurl/src/gurl.h" | |
19 | |
20 // In InstantExtended, iframes are used to display objects which can only be | |
21 // referred by the Instant page using an ID (restricted ID). These IDs need to | |
sreeram
2013/03/14 23:05:36
referred by -> referred to by
or
referred by -> re
Shishir
2013/03/15 17:31:15
Done.
| |
22 // be unique and cached for a while so that the SearchBox API can fetch the | |
23 // object info based on the ID when required by the Instant page. The reason to | |
24 // use a cache of N items as aginst just the last set of results is that there | |
sreeram
2013/03/14 23:05:36
aginst -> against
Shishir
2013/03/15 17:31:15
Done.
| |
25 // may be race conditions - e.g. the user clicks on a result being shown but the | |
26 // result set has internally changed but not yet been displayed. | |
27 // | |
28 // The cache can be used in two modes: | |
29 // | |
30 // 1. To store items and assign restrcited IDs to them. The cache will store | |
sreeram
2013/03/14 23:05:36
restrcited -> restricted
Shishir
2013/03/15 17:31:15
Done.
| |
31 // a max of |max_cache_size_| items and assign them unique ids. | |
sreeram
2013/03/14 23:05:36
ids -> IDs
Shishir
2013/03/15 17:31:15
Done.
| |
32 // | |
33 // 2. To store items that already have restrcited IDs assigned to them (like | |
sreeram
2013/03/14 23:05:36
restrcited -> restricted
(like -> (e.g.,
Shishir
2013/03/15 17:31:15
Sorry, new keyboard!
| |
34 // from another instance of the cache). The cache will then not generate IDs | |
35 // and does not make any garantees of the uniqueness of the IDs. If multiple | |
sreeram
2013/03/14 23:05:36
garantees -> guarantees
Shishir
2013/03/15 17:31:15
Done.
| |
36 // items are inserted with the same ID, the cahce | |
sreeram
2013/03/14 23:05:36
cahce -> cache
... rest of the comment?
Shishir
2013/03/15 17:31:15
Done.
| |
37 | |
38 // T needs to be copyable. | |
39 template <typename T> | |
40 class InstantRestrictedIDCache { | |
41 public: | |
42 typedef std::pair<InstantRestrictedID, T> ItemIDPair; | |
43 typedef std::vector<T> ItemVector; | |
44 typedef std::vector<ItemIDPair> ItemIDVector; | |
45 | |
46 explicit InstantRestrictedIDCache(size_t max_cache_size); | |
47 ~InstantRestrictedIDCache(); | |
48 | |
49 // Adds items to the cache, assigning restricted IDs in the process. May | |
50 // delete older items from the cache. | |
51 void AddItems(const ItemVector& items); | |
52 | |
53 // Adds items to the cache using the supplied restricted IDs. May delete | |
54 // older items from the cache. | |
55 void AddItemsWithRestrictedID(const ItemIDVector& items); | |
56 | |
57 // Returns the last set of items added to the cache either via |AddItems| or | |
58 // |AddItemsWithRestrictedId|. | |
sreeram
2013/03/14 23:05:36
|AddItems| -> AddItems()
|AddItemsWithRestrictedId
Shishir
2013/03/15 17:31:15
Done.
| |
59 void GetCurrentItems(ItemIDVector* items) const; | |
60 | |
61 // Returns true if the |restricted_id| is present in the cache and if so, | |
62 // returns a copy of the item. | |
63 bool GetItemWithRestrictedID(InstantRestrictedID restricted_id, | |
64 T* item) const; | |
65 | |
66 private: | |
67 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, AdditionOverflow); | |
68 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, AutoIDGeneration); | |
69 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, ManualIDGeneration); | |
70 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, MixIDGeneration); | |
71 | |
72 typedef std::map<InstantRestrictedID, ItemIDPair*> ItemIDMap; | |
73 | |
74 // Will drop items from |cache_| if its size is greater than | |
75 // |max_cache_size_|. | |
76 void MaybeDropItemsFromCache(); | |
77 | |
78 std::deque<ItemIDPair*> cache_; | |
sreeram
2013/03/14 23:05:36
std::list<ItemIDPair> seems like a better choice h
Shishir
2013/03/15 17:31:15
There was array access requirement when this CL st
| |
79 STLElementDeleter<std::deque<ItemIDPair*> > cache_deleter_; | |
sreeram
2013/03/14 23:05:36
This doesn't seem necessary. If you want to ensure
Shishir
2013/03/15 17:31:15
Can do that too. But this is an equally good appro
| |
80 std::map<InstantRestrictedID, ItemIDPair*> restricted_id_item_map_; | |
sreeram
2013/03/14 23:05:36
ItemIDMap item_id_map_;
Shishir
2013/03/15 17:31:15
Done.
| |
81 InstantRestrictedID last_restricted_id_; | |
82 size_t last_addition_size_; // The number of items added last. | |
83 const size_t max_cache_size_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(InstantRestrictedIDCache); | |
86 }; | |
87 | |
88 template <typename T> | |
89 InstantRestrictedIDCache<T>::InstantRestrictedIDCache(size_t max_cache_size) | |
90 : cache_deleter_(&cache_), | |
91 last_restricted_id_(0), | |
92 last_addition_size_(0), | |
93 max_cache_size_(max_cache_size) { | |
94 DCHECK(max_cache_size_); | |
95 } | |
96 | |
97 template <typename T> | |
98 InstantRestrictedIDCache<T>::~InstantRestrictedIDCache() { | |
99 } | |
100 | |
101 template <typename T> | |
102 void InstantRestrictedIDCache<T>::AddItems(const ItemVector& items) { | |
103 for (typename ItemVector::const_iterator it = items.begin(); | |
104 it != items.end(); ++it) { | |
105 ItemIDPair* to_add = new ItemIDPair(++last_restricted_id_, *it); | |
106 cache_.push_back(to_add); | |
107 restricted_id_item_map_[last_restricted_id_] = to_add; | |
108 } | |
109 last_addition_size_ = items.size(); | |
110 MaybeDropItemsFromCache(); | |
111 } | |
112 | |
113 template <typename T> | |
114 void InstantRestrictedIDCache<T>::AddItemsWithRestrictedID( | |
115 const ItemIDVector& items) { | |
116 for (typename ItemIDVector::const_iterator it = items.begin(); | |
117 it != items.end(); ++it) { | |
118 ItemIDPair* to_add = new ItemIDPair(*it); | |
119 cache_.push_back(to_add); | |
120 restricted_id_item_map_[it->first] = to_add; | |
121 last_restricted_id_ = std::max(it->first, last_restricted_id_); | |
122 } | |
123 last_addition_size_ = items.size(); | |
124 MaybeDropItemsFromCache(); | |
125 } | |
126 | |
127 template <typename T> | |
128 void InstantRestrictedIDCache<T>::GetCurrentItems(ItemIDVector* items) const { | |
129 items->clear(); | |
130 | |
131 if (last_addition_size_ == 0) | |
132 return; | |
133 | |
134 int start = std::max(0, static_cast<int>(cache_.size() - | |
135 last_addition_size_)); | |
136 for (int i = start; i < static_cast<int>(cache_.size()); ++i) { | |
137 items->push_back(*(cache_[i])); | |
138 } | |
139 } | |
140 | |
141 template <typename T> | |
142 bool InstantRestrictedIDCache<T>::GetItemWithRestrictedID( | |
143 InstantRestrictedID restricted_id, | |
144 T* item) const { | |
145 DCHECK(item); | |
146 | |
147 typename ItemIDMap::const_iterator iter = | |
148 restricted_id_item_map_.find(restricted_id); | |
149 if (iter == restricted_id_item_map_.end()) | |
150 return false; | |
151 | |
152 *item = iter->second->second; | |
153 return true; | |
154 } | |
155 | |
156 template <typename T> | |
157 void InstantRestrictedIDCache<T>::MaybeDropItemsFromCache() { | |
158 while (cache_.size() > max_cache_size_) { | |
159 ItemIDPair* to_erase = cache_.front(); | |
160 | |
161 typename ItemIDMap::iterator iter = | |
162 restricted_id_item_map_.find(to_erase->first); | |
163 if (iter != restricted_id_item_map_.end() && iter->second == to_erase) | |
164 restricted_id_item_map_.erase(to_erase->first); | |
165 | |
166 cache_.pop_front(); | |
167 delete to_erase; | |
168 } | |
169 } | |
170 | |
171 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | |
OLD | NEW |