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 restricted IDs assigned to them (e.g. | |
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(). | |
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, CrazyIDGeneration); | |
69 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, ManualIDGeneration); | |
70 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, MixIDGeneration); | |
71 | |
72 typedef std::list<ItemIDPair> ItemIDList; | |
73 typedef std::map<InstantRestrictedID, typename ItemIDList::iterator> | |
74 ItemIDMap; | |
75 | |
76 // Will drop items from |cache_| if its size is greater than | |
77 // |max_cache_size_|. | |
78 void MaybeDropItemsFromCache(); | |
79 | |
80 ItemIDList cache_; | |
81 ItemIDMap item_id_map_; | |
82 InstantRestrictedID last_restricted_id_; | |
83 typename ItemIDList::iterator last_add_start_; | |
84 const size_t max_cache_size_; | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(InstantRestrictedIDCache); | |
87 }; | |
88 | |
89 template <typename T> | |
90 InstantRestrictedIDCache<T>::InstantRestrictedIDCache(size_t max_cache_size) | |
91 : last_restricted_id_(0), | |
92 last_add_start_(cache_.begin()), | |
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 (size_t i = 0; i < items.size(); ++i) { | |
104 typename ItemIDList::iterator iter = cache_.insert( | |
105 cache_.end(), | |
106 std::make_pair(++last_restricted_id_, items[i])); | |
107 item_id_map_[last_restricted_id_] = iter; | |
108 if (i == 0) | |
109 last_add_start_ = iter; | |
110 } | |
111 | |
112 MaybeDropItemsFromCache(); | |
113 } | |
114 | |
115 template <typename T> | |
116 void InstantRestrictedIDCache<T>::AddItemsWithRestrictedID( | |
117 const ItemIDVector& items) { | |
118 for (size_t i = 0; i < items.size(); ++i) { | |
119 const ItemIDPair& item_id = items[i]; | |
120 typename ItemIDList::iterator iter = cache_.insert(cache_.end(), item_id); | |
121 item_id_map_[item_id.first] = iter; | |
122 last_restricted_id_ = std::max(item_id.first, last_restricted_id_); | |
123 if (i == 0) | |
124 last_add_start_ = iter; | |
125 } | |
126 | |
127 MaybeDropItemsFromCache(); | |
128 } | |
129 | |
130 template <typename T> | |
131 void InstantRestrictedIDCache<T>::GetCurrentItems(ItemIDVector* items) const { | |
132 items->clear(); | |
133 | |
134 for (typename ItemIDList::iterator it = last_add_start_; it != cache_.end(); | |
135 ++it) { | |
136 items->push_back(*it); | |
137 } | |
138 } | |
139 | |
140 template <typename T> | |
141 bool InstantRestrictedIDCache<T>::GetItemWithRestrictedID( | |
142 InstantRestrictedID restricted_id, | |
143 T* item) const { | |
144 DCHECK(item); | |
145 | |
146 typename ItemIDMap::const_iterator iter = item_id_map_.find(restricted_id); | |
147 if (iter == item_id_map_.end()) | |
148 return false; | |
149 | |
150 *item = (*(iter->second)).second; | |
151 return true; | |
152 } | |
153 | |
154 template <typename T> | |
155 void InstantRestrictedIDCache<T>::MaybeDropItemsFromCache() { | |
156 while (cache_.size() > max_cache_size_) { | |
brettw
2013/03/20 21:53:53
I wonder if this could be done using an MRUCache t
Shishir
2013/03/20 22:10:51
This is not a strict MRU cache, but it definitely
| |
157 typename ItemIDList::iterator begin = cache_.begin(); | |
158 | |
159 typename ItemIDMap::iterator iter = item_id_map_.find(begin->first); | |
160 if (iter != item_id_map_.end() && iter->second == begin) | |
161 item_id_map_.erase(begin->first); | |
162 | |
163 if (last_add_start_ == begin) | |
164 ++last_add_start_; | |
165 | |
166 cache_.pop_front(); | |
167 } | |
168 } | |
169 | |
170 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | |
OLD | NEW |