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