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

Side by Side Diff: chrome/common/instant_restricted_id_cache.h

Issue 12498002: InstantExtended: Adding InstantRestrictedIDCache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merging David's and Sreeram's changes. Created 7 years, 9 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 | Annotate | Revision Log
OLDNEW
(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 "content/public/common/page_transition_types.h"
dhollowa 2013/03/14 00:02:43 Needed?
Shishir 2013/03/14 19:53:03 Done.
19 #include "googleurl/src/gurl.h"
20
21 // In InstantExtended, iframes are used to display objects which can only be
dhollowa 2013/03/14 00:02:43 Not only iframes, but Most Visited information. I
Shishir 2013/03/14 19:53:03 Even the Most visited Items is moving to iframes s
dhollowa 2013/03/14 23:40:00 Ya, but the MV items will still have IDs for the t
22 // referred by the Instant page using an ID (restricted ID). These IDs need to
23 // be unique and cached for a while so that the SearchBox API can fetch the
24 // object info based on the ID when required by the Instant page.
25 //
26 // This "cache" class is used for assigning restricted IDs to objects, storing
27 // the last N items and retreiving them using the IDs when necessary. Objects
28 // can also be added which already have IDs assigned, but the caller has to
dhollowa 2013/03/14 00:02:43 "but the caller has to ensure that the IDs do not
Shishir 2013/03/14 19:53:03 Done.
29 // ensure that the IDs do not repeat. In case IDs are repeated, the last added
30 // object with the ID will be returned.
31
32 // T needs to be copyable.
33 template <typename T>
34 class InstantRestrictedIDCache {
35 public:
36 typedef std::pair<InstantRestrictedID, T> RestrictedIDItemPair;
37
38 explicit InstantRestrictedIDCache(size_t max_cache_size);
39 ~InstantRestrictedIDCache();
40
41 // Adds items to the cache, assigning restricted IDs in the process. May
42 // delete older items from the cache.
43 void AddItems(const std::vector<T>& items);
44
45 // Adds items to the cache using the supplied restricted IDs. May delete
dhollowa 2013/03/14 00:02:43 The "May delete..." aspect of this has me a bit wo
Shishir 2013/03/14 19:53:03 Yes the cache is to avoid races. Added comment to
46 // older items from the cache.
47 void AddItemsWithRestrictedID(const std::vector<RestrictedIDItemPair>& items);
48
49 // Returns the last set of items added to the cache either via |AddItems| or
50 // |AddItemsWithRestrictedId|.
51 void GetCurrentItems(std::vector<RestrictedIDItemPair>* items) const;
52
53 // Returns true if the |restricted_id| is present in the cache and if so,
54 // returns a copy of the item.
55 bool GetItemWithRestrictedID(InstantRestrictedID restricted_id,
dhollowa 2013/03/14 00:02:43 This searches ALL items, not just "current" items,
Shishir 2013/03/14 19:53:03 Isnt this explained because we say "in the cache"?
dhollowa 2013/03/14 23:40:00 Ok.
56 T* item) const;
57
58 private:
59 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, AdditionOverflow);
60 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, AutoIDGeneration);
61 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, ManualIDGeneration);
62 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, MixIDGeneration);
63
64 // Will drop items from |cache_| if its size is greater than
65 // |max_cache_size_|.
66 void MaybeDropItemsFromCache();
67
68 std::deque<RestrictedIDItemPair*> cache_;
69 STLElementDeleter<std::deque<RestrictedIDItemPair*> > cache_deleter_;
70 std::map<InstantRestrictedID, RestrictedIDItemPair*> restricted_id_item_map_;
71 InstantRestrictedID last_restricted_id_;
72 size_t last_addition_size_; // The number of items added last.
73 const size_t max_cache_size_;
74
75 DISALLOW_COPY_AND_ASSIGN(InstantRestrictedIDCache);
76 };
77
78 template <typename T>
79 InstantRestrictedIDCache<T>::InstantRestrictedIDCache(size_t max_cache_size)
80 : cache_deleter_(&cache_),
81 last_restricted_id_(0),
82 last_addition_size_(0),
83 max_cache_size_(max_cache_size) {
84 DCHECK(max_cache_size_);
85 }
86
87 template <typename T>
88 InstantRestrictedIDCache<T>::~InstantRestrictedIDCache() {
89 }
90
91 template <typename T>
92 void InstantRestrictedIDCache<T>::AddItems(const std::vector<T>& items) {
93 for (typename std::vector<T>::const_iterator it = items.begin();
94 it != items.end(); ++it) {
95 RestrictedIDItemPair* to_add = new RestrictedIDItemPair(
96 ++last_restricted_id_, *it);
97 cache_.push_back(to_add);
98 restricted_id_item_map_[last_restricted_id_] = to_add;
99 }
100 last_addition_size_ = items.size();
101 MaybeDropItemsFromCache();
102 }
103
104 template <typename T>
105 void InstantRestrictedIDCache<T>::AddItemsWithRestrictedID(
106 const std::vector<RestrictedIDItemPair>& items) {
107 for (typename std::vector<RestrictedIDItemPair>::const_iterator it =
palmer 2013/03/13 23:50:37 NIT: I might make a more friendly-looking typedef
Shishir 2013/03/14 19:53:03 Done.
108 items.begin(); it != items.end(); ++it) {
109 RestrictedIDItemPair* to_add = new RestrictedIDItemPair(*it);
110 cache_.push_back(to_add);
111 restricted_id_item_map_[it->first] = to_add;
112 last_restricted_id_ = std::max(it->first, last_restricted_id_);
113 }
114 last_addition_size_ = items.size();
115 MaybeDropItemsFromCache();
116 }
117
118 template <typename T>
119 void InstantRestrictedIDCache<T>::GetCurrentItems(
120 std::vector<RestrictedIDItemPair>* items) const {
121 items->clear();
122
123 if (last_addition_size_ == 0)
124 return;
125
126 int start = std::max(0, static_cast<int>(cache_.size() -
127 last_addition_size_));
128 for (int i = start; i < static_cast<int>(cache_.size()); ++i) {
palmer 2013/03/13 23:50:37 static_cast is a sign that |start| and |i| should
Shishir 2013/03/14 19:53:03 Like my comment from base/basictypes, we should be
129 items->push_back(*(cache_[i]));
130 }
131 }
132
133 template <typename T>
134 bool InstantRestrictedIDCache<T>::GetItemWithRestrictedID(
135 InstantRestrictedID restricted_id,
136 T* item) const {
137 DCHECK(item);
138
139 typename std::map<InstantRestrictedID, RestrictedIDItemPair*>::const_iterator
palmer 2013/03/13 23:50:37 NIT: Consider a friendly typedef here too.
Shishir 2013/03/14 19:53:03 Done.
140 iter = restricted_id_item_map_.find(restricted_id);
141 if (iter == restricted_id_item_map_.end())
142 return false;
143
144 *item = iter->second->second;
145 return true;
146 }
147
148 template <typename T>
149 void InstantRestrictedIDCache<T>::MaybeDropItemsFromCache() {
150 while (cache_.size() > max_cache_size_) {
151 RestrictedIDItemPair* to_erase = cache_.front();
152
153 typename std::map<InstantRestrictedID, RestrictedIDItemPair*>::iterator
154 iter = restricted_id_item_map_.find(to_erase->first);
155 if (iter != restricted_id_item_map_.end() && iter->second == to_erase)
156 restricted_id_item_map_.erase(to_erase->first);
157
158 cache_.pop_front();
159 delete to_erase;
160 }
161 }
162
163 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698